Пример #1
0
        void Matching()
        {
            const int MaxPlayer = 2;

            while (m_matchList.Count >= MaxPlayer)
            {
                Client[] clients = new Client[MaxPlayer];
                for (int i = 0; i < MaxPlayer; i++)
                {
                    clients[i] = m_matchList.First.Value;
                    m_matchList.RemoveFirst();
                }

                Game game = new Game(this, m_database);

                for (int i = 0; i < MaxPlayer; i++)
                {
                    Client client = clients[i];
                    Player player = new Player();
                    player.AccountId = client.AccountId;
                    player.Name      = client.Name;
                    player.Out       = client.Out;
                    game.AddPlayer(player);
                    client.EnterGame(game, player);
                }

                m_games.Add(game);
            }
        }
Пример #2
0
        public void Practice(string name)
        {
            m_game = new Game(this, m_database);
            Player player = new Player();

            player.AccountId = 1;
            player.Name      = name;
            player.Out       = m_serverSession.Out;
            m_game.AddPlayer(player);
            m_player = player;
        }
Пример #3
0
        public void Practice(Client client)
        {
            Game game = new Game(this, m_database);

            Player player = new Player();

            player.AccountId = client.AccountId;
            player.Name      = client.Name;
            player.Out       = client.Out;
            game.AddPlayer(player);
            client.EnterGame(game, player);

            m_games.Add(game);
        }
        public void Run()
        {
            using (Stream stream = client.GetStream())
            {
                StreamReader reader = new StreamReader(stream);
                StreamWriter writer = new StreamWriter(stream);

                writer.AutoFlush = true;

                //ignore rejoin message as functionality has not been implemented in C#
                reader.ReadLine();

                player = new Player(game.getNewClientID(), game.IsEmpty(), writer, reader);



                if (game.IsEmpty())
                {
                    Console.WriteLine($"Player connected with ID: {player.ID} and recieved the ball");
                }
                else
                {
                    Console.WriteLine($"Player connected with ID: {player.ID}");
                }

                game.AddPlayer(player);

                writer.WriteLine($"ASSIGN_ID {player.ID}");

                writer.WriteLine($"SEND_GAMESTATE {game.GetPlayersAsString()} {game.GetPlayerWithBall().ID}");



                try
                {
                    while (true)
                    {
                        string   line       = reader.ReadLine();
                        string[] substrings = line.Split(' ');

                        string command = substrings[0];

                        switch (command.ToUpper())
                        {
                        case "SEND_BALL":
                            int    playerID        = int.Parse(substrings[1]);
                            Player recievingPlayer = game.GetPlayerFromID(playerID);

                            if (recievingPlayer != null)
                            {
                                game.SendBallTo(player, recievingPlayer, true);
                            }
                            break;
                        }
                    }
                }
                catch (Exception)
                {
                    client.Close();
                }
                finally
                {
                    Console.WriteLine($"Player {player.ID} disconnected.");
                    game.RemovePlayer(player);
                }
            }
        }