/// <summary> /// Event when the "Join Role" is pressed for a particular GUI lobby slot. /// </summary> public void OnClick_JoinRole(string teamRole) { TeamRole dTeamRole = (TeamRole)Enum.Parse(typeof(TeamRole), teamRole); LobbySlot myCurSlot = GetMyLobbySlot(); if (myCurSlot != null) { UnoccupyLobbySlot(myCurSlot.teamRole); } // As host, let everyone know about my change. if (mIsHost) { OccupyLobbySlot(dTeamRole, mUsername, mIsHost, true); LobbyStatePacket lobbyState = CopyLobbyStateToPacket(); mLobby.SendNewTrueLobbyState(lobbyState); } // As client, inform server. else { LobbyUpdateFromClientPacket sendPkt = new LobbyUpdateFromClientPacket(); // sendPkt.IsReady = false; sendPkt.IsOccupied = true; sendPkt.TeamRole = dTeamRole; sendPkt.Username = mUsername; mLobby.SendLobbyUpdateFromClient(sendPkt); } }
/// <summary> /// Event when the "Is Ready" toggle is pressed for a particular GUI lobby slot. /// </summary> public void OnToggle_Ready(bool unused) { LobbySlot slot = GetMyLobbySlot(); // Update GUI. // if (slot.isReadyToggle.isOn) // slot.isReadyToggle.GetComponentInChildren<Text>().text = "Ready"; // else // slot.isReadyToggle.GetComponentInChildren<Text>().text = "Not ready"; // Inform server know. LobbyUpdateFromClientPacket sendPkt = new LobbyUpdateFromClientPacket(); // sendPkt.IsReady = slot.isReadyToggle.isOn; sendPkt.IsOccupied = true; sendPkt.TeamRole = slot.teamRole; sendPkt.Username = mUsername; mLobby.SendLobbyUpdateFromClient(sendPkt); }
/// <summary> /// Event when the "Leave Role" button is pressed for a particular GUI lobby slot. /// </summary> public void OnClick_LeaveRole(string teamRole) { TeamRole dTeamRole = (TeamRole)Enum.Parse(typeof(TeamRole), teamRole); // Update GUI. UnoccupyLobbySlot(dTeamRole); if (mIsHost) // Inform clients. { LobbyStatePacket lobbyState = CopyLobbyStateToPacket(); mLobby.SendNewTrueLobbyState(lobbyState); } else // Inform server. { LobbyUpdateFromClientPacket sendPkt = new LobbyUpdateFromClientPacket(); // sendPkt.IsReady = false; sendPkt.IsOccupied = false; sendPkt.TeamRole = dTeamRole; sendPkt.Username = mUsername; mLobby.SendLobbyUpdateFromClient(sendPkt); } }
/// <summary> /// Let server know about my lobby changes (e.g. changed role). This is called by the main menu. /// </summary> public void SendLobbyUpdateFromClient(LobbyUpdateFromClientPacket clientUpdate) { NwMgr.SendPacket <LobbyUpdateFromClientPacket>("info", clientUpdate); }
/* These functions below are called by Update(). They handle packet-received events relating to the * lobby state. */ /// <summary> /// As the host of the lobby, handle any received packets from my clients. /// </summary> protected void InLobbyHostLogic() { int prevNumActivePlayers = TrueLobbyState.NumActivePlayers; TrueLobbyState.NumActivePlayers = 1 + NwMgr.NetworkHost.ConnectionCount; // If number of active players have changed... if (prevNumActivePlayers != TrueLobbyState.NumActivePlayers) { TrueLobbyState.IsDirty = true; } // At the start, assign myself the first player Id. if (myIdStatus == PlayerIdStatus.None) { if (IsDebug) { Debug.Log("InLobbyHostLogic: Assigning myself a player id."); } myId = PlayerId.One; myIdStatus = PlayerIdStatus.Received; } // A new client has joined the lobby and is requesting a unique player ID. if (NwMgr.PacketQueue.HasPacket <LobbyRequestPlayerIdPacket>()) { if (IsDebug) { Debug.Log("InLobbyHostLogic: Got new player event."); } INetworkConnection recvNwConn; NwMgr.PacketQueue.GetNextPacket <LobbyRequestPlayerIdPacket>(out recvNwConn); // Give client an ID relative to its incoming channel ID. LobbyGivenPlayerIdPacket sendPkt = new LobbyGivenPlayerIdPacket(); sendPkt.PlayerId = DetermineClientPlayerId(recvNwConn); NwMgr.SendPacket <LobbyGivenPlayerIdPacket>("info", recvNwConn.Id, sendPkt); // NumActivePlayers will be handled at the top of this function. } // Received update from client (e.g. changed role). if (NwMgr.PacketQueue.HasPacket <LobbyUpdateFromClientPacket>()) { if (IsDebug) { Debug.Log("InLobbyHostLogic: Got lobby update from client."); } INetworkConnection recvNwConn; LobbyUpdateFromClientPacket recvPkt = NwMgr.PacketQueue.GetNextPacket <LobbyUpdateFromClientPacket>(out recvNwConn); PlayerId clientId = DetermineClientPlayerId(recvNwConn); TrueLobbyState.MergeUpdateFromClient(recvPkt, clientId); TrueLobbyState.IsDirty = true; } // Received alert that a client is disconnecting from server. if (NwMgr.PacketQueue.HasPacket <LobbyPlayerDisconnectPacket>()) { if (IsDebug) { Debug.Log("InLobbyHostLogic: Client is disconnecting."); } INetworkConnection recvNwConn; NwMgr.PacketQueue.GetNextPacket <LobbyPlayerDisconnectPacket>(out recvNwConn); PlayerId lossId = DetermineClientPlayerId(recvNwConn); TrueLobbyState.RemoveClient(lossId); TrueLobbyState.IsDirty = true; } if (TrueLobbyState.IsDirty) { // Broadcast new lobby state. NwMgr.SendPacket <LobbyStatePacket>("info", TrueLobbyState); // Refresh my lobby GUI. Menus.RefreshLobby(TrueLobbyState, myId); TrueLobbyState.IsDirty = false; } }