private void ProcessCommand(List <GwentClient> Pair, NetCommandPackage Command, int WhoIsTerm) { int NextIsTerm = (WhoIsTerm + 1) % 2; if (Command.Command == ConfigurationManager.AppSettings["LeaveGameCommand"]) { NetCommandPackage LeavePkg = new NetCommandPackage(); LeavePkg.Command = ConfigurationManager.AppSettings["LeaveGameCommand"]; Pair[NextIsTerm].SendMessage(LeavePkg); Pair[WhoIsTerm].IsConnectionLost = true; Pair[WhoIsTerm].IsTurnEnd = true; } else if (Command.Command == ConfigurationManager.AppSettings["PassedGameCommand"]) { Pair[WhoIsTerm].IsPassed = true; NetCommandPackage Passed = new NetCommandPackage(); Passed.Command = ConfigurationManager.AppSettings["PassedGameCommand"]; SendToClient(Pair[NextIsTerm], Passed); } else if (Command.Command == ConfigurationManager.AppSettings["TurnEndGameCommand"]) { Pair[WhoIsTerm].IsTurnEnd = true; } if (Command.Command == ConfigurationManager.AppSettings["SyncGameCommand"]) { SendToClient(Pair[NextIsTerm], Command); Pair[WhoIsTerm].Scope = Command.Scope; } }
private void InitBattle(List <GwentClient> Clients, int WhoIsTerm, int NextIsTerm) { NetCommandPackage StartGamePackage = new NetCommandPackage(); StartGamePackage.Command = ConfigurationManager.AppSettings["StartGameCommand"]; Clients[0].SendMessage(StartGamePackage); Clients[1].SendMessage(StartGamePackage); Console.WriteLine("Клиентам отправлено уведосление о начале игры"); GetSyncResponse(Clients); Console.WriteLine("Клиенты Синхронизировались"); Clients[WhoIsTerm].Scope = 0; Clients[NextIsTerm].Scope = 0; }
private void Game(List <GwentClient> Clients) { Random rnd = new Random(); int WhoIsTerm = rnd.Next(1); int NextIsTerm = (WhoIsTerm + 1) % 2; try { InitBattle(Clients, WhoIsTerm, NextIsTerm); NetCommandPackage TurnStart = new NetCommandPackage(); TurnStart.Command = ConfigurationManager.AppSettings["StartTurnGameCommand"]; NetCommandPackage TurnWait = new NetCommandPackage(); TurnWait.Command = ConfigurationManager.AppSettings["TurnWaitGameCommand"]; NextIsTerm = (WhoIsTerm + 1) % 2; while (!Clients[WhoIsTerm].IsConnectionLost && !Clients[NextIsTerm].IsConnectionLost) { if (!Clients[WhoIsTerm].IsPassed) { SendToClient(Clients[WhoIsTerm], TurnStart); SendToClient(Clients[NextIsTerm], TurnWait); Clients[WhoIsTerm].IsTurnEnd = false; ClientTurn(Clients, WhoIsTerm, NextIsTerm); } WhoIsTerm = NextIsTerm; NextIsTerm = (WhoIsTerm + 1) % 2; if (Clients[WhoIsTerm].IsPassed && Clients[NextIsTerm].IsPassed) { int WhoWin = GetWhoWin(Clients, WhoIsTerm, NextIsTerm); int WhoLost = (WhoWin + 1) % 2; Clients[WhoLost].Health--; ProccesRoundRezults(Clients, WhoWin, WhoLost); WhoIsTerm = WhoWin; NextIsTerm = WhoLost; Clients[WhoLost].IsPassed = false; Clients[WhoWin].IsPassed = false; } } Console.WriteLine("Сессия закончилась"); Clients[0].Dispose(); Clients[1].Dispose(); } catch (Exception Exeption) { Console.WriteLine(Exeption.Message); ProcessExeption(Clients, WhoIsTerm, NextIsTerm); } }
private void ProcessExeption(List <GwentClient> Clients, int WhoIsTerm, int NextIsTerm) { try { NetCommandPackage Error = new NetCommandPackage(); Error.Command = ConfigurationManager.AppSettings["ConnectionLost"]; Clients[NextIsTerm].SendMessage(Error); Clients[WhoIsTerm].SendMessage(Error); } catch (Exception InnerExeption) { Console.WriteLine(InnerExeption.Message); } finally { Clients[0].Dispose(); Clients[1].Dispose(); } }
private void ProccesRoundRezults(List <GwentClient> Clients, int WhoWin, int WhoLost) { if (Clients[WhoLost].Health == 0) { NetCommandPackage LostGameCommand = new NetCommandPackage(); LostGameCommand.Command = ConfigurationManager.AppSettings["LostGameCommand"]; Clients[WhoLost].SendMessage(LostGameCommand); NetCommandPackage WinGameCommand = new NetCommandPackage(); WinGameCommand.Command = ConfigurationManager.AppSettings["WinGameCommand"]; Clients[WhoWin].SendMessage(WinGameCommand); Clients[WhoWin].IsConnectionLost = true; Clients[WhoLost].IsConnectionLost = true; } else { NetCommandPackage LostRoundCommand = new NetCommandPackage(); LostRoundCommand.Command = ConfigurationManager.AppSettings["LostRoundCommand"]; SendToClient(Clients[WhoLost], LostRoundCommand); NetCommandPackage WinRoundCommand = new NetCommandPackage(); WinRoundCommand.Command = ConfigurationManager.AppSettings["WinRoundCommand"]; SendToClient(Clients[WhoWin], WinRoundCommand); } }