Esempio n. 1
0
        public void OnPong(IObjectConnection connection, PongObject pongObject)
        {
            var info = Connections[connection];

            info.HasPonged = true;
            info.LastTicks = DateTime.Now.Ticks;
        }
Esempio n. 2
0
        public void OnClientConnected(IObjectConnection connection)
        {
            lock (connectionLock)
                if (this.connection != null)
                {
                    var manager = new TicTacToeManager(this.connection, connection);

                    this.connection.AddEventHandler(manager);
                    connection.AddEventHandler(manager);

                    // Between creating the TicTacToeManager and adding it as an eventhandler to each connection
                    // the other connection could disconnect and the other would be stuck in a ghost game
                    // so check if one the connections has disconnected and notify the manager.
                    if (this.connection.IsStopped)
                    {
                        manager.OnDisconnect(this.connection);
                    }

                    if (connection.IsStopped)
                    {
                        manager.OnDisconnect(connection);
                    }

                    this.connection.RemoveEventHandlers(this);
                    connection.RemoveEventHandlers(this);
                    this.connection = null;
                }
                else
                {
                    this.connection = connection;
                }
        }
Esempio n. 3
0
 public void OnClientDisconnected(IObjectConnection connection)
 {
     lock (connectionLock)
         if (this.connection == connection)
         {
             this.connection = null;
         }
 }
Esempio n. 4
0
        public void OnClientConnected(IObjectConnection connection)
        {
            var name = GenerateUniqueName();

            clients[connection] = name;
            BroadCast(new ClientConnected {
                Name = name
            });
        }
Esempio n. 5
0
        public void OnChatMessageReceived(IObjectConnection connection, string message)
        {
            var name = clients[connection];

            BroadCast(new Message
            {
                SenderName = name,
                Content    = message
            });
        }
Esempio n. 6
0
        public void OnClientDisconnected(IObjectConnection connection)
        {
            var name = clients[connection];

            clients.Remove(connection);
            BroadCast(new ClientDisconnected
            {
                Name = name
            });
        }
Esempio n. 7
0
 public TicTacToeManager(IObjectConnection player1, IObjectConnection player2)
 {
     for (int x = 0; x < grid.Length; x++)
     {
         grid[x] = new object[3];
     }
     this.player1 = player1;
     this.player2 = player2;
     StartGame();
 }
Esempio n. 8
0
 private void RandomTurn()
 {
     if (r.Next(2) == 1)
     {
         playerTurn = player1;
     }
     else
     {
         playerTurn = player2;
     }
 }
Esempio n. 9
0
        public void OnDisconnect(IObjectConnection connection)
        {
            if (connection == player1)
            {
                player2.SendObject(new YouWon());
            }
            else
            {
                player1.SendObject(new YouWon());
            }

            EndGame();
        }
Esempio n. 10
0
 public void OnGameStarted(IObjectConnection connection, GameStarted started)
 {
     if (started.YourTurn)
     {
         OutputBoard();
         Task.Run(YourTurn);
     }
     else
     {
         OutputBoard();
         Console.WriteLine("Waiting for your opponent to make his play");
     }
 }
        public void CallDisconnect(IObjectConnection connection)
        {
            var previous = DisconnectObject;

            while (previous != null)
            {
                if (previous.CanExecute?.Invoke(connection) ?? true)
                {
                    previous.Action.Invoke(connection);
                }

                previous = previous.Next;
            }
        }
Esempio n. 12
0
        private void NextPlayerTurn(int x, int y)
        {
            if (playerTurn == player1)
            {
                playerTurn = player2;
            }
            else
            {
                playerTurn = player1;
            }

            playerTurn.SendObject(new YourTurn {
                X = x, Y = y
            });
        }
 public void CallCommand(string protocol, object parameter, IObjectConnection connection)
 {
     if (CommandObjects.ContainsKey(protocol))
     {
         var commandObj = CommandObjects[protocol];
         if (commandObj.CanExecute?.Invoke(connection) ?? true)
         {
             var deserializedObject = commandObj.DeserializerMethod((dynamic)parameter);
             if (deserializedObject != null)
             {
                 commandObj.CommandMethod(connection, deserializedObject);
             }
         }
     }
 }
Esempio n. 14
0
        public void OnMove(IObjectConnection connection, Move move)
        {
            if (connection != playerTurn)
            {
                return;
            }

            if (CanPlace(move.X, move.Y))
            {
                Place(move.X, move.Y);
                if (HasPlayerWon())
                {
                    EndGame();
                }
                else
                {
                    NextPlayerTurn(move.X, move.Y);
                }
            }
        }
Esempio n. 15
0
 public void OnChatMessageReceived(IObjectConnection connection, Message message)
 {
     Console.WriteLine("[" + message.SenderName + "]: " + message.Content);
 }
 public void BadOnDisconnect(IObjectConnection con, int data)
 {
     HasBadOnDisconnectBeenCalled = true;
 }
 public void OnCommand(IObjectConnection con, string connection)
 {
     HasOnCommandBeenCalled = true;
 }
Esempio n. 18
0
 public void OnDisconnect(IObjectConnection connection)
 {
     Connections.Remove(connection);
 }
Esempio n. 19
0
 public void OnYouLost(IObjectConnection connection, YouWon won)
 {
     Console.Clear();
     Console.WriteLine("You won.");
 }
Esempio n. 20
0
 public void OnYouLost(IObjectConnection connection, YouLost lost)
 {
     Console.Clear();
     Console.WriteLine("You lost.");
 }
Esempio n. 21
0
 public void OnYourTurn(IObjectConnection connection, YourTurn turn)
 {
     grid[turn.X][turn.Y] = 2;
     OutputBoard();
     Task.Run(YourTurn);
 }
Esempio n. 22
0
 public void OnPing(IObjectConnection connection, PingObject pingObject)
 {
     connection.SendObject(new PongObject());
 }
Esempio n. 23
0
 public void OnConnected(IObjectConnection connection)
 {
     Console.WriteLine("Connected to the server.");
     Console.WriteLine("Waiting for another player...");
     this.connection = connection;
 }
 public void BadOnCommand(string connection, IObjectConnection con)
 {
     HasBadOnCommandBeenCalled = true;
 }
Esempio n. 25
0
 public void OnConnect(IObjectConnection connection)
 {
     Connections[connection] = new IdleInfo {
         LastTicks = DateTime.Now.Ticks, HasPonged = true
     };
 }
 public void OnDisconnect(IObjectConnection con)
 {
     HasOnDisconnectBeenCalled = true;
 }
Esempio n. 27
0
 public void OnClientDisconnected(IObjectConnection connection, ClientDisconnected info)
 {
     Console.WriteLine(info.Name + " has disconnected");
 }
Esempio n. 28
0
 public void OnDisconnect(IObjectConnection connection)
 {
     Console.WriteLine("You have been disconnected from the server");
 }
 public void BadOnConnect(IObjectConnection con, string data)
 {
     HasBadOnConnectBeenCalled = true;
 }