public async Task <IActionResult> Shot([FromBody] ShotRequest request) { AssertExtensions.NotNull(request, nameof(request)); ShotModel model = _applicationMapper.Map(request); ShotResultModel result = await _seeBattleGameService.Shot(model); ShotResponse response = _contractMapper.Map(result); return(new JsonResult(response)); }
private void CheckForVictory(ShotResponse response) { if (response.ShotStatus == ShotStatus.HitAndSunk) { // did they win? if (Ships.All(s => s.IsSunk)) { response.ShotStatus = ShotStatus.Victory; } } }
public override async Task <Weapon> CreateWeapon(int x, int y) { ShotResponse shot = await Service.ShootWeapon(_token, _roomId, x, y, 2); var torpedo = new Torpedo { Id = shot.WeaponId, X = shot.X, Y = shot.Y, IsSuccessful = shot.Successful, ShipTypes = shot.ShipTypes.ToList() }; return(torpedo); }
public async Task <Weapon> Shot(WeaponRequest weaponRequest, string _token, string _roomId) { ShotResponse shot = await Service.ShootWeapon(_token, _roomId, weaponRequest.X, weaponRequest.Y, weaponRequest.WeaponTypeId); var mine = new Mine { Id = shot.WeaponId, X = shot.X, Y = shot.Y, IsSuccessful = shot.Successful, ShipTypes = shot.ShipTypes.ToList(), IsDetonated = shot.Successful }; return(mine); }
private void CheckShipsForHit(Coordinate coordinate, ShotResponse response) { response.ShotStatus = ShotStatus.Miss; foreach (var ship in Ships) { // no need to check sunk ships if (ship.IsSunk) { continue; } ShotStatus status = ship.FireAtShip(coordinate); switch (status) { case ShotStatus.HitAndSunk: response.ShotStatus = ShotStatus.HitAndSunk; ShotHistory.Add(coordinate, Response.ShotHistory.Hit); break; case ShotStatus.Hit: response.ShotStatus = ShotStatus.Hit; ShotHistory.Add(coordinate, Response.ShotHistory.Hit); break; } // if they hit something, no need to continue looping if (status != ShotStatus.Miss) { break; } } if (response.ShotStatus == ShotStatus.Miss) { ShotHistory.Add(coordinate, Response.ShotHistory.Miss); } }
/// <summary> /// Обрабатывет результат выстрела /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ResultFire(object sender, ResponseEventArgs e) { ShotResponse response = e.Response as ShotResponse; if (response != null) { Shot shot = response.CurrentShot; if (shot != null) { if (response.IsSuccess && _isMyTurn) { _seaOpponent.SetShot(shot, response.KilledShipInfo); TransferTurn(response.NextShotUserId); } else { _seaPlayer.SetShot(shot, response.KilledShipInfo); TransferTurn(response.NextShotUserId); } } } }
public ShotResponse FireShot(Coordinate coordinate) { var responses = new ShotResponse(); // is this coordinate on the board? if (!IsValidCoordinate(coordinate)) { responses.ShotStatus = ShotStatus.Invalid; return(responses); } // did they already try this position? if (ShotHistory.ContainsKey(coordinate)) { responses.ShotStatus = ShotStatus.Duplicate; return(responses); } CheckShipsForHit(coordinate, responses); CheckForVictory(responses); return(responses); }
public void DoShotCallback(ShotResponse response) { syncContext.Post(new SendOrPostCallback(OnBroadcast <ShotResponse>), response); }
public async Task <IActionResult> CreateWeapon(string roomId, WeaponRequest request) { var userId = HttpContext.GetUserId(); var weaponType = await _context.WeaponTypes .AsNoTracking() .SingleOrDefaultAsync(t => t.Id == request.WeaponTypeId); if (weaponType == null) { return(NotFound(new ErrorResponse { Errors = new List <ErrorModel> { new ErrorModel { Message = "Unable to find weapon type" } } })); } var room = await _context.Rooms .Where(r => r.Id == roomId) .AsNoTracking() .FirstOrDefaultAsync(); if (room == null) { return(NotFound(new ErrorResponse { Errors = new List <ErrorModel> { new ErrorModel { Message = "Unable to find room" } } })); } if (room.GuestUserId == null) { return(BadRequest(new ErrorResponse { Errors = new List <ErrorModel> { new ErrorModel { Message = "Unable to create an instance of weapon: guest player have not joined room yet" } } })); } if (room.GuestUserId != userId && room.HostUserId != userId) { return(BadRequest(new ErrorResponse { Errors = new List <ErrorModel> { new ErrorModel { Message = "Unable to create an instance of weapon: you do not have access to this room" } } })); } var maps = await _context.Maps .Where(m => m.RoomId == roomId) .AsNoTracking() .ToListAsync(); if (maps.Any(m => !m.IsCompleted)) { return(BadRequest(new ErrorResponse { Errors = new List <ErrorModel> { new ErrorModel { Message = "Unable to create an instance of weapon: not all maps in this room is already completed" } } })); } if ((room.HostUserId == userId && room.IsHostTurn) || (room.GuestUserId == userId && !room.IsHostTurn)) { var weapon = _mapper.Map <Weapon>(request); weapon.UserId = userId; weapon.RoomId = roomId; weapon.IsUsed = !weaponType.IsMine; weapon.WeaponType = weaponType; string enemyId; if (room.HostUserId == userId) { enemyId = room.GuestUserId; } else { enemyId = room.HostUserId; } var enemyShips = await _context.Ships.AsNoTracking() .Include(s => s.ShipGroup).ThenInclude(sg => sg.ShipType) .Where(s => s.UserId == enemyId && s.RoomId == roomId).ToListAsync(); if (weaponType.Id == 1) { enemyShips = enemyShips.Where(s => !s.ShipGroup.ShipType.IsSubmarine).ToList(); } if (weaponType.Id == 2) { enemyShips = enemyShips.Where(s => s.ShipGroup.ShipType.IsSubmarine).ToList(); } var shotResponse = new ShotResponse { Successful = false, ShipTypes = new List <int>() }; foreach (var ship in enemyShips) { int x1 = ship.XOffset >= 0 ? ship.X : ship.X + ship.XOffset + 1; int x2 = ship.XOffset >= 0 ? ship.X + ship.XOffset - 1 : ship.X; int y1 = ship.YOffset >= 0 ? ship.Y : ship.Y + ship.YOffset + 1; int y2 = ship.YOffset >= 0 ? ship.Y + ship.YOffset - 1 : ship.Y; if (weapon.X >= x1 && weapon.X <= x2 && weapon.Y >= y1 && weapon.Y <= y2) { if (weapon.WeaponType.IsMine) { ship.HP -= 0.5; } else { ship.HP -= 1; } weapon.IsUsed = true; _context.Ships.Update(ship); await _context.SaveChangesAsync(); shotResponse.Successful = true; shotResponse.ShipTypes.Add(ship.ShipTypeId); } } var enemyMap = await _context.Maps .FirstOrDefaultAsync(m => m.UserId == enemyId && m.RoomId == roomId); if (enemyMap == null) { return(NotFound(new ErrorResponse { Errors = new List <ErrorModel> { new ErrorModel { Message = "Unable to find enemy map" } } })); } if (weapon.WeaponType.IsMine) { enemyMap.EnemyShot_X = null; enemyMap.EnemyShot_Y = null; } else { enemyMap.EnemyShot_X = weapon.X; enemyMap.EnemyShot_Y = weapon.Y; } _context.Maps.Update(enemyMap); await _context.SaveChangesAsync(); weapon.WeaponType = null; _context.Weapons.Add(weapon); await _context.SaveChangesAsync(); room.IsHostTurn = !room.IsHostTurn; _context.Rooms.Update(room); await _context.SaveChangesAsync(); shotResponse.WeaponId = weapon.Id; shotResponse.X = weapon.X; shotResponse.Y = weapon.Y; return(Ok(shotResponse)); } return(BadRequest(new ErrorResponse { Errors = new List <ErrorModel> { new ErrorModel { Message = "Unable to create an instance of weapon: it is not your turn." } } })); }