예제 #1
0
 /// <summary>
 /// Submit a request on behalf of the current child.
 /// </summary>
 /// <returns>Results of the request.</returns>
 /// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
 /// <param name="dayTypeId">(optional)The Id of the day type they are requesting.</param>
 /// <param name="lift">(optional) An Array of ids for Bans they are asking to be lifted.</param>
 /// <param name="message">(optional) Message to send with the request.</param>
 /// <param name="callback">callback that will return the response success or error.</param>
 public static void Request(MonoBehaviour behaviour,
                            int dayTypeId,
                            int[] lift,
                            string message,
                            resultClosure callback)
 {
     behaviour.StartCoroutine(_Request(childId, dayTypeId, lift, message, callback));
 }
예제 #2
0
 /// <summary>
 /// Check if the specified child can use the current activities and optionally log usage.
 /// You should ALWAYS log usage when the child is using the activities, otherwise their usage will not be debited from their quota.
 /// Note that if you specify log as true, usage will be recorded even if the child is technically not allowed to use one of the
 /// <paramref name="activities"/>. This is to allow you the ability to be flexible in allowing usage, but should be used sparingly.
 /// If you are, for instance, just checking if something CAN be done at this time, then make sure you supply false for the log parameter.
 /// </summary>
 /// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
 /// <param name="activities">The activity ids to check.</param>
 /// <param name="callback">Provides the result of the check.</param>
 /// <param name="log">If set to <c>true</c>, then log the usage of these activities as well.</param>
 public static void Check(MonoBehaviour behaviour,
                          int[] activities,
                          resultClosure callback,
                          bool log = false
                          )
 {
     behaviour.StartCoroutine(_Check(null, childId, activities, callback, log));
 }
예제 #3
0
 /// <summary>
 /// Pair your game/app/device to a parents Allow2 account.
 /// </summary>
 /// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
 /// <param name="user">The email address of the Allow2 account being paired.</param>
 /// <param name="pass">The associated password for the Allow2 account being paired.</param>
 /// <param name="deviceName">The name the user would like to use to identify this app/game/device.</param>
 /// <param name="callback">Provides the image of the QR Code.</param>
 public static void Pair(MonoBehaviour behaviour,
                         string user,       // ie: "*****@*****.**",
                         string pass,       // ie: "my super secret password",
                         string deviceName, // ie: "Fred's iPhone"
                         resultClosure callback)
 {
     behaviour.StartCoroutine(_Pair(user, pass, deviceName, callback));
 }
예제 #4
0
 /// <summary>
 /// Check if the specified child can use the current activities and optionally log usage.
 /// Note that if you specify log as true, usage will be recorded even if the child is technically not allowed to use one of the
 /// <paramref name="activities"/>. This is to allow you the ability to be flexible in allowing usage, but should be used sparingly.
 /// If you are, for instance, just checking if something CAN be done at this time, then make sure you supply false for the log parameter.
 /// </summary>
 /// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
 /// <param name="childId">Id of the child for which you wish to check (and possibly log) activities.</param>
 /// <param name="activities">The activity ids to check.</param>
 /// <param name="callback">Provides the result of the check.</param>
 /// <param name="log">If set to <c>true</c>, then log the usage of these activities as well.</param>
 public static void Check(MonoBehaviour behaviour,
                          int childId,    // childId == 0 ? Get Updated Child List and confirm Pairing
                          int[] activities,
                          resultClosure callback,
                          bool log = false
                          )
 {
     behaviour.StartCoroutine(_Check(null, childId, activities, callback, log));
 }
예제 #5
0
        static IEnumerator _Request(int childId,
                                    int dayTypeId,
                                    int[] lift,
                                    string message,
                                    resultClosure callback)
        {
            if (!IsPaired)
            {
                callback(Allow2Error.NotPaired, null);
                yield break;
            }
            if (childId < 1)
            {
                callback(Allow2Error.MissingChildId, null);
                yield break;
            }

            WWWForm body = new WWWForm();

            body.AddField("userId", userId);
            body.AddField("pairToken", pairToken);
            body.AddField("deviceToken", _deviceToken);
            body.AddField("childId", childId);
            //form.AddField("lift", lift.asJson);
            //if (dayTypeId != nil) {
            //    body["dayType"] = JSON(dayTypeId!)
            //    body["changeDayType"] = true
            //}
            string bodyStr = body.ToString();

            byte[] bytes = Encoding.UTF8.GetBytes(bodyStr);
            using (UnityWebRequest www = new UnityWebRequest(ApiUrl + "/api/checkPairing"))
            {
                www.method                    = UnityWebRequest.kHttpVerbPOST;
                www.uploadHandler             = new UploadHandlerRaw(bytes);
                www.downloadHandler           = new DownloadHandlerBuffer();
                www.uploadHandler.contentType = "application/json";
                www.chunkedTransfer           = false;
                yield return(www.SendWebRequest());

                // anything other than a 200 response is a "try again" as far as we are concerned
                if (www.responseCode != 200)
                {
                    callback(Allow2Error.NoConnection, null);   // let the caller know we are having problems
                    yield break;
                }
            }
        }
예제 #6
0
        private static IEnumerator PairingLoop(string myUuid, resultClosure callback)
        {
            while (pairingUuid == myUuid)
            {
                Debug.Log("poll uuid: " + myUuid);
                yield return(_PollPairing(myUuid, delegate(string err, Allow2CheckResult result) {
                    if (IsPaired)
                    {
                        pairingUuid = null; // stop the checker, we have been paired
                    }
                    callback(err, result);
                }));

                yield return(new WaitForSeconds(3));
            }
        }
예제 #7
0
        /// <summary>
        /// Start checking (and optionally logging) the ability for the child to use the given activities.
        /// This starts a process that regularly checks (and optionally logs) usage until stopped using Allow2.StopChecking.
        /// You can call this repeatedly and change the child id at any time, but there will only ever be one process and
        /// it will continue to use the last provided child id.
        /// Note, that if the child is unable or disallowed to use any of the <paramref name="activities"/>, they will still be continually checked/logged until Allow2.StopChecking() is called.
        /// This is to allow you to selectively allow the child to finish an activity, but will put them in negative credit (which will come off future usage).
        /// You should ALWAYS log usage when the child is using the activities, otherwise their usage will not be debited from their quota.
        /// </summary>
        /// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
        /// <param name="childId">The child for which the activites are being checked (and optionally logged).</param>
        /// <param name="activities">The activity ids to check.</param>
        /// <param name="callback">Provides the result of the check.</param>
        /// <param name="log">If set to <c>true</c>, then log the usage of these activities as well.</param>
        public static void StartChecking(MonoBehaviour behaviour,
                                         int childId,
                                         int[] activities,
                                         resultClosure callback,
                                         bool log)
        {
            // change the parameters
            bool changed = (_childId != childId);

            _childId = childId;
            if (changed || (checkerUuid == null))
            {
                //switch checker
                checkerUuid = System.Guid.NewGuid().ToString(); // this will abort the current checker and kill it.
                /*checker = */ behaviour.StartCoroutine(CheckLoop(checkerUuid, childId, activities, callback, log));
            }
        }
예제 #8
0
        static IEnumerator _PollPairing(string myUuid, resultClosure callback)
        {
            JSONNode body = new JSONObject();

            body.Add("uuid", uuid);
            body.Add("deviceToken", _deviceToken);
            string bodyStr = body.ToString();

            byte[] bytes = Encoding.UTF8.GetBytes(bodyStr);
            using (UnityWebRequest www = new UnityWebRequest(ApiUrl + "/api/checkPairing"))
            {
                www.method                    = UnityWebRequest.kHttpVerbPOST;
                www.uploadHandler             = new UploadHandlerRaw(bytes);
                www.downloadHandler           = new DownloadHandlerBuffer();
                www.uploadHandler.contentType = "application/json";
                www.chunkedTransfer           = false;
                yield return(www.SendWebRequest());

                // anything other than a 200 response is a "try again" as far as we are concerned
                if (www.responseCode != 200)
                {
                    callback(Allow2Error.NoConnection, null);   // let the caller know we are having problems
                    yield break;
                }

                Debug.Log(www.downloadHandler.text);
                var json = Allow2_SimpleJSON.JSON.Parse(www.downloadHandler.text);

                string status = json["status"];

                if (status != "success")
                {
                    callback(json["message"] ?? "Unknown Error", null);
                    yield break;
                }

                pairToken = json["pairToken"];
                userId    = json["userId"];
                childId   = json["childId"];
                _children = childId > 0 ? new Dictionary <int, string>() : ParseChildren(json["children"]);

                callback(null, null);
            }
        }
예제 #9
0
        private static IEnumerator CheckLoop(string myUuid,
                                             int childId,
                                             int[] activities,
                                             resultClosure callback,
                                             bool log = false)
        {
            while (checkerUuid == myUuid)
            {
                Debug.Log("check uuid: " + myUuid);
                yield return(_Check(myUuid, childId, activities, delegate(string err, Allow2CheckResult result) {
                    if (!IsPaired && (checkerUuid != myUuid))
                    {
                        checkerUuid = null; // stop the checker, we have been unpaired
                    }
                    callback(err, result);
                }, log));

                yield return(new WaitForSeconds(3));
            }
        }
예제 #10
0
 /// <summary>
 /// Use this routine to notify Allow2 you are starting a pairing session for a QR Code pairing.
 /// Call this when you are about to display a QR code to the user to allow them to pair with ALlow2.
 /// Get the appropriate QR Code using Allow2.GetQR().
 ///
 /// </summary>
 /// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param>
 /// <param name="callback">Callback that will return response success or error</param>
 public static void StartPairing(MonoBehaviour behaviour, resultClosure callback)
 {
     //switch checker
     pairingUuid = System.Guid.NewGuid().ToString(); // this will abort the current poll and kill it.
     behaviour.StartCoroutine(PairingLoop(pairingUuid, callback));
 }
예제 #11
0
        static IEnumerator _Check(string myUuid,
                                  int childId,    // childId == 0 ? Get Updated Child List and confirm Pairing
                                  int[] activities,
                                  resultClosure callback,
                                  bool log) // if set, then this is an autochecker and we drop it if we replace it. If null, it's adhoc, always return a value
        {
            if (!IsPaired)
            {
                callback(Allow2Error.NotPaired, null);
                yield break;
            }

            Debug.Log(userId);
            Debug.Log(pairToken);
            Debug.Log(_deviceToken);

            JSONNode body = new JSONObject();

            body.Add("userId", userId);
            body.Add("pairToken", pairToken);
            body.Add("deviceToken", _deviceToken);
            body.Add("tz", _timezone);
            JSONArray activityJson = new JSONArray();

            foreach (int activity in activities)
            {
                JSONNode activityParams = new JSONObject();
                activityParams.Add("id", activity);
                activityParams.Add("log", log);
                activityJson.Add(activityParams);
            }
            body.Add("activities", activityJson);
            body.Add("log", log);
            if (childId > 0)
            {
                body.Add("childId", childId);
            }
            string bodyStr = body.ToString();

            // check the cache first
            if (resultCache.ContainsKey(bodyStr))
            {
                Allow2CheckResult checkResult = resultCache[bodyStr];

                if (checkResult.Expires.CompareTo(new DateTime()) < 0)
                {
                    // not expired yet, use cached value
                    callback(null, checkResult);
                    yield break;
                }

                // clear cached value and ask the server again
                resultCache.Remove(bodyStr);
            }

            byte[] bytes = Encoding.UTF8.GetBytes(bodyStr);
            using (UnityWebRequest www = new UnityWebRequest(ServiceUrl + "/serviceapi/check"))
            {
                www.method                    = UnityWebRequest.kHttpVerbPOST;
                www.uploadHandler             = new UploadHandlerRaw(bytes);
                www.downloadHandler           = new DownloadHandlerBuffer();
                www.uploadHandler.contentType = "application/json";
                www.chunkedTransfer           = false;
                yield return(www.SendWebRequest());

                if ((myUuid != null) && (checkerUuid != myUuid))
                {
                    Debug.Log("drop response for check: " + myUuid);
                    yield break;    // this check is aborted, just drop the response and don't return;
                }

                Debug.Log(www.downloadHandler.text);
                var json = Allow2_SimpleJSON.JSON.Parse(www.downloadHandler.text);

                if ((www.responseCode == 401) ||
                    ((json["status"] == "error") &&
                     ((json["message"] == "Invalid user.") ||
                      (json["message"] == "invalid pairToken"))))
                {
                    // special case, no longer controlled
                    Debug.Log("No Longer Paired");
                    userId    = 0;
                    pairToken = null;
                    Persist();
                    //childId = 0;
                    //_children = []
                    //_dayTypes = []
                    var failOpen = new Allow2CheckResult();
                    failOpen.Add("subscription", new Allow2_SimpleJSON.JSONArray());
                    failOpen.Add("allowed", true);
                    failOpen.Add("activities", new Allow2_SimpleJSON.JSONArray());
                    failOpen.Add("dayTypes", new Allow2_SimpleJSON.JSONArray());
                    failOpen.Add("allDayTypes", new Allow2_SimpleJSON.JSONArray());
                    failOpen.Add("children", new Allow2_SimpleJSON.JSONArray());
                    callback(null, failOpen);
                    yield break;
                }

                if (www.isNetworkError || www.isHttpError)
                {
                    Debug.Log(www.error);
                    callback(www.error, null);
                    yield break;
                }

                if (json["allowed"] == null)
                {
                    callback(Allow2Error.InvalidResponse, null);
                    yield break;
                }

                var response = new Allow2CheckResult();
                response.Add("activities", json["activities"]);
                response.Add("subscription", json["subscription"]);
                response.Add("dayTypes", json["dayTypes"]);
                response.Add("children", json["children"]);
                var _dayTypes = json["allDayTypes"];
                response.Add("allDayTypes", _dayTypes);
                var oldChildIds = _children.Keys;
                var children    = json["children"];
                _children = ParseChildren(children);
                response.Add("children", children);

                if (oldChildIds != _children.Keys)
                {
                    Persist(); // only persist if the children change, this won't happen often.
                }

                // cache the response
                resultCache[bodyStr] = response;
                callback(null, response);
            }
        }
예제 #12
0
        static IEnumerator _Pair(string user,       // ie: "*****@*****.**",
                                 string pass,       // ie: "my super secret password",
                                 string deviceName, // ie: "Fred's iPhone"
                                 resultClosure callback
                                 )
        {
            if (IsPaired)
            {
                callback(Allow2Error.AlreadyPaired, null);
                yield break;
            }
            WWWForm form = new WWWForm();

            form.AddField("user", user);
            form.AddField("pass", pass);
            form.AddField("deviceToken", _deviceToken);
            form.AddField("name", deviceName);
            form.AddField("uuid", uuid);

            Debug.Log(ApiUrl + "/api/pairDevice");

            using (UnityWebRequest www = UnityWebRequest.Post(ApiUrl + "/api/pairDevice", form))
            {
                yield return(www.SendWebRequest());

                var response = JSONNode.Parse(www.downloadHandler.text);

                if (response == null)
                {
                    if (www.isNetworkError || www.isHttpError)
                    {
                        Debug.Log(www.error.ToString());
                        callback(www.error, null);
                    }
                    yield break;
                }

                //{
                //    "status":"success",
                //    "pairId":21105,
                //    "token":"8314c722-36fe-4256-81f4-8cce6e4da32d"
                //    "name":"Unity Test"
                //    "userId":6
                //    "children": [
                //        {"id":68,"name":"Cody"},
                //        {"id":69,"name":"Mikayla"},
                //        {"id":21423,"name":"Mary"}
                //    ]
                //}

                Debug.Log(www.downloadHandler.text);
                var json = Allow2_SimpleJSON.JSON.Parse(www.downloadHandler.text);

                if (json["status"] != "success")
                {
                    callback(Allow2Error.InvalidResponse, null);
                    yield break;
                }

                userId    = json["userId"];
                pairToken = json["token"];
                _children = ParseChildren(json["children"]);

                Persist();

                // return
                callback(null, null);
            }
        }