Ejemplo n.º 1
0
        public GameDisplayPanel(ParentComponent parent, int index, MultiplayerGame multiplayerGame)
            : base(parent, new Rectangle())
        {
            this.index = index;
            this.bounds = new Rectangle(5, 5 + (componentHeight + componentSpacing) * this.index,
                580,
                componentHeight);
            this.border = new Border(this, 2, Color.Pink);

            this.multiplayerGame = multiplayerGame;

            XNALabel gameNameLbl = new XNALabel(this, new Rectangle(5, 5, 180, 30), this.multiplayerGame.gamename);
            mapNameLbl = new XNALabel(this, new Rectangle(195, 5, 180, 30), this.multiplayerGame.mapname);

            joinGameBtn = new XNAButton(this, new Rectangle(475, 5, 100, 30), "Join game");
            joinGameBtn.onClickListeners += this.JoinGame;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called when the server recieved data from the client.
        /// </summary>
        /// <param name="data">The data that was sent</param>
        public void OnPacketReceived(Packet p)
        {
            switch (p.GetHeader())
            {
                case Headers.KEEP_ALIVE:
                    {
                        lastAliveTicks = System.DateTime.UtcNow.Ticks;
                        break;
                    }
                case Headers.HANDSHAKE_1:
                    {
                        client.SendPacket(new Packet(Headers.HANDSHAKE_2));
                        break;
                    }
                case Headers.HANDSHAKE_3:
                    {
                        // Nothing
                        break;
                    }
                case Headers.CLIENT_DISCONNECT:
                    {
                        this.OnDisconnect();
                        break;
                    }
                case Headers.CLIENT_USERNAME:
                    {
                        this.user = new ServerUser(PacketUtil.DecodePacketString(p, 0), this);

                        Packet newPacket = new Packet(Headers.CLIENT_USER_ID);
                        newPacket.AddInt(this.user.id);
                        this.client.SendPacket(newPacket);

                        // Put the user in channel 1 (the main lobby)
                        ChannelManager.GetInstance().GetChannelByID(1).AddUser(user);

                        break;
                    }
                case Headers.CLIENT_CHANNEL:
                    {
                        Console.Error.WriteLine(this.user + " tried to change his channel! That's not allowed.");
                        //int newChannel = PacketUtil.DecodePacketInt(p, 0);
                        //this.user.ChangeChannel(newChannel);
                        break;
                    }
                case Headers.CHAT_MESSAGE:
                    {
                        // Get the channel
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        // Get the message
                        String message = PacketUtil.DecodePacketString(p, 4);
                        // Send it to the users!
                        ChannelManager.GetInstance().GetChannelByID(channel).SendMessageToUsers(message);
                        break;
                    }
                case Headers.CLIENT_CREATE_GAME:
                    {
                        if (this.user.channelID != 1)
                        {
                            Console.Error.WriteLine("Received a request to create a channel that is NOT from a user in the lobby!");
                            return;
                        }
                        // User that requested this
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        // Name of the game
                        String gameName = PacketUtil.DecodePacketString(p, 4);
                        // Create game
                        MultiplayerGame mg = new MultiplayerGame(
                            MultiplayerGameManager.GetInstance().RequestGameID(),
                            gameName, "<No map selected yet>");
                        mg.host = (ServerUser)ServerUserManager.GetInstance().GetUserByID(userID);
                        // Add game to list
                        MultiplayerGameManager.GetInstance().games.AddLast(mg);
                        Console.Out.WriteLine("Created a game with ID = " + mg.id);

                        // Return the host its channel and game ID
                        Packet gameIDPacket = new Packet(Headers.GAME_ID);
                        gameIDPacket.AddInt(mg.id);
                        this.client.SendPacket(gameIDPacket);

                        this.user.ChangeChannel(mg.id);

                        // Notify all others in the channel that the game was created.
                        ChannelManager.GetInstance().GetChannelByID(1).CreatedGame(mg);
                        break;
                    }
                case Headers.GAME_MAP_CHANGED:
                    {

                        break;
                    }
                case Headers.CLIENT_DESTROY_GAME:
                    {
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByHost(this.user);
                        if (game == null)
                        {
                            Console.Error.WriteLine("Client " + user.id + " tried to destroy a game that is not his!");
                        }
                        else
                        {
                            // Tell everyone in the lobby that the game was destroyed.
                            ChannelManager.GetInstance().GetChannelByID(1).DestroyGame(game);
                            // Tell everyone in the game itsself that the game was destroyed.
                            ChannelManager.GetInstance().GetChannelByID(game.id).DestroyGame(game);
                            // Remove it completely
                            MultiplayerGameManager.GetInstance().games.Remove(game);
                        }
                        break;
                    }
                case Headers.CLIENT_REQUEST_JOIN:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByID(gameID);
                        Packet joinPacket = new Packet(Headers.SERVER_REQUEST_JOIN);
                        joinPacket.AddInt(gameID);
                        joinPacket.AddInt(userID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(game.host.id)).chatListener.client.SendPacket(
                           joinPacket);
                        break;
                    }
                case Headers.CLIENT_OK_JOIN:
                    {
                        // Notify the client of the response by host.
                        int gameID = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByID(gameID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).chatListener.client.SendPacket(p);
                        // Change this user's channel to the game channel.
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).chatListener.user.ChangeChannel(gameID);
                        break;
                    }
                case Headers.CLIENT_GAME_FULL:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByID(gameID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).chatListener.client.SendPacket(p);
                        break;
                    }
                case Headers.CLIENT_LEFT_GAME:
                    {
                        // Put user back in lobby.
                        this.user.ChangeChannel(1);
                        break;
                    }
                case Headers.GAME_COLOR_CHANGED:
                    {
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int color = PacketUtil.DecodePacketInt(p, 8);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).color = color;
                        Channel c = ChannelManager.GetInstance().GetChannelByID(channel);
                        c.ChangeColor(userID, color);
                        break;
                    }
                case Headers.GAME_TEAM_CHANGED:
                    {
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int team = PacketUtil.DecodePacketInt(p, 8);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).team = team;
                        Channel c = ChannelManager.GetInstance().GetChannelByID(channel);
                        c.ChangeTeam(userID, team);
                        break;
                    }
                case Headers.GAME_READY_CHANGED:
                    {
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int readyState = PacketUtil.DecodePacketInt(p, 8);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).readyState = readyState;
                        Channel c = ChannelManager.GetInstance().GetChannelByID(channel);
                        c.ChangeReadyState(userID, readyState);
                        break;
                    }
                case Headers.GAME_KICK_CLIENT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        Packet kickPacket = new Packet(Headers.GAME_KICK_CLIENT);
                        kickPacket.AddInt(userID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).
                            chatListener.client.SendPacket(kickPacket);

                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).ChangeChannel(1);
                        break;
                    }
                case Headers.CLIENT_GAME_START:
                    {
                        System.Timers.Timer gameStartTimer = new System.Timers.Timer();
                        gameStartTimer.Elapsed += new ElapsedEventHandler(GameStart);
                        gameStartTimer.Interval = 1000;
                        gameStartTimer.Start();
                        this.gameStartSeconds = 5;
                        break;
                    }
                default:
                    {
                        String currTime = System.DateTime.Now.ToLongTimeString() + "," + System.DateTime.Now.Millisecond + " ";
                        this.client.log.messageLog.AddLast(new Logger.LogMessage(currTime + "UNKNOWN Header switcher reached default. " +
                            "(" + p.GetHeader() + ")", true));
                        break;
                    }
            }
            TestDelegate bla = delegate()
            {
                if (this.user == null) return;
                if (ServerUI.GetInstance().lastSelectedClientName == this.user.username)
                    ServerUI.GetInstance().RefillMessageLogs(this.client.log.messageLog);
            };
            ((Control)ServerUI.GetInstance()).BeginInvoke(bla);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Adds a game to the lobby list
 /// </summary>
 /// <param name="toAdd">The game to add</param>
 public void AddGame(MultiplayerGame toAdd)
 {
     this.gameList.AddLast(new GameDisplayPanel(gamesPanel, this.gameList.Count(), toAdd));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Removes a game from the game list.
        /// </summary>
        /// <param name="toRemove">What game to remove!</param>
        public void RemoveGame(MultiplayerGame toRemove)
        {
            // Remove the panel.
            for (int i = 0; i < this.gameList.Count(); i++)
            {
                GameDisplayPanel panel = this.gameList.ElementAt(i);
                if (panel.multiplayerGame == toRemove)
                {
                    this.gameList.Remove(panel);
                    panel.Unload();
                }
            }

            // Re-arrange the panels.
            for (int i = 0; i < this.gameList.Count(); i++)
            {
                this.gameList.ElementAt(i).index = i;
            }
        }
Ejemplo n.º 5
0
        public void DataReceived(Packet p)
        {
            ChatServerConnectionManager manager = ChatServerConnectionManager.GetInstance();
            switch (p.GetHeader())
            {
                case Headers.NEW_USER:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        String username = PacketUtil.DecodePacketString(p, 4);
                        User user = new User(username);
                        user.id = userID;
                        user.channelID = manager.user.channelID;
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();

                        if (menu is GameLobby)
                        {
                            GameLobby lobby = ((GameLobby)menu);
                            lobby.UserJoined(user);
                        }

                        break;
                    }
                case Headers.USER_LEFT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        User user = UserManager.GetInstance().GetUserByID(userID);
                        if (user != null)
                        {
                            ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                            if (menu is GameLobby)
                            {
                                GameLobby lobby = ((GameLobby)menu);
                                lobby.UserLeft(user);
                            }
                        }

                        break;
                    }
                // Client received an ID for creating a game.
                case Headers.GAME_ID:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);

                        MultiplayerLobby lobby = ((MultiplayerLobby)MenuManager.GetInstance().GetCurrentlyDisplayedMenu());
                        String gameName = lobby.gameNameInput.textfield.text;

                        MenuManager.GetInstance().ShowMenu(MenuManager.Menu.GameLobby);
                        GameLobby gameLobby = (GameLobby)MenuManager.GetInstance().GetCurrentlyDisplayedMenu();

                        gameLobby.multiplayerGame = new MultiplayerGame(gameID,
                            gameName, "");
                        gameLobby.multiplayerGame.host = manager.user;
                        break;
                    }
                case Headers.SERVER_CREATE_GAME:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            // Confirmation that the game was created? idk
                        }
                        else if (menu is MultiplayerLobby)
                        {
                            MultiplayerGame game = new MultiplayerGame(
                                PacketUtil.DecodePacketInt(p, 0),
                                PacketUtil.DecodePacketString(p, 4),
                                "<No map selected yet>");
                            MultiplayerLobby lobby = (MultiplayerLobby)menu;
                            lobby.AddGame(game);
                        }
                        break;
                    }
                case Headers.SERVER_DESTROY_GAME:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby);
                            XNAMessageDialog dialog =
                                XNAMessageDialog.CreateDialog("The host has disconnected.", XNAMessageDialog.DialogType.OK);
                            // When OK is pressed .. get back to the lobby.
                            dialog.button1.onClickListeners +=
                                delegate(XNAButton source)
                                {
                                    // Change channel
                                    Packet leftGamePacket = new Packet(Headers.CLIENT_LEFT_GAME);
                                    ChatServerConnectionManager.GetInstance().SendPacket(leftGamePacket);
                                };

                        }
                        else if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = (MultiplayerLobby)menu;
                            lobby.RemoveGameByID(gameID);
                        }
                        break;
                    }
                case Headers.GAME_MAP_CHANGED:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = (MultiplayerLobby)menu;
                            MultiplayerGame game = lobby.GetGameByID(PacketUtil.DecodePacketInt(p, 0));
                            if (game != null)
                            {
                                game.mapname = PacketUtil.DecodePacketString(p, 4);
                            }
                        }
                        break;
                    }
                case Headers.SERVER_REQUEST_JOIN:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            Packet newPacket = new Packet();
                            if (lobby.IsFull()) newPacket.SetHeader(Headers.CLIENT_GAME_FULL);
                            else newPacket.SetHeader(Headers.CLIENT_OK_JOIN);
                            newPacket.AddInt(PacketUtil.DecodePacketInt(p, 0));
                            newPacket.AddInt(PacketUtil.DecodePacketInt(p, 4));
                            manager.SendPacket(newPacket);
                        }
                        break;
                    }
                case Headers.CLIENT_OK_JOIN:
                    {
                        // Packet newPacket = new Packet(Headers.CLIENT_CHANNEL);
                        // newPacket.AddInt(PacketUtil.DecodePacketInt(p, 0));
                        MenuManager.GetInstance().ShowMenu(MenuManager.Menu.GameLobby);
                        // manager.SendPacket(newPacket);

                        break;
                    }
                case Headers.CLIENT_GAME_FULL:
                    {
                        XNAMessageDialog.CreateDialog("The game is full.", XNAMessageDialog.DialogType.OK);
                        break;
                    }

                case Headers.GAME_COLOR_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int colorID = PacketUtil.DecodePacketInt(p, 8);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            UserDisplayPanel panel = lobby.GetDisplayPanelByUserId(userID);
                            if (panel != null)
                            {
                                panel.SelectColor(colorID);
                            }
                            else
                            {
                                Console.Out.WriteLine("Tried to change the color of a user that doesn't exist!");
                            }
                        }

                        break;
                    }

                case Headers.GAME_TEAM_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int teamID = PacketUtil.DecodePacketInt(p, 8);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            UserDisplayPanel panel = lobby.GetDisplayPanelByUserId(userID);
                            if (panel != null)
                            {
                                panel.teamDropdown.SelectItem(teamID - 1);
                            }
                            else
                            {
                                Console.Out.WriteLine("Tried to change the team of a user that doesn't exist!");
                            }
                        }
                        break;
                    }

                case Headers.GAME_READY_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int readyState = PacketUtil.DecodePacketInt(p, 8);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            UserDisplayPanel panel = lobby.GetDisplayPanelByUserId(userID);
                            if (panel != null)
                            {
                                panel.readyCheckBox.selected = readyState != 0;
                            }
                            else
                            {
                                Console.Out.WriteLine("Tried to change the ready state of a user that doesn't exist!");
                            }
                        }
                        break;
                    }
                case Headers.GAME_KICK_CLIENT:
                    {
                        MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby);
                        XNAMessageDialog dialog =
                            XNAMessageDialog.CreateDialog("You have been kicked by the host.", XNAMessageDialog.DialogType.OK);

                        break;
                    }
                case Headers.SERVER_GAME_START:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        int seconds = PacketUtil.DecodePacketInt(p, 0);
                        if (seconds != 0)
                        {
                            if (menu is GameLobby)
                            {
                                GameLobby lobby = (GameLobby)menu;
                                lobby.AddMessageToLog("Game will start in " + seconds);
                                lobby.leaveGameButton.visible = false;
                            }
                        }
                        else if (menu is GameLobby)
                        {
                            GameLobby lobby = ((GameLobby)menu);
                            Game1.GetInstance().multiplayerGame = new MultiplayerGame(ChatServerConnectionManager.GetInstance().user.channelID,
                                "<Gamename>", "<Mapname>");

                            StateManager.GetInstance().gameState = StateManager.State.GameInit;
                            MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiPlayerLoadMenu);
                            StateManager.GetInstance().gameState = StateManager.State.GameLoading;
                            // Loading will start now
                            // See Game1.cs for sending the packet that the game is done loading (update loop)
                        }

                        break;
                    }
                case Headers.DONE_LOADING:
                    {
                        int playerID = PacketUtil.DecodePacketInt(p, 0);
                        Console.Out.WriteLine("Done loading packet received! -> " + playerID);
                        Game1.GetInstance().GetPlayerByMultiplayerID(playerID).doneLoading = true;
                        for (int i = 0; i < Game1.GetInstance().players.Count(); i++)
                        {
                            if (!Game1.GetInstance().players.ElementAt(i).doneLoading) return;
                        }

                        StateManager.GetInstance().gameState = StateManager.State.GameRunning;
                        //  int count = 0;
                        Game1.CURRENT_PLAYER.SpawnStartUnits();
                        /*foreach (Player player in Game1.GetInstance().players)
                        {
                            player.SpawnStartUnits();
                            count++;
                        }*/
                        break;
                    }

                case Headers.LOADING_PROGRESS:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        int percentage = PacketUtil.DecodePacketInt(p, 4);

                        User user = Game1.GetInstance().multiplayerGame.GetUserByID(userID);

                        MPLoadScreen loadScreen = ((MPLoadScreen)MenuManager.GetInstance().GetCurrentlyDisplayedMenu());
                        // It may be null for a split second after the loading finishes
                        if( loadScreen != null ) loadScreen.SetPercentageDone(user, percentage);
                        break;
                    }
                case Headers.LOADING_WHAT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        String what = PacketUtil.DecodePacketString(p, 4);

                        User user = Game1.GetInstance().multiplayerGame.GetUserByID(userID);

                        MPLoadScreen loadScreen = ((MPLoadScreen)MenuManager.GetInstance().GetCurrentlyDisplayedMenu());
                        // It may be null for a split second after the loading finishes
                        if (loadScreen != null) loadScreen.SetLoadingWhat(user, what);
                        break;
                    }
                case Headers.MAP_POSITION_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        int mapIndex = PacketUtil.DecodePacketInt(p, 4);

                        GameLobby lobby = (GameLobby)MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        lobby.mapPreviewPanel.playerLocationGroup.OnPlayerIndexChangedMP(
                            userID, mapIndex);

                        break;
                    }
            }
        }