Example #1
0
        public static void FinishMatch(string matchId, string winnerUserId = "", Action <bool, GDMatch> callback = null)
        {
            IDictionary <string, object> inputData = new Dictionary <string, object> ();

            if ("".Equals(winnerUserId))
            {
                inputData.Add("winnerId", winnerUserId);
            }

            GamedoniaBackend.RunCoroutine(
                GamedoniaRequest.post("/matchmaking/finish/" + matchId, Json.Serialize(inputData), null, GamedoniaUsers.GetSessionToken(), null,
                                      delegate(bool success, object data) {
                GDMatch finishedMatch = null;
                if (success)
                {
                    finishedMatch = DeserializeMatch((string)data);
                }
                if (callback != null)
                {
                    callback(success, finishedMatch);
                }
            }
                                      )
                );
        }
Example #2
0
        private static List <GDMatch> DeserializeMatches(List <object> matches)
        {
            List <GDMatch> matchesList = new List <GDMatch> ();

            foreach (object matchObj in matches)
            {
                Dictionary <string, object> matchMap = matchObj as Dictionary <string, object>;
                GDMatch match = new GDMatch();
                match._id        = matchMap ["_id"] as string;
                match.state      = matchMap ["state"] as string;
                match.properties = matchMap ["properties"] as Dictionary <string, object>;
                List <object> usersList = matchMap ["users"] as List <object>;


                foreach (object userObj in usersList)
                {
                    Dictionary <string, object> userMap = userObj as Dictionary <string, object>;
                    GDUserProfile user = new GDUserProfile();
                    user._id     = userMap["_id"] as string;
                    user.profile = userMap["profile"] as Dictionary <string, object>;
                    match.users.Add(user);
                }

                matchesList.Add(match);
            }
            return(matchesList);
        }
Example #3
0
        private static IEnumerator IsMatchReady(string matchId, int requiredPlayers, int waitTime, Action <bool> callback = null)
        {
            int     time             = 0;
            bool    IsMatchReady     = false;
            bool    IsMatchReadyDone = false;
            bool    IsGetMatchDone   = false;
            GDMatch retrievedMatch   = null;

            do
            {
                Debug.Log("Checking if Match [" + matchId + "] is ready");

                GamedoniaMatchMaking.GetMatch(matchId,
                                              delegate(bool success, GDMatch match, List <GDMatchEvent> matchEvents) {
                    IsGetMatchDone = true;
                    if (success)
                    {
                        retrievedMatch = match;
                    }
                }
                                              );


                while (!IsGetMatchDone)
                {
                    yield return(null);
                }

                //Check if match is ready
                if (retrievedMatch.users.Count >= requiredPlayers)
                {
                    IsMatchReady     = true;
                    IsMatchReadyDone = true;
                }
                else
                {
                    //Check if we are under total wait time
                    if (time < waitTime)
                    {
                        yield return(new WaitForSeconds(3));

                        time += 3;
                    }
                    else
                    {
                        IsMatchReadyDone = true;
                    }
                }
            }while(!IsMatchReadyDone);


            callback(IsMatchReady);
        }
Example #4
0
        public static void CreateMatch(GDMatch match, Action<bool, GDMatch> callback = null)
        {
            string json = JsonMapper.ToJson(match);

            GamedoniaBackend.RunCoroutine(
                GamedoniaRequest.post("/matchmaking/create",json,null, GamedoniaUsers.GetSessionToken(), null,
                    delegate (bool success, object data) {
                        GDMatch createdMatch = null;
                        if (success) createdMatch =  DeserializeMatch((string) data);
                        if (callback!=null) callback(success, createdMatch);
                    }
             	 )
            );
        }
Example #5
0
 public static void StartMatch(string matchId, Action <bool, GDMatch> callback = null)
 {
     GamedoniaBackend.RunCoroutine(
         GamedoniaRequest.post("/matchmaking/start/" + matchId, "{}", null, GamedoniaUsers.GetSessionToken(), null,
                               delegate(bool success, object data) {
         GDMatch startedMatch = null;
         if (success)
         {
             startedMatch = DeserializeMatch((string)data);
         }
         if (callback != null)
         {
             callback(success, startedMatch);
         }
     }
                               )
         );
 }
Example #6
0
        public static void CreateMatch(GDMatch match, Action <bool, GDMatch> callback = null)
        {
            string json = JsonMapper.ToJson(match);

            GamedoniaBackend.RunCoroutine(
                GamedoniaRequest.post("/matchmaking/create", json, null, GamedoniaUsers.GetSessionToken(), null,
                                      delegate(bool success, object data) {
                GDMatch createdMatch = null;
                if (success)
                {
                    createdMatch = DeserializeMatch((string)data);
                }
                if (callback != null)
                {
                    callback(success, createdMatch);
                }
            }
                                      )
                );
        }
Example #7
0
 public static void GetMatch(string matchId, Action <bool, GDMatch, List <GDMatchEvent> > callback = null)
 {
     GamedoniaBackend.RunCoroutine(
         GamedoniaRequest.get("/matchmaking/get/" + matchId, GamedoniaUsers.GetSessionToken(),
                              delegate(bool success, object data) {
         GDMatch match = null;
         List <GDMatchEvent> events = new List <GDMatchEvent>();
         if (success)
         {
             Dictionary <string, object> result = (Dictionary <string, object>)Json.Deserialize((string)data);
             match  = DeserializeMatch(result["match"] as Dictionary <string, object>);
             events = DeserializeMatchEvents(result["events"] as List <object>);
         }
         if (callback != null)
         {
             callback(success, match, events);
         }
     }
                              )
         );
 }
Example #8
0
        private static List<GDMatch> DeserializeMatches(List<object> matches)
        {
            List<GDMatch> matchesList = new List<GDMatch> ();

            foreach (object matchObj in matches) {
                Dictionary<string,object> matchMap = matchObj as Dictionary<string,object>;
                GDMatch match = new GDMatch ();
                match._id = matchMap ["_id"] as string;
                match.state = matchMap ["state"] as string;
                match.properties = matchMap ["properties"] as Dictionary<string,object>;
                List<object> usersList = matchMap ["users"] as List<object>;

                foreach(object userObj in usersList) {
                    Dictionary<string,object> userMap = userObj as Dictionary<string,object>;
                    GDUserProfile user = new GDUserProfile();
                    user._id = userMap["_id"] as string;
                    user.profile = userMap["profile"] as Dictionary<string,object>;
                    match.users.Add(user);
                }

                matchesList.Add(match);
            }
            return matchesList;
        }
Example #9
0
        /*
         * TODO Easy method for matchmaking
         */
        public static void QuickMatch(GDMatch match, string criteria, int requiredPlayers, int searchTime, Action<bool, GDMatch> callback= null)
        {
            GamedoniaMatchMaking.FindMatches (criteria, 0,
                delegate(bool success, List<GDMatch> foundMatches) {

                    if (success) {
                        if (foundMatches.Count > 0) {
                            //Join Match
                            GamedoniaMatchMaking.JoinMatch(foundMatches[0]._id,
                                delegate(bool joinSuccess, GDMatch joinedMatch) {

                                    if (success) {
                                        GamedoniaBackend.RunCoroutine(
                                            GamedoniaMatchMaking.IsMatchReady(joinedMatch._id,requiredPlayers, searchTime,
                                                delegate(bool isReady) {

                                                    if (isReady) {
                                                        GamedoniaMatchMaking.StartMatch(joinedMatch._id,
                                                            delegate(bool startSucces, GDMatch startedMatch) {
                                                                if(startSucces) {
                                                                    callback(true,startedMatch);
                                                                }else {
                                                                    callback(false,null);
                                                                }
                                                            }
                                                        );
                                                    }else {
                                                        callback(false,null);
                                                    }

                                                }
                                            )
                                        );
                                    }

                                }
                            );
                        }else {

                            GamedoniaMatchMaking.CreateMatch(match,delegate(bool successCreate, GDMatch createdMatch) {
                                if (successCreate) {

                                    GamedoniaBackend.RunCoroutine(
                                        GamedoniaMatchMaking.IsMatchReady(createdMatch._id,requiredPlayers, searchTime,
                                            delegate(bool isReady) {

                                                if (isReady) {
                                                    GamedoniaMatchMaking.StartMatch(createdMatch._id,
                                                        delegate(bool startSucces, GDMatch startedMatch) {
                                                            if(startSucces) {
                                                                callback(true,startedMatch);
                                                            }else {
                                                                callback(false,null);
                                                            }
                                                        }
                                                    );
                                                }else {
                                                    callback(false,null);
                                                }
                                            }
                                        )
                                    );

                                }
                            });

                        }
                    }

                }
            );
        }
Example #10
0
 /*
  * TODO Easy method for matchmaking
  */
 public static void QuickMatch(GDMatch match, string criteria, int requiredPlayers, int searchTime, Action <bool, GDMatch> callback = null)
 {
     GamedoniaMatchMaking.FindMatches(criteria, 0,
                                      delegate(bool success, List <GDMatch> foundMatches) {
         if (success)
         {
             if (foundMatches.Count > 0)
             {
                 //Join Match
                 GamedoniaMatchMaking.JoinMatch(foundMatches[0]._id,
                                                delegate(bool joinSuccess, GDMatch joinedMatch) {
                     if (success)
                     {
                         GamedoniaBackend.RunCoroutine(
                             GamedoniaMatchMaking.IsMatchReady(joinedMatch._id, requiredPlayers, searchTime,
                                                               delegate(bool isReady) {
                             if (isReady)
                             {
                                 GamedoniaMatchMaking.StartMatch(joinedMatch._id,
                                                                 delegate(bool startSucces, GDMatch startedMatch) {
                                     if (startSucces)
                                     {
                                         callback(true, startedMatch);
                                     }
                                     else
                                     {
                                         callback(false, null);
                                     }
                                 }
                                                                 );
                             }
                             else
                             {
                                 callback(false, null);
                             }
                         }
                                                               )
                             );
                     }
                 }
                                                );
             }
             else
             {
                 GamedoniaMatchMaking.CreateMatch(match, delegate(bool successCreate, GDMatch createdMatch) {
                     if (successCreate)
                     {
                         GamedoniaBackend.RunCoroutine(
                             GamedoniaMatchMaking.IsMatchReady(createdMatch._id, requiredPlayers, searchTime,
                                                               delegate(bool isReady) {
                             if (isReady)
                             {
                                 GamedoniaMatchMaking.StartMatch(createdMatch._id,
                                                                 delegate(bool startSucces, GDMatch startedMatch) {
                                     if (startSucces)
                                     {
                                         callback(true, startedMatch);
                                     }
                                     else
                                     {
                                         callback(false, null);
                                     }
                                 }
                                                                 );
                             }
                             else
                             {
                                 callback(false, null);
                             }
                         }
                                                               )
                             );
                     }
                 });
             }
         }
     }
                                      );
 }