Esempio n. 1
0
        private ApiWorldInstance GetBestInstance(ApiWorldInstance[] viableInstances)
        {
            ApiWorldInstance apiWorldInstance = null;

            apiWorldInstance = SelectRandomWorldInstanceWeighted((from inst in viableInstances
                                                                  where inst.GetAccessType() == ApiWorldInstance.AccessType.Public && inst.count >= 3
                                                                  select inst).ToArray());
            if (apiWorldInstance != null)
            {
                return(apiWorldInstance);
            }
            apiWorldInstance = SelectRandomWorldInstanceWeighted((from inst in viableInstances
                                                                  where inst.GetAccessType() == ApiWorldInstance.AccessType.Public
                                                                  select inst).ToArray());
            if (apiWorldInstance != null)
            {
                return(apiWorldInstance);
            }
            apiWorldInstance = SelectRandomWorldInstanceWeighted((from inst in viableInstances
                                                                  where inst.GetAccessType() != 0 && inst.count >= 3
                                                                  select inst).ToArray());
            if (apiWorldInstance != null)
            {
                return(apiWorldInstance);
            }
            apiWorldInstance = SelectRandomWorldInstanceWeighted((from inst in viableInstances
                                                                  where inst.GetAccessType() != ApiWorldInstance.AccessType.Public
                                                                  select inst).ToArray());
            if (apiWorldInstance != null)
            {
                return(apiWorldInstance);
            }
            return(null);
        }
Esempio n. 2
0
        public ApiWorldInstance FetchInstance(int index, Action <ApiWorldInstance> success, Action <string> error = null)
        {
            if (index < 0 || index >= mWorldInstances.Count)
            {
                if (error != null)
                {
                    error("Instance index out of range.");
                }
                return(null);
            }
            string           idWithTags       = mWorldInstances[index].idWithTags;
            ApiDictContainer apiDictContainer = new ApiDictContainer();

            apiDictContainer.OnSuccess = delegate(ApiContainer c)
            {
                Dictionary <string, object> responseDictionary = (c as ApiDictContainer).ResponseDictionary;
                mWorldInstances[index] = new ApiWorldInstance(this, responseDictionary, c.DataTimestamp);
                if (success != null)
                {
                    success(mWorldInstances[index]);
                }
            };
            apiDictContainer.OnError = delegate(ApiContainer c)
            {
                if (error != null)
                {
                    error(c.Error);
                }
            };
            ApiDictContainer responseContainer = apiDictContainer;

            API.SendGetRequest("worlds/" + base.id + "/" + idWithTags, responseContainer, null, disableCache: true);
            return(mWorldInstances[index]);
        }
Esempio n. 3
0
 private IEnumerable <ApiWorldInstance> GetViableInstances(string forUserId, List <string> instanceIdsToIgnore = null, bool excludePublicInstances = false, bool includePublicInstancesOnly = false)
 {
     if (instanceIdsToIgnore == null)
     {
         instanceIdsToIgnore = new List <string>();
     }
     if (worldInstances != null)
     {
         List <ApiWorldInstance> list = new List <ApiWorldInstance>();
         for (int i = 0; i < worldInstances.Count; i++)
         {
             ApiWorldInstance apiWorldInstance = worldInstances[i];
             int num = Mathf.Min(capacity, Mathf.Max(6, Mathf.FloorToInt((float)capacity * 0.66f)));
             ApiWorldInstance.AccessType accessType = apiWorldInstance.GetAccessType();
             string instanceCreator = apiWorldInstance.GetInstanceCreator();
             bool   flag            = accessType == ApiWorldInstance.AccessType.Public || accessType == ApiWorldInstance.AccessType.FriendsOfGuests;
             if (apiWorldInstance.count < capacity && apiWorldInstance.count < num && !instanceIdsToIgnore.Contains(apiWorldInstance.idWithTags) && !instanceIdsToIgnore.Contains(apiWorldInstance.idOnly) && accessType != ApiWorldInstance.AccessType.InviteOnly && accessType != ApiWorldInstance.AccessType.InvitePlus && (flag || !includePublicInstancesOnly) && (!excludePublicInstances || !flag) && (accessType != ApiWorldInstance.AccessType.FriendsOnly || APIUser.IsFriendsWith(instanceCreator)) && (accessType != ApiWorldInstance.AccessType.FriendsOfGuests || (instanceCreator != null && !(instanceCreator != forUserId))))
             {
                 list.Add(apiWorldInstance);
             }
         }
         return(from instance in list
                orderby instance.count descending
                select instance);
     }
     return(new ApiWorldInstance[0]);
 }
Esempio n. 4
0
        public ApiWorldInstance GetBestInstance(string forUserId, List <string> instanceIdsToIgnore = null, bool excludePublicInstances = false, bool includePublicInstancesOnly = false)
        {
            if (string.IsNullOrEmpty(forUserId))
            {
                forUserId = APIUser.CurrentUser.id;
            }
            ApiWorldInstance[] viableInstances = GetViableInstances(forUserId, instanceIdsToIgnore, excludePublicInstances, includePublicInstancesOnly).ToArray();
            ApiWorldInstance   bestInstance    = GetBestInstance(viableInstances);

            return((bestInstance != null) ? bestInstance : GetNewInstance(forUserId, excludePublicInstances, includePublicInstancesOnly));
        }
Esempio n. 5
0
        public ApiWorldInstance GetNewInstance(string forUserId, bool excludePublicInstances, bool includePublicInstancesOnly = false)
        {
            if (string.IsNullOrEmpty(forUserId))
            {
                forUserId = APIUser.CurrentUser.id;
            }
            string tags = string.Empty;

            if (releaseStatus == "private")
            {
                tags = ApiWorldInstance.BuildAccessTags(ApiWorldInstance.AccessType.InviteOnly, forUserId);
            }
            else if (excludePublicInstances)
            {
                tags = ApiWorldInstance.BuildAccessTags((!includePublicInstancesOnly) ? ApiWorldInstance.AccessType.FriendsOnly : ApiWorldInstance.AccessType.InviteOnly, forUserId);
            }
            return(GetNewInstance(tags));
        }
Esempio n. 6
0
        public ApiWorldInstance GetNewInstance(string tags = "")
        {
IL_0000:
            int instanceIndex;

            while (true)
            {
                instanceIndex = Random.Range(1, 99999);
                if (worldInstances == null || !worldInstances.Any((ApiWorldInstance wi) => wi.idOnly == instanceIndex.ToString()))
                {
                    break;
                }
            }
            ApiWorldInstance apiWorldInstance = new ApiWorldInstance(this, instanceIndex.ToString() + tags, 0);

            worldInstances.Add(apiWorldInstance);
            return(apiWorldInstance);

IL_006a:
            goto IL_0000;
        }
Esempio n. 7
0
        public void PutInstance(ApiWorldInstance inst, Action success, Action <string> error = null)
        {
            string idWithTags = inst.idWithTags;
            int    num        = -1;

            for (int i = 0; i < mWorldInstances.Count; i++)
            {
                if (mWorldInstances[i].idWithTags == idWithTags)
                {
                    mWorldInstances[i] = inst;
                }
            }
            bool newInstance = num == -1;
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            if (newInstance)
            {
                dictionary["id"] = inst.idWithTags;
                ApiWorldInstance.AccessType accessType = inst.GetAccessType();
                if (accessType == ApiWorldInstance.AccessType.InviteOnly)
                {
                    dictionary["private"] = inst.instanceCreator;
                }
                else
                {
                    dictionary["private"] = "false";
                }
                if (accessType == ApiWorldInstance.AccessType.FriendsOnly)
                {
                    dictionary["friends"] = inst.instanceCreator;
                }
                else
                {
                    dictionary["friends"] = "false";
                }
            }
            dictionary["name"] = inst.instanceName;
            string tagString = inst.GetTagString();

            if (tagString != null)
            {
                dictionary["tags"] = tagString;
            }
            ApiDictContainer apiDictContainer = new ApiDictContainer();

            apiDictContainer.OnSuccess = delegate(ApiContainer c)
            {
                Dictionary <string, object> responseDictionary = (c as ApiDictContainer).ResponseDictionary;
                if (newInstance && responseDictionary != null)
                {
                    mWorldInstances.Add(new ApiWorldInstance(this, responseDictionary, c.DataTimestamp));
                }
                if (success != null)
                {
                    success();
                }
            };
            apiDictContainer.OnError = delegate(ApiContainer c)
            {
                if (error != null)
                {
                    error(c.Error);
                }
            };
            ApiDictContainer responseContainer = apiDictContainer;

            API.SendPutRequest("worlds/" + base.id + "/" + idWithTags, responseContainer, dictionary);
        }
Esempio n. 8
0
 public void Fetch(string instanceID = null, string platforms = null, Action <ApiContainer> onSuccess = null, Action <ApiContainer> onFailure = null, Dictionary <string, object> parameters = null, bool compatibleVersionsOnly = true)
 {
     if (string.IsNullOrEmpty(base.id))
     {
         onFailure(new ApiContainer
         {
             Error = "APIWorld.Fetch called with null id."
         });
     }
     else if (localWorlds.ContainsKey(base.id))
     {
         ApiModelContainer <ApiWorld> obj = new ApiModelContainer <ApiWorld>(localWorlds[base.id]);
         onSuccess(obj);
     }
     else
     {
         if (parameters == null)
         {
             parameters = new Dictionary <string, object>();
         }
         if (compatibleVersionsOnly)
         {
             parameters["maxUnityVersion"] = VERSION.UnityVersion;
             parameters["minUnityVersion"] = MIN_LOADABLE_VERSION.UnityVersion;
             parameters["maxAssetVersion"] = VERSION.ApiVersion;
             parameters["minAssetVersion"] = MIN_LOADABLE_VERSION.ApiVersion;
         }
         if (platforms != null)
         {
             parameters["platform"] = platforms;
         }
         if (!string.IsNullOrEmpty(instanceID))
         {
             ApiDictContainer apiDictContainer = new ApiDictContainer("users");
             apiDictContainer.OnSuccess = delegate(ApiContainer c)
             {
                 List <object>         json       = (c as ApiDictContainer).ResponseDictionary["users"] as List <object>;
                 IEnumerable <APIUser> enumerable = API.ConvertJsonListToModelList <APIUser>(json, c.DataTimestamp);
                 if (enumerable == null)
                 {
                     c.Error = "Failed to decode user model";
                     onFailure(c);
                 }
                 else
                 {
                     int num = enumerable.Count();
                     if (instances == null)
                     {
                         instances = new Dictionary <string, int>();
                     }
                     if (!mWorldInstances.Any((ApiWorldInstance w) => w.idWithTags == instanceID))
                     {
                         mWorldInstances.Add(new ApiWorldInstance(this, instanceID, num));
                         instances.Add(instanceID, num);
                     }
                     ApiWorldInstance apiWorldInstance = worldInstances.First((ApiWorldInstance w) => w.idWithTags == instanceID);
                     apiWorldInstance.count = num;
                     apiWorldInstance.users = enumerable.ToList();
                     onSuccess(c);
                 }
             };
             apiDictContainer.OnError = onFailure;
             ApiDictContainer responseContainer = apiDictContainer;
             API.SendRequest("worlds/" + base.id + "/" + instanceID, HTTPMethods.Get, responseContainer, parameters, authenticationRequired: true, disableCache: true);
         }
         else
         {
             ApiModelContainer <ApiWorld> apiModelContainer = new ApiModelContainer <ApiWorld>(this);
             apiModelContainer.OnSuccess = onSuccess;
             apiModelContainer.OnError   = onFailure;
             ApiModelContainer <ApiWorld> responseContainer2 = apiModelContainer;
             API.SendRequest("worlds/" + base.id, HTTPMethods.Get, responseContainer2, parameters, authenticationRequired: true, disableCache: true);
         }
     }
 }