Ejemplo n.º 1
0
        // Lives in a Thread
        private void AcceptConnections()
        {
            while (true)
            {
                Console.WriteLine("waiting for connection");
                TcpClient client = ConnectionListener.AcceptTcpClient();
                // remove disconnected players
                Dispatcher.RemoveDisconnectedPlayers();

                // Try to connect Red player first.
                if (!Dispatcher.ConnectPlayer(Side.Red, client))
                {
                    // If that didn't work, try to connect White player.
                    if (!Dispatcher.ConnectPlayer(Side.White, client))
                    {
                        Console.WriteLine("Player connect attempt failed.");
                        // If that didn't work either, send a message to the connection saying Join didn't work.
                        Dispatcher.SendMessage(client, JoinMessage(Side.Unknown));
                    }
                    else
                    {
                        Console.WriteLine("White player connected at " + Dispatcher.WhoIsPlayer(Side.White));
                        // Connected as white! notify game client.
                        Dispatcher.SendMessage(Side.White, JoinMessage(Side.White));
                    }
                }
                else
                {
                    Console.WriteLine("Red player connected at " + Dispatcher.WhoIsPlayer(Side.Red));
                    // Connected as red! notify game client.
                    Dispatcher.SendMessage(Side.Red, JoinMessage(Side.Red));
                }

                // if both players were connected, set server mode to ongoing game
                if (Dispatcher.PlayerConnected(Side.Red) && Dispatcher.PlayerConnected(Side.White))
                {
                    Mode = ServerMode.OngoingGame;
                }
                else
                {
                    Mode = ServerMode.WaitingForPlayers;
                }
            }
        }