Пример #1
0
        public static NetOutgoingMessage CreateFoundGameMessage(NetPeer sender, FoundGame foundGame)
        {
            NetOutgoingMessage output = sender.CreateMessage();

            output.Write(ByteSerializer.ObjectToByteArray(foundGame));
            return(output);
        }
Пример #2
0
        void ListFoundHosts()
        {
            foreach (KeyValuePair <IPEndPoint, FoundGame> valuePair in DiscoveredHosts)
            {
                FoundGame fs = valuePair.Value;

                Console.WriteLine($"Host {valuePair.Key}:");
                Console.WriteLine($" Grid {fs.gridSizeX} * {fs.gridSizeY}");
                Console.WriteLine($" {fs.maxPlayers} Max players, {fs.minPlayers} Min players");
                Console.WriteLine($" {fs.currentPlayers} Current players");
            }
        }
Пример #3
0
        void ReadMessage()
        {
            NetIncomingMessage inMsg;

            if ((inMsg = peer.ReadMessage()) != null)
            {
#if DEBUG
                Console.WriteLine($"msg: {inMsg.MessageType}");
#endif
                switch (inMsg.MessageType)
                {
                case NetIncomingMessageType.Data:
                    // Handle custom messages
                    Process(inMsg);
                    break;

                case NetIncomingMessageType.DiscoveryResponse:
                    try
                    {
                        // Parse game data
                        FoundGame fg = ByteSerializer.ByteArrayToObject <FoundGame>(inMsg.Data);
                        // skip if game is already discovered / up to date
                        if (DiscoveredHosts.Contains(new KeyValuePair <IPEndPoint, FoundGame>(inMsg.SenderEndPoint, fg)))
                        {
                            break;
                        }
                        // Add server to list
                        DiscoveredHosts.Add(inMsg.SenderEndPoint, fg);
                        Console.WriteLine($"Discovered host at: {inMsg.SenderEndPoint}");
                    }
                    catch { }

                    break;

                case NetIncomingMessageType.DiscoveryRequest:
                    if (isHost && !gameActive)
                    {
                        peer.SendDiscoveryResponse(PacketFactory.CreateFoundGameMessage(peer, FoundGame.CreateFromGrid(grid, peer.ConnectionsCount + 1)), inMsg.SenderEndPoint);
                    }
                    break;

                case NetIncomingMessageType.StatusChanged:
                    // handle connection status messages
                    if (isHost)
                    {
                        switch (inMsg.SenderConnection.Status)
                        {
                        case NetConnectionStatus.Connected:
                            if (isHost)
                            {
                                PlayerConnection np = new PlayerConnection(Player.GetRandomisedPlayer(), inMsg.SenderConnection.RemoteEndPoint);
                                peers.Add(np);
                                players.Add(np.player);
                            }

                            Console.WriteLine($"{inMsg.SenderConnection.RemoteEndPoint} {inMsg.SenderConnection.Status}");
                            Console.WriteLine($"{peers.Count + 1} players currently connected");
                            break;

                        case NetConnectionStatus.Disconnected:
                            if (isHost)
                            {
                                PlayerConnection p = (from peer in peers where Equals(inMsg.SenderConnection.RemoteEndPoint, peer.ipEndPoint) select peer).Single();
                                peers.Remove(p);
                                players.Remove(p.player);
                            }

                            Console.WriteLine($"{inMsg.SenderConnection.RemoteEndPoint} {inMsg.SenderConnection.Status}");
                            Console.WriteLine($"{peers.Count + 1} players currently connected");
                            break;
                        }
                    }
                    else
                    {
                        switch (inMsg.SenderConnection.Status)
                        {
                        case NetConnectionStatus.Connected:
                        case NetConnectionStatus.Disconnected:
                            Console.WriteLine($"{inMsg.SenderConnection.RemoteEndPoint} {inMsg.SenderConnection.Status}");
                            Console.WriteLine($"{peer.Connections.Count + 1} players currently connected");
                            break;
                        }
                    }

                    break;

                case NetIncomingMessageType.ConnectionApproval:
                    if (isHost)
                    {
                        if (gameActive)
                        {
                            inMsg.SenderConnection.Deny("Game already started");
                            break;
                        }

                        // not max players
                        if (grid.maxPlayers >= peer.ConnectionsCount + 1)
                        {
                            inMsg.SenderConnection.Approve();
                        }
                        else
                        {
                            inMsg.SenderConnection.Deny("Game is full");
                        }
                    }
                    else
                    {
                        inMsg.SenderConnection.Approve();      // TODO: Validate with gameID
                    }
                    break;

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

                default: throw new ArgumentOutOfRangeException();
                }

                peer.Recycle(inMsg);
            }
        }