public async Task Atack(string row, string col) { var socketId = Context.ConnectionId; int posX = Int32.Parse(row); int posY = Int32.Parse(col); if (duelsController.isPlayerTurn(socketId) == false) { await Clients.Caller.SendAsync("invalidTurn", row, col); } else { string enemySockeId = duelsController.GetOpponentSocketId(socketId); //returns active caller's strategy Strategy activeStrategy = StrategyHolder.GetPlayerStrategy(socketId); //executing the strategy returns all affected cells as AttackOutcome and cell coordinates List <CellOutcome> outcomes = strategyController.Attack(posX, posY, socketId); //for every outcome we inform both players foreach (CellOutcome outcome in outcomes) { switch (outcome.attackOutcome) { case AttackOutcome.Hit: await Clients.Client(enemySockeId).SendAsync("pingAttack", outcome.posX, outcome.posY, "Hit", false); await Clients.Caller.SendAsync("pingAttack", outcome.posX, outcome.posY, "Hit", true); break; case AttackOutcome.Armor: duelsController.ChangeTurns(socketId); await Clients.Client(enemySockeId).SendAsync("pingAttack", outcome.posX, outcome.posY, "Armor", false); await Clients.Caller.SendAsync("pingAttack", outcome.posX, outcome.posY, "Armor", true); break; //For now, both [Missed and Invalid] trigger default switch default: duelsController.ChangeTurns(socketId); await Clients.Client(enemySockeId).SendAsync("pingAttack", outcome.posX, outcome.posY, "Missed", false); await Clients.Caller.SendAsync("pingAttack", outcome.posX, outcome.posY, "Missed", true); //turn change using the proxy class TurnOutcome turn = proxyPlayerTurn.ChangeTurn(); await Clients.Client(turn.InactiveId).SendAsync("changedTurn", turn.CallerTurn); await Clients.Client(turn.ActiveId).SendAsync("changedTurn", turn.OpponetTurn); break; } } } }
public List <CellOutcome> Attack(int posx, int posy, string socketId) { Strategy strategy = StrategyHolder.GetPlayerStrategy(socketId); List <Ship> ships = GetEnemyShips(GetOpponentSocketId(socketId)); List <Cell> cells = GetEnemyCells(GetOpponentArenaId(socketId)); List <CellOutcome> outcomes = strategy.Attack(posx, posy, cells, ships); _context.SaveChanges(); return(outcomes); }
public async Task HiddenAttack() { var socketId = Context.ConnectionId; string enemySockeId = duelsController.GetOpponentSocketId(socketId); List <Ship> ships = strategyController.GetEnemyShips(strategyController.GetOpponentSocketId(socketId)); List <Cell> cells = strategyController.GetEnemyCells(strategyController.GetOpponentArenaId(socketId)); AttackChangerVisitor atackChangerVisitor = new AttackChangerVisitor(cells, ships); Strategy activeStrategy = StrategyHolder.GetPlayerStrategy(socketId); List <CellOutcome> outcomes = activeStrategy.Accept(atackChangerVisitor); foreach (CellOutcome outcome in outcomes) { switch (outcome.attackOutcome) { case AttackOutcome.Hit: await Clients.Client(enemySockeId).SendAsync("pingAttack", outcome.posX, outcome.posY, "Hit", false); await Clients.Caller.SendAsync("pingAttack", outcome.posX, outcome.posY, "Hit", true); break; case AttackOutcome.Armor: duelsController.ChangeTurns(socketId); await Clients.Client(enemySockeId).SendAsync("pingAttack", outcome.posX, outcome.posY, "Armor", false); await Clients.Caller.SendAsync("pingAttack", outcome.posX, outcome.posY, "Armor", true); break; //For now, both [Missed and Invalid] trigger default switch default: duelsController.ChangeTurns(socketId); await Clients.Client(enemySockeId).SendAsync("pingAttack", outcome.posX, outcome.posY, "Missed", false); await Clients.Caller.SendAsync("pingAttack", outcome.posX, outcome.posY, "Missed", true); //turn change using the proxy class TurnOutcome turn = proxyPlayerTurn.ChangeTurn(); await Clients.Client(turn.InactiveId).SendAsync("changedTurn", turn.CallerTurn); await Clients.Client(turn.ActiveId).SendAsync("changedTurn", turn.OpponetTurn); break; } } }