/// <summary> /// Gets the game by a user /// </summary> /// <param name="host">The host that has a game</param> /// <returns>Null if that user does not have a game, the MultiplayerGame otherwise.</returns> public MultiplayerGame GetGameByHost(User host) { foreach (MultiplayerGame game in this.games) { if (game.host == host) return game; } return null; }
public UserDisplayPanel(ParentComponent parent, User user, int index) : base(parent, new Rectangle()) { this.user = user; this.UpdateBounds(index); Boolean enabled = false; if (user.id == ChatServerConnectionManager.GetInstance().user.id) enabled = true; readyCheckBox = new XNACheckBox(this, new Rectangle(10, 10, 20, 20), ""); readyCheckBox.onClickListeners += this.OnReadyChanged; readyCheckBox.enabled = enabled; // readyCheckBox usernameLbl = new XNALabel(this, new Rectangle(40, 5, 230, 30), user.username); usernameLbl.font = MenuManager.BIG_TEXTFIELD_FONT; teamDropdown = new XNADropdown(this, new Rectangle(280, 5, 50, 30)); teamDropdown.dropdownLineSpace = 15; teamDropdown.arrowSize = 8; teamDropdown.onOptionSelectedChangedListeners += this.OnTeamChanged; for (int i = 0; i < 8; i++) teamDropdown.AddOption((i + 1) + ""); teamDropdown.enabled = enabled; colorDropdown = new XNADropdown(this, new Rectangle(340, 5, 50, 30)); colorDropdown.dropdownLineSpace = 15; colorDropdown.arrowSize = 8; for (int i = 0; i < colors.Length; i++) { colorDropdown.AddOption(""); colorDropdown.SetBackgroundColor(i + 1, colors[i]); } colorDropdown.onOptionSelectedChangedListeners += this.OnColorChanged; colorDropdown.enabled = enabled; kickBtn = new XNAButton(this, new Rectangle(400, 5, 75, 30), "Kick"); kickBtn.visible = false; if (((GameLobby)MenuManager.GetInstance().GetCurrentlyDisplayedMenu()).IsCurrentUserHost() && this.user.id != ChatServerConnectionManager.GetInstance().user.id) { kickBtn.visible = true; kickBtn.onClickListeners += this.OnKickButtonPressed; } if (this.user.id == 1) { // Wotuu this.SelectColor(1); this.readyCheckBox.selected = true; this.teamDropdown.SelectItem(0); } else if (this.user.id == 2) { this.SelectColor(3); this.readyCheckBox.selected = true; this.teamDropdown.SelectItem(1); } }
/// <summary> /// Sets what the user is currently loading. /// </summary> /// <param name="user">The user.</param> /// <param name="what">What said user is loading.</param> public void SetLoadingWhat(User user, String what) { int userCount = Game1.GetInstance().multiplayerGame.GetUserCount(); for (int i = 0; i < userCount; i++) { if (Game1.GetInstance().multiplayerGame.GetUser(i) == user) { this.loadingWhatLabels[i].text = what; break; } } }
public void DataReceived(Packet p) { switch (p.GetHeader()) { case Headers.KEEP_ALIVE: { ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.KEEP_ALIVE)); break; } case Headers.HANDSHAKE_2: { ChatServerConnectionManager.GetInstance().SetLoginStatus("Connected"); // Finish handshake ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.HANDSHAKE_3)); // Request for a user ID Packet newPacket = new Packet(Headers.CLIENT_USERNAME); newPacket.AddString(ChatServerConnectionManager.GetInstance().user.username); ChatServerConnectionManager.GetInstance().SendPacket(newPacket); MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby); break; } case Headers.SERVER_DISCONNECT: { ChatServerConnectionManager.GetInstance().DisconnectFromServer(); // Create a dialog, and add a listener to the OK button. ChatServerConnectionManager.GetInstance().OnDisconnect(); break; } case Headers.CLIENT_USER_ID: { // Set the received user ID. ChatServerConnectionManager.GetInstance().user.id = PacketUtil.DecodePacketInt(p, 0); // Console.Out.WriteLine("Received user ID from the server: " + ChatServerConnectionManager.GetInstance().user.id); break; } case Headers.CLIENT_CHANNEL: { UserManager.GetInstance().users.Clear(); // UserManager.GetInstance().users.Clear(); ChatServerConnectionManager.GetInstance().user.channelID = PacketUtil.DecodePacketInt(p, 0); // Console.Out.WriteLine("Switched channel to: " + ChatServerConnectionManager.GetInstance().user.channelID); break; } case Headers.CHAT_MESSAGE: { // Get the channel int channel = PacketUtil.DecodePacketInt(p, 0); // Get the message String message = PacketUtil.DecodePacketString(p, 4); ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu(); if (menu is MultiplayerLobby) { MultiplayerLobby lobby = ((MultiplayerLobby)menu); lobby.AddMessageToLog(message); } else if (menu is GameLobby) { GameLobby lobby = ((GameLobby)menu); lobby.AddMessageToLog(message); } break; } 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 = ChatServerConnectionManager.GetInstance().user.channelID; ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu(); if (UserManager.GetInstance().GetUserByID(user.id) == null) UserManager.GetInstance().users.AddLast(user); if (menu is MultiplayerLobby) { MultiplayerLobby lobby = ((MultiplayerLobby)menu); lobby.AddUser(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 MultiplayerLobby) { MultiplayerLobby lobby = ((MultiplayerLobby)menu); lobby.RemoveUser(user); } } break; } } }
public void DataReceived(Packet p) { switch (p.GetHeader()) { case Headers.KEEP_ALIVE: { ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.KEEP_ALIVE)); break; } case Headers.HANDSHAKE_2: { ChatServerConnectionManager.GetInstance().SetLoginStatus("Connected"); // Finish handshake ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.HANDSHAKE_3)); // Request for a user ID Packet newPacket = new Packet(Headers.CLIENT_USERNAME); newPacket.AddString(ChatServerConnectionManager.GetInstance().user.username); // Random user names are for testing only, so we don't want to show the menu if (!ChatServerConnectionManager.GetInstance().useRandomUsername) { MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby); } ChatServerConnectionManager.GetInstance().SendPacket(newPacket); break; } case Headers.SERVER_DISCONNECT: { ChatServerConnectionManager.GetInstance().DisconnectFromServer(); // Create a dialog, and add a listener to the OK button. ChatServerConnectionManager.GetInstance().OnDisconnect(); break; } case Headers.CLIENT_USER_ID: { // Set the received user ID. ChatServerConnectionManager.GetInstance().user.id = PacketUtil.DecodePacketInt(p, 0); // Console.Out.WriteLine("Received user ID from the server: " + ChatServerConnectionManager.GetInstance().user.id); break; } case Headers.GAME_MAP_CHANGED: { int gameID = PacketUtil.DecodePacketInt(p, 0); String mapName = PacketUtil.DecodePacketString(p, 4); ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu(); if (menu is MultiplayerLobby) { MultiplayerLobby lobby = ((MultiplayerLobby)menu); MultiplayerGame game = lobby.GetGameByID(gameID); game.mapname = mapName; for (int i = 0; i < lobby.gameList.Count(); i++) { GameDisplayPanel panel = lobby.GetGameDisplayPanelByIndex(i); if (panel.multiplayerGame.id == gameID) { panel.ChangeMap(mapName); break; } } } else if (menu is GameLobby) { GameLobby lobby = ((GameLobby)menu); lobby.mapPreviewPanel.ChangeMap(mapName); } Console.Out.WriteLine("Received GAME_MAP_CHANGED packet"); break; } case Headers.CLIENT_CHANNEL: { UserManager.GetInstance().users.Clear(); // UserManager.GetInstance().users.Clear(); ChatServerConnectionManager.GetInstance().user.channelID = PacketUtil.DecodePacketInt(p, 0); // Console.Out.WriteLine("Switched channel to: " + ChatServerConnectionManager.GetInstance().user.channelID); break; } case Headers.CHAT_MESSAGE: { // Get the channel int channel = PacketUtil.DecodePacketInt(p, 0); // Get the message String message = PacketUtil.DecodePacketString(p, 4); ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu(); if (menu is MultiplayerLobby) { MultiplayerLobby lobby = ((MultiplayerLobby)menu); lobby.AddMessageToLog(message); } else if (menu is GameLobby) { GameLobby lobby = ((GameLobby)menu); lobby.AddMessageToLog(message); } break; } 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 = ChatServerConnectionManager.GetInstance().user.channelID; ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu(); if (UserManager.GetInstance().GetUserByID(user.id) == null) UserManager.GetInstance().users.AddLast(user); if (menu is MultiplayerLobby) { MultiplayerLobby lobby = ((MultiplayerLobby)menu); lobby.AddUser(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 MultiplayerLobby) { MultiplayerLobby lobby = ((MultiplayerLobby)menu); lobby.RemoveUser(user); } } break; } } }
/// <summary> /// Removes a user from the list. /// </summary> /// <param name="user">The user to remove.</param> public void RemoveUser(User toRemove) { UserManager.GetInstance().RemoveUserByID(toRemove.id); if (UserManager.GetInstance().users.Count == 0) { usersField.text = "No users for some odd reason."; return; } String result = "(" + UserManager.GetInstance().users.First.Value.id + ") " + UserManager.GetInstance().users.First.Value.username; for (int i = 1; i < UserManager.GetInstance().users.Count; i++) { User user = UserManager.GetInstance().users.ElementAt(i); result += "\n" + "(" + user.id + ") " + user.username; } usersField.text = result; }
/// <summary> /// Adds a user to the user log. /// </summary> /// <param name="user">The user to add</param> public void AddUser(User toAdd) { String result = "(" + UserManager.GetInstance().users.First.Value.id + ") " + UserManager.GetInstance().users.First.Value.username; for (int i = 1; i < UserManager.GetInstance().users.Count; i++) { User user = UserManager.GetInstance().users.ElementAt(i); result += "\n" + "(" + user.id + ") " + user.username; } usersField.text = result; }
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; } } }
/// <summary> /// Adds a user to this game. /// </summary> /// <param name="user">The user to add</param> public void AddUser(User user) { this.users.AddLast(user); }
/// <summary> /// Removes a user from the list. /// </summary> /// <param name="user">The user to remove.</param> public void RemoveUser(User user) { this.users.Remove(user); }
/// <summary> /// Sets the amount done for a certain user. /// </summary> /// <param name="user">The user to set.</param> /// <param name="percentage">The percentage to set to</param> public void SetPercentageDone(User user, int percentage) { int userCount = Game1.GetInstance().multiplayerGame.GetUserCount(); for (int i = 0; i < userCount; i++) { if (Game1.GetInstance().multiplayerGame.GetUser(i) == user) { this.progressBars[i].currentValue = percentage; break; } } }
/// <summary> /// Call this when a player left. /// </summary> /// <param name="user">The user that left.</param> public void UserLeft(User user) { // Console.Out.WriteLine(this.creationTime + ": " + user + " has left the game lobby (from " + ChatServerConnectionManager.GetInstance().user + ")"); Boolean removed = false; for (int i = 0; i < userDisplayPanels.Count; i++) { UserDisplayPanel p = userDisplayPanels.ElementAt(i); if (removed) { p.UpdateBounds(i); } else if (p.user.id == user.id) { // Console.Out.WriteLine(this.creationTime + ": " + "Removed a panel from the list because " + p.user + " == " + user.id + "!"); userDisplayPanels.Remove(p); // Console.Out.WriteLine(this.creationTime + ": " + "Users now in lobby: " + this.userDisplayPanels.Count); p.Unload(); i--; removed = true; } } }
/// <summary> /// When a player joined, call this one. /// </summary> /// <param name="user">The user that joined.</param> public void UserJoined(User user) { // I prefer the ID function .. if (!UserExists(user.id)) { // Console.Out.WriteLine(this.creationTime + ": " + user + " has joined the game lobby (from " + ChatServerConnectionManager.GetInstance().user + ")"); this.userDisplayPanels.AddLast(new UserDisplayPanel(gameOptionsPanel, user, this.userDisplayPanels.Count)); // Console.Out.WriteLine(this.creationTime + ": " + "Users now in lobby: " + this.userDisplayPanels.Count); } }
/// <summary> /// Checks whether the user exists in this lobby. /// </summary> /// <param name="user">The user to check</param> /// <returns></returns> public Boolean UserExists(User user) { foreach (UserDisplayPanel p in userDisplayPanels) { if (p.user == user) return true; } return false; }
/// <summary> /// Checks whether the user exists in this lobby. /// </summary> /// <param name="user">The user to check</param> /// <returns></returns> public Boolean UserExists(User user) { for( int i = 0; i < this.userDisplayPanels.Count(); i ++){ UserDisplayPanel p = this.userDisplayPanels.ElementAt(i); if (p.user == user) return true; } return false; }