SES-148 Add changing weapoin mechanic

This commit is contained in:
Karol Górzyński 2021-01-13 19:29:32 +01:00
parent 3ca90663b2
commit 71f01a167c
3 changed files with 80 additions and 0 deletions

View File

@ -4,11 +4,14 @@ using SessionCompanion.ViewModels.CharacterWeaponViewModels;
namespace SessionCompanion.Services.Interfaces namespace SessionCompanion.Services.Interfaces
{ {
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.ViewModels.ApiResponses;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
public interface ICharacterWeaponService : IServiceBase<CharacterWeaponViewModel, CharacterWeapon> public interface ICharacterWeaponService : IServiceBase<CharacterWeaponViewModel, CharacterWeapon>
{ {
Task<List<CharacterWeaponWithWeaponDetailsViewModel>> GetCharacterWeaponsList(int characterId); Task<List<CharacterWeaponWithWeaponDetailsViewModel>> GetCharacterWeaponsList(int characterId);
Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterWeapon(CharacterWeaponViewModel model);
} }
} }

View File

@ -12,6 +12,8 @@ namespace SessionCompanion.Services.Services
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.ViewModels.ApiResponses;
public class CharacterWeaponService : ServiceBase<CharacterWeaponViewModel, CharacterWeapon>, ICharacterWeaponService public class CharacterWeaponService : ServiceBase<CharacterWeaponViewModel, CharacterWeapon>, ICharacterWeaponService
{ {
@ -30,5 +32,61 @@ namespace SessionCompanion.Services.Services
var result = Mapper.Map<List<CharacterWeaponWithWeaponDetailsViewModel>>(characterWeapons); var result = Mapper.Map<List<CharacterWeaponWithWeaponDetailsViewModel>>(characterWeapons);
return result; return result;
} }
public async Task<Either<SuccessResponse,ErrorResponse>> ChangeCharacterWeapon(CharacterWeaponViewModel model)
{
// Dodaj optional rozbro postac
var weaponsInUse = await Repository.Get(c => c.CharacterId.Equals(model.CharacterId)).Where(w => w.InUse.Equals(true)).ToListAsync();
var weapon = Mapper.Map<CharacterWeapon>(model);
if (weaponsInUse.Count() == 0)
{
// no weapon in use
// just use new one
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
var weaponInBothHands = weaponsInUse.Where(w => w.HoldInLeftHand.Equals(true) && w.HoldInRightHand.Equals(true));
if ((model.HoldInLeftHand && model.HoldInRightHand) || (weaponInBothHands.Count() > 0))
{
// our model weapon uses both hands
// or there is weapon already in both hands
foreach (var w in weaponsInUse)
{
w.InUse = false;
w.HoldInLeftHand = false;
w.HoldInRightHand = false;
await Repository.Update(w);
}
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
var weaponsToChange = weaponsInUse.Where(w => w.HoldInLeftHand.Equals(model.HoldInLeftHand) && w.HoldInRightHand.Equals(model.HoldInRightHand));
if (weaponsToChange.Count() == 1)
{
// there is weapon in the same hand set as our
// we update it
var weaponToChange = weaponsToChange.Single();
weaponToChange.InUse = false;
weaponToChange.HoldInLeftHand = false;
weaponToChange.HoldInRightHand = false;
await Repository.Update(weaponToChange);
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
// weapon is armed in empty hand
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
} }
} }

View File

@ -86,6 +86,8 @@ namespace SessionCompanion.Controllers
[HttpPut("addArmor")] [HttpPut("addArmor")]
public async Task<Either<SuccessResponse, ErrorResponse>> AddCharacterArmor(CharacterArmorViewModel characterArmorViewModel) public async Task<Either<SuccessResponse, ErrorResponse>> AddCharacterArmor(CharacterArmorViewModel characterArmorViewModel)
{ {
if (!ModelState.IsValid)
return new ErrorResponse() { StatusCode = 500, Message = "Invalid model!" };
try try
{ {
await _characterArmorService.Create(characterArmorViewModel); await _characterArmorService.Create(characterArmorViewModel);
@ -97,6 +99,7 @@ namespace SessionCompanion.Controllers
return new ErrorResponse() { StatusCode = 500, Message = e.Message }; return new ErrorResponse() { StatusCode = 500, Message = e.Message };
} }
} }
/// <summary> /// <summary>
/// Metoda dodaje Weapon do danej postaci /// Metoda dodaje Weapon do danej postaci
/// </summary> /// </summary>
@ -118,5 +121,21 @@ namespace SessionCompanion.Controllers
return new ErrorResponse() { StatusCode = 500, Message = e.Message }; return new ErrorResponse() { StatusCode = 500, Message = e.Message };
} }
} }
[HttpPut("changeWeapon")]
public async Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterWeapon(CharacterWeaponViewModel characterWeaponViewModel)
{
if (!ModelState.IsValid)
return new ErrorResponse() { StatusCode = 500, Message = "Invalid model!" };
try
{
var response = await _characterWeaponService.ChangeCharacterWeapon(characterWeaponViewModel);
return response;
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
} }
} }