public void submitScore(Action <bool, string> callback) { submitScoreCallback = callback; string gameObjectName = "OpenKitSubmitScoreObject." + DateTime.Now.Ticks; callbackGameObjectName = gameObjectName; // Create a new OKScore gameobject (called scoreComponent) and give it a unique name // This allows us to track unique score submission requests and handle // async native code #if !UNITY_EDITOR GameObject gameObject = new GameObject(gameObjectName); DontDestroyOnLoad(gameObject); OKScore scoreComponent = gameObject.AddComponent <OKScore>(); scoreComponent.submitScoreCallback = callback; scoreComponent.scoreValue = scoreValue; scoreComponent.OKLeaderboardID = OKLeaderboardID; scoreComponent.displayString = displayString; scoreComponent.metadata = metadata; scoreComponent.callbackGameObjectName = gameObjectName; OKManager.SubmitScore(scoreComponent); #endif }
public void submitScore(OKScore score) { if(score.displayString == null) { //Set the displayString to blank if it's null because you can't pass null strings to JNI functions score.displayString = ""; } OKAndroidPlugin.CallStatic("submitScore", score.scoreValue, score.OKLeaderboardID, score.metadata, score.displayString, score.GetCallbackGameObjectName()); }
private void ScoreDidLoadMetadata(OKScore score) { if (Cancelled) { return; } _pendingBufferLoad.Remove(score); if (_pendingBufferLoad.Count == 0) { DoDidFinish(); } }
public void SubmitScore(Action <OKScore, OKException> callback) { OKUser u = GetUser(); if (u == null) { throw new Exception("You need a user to submit a score"); } Dictionary <string, object> score = new Dictionary <string, object>(); score.Add("leaderboard_id", LeaderboardID); score.Add("value", scoreValue); score.Add("display_string", displayString); score.Add("metadata", metadata); score.Add("user_id", u.OKUserID.ToString()); Dictionary <string, object> reqParams = new Dictionary <string, object>(); reqParams.Add("score", score); OKUploadBuffer buff = null; if (MetadataBuffer != null) { buff = new OKUploadBuffer() { Bytes = MetadataBuffer, ParamName = "score[meta_doc]", FileName = "upload" }; } Action <JSONObject, OKCloudException> handler = (responseObj, e) => { if (e == null) { OKScore retScore = new OKScore(responseObj); callback(retScore, null); } else { callback(null, e); } }; OKCloudAsyncRequest.Post("/scores", reqParams, buff, handler); }
// Helper function for getting scores from OpenKit, internal use only private void GetScores(ScoreRequestType rt, Dictionary <string, object> requestParams, Action <List <OKScore>, OKException> requestHandler) { Action <JSONObject, OKCloudException> internalHandler = (responseObj, e) => { if (e == null) { if (responseObj.type == JSONObject.Type.ARRAY) { OKLog.Info("Successfully got " + responseObj.list.Count + " scores"); // OKLog.Info("GetScores Response json: " + responseObj.ToString()); List <OKScore> scoresList = new List <OKScore>(responseObj.list.Count); for (int x = 0; x < responseObj.list.Count; x++) { OKScore score = new OKScore(responseObj[x]); scoresList.Add(score); } requestHandler(scoresList, null); } else { requestHandler(null, new OKException("Expected an array of scores but did not get back an Array JSON")); } } else { requestHandler(null, e); } }; switch (rt) { case ScoreRequestType.Global: OKCloudAsyncRequest.Get("/best_scores", requestParams, internalHandler); break; case ScoreRequestType.Social: OKCloudAsyncRequest.Post("/best_scores/social", requestParams, internalHandler); break; } }
private void GetUsersTopScore(OKUser currentUser, Action <OKScore, OKException> requestHandler) { if (currentUser == null) { requestHandler(null, new OKException("No OKUser logged in, can't get best score")); return; } Dictionary <string, object> requestParams = new Dictionary <string, object>(); requestParams.Add("leaderboard_id", this.LeaderboardID); requestParams.Add("leaderboard_range", "all_time"); requestParams.Add("user_id", currentUser.OKUserID); OKCloudAsyncRequest.Get("/best_scores/user", requestParams, (JSONObject responseObj, OKCloudException e) => { if (e == null) { if (responseObj.type == JSONObject.Type.OBJECT) { OKScore topScore = new OKScore(responseObj); requestHandler(topScore, null); } else if (responseObj.type == JSONObject.Type.NULL) { requestHandler(null, null); } else { requestHandler(null, new OKException("Expected a single score JSON object but got something else")); } } else { requestHandler(null, e); } }); }
private void GetUsersTopScore(OKUser currentUser, Action<OKScore,OKException> requestHandler) { if(currentUser == null) { requestHandler(null, new OKException("No OKUser logged in, can't get best score")); return; } Dictionary<string, object> requestParams = new Dictionary<string, object>(); requestParams.Add("leaderboard_id", this.LeaderboardID); requestParams.Add("leaderboard_range","all_time"); requestParams.Add("user_id",currentUser.OKUserID); OKCloudAsyncRequest.Get("/best_scores/user",requestParams, (JSONObject responseObj, OKCloudException e) => { if(e == null) { if(responseObj.type == JSONObject.Type.OBJECT) { OKScore topScore = new OKScore(responseObj); requestHandler(topScore, null); } else if (responseObj.type == JSONObject.Type.NULL) { requestHandler(null, null); } else { requestHandler(null, new OKException("Expected a single score JSON object but got something else")); } } else { requestHandler(null, e); } }); }
public void SubmitScore(OKScore score) { nativeBridge.submitScore(score); }
public static void SubmitScore(OKScore score) { OKManagerImpl.Instance.SubmitScore(score); }
private void ScoreDidLoadMetadata(OKScore score) { if (Cancelled) return; _pendingBufferLoad.Remove(score); if(_pendingBufferLoad.Count == 0) { DoDidFinish(); } }
private void ScoreDidLoadMetadata(OKScore score) { _pending.Remove(score); if(_pending.Count == 0) _handler(this); }
// Notes about posting a score: // // If the user is not logged in, the score will not be submitted successfully. // // When submitting a score natively, if the score submission fails, the score is cached locally on the device and resubmitted // when the user logs in or next time the app loads, whichever comes first. // // Metadata (optional) is stored and retrieved with each score. It can be used // to save additional state information with each score. // // The display string can be used to append units or create a custom format // for your score to be displayed. The score value, passed in constructor, // is only used for sorting scores on the backend (to determine which is best), // the display string is used for displaying scores in the UI. void SubmitSampleScore() { int lapTime = 5400; // value in hundredths int total_sec = lapTime / 100; int total_min = total_sec / 60; int hour = total_min / 60; int min = total_min % 60; int sec = total_sec % 60; int hun = lapTime % 100; string scoreString = "" + hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00") + "." + hun.ToString("00"); OKScore score = new OKScore(lapTime, SampleLeaderboardID); score.gameCenterLeaderboardCategory = "openkitlevel3"; score.displayString = scoreString + " seconds"; Action<bool, string> nativeHandle = (success, errorMessage) => { if (success) { Debug.Log("Score submitted successfully!"); } else { Debug.Log("Score did not submit. Error: " + errorMessage); } }; Action<OKScore, OKException> defaultHandle = (retScore, err) => { if (err == null) { Debug.Log("Score submitted successfully: " + retScore.ToString()); } else { Debug.Log("Score post failed: " + err.Message); } }; bool dropToNative = true; if (dropToNative) { score.SubmitScoreNatively(nativeHandle); } else { score.MetadataBuffer = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x80 }; score.SubmitScore(defaultHandle); } }
void OnGUI() { ///Scale the button sizes for retina displays float screenScale = (float)(Screen.width / 480.0); Matrix4x4 scaledMatrix = Matrix4x4.Scale(new Vector3(screenScale, screenScale, screenScale)); GUI.matrix = scaledMatrix; if(GUI.Button(new Rect(30,10,400,100), "Show Leaderboards & Achievements")) { // Show leaderboards. // If the user is not logged into OpenKit, the login UI // will be shown ontop of the leaderboards OKManager.ShowLeaderboards(); } if(GUI.Button(new Rect(30,120,400,100), "Show Login UI")) { // Show the OpenKit Login UI OKManager.ShowLoginToOpenKit(); } if(GUI.Button(new Rect(30,230,400,100), "Submit Score to Level 3 Leaderboard")) { #if !UNITY_EDITOR // Submit a score to a leaderboard, with a value of 2134 to leaderboard ID 4 // If the user is not logged in, the score will not be submitted successfully string scoreString = "" + DateTime.Now.Month; scoreString += DateTime.Now.Day; scoreString += DateTime.Now.Hour; scoreString += DateTime.Now.Minute; long scoreValue = long.Parse(scoreString); OKScore score = new OKScore(scoreValue, 4); // Set the displayString to include the units of the score score.displayString = score.scoreValue + " points"; // Store some metadata in the score-- this is not used by OpenKit but is stored and returned with each score score.metadata = 1; score.submitScore(scoreSubmitHandler); #endif } if(GUI.Button(new Rect(30,340,400,100), "Unlock Achievement")) { //Unlock achievement by setting its progress for the current user // to 5. The achievement ID is pulled from the OpenKit dashboard, // and we know that the target goal of the achievement is also 5 which is set in the dashboard, // so this unlocks the achievement OKAchievementScore achievementScore = new OKAchievementScore(5, 3); achievementScore.submitAchievementScore(achievementScoreSubmitHandler); } if(GUI.Button(new Rect(30,450,400,100), "Store dictionary")) { //Store a dictionary ArrayList y = new ArrayList(); y.Add("First element."); y.Add("Second!"); Dictionary<string, object> x = new Dictionary<string, object>(); x.Add("prop1", "YEAAAAAAH BUDDY."); x.Add("prop2", 99); x.Add("prop3", y); // Cloud store. OKCloud.Set(x, "aKey", delegate(object obj, OKCloudException err) { if (err == null) { OKLog.Info("Stored object of type: " + obj.GetType().Name); } else { OKLog.Info("Error during store: " + err); } }); } if(GUI.Button(new Rect(30,560,400,100), "Retrieve dictionary")) { //Retrieve the dictionary OKCloud.Get("aKey", delegate(JSONObject obj, OKCloudException err) { if (err == null) { OKLog.Info("Retrieved object of type: " + obj.GetType().Name); OKLog.Info("Obj: " + obj); OKLog.Info("Can I get an element of an Array? " + obj.GetField("prop3")[1]); } else { OKLog.Info("Error during store: " + err); } }); } }
// Helper function for getting scores from OpenKit, internal use only private void GetScores(ScoreRequestType rt, Dictionary<string, object> requestParams,Action<List<OKScore>, OKException> requestHandler) { Action<JSONObject, OKCloudException> internalHandler = (responseObj, e) => { if(e == null) { if(responseObj.type == JSONObject.Type.ARRAY) { OKLog.Info("Successfully got " + responseObj.list.Count + " scores"); // OKLog.Info("GetScores Response json: " + responseObj.ToString()); List<OKScore> scoresList = new List<OKScore>(responseObj.list.Count); for(int x = 0; x < responseObj.list.Count; x++) { OKScore score = new OKScore(responseObj[x]); scoresList.Add(score); } requestHandler(scoresList, null); } else { requestHandler(null, new OKException("Expected an array of scores but did not get back an Array JSON")); } } else { requestHandler(null, e); } }; switch (rt) { case ScoreRequestType.Global: OKCloudAsyncRequest.Get("/best_scores", requestParams, internalHandler); break; case ScoreRequestType.Social: OKCloudAsyncRequest.Post("/best_scores/social", requestParams, internalHandler); break; } }
// Notes about posting a score: // // If the user is not logged in, the score will not be submitted successfully. // // When submitting a score natively, if the score submission fails, the score is cached locally on the device and resubmitted // when the user logs in or next time the app loads, whichever comes first. // // Metadata (optional) is stored and retrieved with each score. It can be used // to save additional state information with each score. // // The display string can be used to append units or create a custom format // for your score to be displayed. The score value, passed in constructor, // is only used for sorting scores on the backend (to determine which is best), // the display string is used for displaying scores in the UI. void SubmitSampleScore() { int lapTime = 5400; // value in hundredths int total_sec = lapTime / 100; int total_min = total_sec / 60; int hour = total_min / 60; int min = total_min % 60; int sec = total_sec % 60; int hun = lapTime % 100; string scoreString = "" + hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00") + "." + hun.ToString("00"); OKScore score = new OKScore(lapTime, SampleLeaderboardID); score.displayString = scoreString; score.gameCenterLeaderboardCategory = SampleLeaderboardGameCenterCategory; // OKScore can be submitted to OpenKit in C# native unity, or platform native code (e.g. iOS and Android native cdoe). // When possible you should use the platform native versions of OKScore.SubmitScore because both iOS and Android SDKs // have local caching built in, as well as features like submit to GameCenter (iOS). Action<bool, string> nativeHandle = (success, errorMessage) => { if (success) { Debug.Log("Score submitted successfully!"); } else { Debug.Log("Score did not submit. Error: " + errorMessage); } }; Action<OKScore, OKException> defaultHandle = (retScore, err) => { if (err == null) { Debug.Log("Score submitted successfully: " + retScore.ToString()); } else { Debug.Log("Score post failed: " + err.Message); } }; if(submitScoreNatively) { score.SubmitScoreNatively(nativeHandle); } else { score.MetadataBuffer = new byte[] { 0x20, 0x20, 0x20, 0x20, 0x80 }; score.SubmitScore(defaultHandle); } }