public async Task ReadySingleton() { var socketId = Context.ConnectionId; int playerId = playersController.GetPlayerId(socketId); int battleArenaId = baController.GetBAId(playerId); Duel duel = duelsController.CreateDuel(); if (duel.FirstPlayerSocketId == null) { duel.FirstPlayerSocketId = socketId; duel.FirstPlayerBAId = battleArenaId; _context.SaveChanges(); await Clients.Caller.SendAsync("pingMessage", playerId, "Waiting for opponent..."); } else if (duel.FirstPlayerSocketId != null && duel.SecondPlayerSocketId == null) { duel.SecondPlayerSocketId = socketId; duel.SecondPlayerBAId = battleArenaId; _context.SaveChanges(); string firstTurnSocketId = duelsController.SetTurn(duel); await Clients.All.SendAsync("pingMessage", null, "The game has started!"); await Clients.All.SendAsync("pingGameStarted"); TurnOutcome turn = proxyPlayerTurn.GetCurrentTurn(); await Clients.Client(turn.InactiveId).SendAsync("changedTurn", turn.CallerTurn); await Clients.Client(turn.ActiveId).SendAsync("changedTurn", turn.OpponetTurn); } else { await Clients.Caller.SendAsync("pingMessage", playerId, "No available duels at the moment..."); } }
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 void ClickPos(Pos p) { if (gameRunning) { Intersection i = intersections[p.gridLoc.key()]; TurnOutcome outcome = TryPlay(i); if (outcome == TurnOutcome.SUCESS) { playerPassed[currentPlayer] = false; AdvanceTurn(); } } }
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; } } }