private void SetupMatchmakeListener() { // Attach an event handler for when the user is matched with other users. // This only needs to be done once. // For more information have a look at: https://heroiclabs.com/docs/development/matchmaker/ _client.OnMatchmakeMatched = (INMatchmakeMatched matched) => { // Set the local cache to null now that we've been offered a match. _matchmakeTicket = null; Debug.Log("Matchmaker found an opponent."); // The match token is used to join a multiplayer match. // Lets accept the match and join it. var message = NMatchJoinMessage.Default(matched.Token); _client.Send(message, (INResultSet <INMatch> matches) => { Debug.Log("Successfully joined matches."); _match = matches.Results[0]; foreach (var presence in _match.Presence) { Debug.LogFormat("Presence initial state: User handle '{0}' is in the match.", presence.Handle); } // Lets enable the button now that we've joined a match. _matchSendDataButtonEnable = true; }, (INError error) => { Debug.LogErrorFormat("Send error: code '{1}' with '{0}'.", error.Message, error.Code); }); }; }
//Called when "Join Match" is clicked in the Match List Panel public void JoinMatch() { Debug.Log("Joining the match: " + matchName); RegisterOnMatchData(); ManualResetEvent joinEvent = new ManualResetEvent(false); matchID = matchNameMatchGuid[matchName].ToByteArray(); string opponentName = matchGuidMatchSettings[matchNameMatchGuid[matchName]].matchCreator; string maxHealth = matchGuidMatchSettings[matchNameMatchGuid[matchName]].maxHealth; client = NakamaData.Singleton.Client; client.Send(NMatchJoinMessage.Default(matchID), (INMatch match) => { joinEvent.Set(); }, (INError err) => { Debug.Log("Failed to Join match. Error: " + err); joinEvent.Set(); }); joinEvent.WaitOne(1000, false); SendMatchInfoToMatchRoom("remove", 0); SendMessages.Singleton.LeaveRoom(); SendMessages.Singleton.JoinRoom(matchName); GameManager.Singleton.StartNewGamePlay(matchName, Convert.ToInt32(maxHealth), opponentName); matchListPanel.gameObject.SetActive(false); }
public void SendDataMatch() { INError error = null; INMatch m = null; ManualResetEvent evt1 = new ManualResetEvent(false); client1.Send(NMatchCreateMessage.Default(), (INMatch match) => { m = match; client2.Send(NMatchJoinMessage.Default(match.Id), (INResultSet <INMatch> match2) => { evt1.Set(); }, (INError err) => { error = err; evt1.Set(); }); }, (INError err) => { error = err; evt1.Set(); }); evt1.WaitOne(5000, false); Assert.IsNull(error); Assert.IsNotNull(m); byte[] data = Encoding.ASCII.GetBytes("test-data"); long opCode = 9; INMatchData d = null; ManualResetEvent evt2 = new ManualResetEvent(false); client2.OnMatchData = (INMatchData matchData) => { d = matchData; evt2.Set(); }; client1.Send(NMatchDataSendMessage.Default(m.Id, opCode, data), false, (bool completed) => { // No action. }, (INError err) => { error = err; evt2.Set(); }); evt2.WaitOne(5000, false); Assert.IsNull(error); Assert.IsNotNull(d); Assert.AreEqual(d.Id, m.Id); Assert.AreEqual(d.OpCode, opCode); Assert.AreEqual(d.Data, data); }
public void PresenceUpdateLeaveMatch() { INError error = null; ManualResetEvent evt1 = new ManualResetEvent(false); INMatch m = null; client1.Send(NMatchCreateMessage.Default(), (INMatch match) => { m = match; client2.Send(NMatchJoinMessage.Default(m.Id), (INResultSet <INMatch> match2) => { evt1.Set(); }, (INError err) => { error = err; evt1.Set(); }); }, (INError err) => { error = err; evt1.Set(); }); evt1.WaitOne(5000, false); Assert.IsNull(error); Assert.IsNotNull(m); ManualResetEvent evt2 = new ManualResetEvent(false); string leftUserId = null; client1.OnMatchPresence = (INMatchPresence presence) => { leftUserId = presence.Leave[0].UserId; evt2.Set(); }; client2.Send(NMatchLeaveMessage.Default(m.Id), (bool complete) => { // No action. }, (INError err) => { error = err; evt2.Set(); }); evt2.WaitOne(5000, false); Assert.IsNull(error); Assert.AreEqual(userId2, leftUserId); }
//Create Match Button Pressed public void CreateandJoinMatch() { if (createdMatchNameInput.text.Length < 10 || createdMatchNameInput.text.Length > 30) { createMatchPanelErrorText.text = "Invalid match name! Must be between 10 and 30 characters. Currently only " + createdMatchNameInput.text.Length + " characters."; return; } Debug.Log("Creating a Match!! Name: " + createdMatchNameInput.text); matchName = createdMatchNameInput.text; RegisterOnMatchData(); client = NakamaData.Singleton.Client; ManualResetEvent createEvent = new ManualResetEvent(false); client.Send(NMatchCreateMessage.Default(), (INMatch match) => { matchID = match.Id; matchValues = match; client.Send(NMatchJoinMessage.Default(match.Id), (INMatch match2) => { createEvent.Set(); }, (INError err) => { Debug.Log("Failed to Join created match. Error: " + err); createEvent.Set(); }); }, (INError err) => { Debug.Log("Failed to create a match. Error: " + err); createEvent.Set(); }); createEvent.WaitOne(5000, false); createMatchPanel.gameObject.SetActive(false); SendMatchInfoToMatchRoom("add", Convert.ToInt32(maxHealthSlider.value)); PlayerPrefs.SetString("MatchCreated", new Guid(matchID).ToString()); SendMessages.Singleton.LeaveRoom(); SendMessages.Singleton.JoinRoom(matchName); GameManager.Singleton.StartNewGamePlay(createdMatchNameInput.text, Convert.ToInt32(maxHealthSlider.value)); }
// entering match room public void GoBtnClick() { Debug.Log("GoBtnClick"); // join match var message = NMatchJoinMessage.Default(this.matchId); Application.client.Send(message, (INMatch match) => { // `match.Presence` is the list of current participants. // `match.Self` is the presence identifying the current user/session. Debug.Log("Successfully joined match"); Application.data["userEnterId"] = this.userEnterId; Application.data["matchId"] = this.matchId; }, (INError error) => { Debug.LogErrorFormat("Could not join match: '{0}'.", error.Message); }); }
public void PresenceUpdateJoinMatch() { INError error = null; ManualResetEvent evt1 = new ManualResetEvent(false); INMatch m = null; client1.Send(NMatchCreateMessage.Default(), (INMatch match) => { m = match; evt1.Set(); }, (INError err) => { error = err; evt1.Set(); }); evt1.WaitOne(5000, false); Assert.IsNull(error); Assert.IsNotNull(m); ManualResetEvent evt2 = new ManualResetEvent(false); byte[] joinedUserId = null; client1.OnMatchPresence += (object source, NMatchPresenceEventArgs args) => { joinedUserId = args.MatchPresence.Join[0].UserId; evt2.Set(); }; client2.Send(NMatchJoinMessage.Default(m.Id), (INMatch match) => { // No action. }, (INError err) => { error = err; evt2.Set(); }); evt2.WaitOne(5000, false); Assert.IsNull(error); Assert.AreEqual(userId2, joinedUserId); }
public void JoinMatch(string nakamaMatchId) { var joinMatchMessage = NMatchJoinMessage.Default(nakamaMatchId); _client.Send( joinMatchMessage, (INResultSet <INMatch> matches) => { if (OnMatchJoined != null) { OnMatchJoined(matches); } }, (INError Error) => { if (OnMatchJoined != null) { OnMatchJoined(null); } } ); }
public void CreateAndJoinMatch() { ManualResetEvent evt = new ManualResetEvent(false); INError error = null; client1.Send(NMatchCreateMessage.Default(), (INMatch match) => { client2.Send(NMatchJoinMessage.Default(match.Id), (INResultSet <INMatch> match2) => { evt.Set(); }, (INError err) => { error = err; evt.Set(); }); }, (INError err) => { error = err; evt.Set(); }); evt.WaitOne(5000, false); Assert.IsNull(error); }
void JoinMatchByMatchToken(INMatchToken token) { var message = NMatchJoinMessage.Default(token); client.Send(message, OnMatchJoined, OnError); }
public void SendDataSubsetMatch() { INError error = null; INMatch m = null; INUserPresence p = null; ManualResetEvent evt1 = new ManualResetEvent(false); client1.Send(NMatchCreateMessage.Default(), (INMatch match) => { m = match; client2.Send(NMatchJoinMessage.Default(match.Id), (INResultSet <INMatch> matches2) => { var match2 = matches2.Results[0]; foreach (var up in match2.Presence) { if (up.UserId.SequenceEqual(userId2)) { p = up; break; } } evt1.Set(); }, (INError err) => { error = err; evt1.Set(); }); }, (INError err) => { error = err; evt1.Set(); }); evt1.WaitOne(5000, false); Assert.IsNull(error); Assert.IsNotNull(m, "m was null"); Assert.IsNotNull(p, "p was null"); byte[] data = Encoding.ASCII.GetBytes("test-data"); long opCode = 9; INMatchData d = null; ManualResetEvent evt2 = new ManualResetEvent(false); client1.OnMatchData = (INMatchData matchData) => { d = matchData; evt2.Set(); }; var msg = new NMatchDataSendMessage.Builder(m.Id, opCode, data).Presences(new INUserPresence[] { p }).Build(); client2.Send(msg, false, (bool completed) => { // No action. }, (INError err) => { error = err; evt2.Set(); }); evt2.WaitOne(2000, false); Assert.IsNull(error); Assert.IsNull(d); }
public void StartMatchMaking(Action onMatchMakeSucceeded, Action onMatchMakeFailed) { INMatchmakeTicket matchmake = null; IList <INUserPresence> matchParticipants = null; // Look for a match for two participants. Yourself and one more. var message = NMatchmakeAddMessage.Default(numMatchParticipants); _client.Send(message, (INMatchmakeTicket result) => { Debug.Log("Added user to matchmaker pool."); var cancelTicket = result.Ticket; Debug.LogFormat("The cancellation code {0}", cancelTicket); }, (INError err) => { Debug.LogErrorFormat("Error: code '{0}' with '{1}'.", err.Code, err.Message); }); _client.OnMatchmakeMatched = (INMatchmakeMatched matched) => { // a match token is used to join the match. Debug.LogFormat("Match token: '{0}'", matched.Token); matchParticipants = matched.Presence; // a list of users who've been matched as opponents. foreach (var presence in matched.Presence) { Debug.LogFormat("User id: '{0}'.", presence.UserId); Debug.LogFormat("User handle: '{0}'.", presence.Handle); } // list of all match properties foreach (var userProperty in matched.UserProperties) { foreach (KeyValuePair <string, object> entry in userProperty.Properties) { Debug.LogFormat("Property '{0}' for user '{1}' has value '{2}'.", entry.Key, userProperty.Id, entry.Value); } foreach (KeyValuePair <string, INMatchmakeFilter> entry in userProperty.Filters) { Debug.LogFormat("Filter '{0}' for user '{1}' has value '{2}'.", entry.Key, userProperty.Id, entry.Value.ToString()); } } var jm = NMatchJoinMessage.Default(matched.Token); _client.Send(jm, (INResultSet <INMatch> matches) => { Debug.Log("Successfully joined match."); _match = matches.Results[0]; _matchParticipants = matchParticipants; // foreach(Action a in OnMatchJoinedActions) { // a(); // } foreach (INMatch match in matches.Results) { Debug.LogFormat("Match id: {0} Presence", match.Id); foreach (INUserPresence presence in match.Presence) { Debug.LogFormat("User handle: {0} id {1}.", presence.Handle, presence.UserId); } } Enqueue(() => { onMatchJoined(_match); }); }, (INError error) => { Debug.LogErrorFormat("Error: code '{0}' with '{1}'.", error.Code, error.Message); }); // TODO callback to UI onMatchMakeSucceeded(); }; }
public void MatchmakingJoin() { ManualResetEvent evt1 = new ManualResetEvent(false); ManualResetEvent evt2 = new ManualResetEvent(false); INError error = null; INError error1 = null; INError error2 = null; INMatchmakeMatched res1 = null; INMatchmakeMatched res2 = null; client1.OnMatchmakeMatched = (INMatchmakeMatched matched) => { res1 = matched; evt1.Set(); }; client2.OnMatchmakeMatched = (INMatchmakeMatched matched) => { res2 = matched; evt2.Set(); }; client1.Send(NMatchmakeAddMessage.Default(2), (INMatchmakeTicket ticket1) => { client2.Send(NMatchmakeAddMessage.Default(2), (INMatchmakeTicket ticket2) => { // No action. }, (INError err) => { error = err; }); }, (INError err) => { error = err; }); evt1.WaitOne(5000, false); evt2.WaitOne(5000, false); Assert.IsNull(error); Assert.IsNull(error1); Assert.IsNull(error2); Assert.AreEqual(res1.Token.Token, res2.Token.Token); ManualResetEvent evt1m = new ManualResetEvent(false); ManualResetEvent evt2m = new ManualResetEvent(false); INMatch m1 = null; INMatch m2 = null; INError error1m = null; INError error2m = null; client1.Send(NMatchJoinMessage.Default(res1.Token), (INResultSet <INMatch> matches) => { m1 = matches.Results[0]; evt1m.Set(); }, (INError err) => { error1m = err; evt1m.Set(); }); client2.Send(NMatchJoinMessage.Default(res2.Token), (INResultSet <INMatch> matches) => { m2 = matches.Results[0]; evt2m.Set(); }, (INError err) => { error2m = err; evt2m.Set(); }); evt1m.WaitOne(5000, false); Assert.IsNull(error1m); Assert.IsNull(error2m); Assert.IsNotNull(m1); Assert.IsNotNull(m2); Assert.AreEqual(m1.Id, m2.Id); }