/// <summary> /// Unlocks the achievement specified in the parameter, its achievementID. /// </summary> /// <returns> /// FizzyoRequestReturnType.SUCCESS is upload is successful. /// FizzyoRequestReturnType.FAILED_TO_CONNECT if connection failed. /// </returns> public FizzyoRequestReturnType UnlockAchievement(string achievementId) { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } Dictionary <string, string> formData = new Dictionary <string, string>(); formData.Add("gameSecret", FizzyoFramework.Instance.FizzyoConfigurationProfile.GameSecret); formData.Add("userId", FizzyoFramework.Instance.User.UserID); formData.Add("achievementId", achievementId); var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "games/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID + "/achievements/" + achievementId + "/unlock", formData); webRequest.SendWebRequest(); while (!webRequest.isDone) { } ; if (webRequest.error != null) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } //Refresh unlocked achievements to get the latest unlocked. LoadUnlockedAchievements(); return(FizzyoRequestReturnType.SUCCESS); }
/// <summary> /// Loads in the top 20 high-scores for the current game. /// </summary> /// <returns> /// A JSON formatted string containing tag and score for the top 20 scores of the game /// </returns> public FizzyoRequestReturnType GetHighscores() { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } var webRequest = FizzyoNetworking.GetWebRequest(FizzyoNetworking.ApiEndpoint + "games/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID + "/highscores"); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } string topScoresJSONData = webRequest.downloadHandler.text; highscores = JsonUtility.FromJson <AllHighscoreData>(topScoresJSONData).highscores; return(FizzyoRequestReturnType.SUCCESS); }
/// <summary> /// Uploads a players Score /// </summary> /// <returns> /// String - "High Score Upload Complete" - If upload completes /// String - "High Score Upload Failed" - If upload fails /// </returns> public FizzyoRequestReturnType PostScore(int score) { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } Dictionary <string, string> formData = new Dictionary <string, string>(); formData.Add("gameSecret", FizzyoFramework.Instance.FizzyoConfigurationProfile.GameSecret); formData.Add("userId", FizzyoFramework.Instance.User.UserID); formData.Add("score", score.ToString()); var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "games/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID + "/highscores", formData); webRequest.SendWebRequest(); while (!webRequest.isDone) { } ; if (webRequest.error != null) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } return(FizzyoRequestReturnType.SUCCESS); }
/// <summary> /// Loads in the users tag /// </summary> public UserTagReturnType LoadUserTag() { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(UserTagReturnType.FAILED_TO_CONNECT); } //https://api.fizzyo-ucl.co.uk/api/v1/users/:id var webRequest = FizzyoNetworking.GetWebRequest(FizzyoNetworking.ApiEndpoint + "users/" + FizzyoFramework.Instance.User.UserID); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(UserTagReturnType.FAILED_TO_CONNECT); } UserTag allData = JsonUtility.FromJson <UserTag>(webRequest.downloadHandler.text); if (Regex.IsMatch(allData.gamerTag, "^[A-Z]{3}$")) { PlayerPrefs.SetInt("tagDone", 1); return(UserTagReturnType.SUCCESS); } else { return(UserTagReturnType.NOT_SET); } }
internal FizzyoRequestReturnType LoadUnlockedAchievements() { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } //get unlocked achievements var webRequest = FizzyoNetworking.GetWebRequest(FizzyoNetworking.ApiEndpoint + "users/" + FizzyoFramework.Instance.User.UserID + "/unlocked-achievements/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } string unlockedJSONData = webRequest.downloadHandler.text; unlockedAchievements = JsonUtility.FromJson <AllAchievementData>(unlockedJSONData).unlockedAchievements; return(FizzyoRequestReturnType.SUCCESS); }
/// <summary> /// Uses a username and password to access the Fizzyo API and load in the users access token and user Id /// This is currently incomplete as it does not use Windows live authorization /// </summary> private LoginReturnType PostAuthentication(string username, string password) { Dictionary <string, string> formData = new Dictionary <string, string>(); formData.Add("username", username); formData.Add("password", password); var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "auth/test-token", formData); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(LoginReturnType.INCORRECT); } AllUserData allData = JsonUtility.FromJson <AllUserData>(webRequest.downloadHandler.text); UserID = allData.user.id; AccessToken = allData.accessToken; LoggedIn = true; return(LoginReturnType.SUCCESS); }
/// <summary> /// Uploads a players calibration data and also sets the values in player prefs /// </summary> /// <returns> /// String - "Upload Complete" - If upload completes /// String - "Upload Failed" - If upload fails /// </returns> public CalibrationReturnType Calibration(float pressure, float time) { PlayerPrefs.SetFloat("calPressure", pressure); PlayerPrefs.SetFloat("calTime", time); PlayerPrefs.SetInt("calDone", 1); if (PlayerPrefs.GetInt("online") == 0) { return(CalibrationReturnType.FAILED_TO_CONNECT); } //string uploadCal = FizzyoFramework.Instance.FizzyoConfigurationProfile.ApiPath + "/api/v1/users/" + FizzyoFramework.Instance.User.UserID + "/calibration"; DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); TimeSpan diff = DateTime.UtcNow - origin; int calibratedOn = (int)Math.Floor(diff.TotalSeconds); if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(CalibrationReturnType.FAILED_TO_CONNECT); } Dictionary <string, string> formData = new Dictionary <string, string>(); formData.Add("calibratedOn", calibratedOn.ToString()); formData.Add("pressure", pressure.ToString()); formData.Add("time", time.ToString()); var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "users/" + FizzyoFramework.Instance.User.UserID + "/calibration", formData); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(CalibrationReturnType.FAILED_TO_CONNECT); } return(CalibrationReturnType.SUCCESS); }
/// <summary> /// Loads in the users calibration data /// </summary> private CalibrationReturnType LoadCalibrationData() { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(CalibrationReturnType.FAILED_TO_CONNECT); } //https://api.fizzyo-ucl.co.uk/api/v1/users/<userId>/calibration var webRequest = FizzyoNetworking.GetWebRequest(FizzyoNetworking.ApiEndpoint + "users/" + FizzyoFramework.Instance.User.UserID + "/calibration"); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(CalibrationReturnType.FAILED_TO_CONNECT); } CalibrationData allData = JsonUtility.FromJson <CalibrationData>(webRequest.downloadHandler.text); Debug.Log(JsonUtility.ToJson(allData)); PlayerPrefs.SetInt("calLoaded", 1); if ((allData.pressure == 0) || (allData.time == 0)) { return(CalibrationReturnType.NOT_SET); } else { Debug.Log(allData.pressure); Debug.Log(allData.time); PlayerPrefs.SetInt("calDone", 1); PlayerPrefs.SetFloat("calPressure", allData.pressure); PlayerPrefs.SetFloat("calTime", allData.time); return(CalibrationReturnType.SUCCESS); } }
///<summary> ///Once the game shuts down, information from the session is sent to the server. /// ///It will send: /// 1. Amount of sets in this session /// 2. Amounts of breaths in this session /// 3. Amount of good breaths in this session /// 4. Amount of bad breaths in this session /// 5. User-s highest score for this session /// 6. Start time of the session /// 7. End time of the session. /// Note: Time represented as Unix Epoch time. /// </summary> public FizzyoRequestReturnType PostAnalytics() { if (FizzyoFramework.Instance != null && FizzyoFramework.Instance.FizzyoConfigurationProfile != null) { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } ///https://api.fizzyo-ucl.co.uk/api/v1/games/<id>/sessions Dictionary <string, string> formData = new Dictionary <string, string>(); formData.Add("secret", FizzyoFramework.Instance.FizzyoConfigurationProfile.GameSecret); formData.Add("userId", FizzyoFramework.Instance.User.UserID); formData.Add("sessionId", SessionId.ToString()); formData.Add("setCount", _setCount.ToString()); formData.Add("breathCount", BreathCount.ToString()); formData.Add("goodBreathCount", GoodBreathCount.ToString()); formData.Add("badBreathCount", BadBreathCount.ToString()); formData.Add("score", _score.ToString()); formData.Add("startTime", StartTime.ToString()); formData.Add("endTime", EndTime.ToString()); var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "games/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID + "/sessions", formData); webRequest.SendWebRequest(); while (!webRequest.isDone) { } ; if (webRequest.error != null) { Debug.Log("[FizzyoAnalytics] Posting analytics failed. "); return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } Debug.Log("[FizzyoAnalytics] Posting analytics successful."); return(FizzyoRequestReturnType.SUCCESS); } return(FizzyoRequestReturnType.FAILED_TO_CONNECT); }
/// <summary> /// Uploads a players session data and achievements /// </summary> /// <param name="goodBreathCount"> /// Integer that contains the amount of good breaths that were completed in the session /// </param> /// <param name="badBreathCount"> /// Integer that contains the amount of bad breaths that were completed in the session /// </param> /// <param name="score"> /// Integer that holds the players score for that session /// </param> /// <param name="startTime"> /// Integer that holds the time that the session was started /// </param> /// <param name="setCount"> /// Integer that holds the amount of sets that were completed in the session /// </param> /// <param name="breathCount"> /// Integer that holds the amount of breaths that were completed in the session /// </param> /// <returns> /// String - "Session Upload Complete /nAchievement Upload Complete" - If session upload completes and achievement upload completes /// /// String - "Session Upload Complete /nAchievement Upload Failed" - If session upload completes and achievement upload fails /// /// String - "Session Upload Failed /nAchievement Upload Complete" - If session upload fails and achievement upload completes /// /// String - "Session Upload Failed /nAchievement Upload Failed" - If session upload fails and achievement upload fails /// </returns> public static string Session(int goodBreathCount, int badBreathCount, int score, int startTime, int setCount, int breathCount) { if (PlayerPrefs.GetInt("online") == 0 || FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return("Session Upload Failed"); } DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); TimeSpan diff = DateTime.UtcNow - origin; int endTime = (int)Math.Floor(diff.TotalSeconds); Dictionary <string, string> formData = new Dictionary <string, string>(); formData.Add("id", FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID); formData.Add("secret", FizzyoFramework.Instance.FizzyoConfigurationProfile.GameSecret); formData.Add("userId", FizzyoFramework.Instance.User.UserID); formData.Add("setCount", setCount.ToString()); formData.Add("breathCount", breathCount.ToString()); formData.Add("goodBreathCount", goodBreathCount.ToString()); formData.Add("badBreathCount", badBreathCount.ToString()); formData.Add("score", score.ToString()); formData.Add("startTime", startTime.ToString()); formData.Add("endTime", endTime.ToString()); var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "game/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID + "/sessions", formData); webRequest.SendWebRequest(); string status = "Session Upload Complete"; while (!webRequest.isDone) { } if (webRequest.error != null) { status = "Session Upload Failed"; } return(status); }
internal FizzyoRequestReturnType LoadAllAchievements() { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } //Get all achievements from server var webRequest = FizzyoNetworking.GetWebRequest(FizzyoNetworking.ApiEndpoint + "games/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID + "/achievements"); webRequest.SendWebRequest(); while (!webRequest.isDone) { } string achievementsJSONData = webRequest.downloadHandler.text; allAchievements = JsonUtility.FromJson <AllAchievementData>(achievementsJSONData).achievements; return(FizzyoRequestReturnType.SUCCESS); }
/// <summary> /// Uploads a player tag to the Fizzyo API /// </summary> /// <returns> /// String - "Tag Upload Complete" - If upload completes /// String - "Tag Upload Failed" - If upload fails /// String - "Please Select A Different Tag" - If tag contains profanity /// </returns> public UserTagReturnType PostUserTag(string tag) { if (PlayerPrefs.GetInt("online") == 0) { return(UserTagReturnType.FAILED_TO_CONNECT); } string[] tagFilter = { "ASS", "FUC", "FUK", "FUQ", "F*X", "FCK", "COC", "C*K", "COQ", "KOX", "KOC", "KOK", "KOQ", "CAC", "CAK", "CAQ", "KAC", "KAK", "KAQ", "DIC", "DIK", "DIQ", "DIX", "DCK", "PNS", "PSY", "F*G", "FGT", "NGR", "NIG", "CNT", "KNT", "SHT", "DSH", "TWT", "BCH", "CUM", "CLT", "KUM", "KLT", "SUC", "SUK", "SUQ", "SCK", "LIC", "LIK", "LIQ", "LCK", "J*Z", "JZZ", "GAY", "GEY", "GEI", "GAI", "VAG", "VGN", "SJV", "FAP", "PRN", "LOL", "JEW", "JOO", "GVR", "PUS", "PIS", "PSS", "SNM", "TIT", "FKU", "FCU", "FQU", "HOR", "SLT", "JAP", "WOP", "KIK", "KYK", "KYC", "KYQ", "DYK", "DYQ", "DYC", "KKK", "JYZ", "PRK", "PRC", "PRQ", "MIC", "MIK", "MIQ", "MYC", "MYK", "MYQ", "GUC", "GUK", "GUQ", "GIZ", "GZZ", "SEX", "SXX", "SXI", "SXE", "SXY", "XXX", "WAC", "WAK", "WAQ", "WCK", "POT", "THC", "VAJ", "VJN", "NUT", "STD", "LSD", "POO", "AZN", "PCP", "DMN", "ORL", "ANL", "ANS", "MUF", "MFF", "PHK", "PHC", "PHQ", "XTC", "TOK", "TOC", "TOQ", "MLF", "RAC", "RAK", "RAQ", "RCK", "SAC", "SAK", "SAQ", "PMS", "NAD", "NDZ", "NDS", "WTF", "SOL", "SOB", "FOB", "SFU", "PEE", "DIE", "BUM", "BUT", "IRA" }; if (tagFilter.Contains(tag) || !Regex.IsMatch(tag, "^[A-Z]{3}$")) { return(UserTagReturnType.BANNED_TAG); } if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(UserTagReturnType.FAILED_TO_CONNECT); } var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "users/" + FizzyoFramework.Instance.User.UserID + "/gamer-tag", null); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(UserTagReturnType.FAILED_TO_CONNECT); } PlayerPrefs.SetInt("tagDone", 1); PlayerPrefs.SetString("userTag", tag); return(UserTagReturnType.SUCCESS); }
/// <summary> /// Uploads a players achievements for a session /// </summary> /// <returns> /// FizzyoRequestReturnType.SUCCESS is upload is successful. /// FizzyoRequestReturnType.FAILED_TO_CONNECT if connection failed. /// </returns> private FizzyoRequestReturnType PostAchievements() { if (FizzyoNetworking.loginResult != LoginReturnType.SUCCESS) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } string achievementsToUpload = PlayerPrefs.GetString("achievementsToUpload"); if (achievementsToUpload != "") { string[] achievementsToUploadArray = achievementsToUpload.Split(','); for (int i = 0; i < achievementsToUploadArray.Length; i++) { if (achievementsToUploadArray[i] != "") { Dictionary <string, string> formData = new Dictionary <string, string>(); formData.Add("gameSecret", FizzyoFramework.Instance.FizzyoConfigurationProfile.GameSecret); formData.Add("userId", FizzyoFramework.Instance.User.UserID); var webRequest = FizzyoNetworking.PostWebRequest(FizzyoNetworking.ApiEndpoint + "games/" + FizzyoFramework.Instance.FizzyoConfigurationProfile.GameID + "/achievements/" + achievementsToUploadArray[i] + "/unlock", formData); webRequest.SendWebRequest(); while (!webRequest.isDone) { } if (webRequest.error != null) { return(FizzyoRequestReturnType.FAILED_TO_CONNECT); } } } } string achievementsToProgress = PlayerPrefs.GetString("achievementsToProgress"); string[] achievementsToProgressArray = achievementsToProgress.Split(','); AllAchievementData allUserProgress = JsonUtility.FromJson <AllAchievementData>(PlayerPrefs.GetString(FizzyoFramework.Instance.User.UserID + "AchievementProgress")); AllAchievementData allAchievements = JsonUtility.FromJson <AllAchievementData>(PlayerPrefs.GetString("achievements")); // Add achievement progress to player preferences for (int i = 0; i < achievementsToProgressArray.Length; i++) { if (achievementsToProgressArray[i] != "") { for (int j = 0; j < allUserProgress.achievements.Length; j++) { if (allUserProgress.achievements[j].id == achievementsToProgressArray[i]) { for (int k = 0; k < allAchievements.achievements.Length; k++) { if (allUserProgress.achievements[j].id == allAchievements.achievements[k].id) { allUserProgress.achievements[j].unlockProgress = allAchievements.achievements[k].unlockProgress; string newAllData = JsonUtility.ToJson(allUserProgress); PlayerPrefs.SetString(FizzyoFramework.Instance.User.UserID + "AchievementProgress", newAllData); break; } } break; } } } } return(FizzyoRequestReturnType.SUCCESS); }