/// <summary> /// Initializes and starts a new real time session for users /// </summary> /// <param name="_info">The session information for this game</param> public void StartNewRTSession(RTSessionInfo _info) { Debug.Log("GSM| Creating new RT Session instance..."); m_SessionInfo = _info; m_GameSparksRTUnity = this.gameObject.AddComponent <GameSparksRTUnity>(); //Adds the RT script to the game //In order to create a new RT game we need a 'FindMatchResponse' //This would usually come from the server directly after a successful MatchmakingRequest //However, in our case, we want the game to be create only when all the players ready up //Therefore, the details from the response is passed in from the gameInfo and a mock-up of a FindMatchReponse is passed in GSRequestData mockedReponse = new GSRequestData() .AddNumber("port", (double)_info.GetPortID()) .AddString("host", _info.GetHostURL()) .AddString("accessToken", _info.GetAccessToken()); //Construct a dataset from the game-details FindMatchResponse response = new FindMatchResponse(mockedReponse); //Create a match-response from that data and pass it into the game-configuration //So in the game-configuration method we pass in the response which gives the Instance its connection settings //In this example, a lambda expression is used to pass in actions for //OnPlayerConnect, OnPlayerDisconnect, OnReady, and OnPacket //The methods do exactly what they are named. For example, OnPacket gets called when a packet is received m_GameSparksRTUnity.Configure(response, (peerId) => { OnPlayerConnectedToGame(peerId); }, (peerId) => { OnPlayerDisconnected(peerId); }, (ready) => { OnRTReady(ready); }, (packet) => { OnPacketReceived(packet); }); m_GameSparksRTUnity.Connect(); //When the configuration is set, connect the game }
/// <summary> /// Triggers when a match is found either through hosting or through selecting a match. Sets the lobby screen variables /// </summary> /// <param name="_message">The message from the GameSparks API</param> private void OnMatchFound(GameSparks.Api.Messages.MatchFoundMessage _message) { m_TempRTSessionInfo = new RTSessionInfo(_message); // we'll store the match data until we need to create an RT session instance //Trigger Cloud event to gather max player count for this match and also grab the match name, placing these variables on the lobby screen. new LogEventRequest() .SetEventKey("GetFoundMatchInfo") .SetEventAttribute("MatchShortCode", _message.MatchShortCode) .Send((_nameResponse) => { var matchInfo = JsonUtility.FromJson <JSONFoundMatchInfoConverter>(_nameResponse.ScriptData.JSON); StringBuilder matchDetails = new StringBuilder(); m_MaxPlayerCount = matchInfo.m_MaxPlayerCount.ToString(); m_PlayerCountText.GetComponent <Text>().text = _message.Participants.Count() + "/" + m_MaxPlayerCount; m_MatchNameText.GetComponent <Text>().text = _message.MatchShortCode + ": " + _message.MatchData.GetString("cleanRoomName"); Debug.Log("Room Name: " + _message.MatchData.GetString("cleanRoomName")); }); //Add player name to the player list StringBuilder playerList = new StringBuilder(); foreach (GameSparks.Api.Messages.MatchFoundMessage._Participant player in _message.Participants) { playerList.AppendLine(player.DisplayName); //Add the player number and display name to the list if (!GameSparksManager.m_PlayerIds.ContainsKey(player.Id)) { GameSparksManager.m_PlayerIds.Add(player.Id, false); } } m_PlayerListText.GetComponent <Text>().text = playerList.ToString(); //Display the users in the room m_ReadyButton.GetComponent <Button>().interactable = true; //Allow user to ready up }
/// <summary> /// Triggers when a match is found either through hosting or through selecting a match. Sets the lobby screen variables /// </summary> /// <param name="_message">The message from the GameSparks API</param> private void OnMatchFound(GameSparks.Api.Messages.MatchFoundMessage _message) { //If we don't have ourselves as an ID yet, then we can't find a match (though from a GameSparks perspective we can) if (!GameSparksManager.m_PlayerIds.ContainsValue(true)) { return; } //Get matchGroup string matchGroup = _message.MatchGroup; if (string.IsNullOrEmpty(matchGroup)) { matchGroup = ""; } GameSparksManager.Instance().SetMatchGroup(matchGroup); //Don't transition to the lobby screen if just looking for a match if (!m_hosting && (!m_QuickConnect || m_SwitchedToManualMatchMaking)) { m_JoinMatchButton.GetComponent <Button>().interactable = false; m_JoinMatchButtonInteractable = false; //Disconnect from match instance so we can join the match we want to join (since we're looking at matches) new LogEventRequest().SetEventKey("Disconnect").SetEventAttribute("MatchShortCode", _message.MatchShortCode). SetEventAttribute("MatchGroup", matchGroup).Send((_disconnectResponse) => { m_JoinMatchButtonInteractable = true; foreach (GameObject _potentialMatch in GameObject.FindGameObjectsWithTag("PotentialMatchButton")) { _potentialMatch.GetComponent <Button>().interactable = true; } m_SearchMatchErrorText.GetComponent <Text>().text = ""; m_SearchMatchErrorText.SetActive(false); }); return; } DisplayPanel(PanelSelection.LobbyScreen); GameSparksManager.Instance().m_MyMatchId = _message.MatchId; m_TempRTSessionInfo = new RTSessionInfo(_message); // we'll store the match data until we need to create an RT session instance //Trigger Cloud event to gather max player count for this match and also grab the match name, placing these variables on the lobby screen. new LogEventRequest() .SetEventKey("GetFoundMatchInfo") .SetEventAttribute("MatchShortCode", _message.MatchShortCode) .Send((_nameResponse) => { var matchInfo = JsonUtility.FromJson <JSONFoundMatchInfoConverter>(_nameResponse.ScriptData.JSON); StringBuilder matchDetails = new StringBuilder(); m_MaxPlayerCount = matchInfo.m_MaxPlayerCount.ToString(); m_PlayerCountText.GetComponent <Text>().text = _message.Participants.Count() + "/" + m_MaxPlayerCount; m_MatchNameText.GetComponent <Text>().text = _message.MatchShortCode + ": " + _message.MatchData.GetString("m_cleanRoomName"); m_ReadyButton.GetComponent <Button>().interactable = true; //Allow user to ready up }); //Add player name to the player list StringBuilder playerList = new StringBuilder(); foreach (GameSparks.Api.Messages.MatchFoundMessage._Participant player in _message.Participants) { playerList.AppendLine(player.DisplayName); //Add the player number and display name to the list if (!GameSparksManager.m_PlayerIds.ContainsKey(player.Id)) { GameSparksManager.m_PlayerIds.Add(player.Id, false); } } m_PlayerListText.GetComponent <Text>().text = playerList.ToString(); //Display the users in the room }