コード例 #1
0
ファイル: Game1.cs プロジェクト: JackDobie/Server
        public void SetWordPacket(string _correctWord)
        {
            GameSetWordPacket packet = new GameSetWordPacket(_correctWord);

            SendPacket(packet);
        }
コード例 #2
0
        private void ClientMethod(int index)
        {
            try
            {
                Packet receivedMessage;

                while ((receivedMessage = clients[index].Read()) != null)
                {
                    switch (receivedMessage.packetType)
                    {
                    case PacketType.ChatMessage:
                        ChatMessagePacket chatPacket = (ChatMessagePacket)receivedMessage;
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (cli.Value != clients[index])
                            {
                                cli.Value.Send(chatPacket);
                            }
                        }
                        break;

                    case PacketType.NewName:
                        NewNamePacket namePacket = (NewNamePacket)receivedMessage;
                        try
                        {
                            clients[index].name = namePacket.newName;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Exception: " + e.Message);
                            break;
                        }
                        SendClientList();
                        break;

                    case PacketType.Connect:
                        ConnectPacket connectPacket = (ConnectPacket)receivedMessage;
                        clients[index].isGame = false;
                        clients[index].name   = connectPacket.userName;
                        SendClientList();
                        break;

                    case PacketType.Disconnect:
                        break;

                    case PacketType.PrivateMessage:
                        PrivateMessagePacket msgPacket = (PrivateMessagePacket)receivedMessage;
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (cli.Value.name == msgPacket.receiver)
                            {
                                cli.Value.Send(msgPacket);
                            }
                        }
                        break;

                    case PacketType.GameConnect:
                        GameConnectPacket gameConnectPacket = (GameConnectPacket)receivedMessage;
                        clients[index].isGame           = true;
                        clients[index].ID               = gameConnectPacket.ID;
                        clients[index].connectedPlayers = gameConnectPacket.connectedPlayers;
                        if (clients[index].connectedPlayers.Count >= 2)
                        {
                            break;
                        }
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (cli.Value.isGame)
                            {
                                if (cli.Value.connectedPlayers.Count < 2)
                                {
                                    if (cli.Value.ID != clients[index].ID)
                                    {
                                        cli.Value.connectedPlayers.Add(gameConnectPacket.ID);
                                        clients[index].connectedPlayers.Add(cli.Value.ID);
                                        cli.Value.Send(new GameConnectPacket(gameConnectPacket.ID, cli.Value.connectedPlayers, GameConnectPacket.PlayerType.Chooser));
                                        clients[index].Send(new GameConnectPacket(gameConnectPacket.ID, clients[index].connectedPlayers, GameConnectPacket.PlayerType.Guesser));
                                        break;     //break out of foreach so only connect to one other player
                                    }
                                }
                            }
                        }
                        break;

                    case PacketType.GameDisconnect:
                        GameDisconnectPacket gameDisconnectPacket = (GameDisconnectPacket)receivedMessage;
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (clients[index].connectedPlayers.Contains(cli.Value.ID))
                            {
                                if (cli.Value.ID != clients[index].ID)
                                {
                                    cli.Value.Send(gameDisconnectPacket);
                                    break;     //send to the other player, not to the player that sent the packet
                                }
                            }
                        }
                        break;

                    case PacketType.GameResult:
                        GameResultPacket resultPacket = (GameResultPacket)receivedMessage;
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (clients[index].connectedPlayers.Contains(cli.Value.ID))
                            {
                                cli.Value.Send(resultPacket);
                            }
                        }
                        break;

                    case PacketType.GameUpdateDisplayedWord:
                        GameUpdateWordPacket updateWordPacket = (GameUpdateWordPacket)receivedMessage;
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (clients[index].connectedPlayers.Contains(cli.Value.ID))
                            {
                                cli.Value.Send(updateWordPacket);
                            }
                        }
                        break;

                    case PacketType.GameUpdateHangmanState:
                        GameUpdateHangmanPacket updateHangmanPacket = (GameUpdateHangmanPacket)receivedMessage;
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (clients[index].connectedPlayers.Contains(cli.Value.ID))
                            {
                                cli.Value.Send(updateHangmanPacket);
                            }
                        }
                        break;

                    case PacketType.GameSetWord:
                        GameSetWordPacket setWordPacket = (GameSetWordPacket)receivedMessage;
                        foreach (KeyValuePair <int, Client> cli in clients)
                        {
                            if (clients[index].connectedPlayers.Contains(cli.Value.ID))
                            {
                                cli.Value.Send(setWordPacket);
                            }
                        }
                        break;

                    default:
                        Console.WriteLine("Received packet of type " + receivedMessage.packetType);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                clients[index].Close();
                clients.TryRemove(index, out Client c);
                SendClientList();
            }
        }
コード例 #3
0
ファイル: Game1.cs プロジェクト: JackDobie/Server
        void ProcessServerResponse()
        {
            int byteNum;

            try
            {
                while (connected)
                {
                    if ((byteNum = reader.ReadInt32()) != 0)
                    {
                        byte[]       buffer    = reader.ReadBytes(byteNum);
                        MemoryStream memstream = new MemoryStream(buffer);

                        Packet packet = formatter.Deserialize(memstream) as Packet;

                        switch (packet.packetType)
                        {
                        case PacketType.GameConnect:
                            GameConnectPacket connectPacket = (GameConnectPacket)packet;
                            connectedPlayers.Add(connectPacket.ID);
                            currentPlayerType = connectPacket.playerType == GameConnectPacket.PlayerType.Chooser ? PlayerType.Chooser : PlayerType.Guesser;
                            SwitchScreen(GameScreen.WaitForStart);
                            break;

                        case PacketType.GameDisconnect:
                            stopGame        = true;
                            stopGameMessage = "The other player has disconnected";
                            break;

                        case PacketType.GameSetWord:
                            GameSetWordPacket wordPacket = (GameSetWordPacket)packet;
                            correctWord    = wordPacket.correctWord;
                            displayedWord  = "";
                            displayedWord += new string('_', correctWord.Length);
                            SwitchScreen(GameScreen.Main);
                            break;

                        case PacketType.GameUpdateDisplayedWord:
                            if (currentPlayerType == PlayerType.Chooser)
                            {
                                GameUpdateWordPacket updateWordPacket = (GameUpdateWordPacket)packet;
                                displayedWord = updateWordPacket.displayedWord;
                            }
                            break;

                        case PacketType.GameUpdateHangmanState:
                            if (currentPlayerType == PlayerType.Chooser)
                            {
                                GameUpdateHangmanPacket updateHangmanPacket = (GameUpdateHangmanPacket)packet;
                                hangmanState = updateHangmanPacket.hangmanState;
                            }
                            break;

                        case PacketType.GameResult:
                            GameResultPacket resultPacket = (GameResultPacket)packet;
                            if (resultPacket.win)
                            {
                                stopGame        = true;
                                stopGameMessage = currentPlayerType == PlayerType.Guesser ? "You win!" : "The guesser won!";
                            }
                            else
                            {
                                stopGame        = true;
                                stopGameMessage = currentPlayerType == PlayerType.Guesser ? "You lost!\nThe correct word was " + correctWord : "The guesser lost!\nThe correct word was " + correctWord;
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                connected = false;
            }
        }