/// <summary> /// As a client of the lobby, handle any received-packet events from the host of the lobby. /// </summary> protected void InLobbyClientLogic() { // Need to delay a bit before transferring packets. NwMgr needs to establish connection event after calling Run() // before sending packets. if (inLobbyClientLogicCurStatus == inLobbyClientLogicStatus.NotStarted) { inLobbyClientLogicStartDelayFinish = Time.time + inLobbyClientLogicStartDelayTime; inLobbyClientLogicCurStatus = inLobbyClientLogicStatus.DelayingStart; return; } else if (inLobbyClientLogicCurStatus == inLobbyClientLogicStatus.DelayingStart) { if (Time.time > inLobbyClientLogicStartDelayFinish) { inLobbyClientLogicCurStatus = inLobbyClientLogicStatus.Started; } else { return; } } // If just joined the lobby, request a player id. if (myIdStatus == PlayerIdStatus.None) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Requesting a player id."); } NwMgr.SendPacket <LobbyRequestPlayerIdPacket>("info", new LobbyRequestPlayerIdPacket()); myIdStatus = PlayerIdStatus.Requested; } // If received my requested player id. if (NwMgr.PacketQueue.HasPacket <LobbyGivenPlayerIdPacket>()) { LobbyGivenPlayerIdPacket recvPkt = NwMgr.PacketQueue.GetNextPacket <LobbyGivenPlayerIdPacket>(); myId = recvPkt.PlayerId; myIdStatus = PlayerIdStatus.Received; if (IsDebug) { Debug.Log("InLobbyClientLogic: Received my player id: " + myId); } } // Retrieved true lobby state from server. if (NwMgr.PacketQueue.HasPacket <LobbyStatePacket>()) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Received lobby state from server."); } TrueLobbyState = NwMgr.PacketQueue.GetNextPacket <LobbyStatePacket>(); Menus.RefreshLobby(TrueLobbyState, myId); // Go in-game mode if signalled by server. if (TrueLobbyState.IsStartGame) { Debug.Log("Lobby.cs: Got packet to start game. Time: " + Time.time); Menus.OnClick_StartGame(); } } // Received alert that server is disconnecting. Exit the lobby. if (NwMgr.PacketQueue.HasPacket <LobbyPlayerDisconnectPacket>()) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Received alert that server is disconnecting."); } NwMgr.PacketQueue.GetNextPacket <LobbyPlayerDisconnectPacket>(); Menus.OnClick_Disconnect("Server has ended the game."); return; } // Received signal to return to the lobby. if (NwMgr.PacketQueue.HasPacket <LobbyReturnPacket>()) { if (IsDebug) { Debug.Log("InLobbyClientLogic: Received signal to return to lobby."); } NwMgr.PacketQueue.GetNextPacket <LobbyReturnPacket>(); Menus.OnClick_ReturnToLobby(); } }
/* 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; } }