SES-148 Add changing weapoin mechanic
This commit is contained in:
parent
3ca90663b2
commit
71f01a167c
@ -4,11 +4,14 @@ using SessionCompanion.ViewModels.CharacterWeaponViewModels;
|
||||
|
||||
namespace SessionCompanion.Services.Interfaces
|
||||
{
|
||||
using SessionCompanion.Extensions.EitherType;
|
||||
using SessionCompanion.ViewModels.ApiResponses;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public interface ICharacterWeaponService : IServiceBase<CharacterWeaponViewModel, CharacterWeapon>
|
||||
{
|
||||
Task<List<CharacterWeaponWithWeaponDetailsViewModel>> GetCharacterWeaponsList(int characterId);
|
||||
Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterWeapon(CharacterWeaponViewModel model);
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ namespace SessionCompanion.Services.Services
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SessionCompanion.Extensions.EitherType;
|
||||
using SessionCompanion.ViewModels.ApiResponses;
|
||||
|
||||
public class CharacterWeaponService : ServiceBase<CharacterWeaponViewModel, CharacterWeapon>, ICharacterWeaponService
|
||||
{
|
||||
@ -30,5 +32,61 @@ namespace SessionCompanion.Services.Services
|
||||
var result = Mapper.Map<List<CharacterWeaponWithWeaponDetailsViewModel>>(characterWeapons);
|
||||
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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,6 +86,8 @@ namespace SessionCompanion.Controllers
|
||||
[HttpPut("addArmor")]
|
||||
public async Task<Either<SuccessResponse, ErrorResponse>> AddCharacterArmor(CharacterArmorViewModel characterArmorViewModel)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return new ErrorResponse() { StatusCode = 500, Message = "Invalid model!" };
|
||||
try
|
||||
{
|
||||
await _characterArmorService.Create(characterArmorViewModel);
|
||||
@ -97,6 +99,7 @@ namespace SessionCompanion.Controllers
|
||||
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Metoda dodaje Weapon do danej postaci
|
||||
/// </summary>
|
||||
@ -118,5 +121,21 @@ namespace SessionCompanion.Controllers
|
||||
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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user