Beispiel #1
0
        void NextTurn()
        {
            playerTurnIndex++;
            PlayerConnection client = clients[playerTurnIndex % clients.Count];

            server.SendToAll(client.CreateNextTurnMessage(server), NetDeliveryMethod.ReliableOrdered);
        }
Beispiel #2
0
        public static NetOutgoingMessage CreateNextTurnMessage(this PlayerConnection client, NetPeer sender)
        {
            NetOutgoingMessage output = sender.CreateMessage();

            output.Write(PacketType.NextTurn);
            output.Write(client.player.Name);
            return(output);
        }
Beispiel #3
0
        public static NetOutgoingMessage CreatePlayerDisconnectedMessage(this PlayerConnection client, NetPeer sender)
        {
            NetOutgoingMessage output = sender.CreateMessage();

            output.Write(PacketType.PlayerDisconnected);
            output.Write(ByteSerializer.ObjectToByteArray(client.player));
            return(output);
        }
Beispiel #4
0
        void PlayerDisconnect(PlayerConnection client)
        {
            // Remove client from clients list
            clients.Remove(client);
            // Send PlayerDisconnect message to other players
            NetOutgoingMessage outMsg = client.CreatePlayerDisconnectedMessage(server);

            server.SendToAll(outMsg, client.connection, NetDeliveryMethod.ReliableOrdered, 0);
        }
Beispiel #5
0
 void PlayerConnect(PlayerConnection client)
 {
     // NOTE: At this point we assume the player have already been checked for duplications
     // Add client to clients list
     clients.Add(client);
     // if there are others than the new client,
     if (clients.Count > 1)
     {
         // Send PlayerConnected message to other players
         NetOutgoingMessage outMsg = client.CreatePlayerConnectedMessage(server);
         server.SendToAll(outMsg, client.connection, NetDeliveryMethod.ReliableOrdered, 0);
     }
 }
Beispiel #6
0
        void SendGameData(PlayerConnection recipient)
        {
            NetOutgoingMessage outMsg = server.CreateMessage();

            // Grid
            outMsg = grid.CreateGridDataMessage(server);
            server.SendMessage(outMsg, recipient.connection, NetDeliveryMethod.ReliableOrdered);
            // Players
            foreach (PlayerConnection c in clients)
            {
                if (c == recipient)
                {
                    continue;                 // Skip recipient
                }
                outMsg = c.CreatePlayerConnectedMessage(server);
                server.SendMessage(outMsg, recipient.connection, NetDeliveryMethod.ReliableOrdered);
            }

            // Turn
            outMsg = clients[playerTurnIndex % clients.Count].CreateNextTurnMessage(server);
            server.SendMessage(outMsg, recipient.connection, NetDeliveryMethod.ReliableOrdered);
        }
Beispiel #7
0
        public void Run()
        {
            Running = true;
            Console.WriteLine("Starting network loop");
            while (Running)
            {
                server.MessageReceivedEvent.WaitOne(); // Q: should we wait every loop or only the first?
                NetIncomingMessage inMsg;
                while ((inMsg = server.ReadMessage()) != null)
                {
                    Console.WriteLine($"msg {inMsg.MessageType}");
                    switch (inMsg.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                        Process(inMsg);
                        break;

                    case NetIncomingMessageType.DebugMessage:
                        Console.WriteLine(inMsg.ReadString());
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        Console.WriteLine(inMsg.SenderConnection.Status);
                        switch (inMsg.SenderConnection.Status)
                        {
                        case NetConnectionStatus.Connected:
                            PlayerConnection c = GetPlayerConnection(inMsg.SenderConnection);
                            Console.WriteLine($"{c.player.Name} has connected from {c.connection.RemoteEndPoint}.");
                            SendGameData(c);
                            // Check if there are enough players to start the game
                            if (clients.Count >= grid.minPlayers)
                            {
                                // TODO: start voting to start game
                                // Start game
                                server.SendToAll(PacketFactory.CreateStartGameMessage(server), NetDeliveryMethod.ReliableOrdered);
                            }

                            break;

                        case NetConnectionStatus.Disconnected:
                            PlayerConnection pc = GetPlayerConnection(inMsg.SenderConnection);
                            Console.WriteLine($"{pc.player.Name} has disconnected.");
                            PlayerDisconnect(pc);
                            break;
                        }

                        break;

                    case NetIncomingMessageType.ConnectionApproval:
                        // Compare existing connections
                        if ((from client in clients where client.connection == inMsg.SenderConnection select client).Count() != 0)
                        {
                            // If there is an existing connection, reject connection
                            string reason = "Connection already exists";
                            Console.WriteLine($"Player failed to connect: {reason}");
                            inMsg.SenderConnection.Deny(reason);
                            break;
                        }

                        // Check max players
                        if (clients.Count < grid.maxPlayers == false)
                        {
                            string reason = "Max players reached";
                            Console.WriteLine($"Player failed to connect: {reason}");
                            inMsg.SenderConnection.Deny(reason);
                            break;
                        }

                        // Parse player data
                        Player newPlayer = (Player)ByteSerializer.ByteArrayToObject(inMsg.Data);

                        // Compare with existing players
                        // If name or shape and drawingColor already is used
                        if ((from client in clients
                             where string.Equals(newPlayer.Name, client.player.Name, StringComparison.CurrentCultureIgnoreCase) ||     //TODO: refactor into two where
                             newPlayer.Shape == client.player.Shape && newPlayer.Color == client.player.Color
                             select client).Count() != 0)
                        {
                            // If there is a match, reject connection
                            string reason = "Player already exists";
                            Console.WriteLine($"Player failed to connect: {reason}");
                            inMsg.SenderConnection.Deny(reason);
                            break;
                        }

                        // If no match found, accept connection
                        PlayerConnect(new PlayerConnection(inMsg.SenderConnection, (Player)ByteSerializer.ByteArrayToObject(inMsg.Data)));
                        inMsg.SenderConnection.Approve();
                        // Send game data
                        break;

                    case NetIncomingMessageType.DiscoveryRequest:     // TODO: send found server data
                        Console.WriteLine("Discovery Request from Client");
                        NetOutgoingMessage outMsg = server.CreateMessage();
                        FoundServer        v      = new FoundServer(grid.sizeX, grid.sizeY, grid.maxPlayers, grid.minPlayers, clients.Count);
                        outMsg.Write(ByteSerializer.ObjectToByteArray(v));
                        server.SendDiscoveryResponse(outMsg, inMsg.SenderEndPoint);
                        break;

                    default:
                        Console.WriteLine($"Unhandled message received of type: {inMsg.MessageType}");
                        break;
                    }

                    server.Recycle(inMsg);
                }
            }

            Running = false;
        }