public void LeaveGame() { Connection.Socket.LeaveMatchAsync(Matchmaker.Match); Matchmaker.CancelSearch(); GlobalModel.ResetGameFlags(); ExitScene("MainMenu"); }
public async void Search(string query, int minCount, int maxCount) { ServerConnection.Instance.Socket.ReceivedMatchmakerMatched += async matched => { var match = await ServerConnection.Instance.Socket.JoinMatchAsync(matched); Match = match; #if UNITY_EDITOR Debug.LogFormat("Received: {0}", matched); Debug.LogFormat("Self: {0}", match.Self); Debug.LogFormat("Presences: {0}", match.Presences.ToJson()); #endif foreach (var u in matched.Users) { if (u.Presence.UserId != GlobalModel.Me.User.Id) { GlobalModel.Opponent = u.Presence; } } ServerConnection.Instance.Socket.ReceivedMatchState += GameController.RecieveState; if (string.Compare(GlobalModel.Me?.User.Id, GlobalModel.Opponent?.UserId) > 0) { int myChar = new System.Random().Next(2); GlobalModel.SetMyCharacter(myChar); GameController.SendState(GameController.ACTION_PICKED_CHARACTER, myChar.ToJson()); } IsMatchReady = true; }; ServerConnection.Instance.Socket.ReceivedMatchPresence += presenceEvent => { if (presenceEvent.Leaves != null && (presenceEvent.Leaves as IList).Count != 0) { Debug.Log("Opponent Left the game..."); Instance.Socket.LeaveMatchAsync(Match); CancelSearch(); GlobalModel.ResetGameFlags(); } }; matchticket = await ServerConnection.Instance.Socket.AddMatchmakerAsync(query, minCount, maxCount); }