Inheritance: ServiceControl
Exemple #1
0
        public void ParseCommand(string command, string data)
        {
            Game game;

            switch (command)
            {
            case "CREATE":
                SendToClient($"CREATE;{GameService.CreateGame(_client)}");
                PrintConsoleMessage(ConsoleColor.Blue, $"{_client.LocalEndPoint} Created a game.", null);
                break;

            case "JOIN":
                var result = GameService.JoinGame(_client, data);
                SendToClient($"JOIN;{result}");
                if (!result)
                {
                    break;
                }
                if (GameService.GetActiveGame(data).IsStarted)
                {
                    SendToClient(
                        $"SENDGAMESTATUS;{JsonConvert.SerializeObject(GameService.GameStatuses.SingleOrDefault(g => g.GameId == int.Parse(data)))}");
                }
                game = GameService.GetActiveGame(data);
                SendToSocket(game.PlayerOne, "OPPONENTJOIN;");
                PrintConsoleMessage(ConsoleColor.Cyan, $"{_client.LocalEndPoint} Joined game {data}.", null);
                break;

            case "GETGAMES":
                SendToClient($"GETGAMES;{JsonConvert.SerializeObject(GameService.GetGameIds())}");
                PrintConsoleMessage(ConsoleColor.Gray, $"{_client.LocalEndPoint} Requested game list.", null);
                break;

            case "LEAVE":
                SendToClient("LEAVE;");
                GameService.LeaveGame(_client, data);
                PrintConsoleMessage(ConsoleColor.Yellow, $"{_client.LocalEndPoint} Has left game {data}.", null);
                break;

            case "START":
                game = GameService.GetActiveGame(data);
                var newGame = InitGamePlay(game);
                GameService.GameStatuses.Add(newGame);
                SendToSocket(game.PlayerOne, $"START;{JsonConvert.SerializeObject(newGame)}");
                SendToSocket(game.PlayerTwo, $"START;{JsonConvert.SerializeObject(newGame)}");
                PrintConsoleMessage(ConsoleColor.Green, $"{_client.LocalEndPoint} Started game {data}.", null);
                break;

            case "TURN":
                var gameStatus = JsonConvert.DeserializeObject <GameStatus>(data);
                game           = GameService.GetActiveGame(gameStatus.GameId.ToString());
                game.ScoreGrid = gameStatus.Grid;
                GameService.GameStatuses.Remove(GameService.GameStatuses.Find(g => g.GameId == gameStatus.GameId));
                GameService.GameStatuses.Add(gameStatus);
                SendToSocket(_client == game.PlayerOne ? game.PlayerTwo : game.PlayerOne,
                             $"TURN;{JsonConvert.SerializeObject(gameStatus)}");
                PrintConsoleMessage(ConsoleColor.White,
                                    $"{_client.LocalEndPoint} Played a round in game {game.GameId}.", null);
                if (CheckVictoryCondition(gameStatus.Grid))
                {
                    GameOver(game.PlayerOne, game.PlayerTwo, gameStatus.Turn == "X" ? "O" : "X");
                    PrintConsoleMessage(ConsoleColor.Magenta,
                                        $"{_client.LocalEndPoint} Has won game {game.GameId}!", null);
                }
                else if (gameStatus.Grid.Cast <string>().All(f => f != null))
                {
                    GameOver(game.PlayerOne, game.PlayerTwo, null);
                    PrintConsoleMessage(ConsoleColor.Magenta, $"{_client.LocalEndPoint} Game {game.GameId} ended with a draw.", null);
                }
                break;
            }
        }
Exemple #2
0
 public static void Initialize()
 {
     loginService  = new LoginService();
     updateService = new UpdateService();
     gameService   = new GameService();
 }