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); }
public void CreateMatch() { ManualResetEvent evt = new ManualResetEvent(false); INError error = null; client1.Send(NMatchCreateMessage.Default(), (INMatch match) => { evt.Set(); }, (INError err) => { error = err; evt.Set(); }); evt.WaitOne(5000, false); Assert.IsNull(error); }
// +++ public functions +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ public void CreateMatch() { var createMatchMessage = NMatchCreateMessage.Default(); _client.Send( createMatchMessage, (INMatch match) => { if (OnMatchCreated != null) { OnMatchCreated(match); } }, (INError error) => { if (OnMatchCreated != null) { OnMatchCreated(null); } } ); }
//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)); }
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 SendDataNoEchoMatch() { INError error = null; INMatch m = null; ManualResetEvent evt1 = new ManualResetEvent(false); 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); INMatchData d = null; ManualResetEvent evt2 = new ManualResetEvent(false); client1.OnMatchData = (INMatchData data) => { d = data; evt2.Set(); }; client1.Send(NMatchDataSendMessage.Default(m.Id, 9, Encoding.ASCII.GetBytes("test-data")), false, (bool completed) => { // No action. }, (INError err) => { error = err; evt2.Set(); }); evt2.WaitOne(1000, false); Assert.IsNull(error); Assert.IsNull(d); }
// match button clicked public void MatchBtnClick() { // change matching status this.matching = !this.matching; Text matchBtnText = (Text)this.matchBtn.GetComponentInChildren(typeof(Text)); // broadcast matching info if (this.matching) { matchBtnText.text = "Cancel Matching"; // create match var message = NMatchCreateMessage.Default(); Application.client.Send(message, (INMatch match) => { // user enter range id, to distinguish between player1 and player2... Application.data["userEnterId"] = this.userEnterId; // Use this match ID to reference the match later. Application.data["matchId"] = match.Id; this.matchId = match.Id; // broadcast this.sendTopic(this.matchId); Debug.Log("Successfully created match: " + Encoding.UTF8.GetString(this.matchId)); }, (INError error) => { Debug.LogErrorFormat("Could not create match: '{0}'.", error.Message); }); } else { // restore to default status matchBtnText.text = "Match"; this.matchId = null; } }
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); }