Example #1
0
        private void StartNewMatch(OthelloPlayerServer othelloPlayer1, OthelloPlayerServer othelloPlayer2)
        {
            // Informs clients that an opponent has be found
            othelloPlayer1.OpponentFound(othelloPlayer2.Name, othelloPlayer2.PlayerType);
            othelloPlayer2.OpponentFound(othelloPlayer1.Name, othelloPlayer1.PlayerType);

            // GameManager will now handle clients and put them as InGame
            CreateMatch(othelloPlayer1, othelloPlayer2).Init();
        }
Example #2
0
        /// <summary>
        /// Matchmake a client
        /// </summary>
        /// <param name="othelloTCPClient"></param>
        /// <returns></returns>
        private bool Matchmake(OthelloPlayerServer othelloPlayer, PlayerType opponentType)
        {
            if (IsKnown(othelloPlayer))
            {
                registratedClients.Remove(othelloPlayer);
                searchingClients.TryAdd(othelloPlayer, opponentType);

                return(true);
            }
            else
            {
                Console.Error.WriteLine("Duplicate call for matchmaking with SingleClient");
                return(false);
            }
        }
Example #3
0
        /// <summary>
        /// Register a new client
        /// </summary>
        /// <param name="othelloPlayer"></param>
        /// <returns></returns>
        private bool Register(OthelloPlayerServer othelloPlayer)
        {
            if (!IsKnown(othelloPlayer))
            {
                // Add the client to the dictionnary
                registratedClients.Add(othelloPlayer);

                // Informs the client that he is now known to the server
                othelloPlayer.RegisterSuccessful(othelloPlayer.Name, othelloPlayer.PlayerType);

                return(true);
            }
            else
            {
                Console.Error.WriteLine("Duplicate call for matchmaking with SingleClient");
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// GameManager will now handle clients and put them as InGame
        /// </summary>
        /// <param name="othelloPlayer1"></param>
        /// <param name="othelloPlayer2"></param>
        private GameHandler CreateMatch(OthelloPlayerServer othelloPlayer1, OthelloPlayerServer othelloPlayer2)
        {
            GameHandler match;

            if (new Random().Next() % 2 == 0)
            {
                match = new GameHandler(othelloPlayer1, othelloPlayer2);
            }
            else
            {
                match = new GameHandler(othelloPlayer2, othelloPlayer1);
            }

            // Store the match
            matches.Add(match);

            return(match);
        }
Example #5
0
        private void DisconnectClient(OthelloPlayerServer client)
        {
            try
            {
                if (IsKnown(client))
                {
                    // Get the match
                    var currentMatch = GetMatch(client);

                    if (currentMatch == null)
                    {
                        // Remove the client from the matchmaking
                        registratedClients.Remove(client);
                        searchingClients.TryRemove(client, out PlayerType playerType);
                    }
                    else
                    {
                        // Warn the opponent
                        if (currentMatch.OthelloPlayer1.Equals(client))
                        {
                            currentMatch.OthelloPlayer2.OpponentDisconnected();
                        }
                        else if (currentMatch.OthelloPlayer2.Equals(client))
                        {
                            currentMatch.OthelloPlayer1.OpponentDisconnected();
                        }
                        else
                        {
                            throw new Exception("Opponent not found");
                        }
                    }
                }
                else
                {
                    Toolbox.LogError(new Exception("Client was not registred"));
                }
            }
            catch (Exception ex)
            {
                Toolbox.LogError(ex);
            }
        }
Example #6
0
        public void HandleOrder(IOrderHandler sender, Order order)
        {
            // If null, sender is this object otherwise the order has been redirected
            sender = sender ?? this;

            switch (order)
            {
            case PlayMoveOrder castedOrder:
                // Place a token on the board
                GameManager.PlayMove(castedOrder.Coords, (sender as OthelloPlayerServer).Color);

                // Send gameBoard to clients
                BroadcastGameboard();
                break;

            case GameStateRequestOrder castedOrder:
                // A client asked for the gameState, send it back to him
                (sender as OthelloPlayerServer).UpdateGameboard(GameManager.Export());
                break;

            case AvatarChangedOrder castedOrder:
                OthelloPlayerServer opponent = GetOpponent((sender as OthelloPlayerServer));
                opponent.OpponentAvatarChanged(castedOrder.AvatarID);
                break;

            case SaveRequestOrder castedOrder:
                // Send the saved game
                (sender as OthelloPlayerServer).SaveSuccessful(GameManager.Save());
                break;

            case UndoRequestOrder castedOrder:
                try
                {
                    // Goes one step back
                    GameManager.MoveBack();

                    // Goes back one more if the battle is against an IA
                    if (BattleType == BattleType.AgainstAI)
                    {
                        GameManager.MoveBack();
                    }

                    // Send gameboard
                    BroadcastGameboard();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Undo not available");
                    Toolbox.LogError(ex);
                }
                break;

            case RedoRequestOrder castedOrder:
                try
                {
                    // Goes one step forward
                    GameManager.MoveForward();

                    // Goes forward one more if the battle is against an IA
                    if (BattleType == BattleType.AgainstAI)
                    {
                        GameManager.MoveForward();
                    }

                    // Send gameboard
                    BroadcastGameboard();
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Redo not available");
                    Toolbox.LogError(ex);
                }
                break;

            case PlayerReadyOrder castedOrder:
                lock (locker)
                {
                    var castedSender = (sender as OthelloPlayerServer);

                    if (castedSender.Color == Color.Black)
                    {
                        client1Ready = true;
                    }
                    else if (castedSender.Color == Color.White)
                    {
                        client2Ready = true;
                    }

                    if (client1Ready && client2Ready)
                    {
                        StartGame();
                    }
                }
                break;

            case Order unknownOrder:
                throw new Exception("Unknown order received !");
            }
        }
Example #7
0
 private OthelloPlayerServer GetOpponent(OthelloPlayerServer othelloPlayer)
 {
     return(othelloPlayer.Equals(OthelloPlayer1) ? OthelloPlayer2 : OthelloPlayer1);
 }
Example #8
0
        /// <summary>
        /// Start to listen and accept clients
        /// </summary>
        /// <param name="env">Local or Online</param>
        /// <returns></returns>
        public bool StartListening(GameType env)
        {
            Environnement = env;
            if (Environnement == GameType.Local)
            {
                listener = new TcpListener(IPAddress.Parse(Tools.Properties.Settings.Default.LocalHostname), Tools.Properties.Settings.Default.LocalPort);
            }
            else
            {
                listener = new TcpListener(IPAddress.Parse(Tools.Properties.Settings.Default.OnlineHostname), Tools.Properties.Settings.Default.OnlinePort);
            }

            try
            {
                Running = true;

                // Start to listen
                listener.Start(100);

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for connections...");

                #region AcceptConnection

                // Accept any new connection
                new Task(() =>
                {
                    // Infinite loop
                    while (Running)
                    {
                        // Accept connection
                        if (listener.Pending())
                        {
                            // Store the new connection inside the client list
                            var newConnection = listener.AcceptTcpClient();

                            // PlayerType will be fetched during the register method inside the matchmaking
                            var client = new OthelloTCPClient();
                            client.Bind(newConnection);

                            // Generate a new player
                            var othelloPlayer = new OthelloPlayerServer(client);
                            othelloPlayer.SetOrderHandler(Matchmaker.Instance);

                            // DEBUG
                            Console.WriteLine("NEW CLIENT CONNECTED");
                        }

                        // Wait before reading again
                        Thread.Sleep(1);
                    }
                }).Start();

                #endregion
            }
            catch (Exception ex)
            {
                Toolbox.LogError(ex);
            }

            return(true);
        }
Example #9
0
 public LoadRequest(OthelloPlayerServer emitter, SaveFile savefile)
 {
     Player1  = emitter;
     SaveFile = savefile;
 }
Example #10
0
 public bool IsKnown(OthelloPlayerServer client)
 {
     return(registratedClients.Contains(client) || searchingClients.Keys.Contains(client));
 }
Example #11
0
 /// <summary>
 /// Retrieves the match in which the player plays
 /// </summary>
 /// <param name="othelloPlayer"></param>
 /// <returns>Match in which the player plays</returns>
 public GameHandler GetMatch(OthelloPlayerServer othelloPlayer)
 {
     return(matches.Where(match => match.OthelloPlayer1 == othelloPlayer || match.OthelloPlayer2 == othelloPlayer).FirstOrDefault());
 }