/// <summary> /// /// </summary> /// <param name="user"></param> /// <returns></returns> public virtual bool StartGameManually(LobbyUserPeerExtension user) { var member = GetMemberByExtension(user); if (!Config.EnableManualStart) { SendChatMessage(member, "You cannot start the game manually", true); return(false); } // If not game maester if (GameMaster != member) { SendChatMessage(member, "You're not the master of this game", true); return(false); } if (State != LobbyState.Preparations) { SendChatMessage(member, "Invalid lobby state", true); return(false); } if (IsDestroyed) { SendChatMessage(member, "Lobby is destroyed", true); return(false); } if (membersByUsernameList.Values.Any(m => !m.IsReady && m != _gameMaster)) { SendChatMessage(member, "Not all players are ready", true); return(false); } if (membersByUsernameList.Count < MinPlayers) { SendChatMessage( member, string.Format("Not enough players. Need {0} more ", (MinPlayers - membersByUsernameList.Count)), true); return(false); } var lackingTeam = teamsList.Values.FirstOrDefault(t => t.MinPlayers > t.PlayersCount); if (lackingTeam != null) { var msg = string.Format("Team {0} does not have enough players", lackingTeam.Name); SendChatMessage(member, msg, true); return(false); } return(StartGame()); }
/// <summary> /// Get or create lobby extension for the peer /// </summary> /// <param name="peer"></param> /// <returns></returns> protected virtual LobbyUserPeerExtension GetOrCreateLobbyUserPeerExtension(IPeer peer) { var extension = peer.GetExtension <LobbyUserPeerExtension>(); if (extension == null) { extension = new LobbyUserPeerExtension(peer); peer.AddExtension(extension); } return(extension); }
/// <summary> /// Remove player from lobby /// </summary> /// <param name="lobbyUser"></param> public void RemovePlayer(LobbyUserPeerExtension lobbyUser) { var username = TryGetUsername(lobbyUser.Peer); membersByUsernameList.TryGetValue(username, out LobbyMember member); // If this player was never in the lobby if (member == null) { return; } membersByUsernameList.Remove(username); membersByPeerIdList.Remove(lobbyUser.Peer.Id); if (lobbyUser.CurrentLobby == this) { lobbyUser.CurrentLobby = null; } // Remove member from it's current team if (member.Team != null) { member.Team.RemoveMember(member); } // Change the game master if (GameMaster == member) { PickNewGameMaster(); } // Unsubscribe lobbyUser.Peer.OnPeerDisconnectedEvent -= OnPeerDisconnected; Unsubscribe(lobbyUser.Peer); // Notify player himself that he's removed if (lobbyUser.Peer.IsConnected) { lobbyUser.Peer.SendMessage((short)MstMessageCodes.LeftLobby, Id); } OnPlayerRemoved(member); OnPlayerRemovedEvent?.Invoke(member); }
/// <summary> /// Set the lobby property /// </summary> /// <param name="setter"></param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public virtual bool SetProperty(LobbyUserPeerExtension setter, string key, string value) { if (!Config.AllowPlayersChangeLobbyProperties) { return(false); } if (Config.EnableGameMasters) { membersByPeerIdList.TryGetValue(setter.Peer.Id, out LobbyMember member); if (GameMaster != member) { return(false); } } return(SetProperty(key, value)); }
public LobbyDataPacket GenerateLobbyData(LobbyUserPeerExtension user) { var info = new LobbyDataPacket { LobbyType = Type ?? "", GameMaster = GameMaster != null ? GameMaster.Username : "", LobbyName = Name, LobbyId = Id, LobbyProperties = propertiesList.ToDictionary(), Members = membersByUsernameList.Values.ToDictionary(m => m.Username, GenerateMemberData), Teams = teamsList.Values.ToDictionary(t => t.Name, t => t.GenerateData()), Controls = controls, LobbyState = State, MaxPlayers = MaxPlayers, EnableTeamSwitching = Config.EnableTeamSwitching, EnableReadySystem = Config.EnableReadySystem, EnableManualStart = Config.EnableManualStart, CurrentUserUsername = TryGetUsername(user.Peer) }; return(info); }
/// <summary> /// This will be called before adding a player to lobby. /// Override it to add custom checks for bans and etc. /// </summary> protected virtual bool IsPlayerAllowed(string username, LobbyUserPeerExtension user) { return(true); }
protected virtual LobbyMember CreateMember(string username, LobbyUserPeerExtension extension) { return(new LobbyMember(username, extension)); }
/// <summary> /// Get member of the lobby by extension /// </summary> /// <param name="playerExt"></param> /// <returns></returns> public LobbyMember GetMemberByExtension(LobbyUserPeerExtension playerExt) { membersByPeerIdList.TryGetValue(playerExt.Peer.Id, out LobbyMember member); return(member); }
/// <summary> /// Add player to lobby /// </summary> /// <param name="lobbyUser"></param> /// <param name="error"></param> /// <returns></returns> public bool AddPlayer(LobbyUserPeerExtension lobbyUser, out string error) { error = null; if (lobbyUser.CurrentLobby != null) { error = "You're already in a lobby"; return(false); } var username = TryGetUsername(lobbyUser.Peer); if (string.IsNullOrEmpty(username)) { error = "Invalid username"; return(false); } if (membersByUsernameList.ContainsKey(username)) { error = "Already in the lobby"; return(false); } if (IsDestroyed) { error = "Lobby is destroyed"; return(false); } if (!IsPlayerAllowed(username, lobbyUser)) { error = "You're not allowed"; return(false); } if (membersByUsernameList.Values.Count >= MaxPlayers) { error = "Lobby is full"; return(false); } if (!Config.AllowJoiningWhenGameIsLive && State != LobbyState.Preparations) { error = "Game is already in progress"; return(false); } // Create an "instance" of the member var member = CreateMember(username, lobbyUser); // Add it to a team var team = PickTeamForPlayer(member); if (team == null) { error = "Invalid lobby team"; return(false); } if (!team.AddMember(member)) { error = "Not allowed to join a team"; return(false); } membersByUsernameList[member.Username] = member; membersByPeerIdList[lobbyUser.Peer.Id] = member; // Set this lobby as player's current lobby lobbyUser.CurrentLobby = this; if (GameMaster == null) { PickNewGameMaster(false); } Subscribe(lobbyUser.Peer); lobbyUser.Peer.OnPeerDisconnectedEvent += OnPeerDisconnected; OnPlayerAdded(member); OnPlayerAddedEvent?.Invoke(member); return(true); }
public LobbyMember(string username, LobbyUserPeerExtension ext) { Username = username; Extension = ext; Properties = new MstProperties(); }