public override async void TouchesBegan(NSSet touches, UIEvent evt) { foreach (var touch in touches) { var location = (touch as UITouch).LocationInNode(this); if (button.ContainsPoint(location)) { GameInfo.CurrentTaps++; if (GameInfo.GameMode == GameMode.Hard) { int x = rand.Next(100) - 50; int y = rand.Next(100) - 50; button.Position = new PointF(FrameMidX + x, FrameMidY + y); } } } if (GKLocalPlayer.LocalPlayer.Authenticated) { var tapOnceAchievement = new GKAchievement(TapOnceId, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = 100 }; await GKAchievement.ReportAchievementsAsync(new [] { tapOnceAchievement }, null); } }
public void submitAchievement(string identifier, double percentComplete, string achievementName) { if (earnedAchievementCache == null) { GKAchievement.LoadAchievements(new GKCompletionHandler(delegate(GKAchievement[] achievements, NSError error) { NSMutableDictionary tempCache = new NSMutableDictionary(); if (achievements != null) { foreach (var achievement in achievements) { tempCache.Add(new NSString(achievement.Identifier), achievement); } } earnedAchievementCache = tempCache; submitAchievement(identifier, percentComplete, achievementName); })); } else { GKAchievement achievement = (GKAchievement)earnedAchievementCache.ValueForKey(new NSString(identifier)); if (achievement != null) { if (achievement.PercentComplete >= 100.0 || achievement.PercentComplete >= percentComplete) { achievement = null; } else { achievement.PercentComplete = percentComplete; } } else { achievement = new GKAchievement(identifier); achievement.PercentComplete = percentComplete; earnedAchievementCache.Add(new NSString(achievement.Identifier), achievement); } if (achievement != null) { achievement.ReportAchievement(new Action <NSError> ((error) => { if (error == null && achievement != null) { if (percentComplete == 100) { new UIAlertView("Achievement Earned", "Great job! You earned an achievement: " + achievementName, null, "OK", null).Show(); } else if (percentComplete > 0) { new UIAlertView("Achievement Progress", "Great job! You're " + percentComplete + " % of the way to " + achievementName, null, "OK", null).Show(); } } else { new UIAlertView("Achievement submittion failed", "Submittion failed because: " + error, null, "OK", null).Show(); } })); } } }
/// <summary> /// Increments the achievement. /// </summary> /// <param name="achievementCode">The achievement code.</param> /// <param name="progress">The progress.</param> /// <returns> /// Task /// </returns> public async Task <bool> IncrementAchievement(string achievementCode, int progress) { var currentAchievement = new GKAchievement(achievementCode); var finalPercent = currentAchievement.PercentComplete + progress; return(await InternalUpdateAchievement(currentAchievement, finalPercent)); }
/// <summary> /// Unlocks the archievement. /// </summary> public async Task <bool> UnlockAchievement(string achievementCode) { var currentAchievement = new GKAchievement(achievementCode); var percentComplete = 100.0d; return(await InternalUpdateAchievement(currentAchievement, percentComplete)); }
public void submitAchievement(string identifier, double percentComplete, string achievementName) { if(earnedAchievementCache == null) { GKAchievement.LoadAchievements (new GKCompletionHandler (delegate(GKAchievement[] achievements, NSError error) { NSMutableDictionary tempCache = new NSMutableDictionary(); if(achievements !=null) { foreach(var achievement in achievements) { tempCache.Add(new NSString(achievement.Identifier), achievement); } } earnedAchievementCache = tempCache; submitAchievement(identifier,percentComplete,achievementName); })); } else { GKAchievement achievement =(GKAchievement) earnedAchievementCache.ValueForKey (new NSString(identifier)); if (achievement != null) { if (achievement.PercentComplete >= 100.0 || achievement.PercentComplete >= percentComplete) { achievement = null; } else achievement.PercentComplete = percentComplete; } else { achievement = new GKAchievement(identifier); achievement.PercentComplete = percentComplete; earnedAchievementCache.Add(new NSString(achievement.Identifier),achievement); } if(achievement != null) { achievement.ReportAchievement (new GKNotificationHandler (delegate(NSError error) { if(error == null && achievement != null) { if(percentComplete == 100) { new UIAlertView ("Achievement Earned", "Great job! You earned an achievement: " + achievementName, null, "OK", null).Show (); } else if(percentComplete >0) { new UIAlertView ("Achievement Progress", "Great job! You're "+percentComplete+" % of the way to " + achievementName, null, "OK", null).Show (); } } else { new UIAlertView ("Achievement submittion failed", "Submittion failed because: " + error, null, "OK", null).Show (); } })); } } }
void LoadAchievementInfo(GKAchievementDescription[] descriptions, NSError error) { if (descriptions == null) { return; } GKAchievement.LoadAchievements(delegate(GKAchievement[] achievements, NSError err) { int completeOffset = 0; int incompleteOffset = 0; foreach (var description in descriptions) { bool completed = false; foreach (var achievement in achievements) { if (description.Identifier == achievement.Identifier) { completed |= achievement.Completed; } } int xOffset = completed ? -75 : 75; int yOffset = completed ? completeOffset : incompleteOffset; var achievementLabel = new SKLabelNode("GillSans-Bold") { Text = description.Title, FontSize = 10, Position = new CGPoint(FrameMidX + xOffset, FrameMidY + 50 + yOffset + 25) }; AddChild(achievementLabel); description.LoadImage(delegate(UIImage image, NSError imageError) { if (image == null) { image = UIImage.FromFile("Images/DefaultPlayerPhoto.png"); } var sprite = SKSpriteNode.FromTexture(SKTexture.FromImage(image), new CGSize(32, 32)); sprite.Position = new CGPoint(FrameMidX + xOffset, FrameMidY + 50 + yOffset + 50); AddChild(sprite); }); if (completed) { completeOffset -= 50; } else { incompleteOffset -= 50; } } }); }
public void SendAchievement(string id, int completion) { if (_enabled) { GKAchievement[] achievements = new GKAchievement[1]; achievements[0] = new GKAchievement(id, GKLocalPlayer.LocalPlayer); achievements[0].PercentComplete = completion; GKAchievement.ReportAchievements(achievements, null, (error) => {}); } }
// Try to submit all stored achievements to update any achievements that were not successful. public void resubmitStoredAchievements() { if (storedAchievements != null) { foreach (NSString key in storedAchievements.Keys) { GKAchievement achievement = (GKAchievement)storedAchievements.ObjectForKey(key); this.submitAchievement(achievement); } } }
/// <summary> /// Gets the achievements. /// </summary> /// <returns> /// Task /// </returns> public async Task <List <WaveEngine.Social.Achievement> > GetAchievements() { var achievements = await GKAchievement.LoadAchievementsAsync() ?? new GKAchievement[] { }; var achievementsList = achievements.ToList(); var waveAchievements = Mapper.MapAchievements(achievementsList); return(waveAchievements); }
void submitAchievementHandleTouchUpInside (object sender, EventArgs e) { //Create the achievement we want to submit. NSString identifier = new NSString ("com.appledts.GameCenterSampleApps.achievement"); GKAchievement achievement = new GKAchievement ("com.appledts.GameCenterSampleApps.achievement"); achievementsPercentageComplete += 25; achievement.PercentComplete = achievementsPercentageComplete; player.submitAchievement (achievement); }
public void DoAwardAchievement(string achievementId, double percentageComplete) { GKAchievement a = new GKAchievement(achievementId); a.PercentComplete = percentageComplete; a.ReportAchievement(delegate(NSError error){ if (error != null) { // Retain the achievement object and try again later (not shown). } }); }
/// <summary> /// Reports the progress of an achivement to Game Center. /// The returned GKAchievement can be used for more advanced functionalities such as sending challenges. /// Raises AchievementReported and AchievementReportFailed events on completion. /// </summary> /// <returns>The native achievement.</returns> /// <param name="achievementId">The ID of the achievementId to report to.</param> /// <param name="percentComplete">The progress from 0.0 to 100.0.</param> public static GKAchievement ReportAchievement(string achievementId, double percentComplete) { var achievement = new GKAchievement(achievementId); if (achievement.IsNil) { return(null); } achievement.percentComplete = percentComplete; achievement.ReportAchievement(_AchievementReported); return(achievement); }
public void ResetAchievements() { if (IsSignedInToLive) { GKAchievement.ResetAchivements(delegate(NSError error) { if (error != null) { // Oh oh something went wrong. } }); } }
// Reset all the achievements for local player public void resetAchievements() { GKAchievement.ResetAchivements(new GKNotificationHandler(delegate(NSError error) { if (error == null) { new UIAlertView("Achievement reset", "Achievement reset successfully", null, "OK", null).Show(); } else { new UIAlertView("Reset failed", "Reset failed because: " + error, null, "OK", null).Show(); } })); }
private async Task <GKAchievement> GetAchievementByCode(string achievementCode) { var achievements = await GKAchievement.LoadAchievementsAsync() ?? new GKAchievement[] { }; var currentAchievement = achievements.FirstOrDefault(a => a.Identifier == achievementCode); if (currentAchievement == null) { throw new ArgumentException("achievementCode was not found"); } return(currentAchievement); }
public void ResetAchievement() { earnedAchievementCache = null; GKAchievement.ResetAchivements(error => { if (error == null) { new UIAlertView("Achievement reset", "Achievement reset successfully", null, "OK", null).Show(); } else { new UIAlertView("Reset failed", string.Format("Reset failed because: {0}", error), null, "OK", null).Show(); } }); }
public void resetAchievement() { earnedAchievementCache = null; GKAchievement.ResetAchivements(new Action <NSError> ((error) => { if (error == null) { new UIAlertView("Achievement reset", "Achievement reset successfully", null, "OK", null).Show(); } else { new UIAlertView("Reset failed", "Reset failed because: " + error, null, "OK", null).Show(); } })); }
/* ************************************************************************ * * Achivements * * ************************************************************************* */ // Create an entry for an achievement that hasn't been submitted to the server void storeAchievement(GKAchievement achievement) { GKAchievement currentStorage = (GKAchievement)storedAchievements.ValueForKey(new NSString(achievement.Identifier)); if (currentStorage == null) { storedAchievements.Add(new NSString(achievement.Identifier), achievement); } else if (currentStorage.PercentComplete < achievement.PercentComplete) { storedAchievements.SetValueForKey(achievement, new NSString(achievement.Identifier)); this.writeStoredAchievements(); } }
public void DoAwardAchievement(string achievementId, double percentageComplete) { UIApplication.SharedApplication.InvokeOnMainThread(delegate { GKAchievement a = new GKAchievement(achievementId); a.PercentComplete = percentageComplete; GKAchievement.ReportAchievements(new[] { a }, delegate(NSError error) { if (error != null) { // Retain the achievement object and try again later (not shown). } }); }); }
/// <summary> /// Gets the achievements. /// </summary> /// <returns> /// The achievements. /// </returns> public Task <IEnumerable <Achievement> > GetAchievements() { var tcs = new TaskCompletionSource <IEnumerable <Achievement> >(); UIKit.UIApplication.SharedApplication.InvokeOnMainThread(async() => { var achievements = await GKAchievement.LoadAchievementsAsync() ?? new GKAchievement[] { }; var achievementsList = achievements.ToList(); var result = await IOSMapper.MapAchievements(achievementsList); tcs.TrySetResult(result); }); return(tcs.Task); }
/// <summary> /// Unlocks the achievement. /// </summary> /// <param name="achievementCode">The achievement code.</param> /// <returns> /// <c>true</c> if the achievement has been unlocked; otherwise, <c>false</c>. /// </returns> public Task <bool> UnlockAchievement(string achievementCode) { var tcs = new TaskCompletionSource <bool>(); UIKit.UIApplication.SharedApplication.InvokeOnMainThread(async() => { var currentAchievement = new GKAchievement(achievementCode); var percentComplete = 100.0d; var result = await InternalUpdateAchievement(currentAchievement, percentComplete); tcs.TrySetResult(result); }); return(tcs.Task); }
public void ResetAchievements() { if (IsSignedInToLive) { UIApplication.SharedApplication.InvokeOnMainThread(delegate { GKAchievement.ResetAchivements(delegate(NSError error) { if (error != null) { // Oh oh something went wrong. } }); }); } }
/// <summary> /// Increments the achievement progress. /// </summary> /// <param name="achievementCode">The achievement code.</param> /// <param name="progress">The progress.</param> /// <returns> /// <c>true</c> if the achievement has been incremented; otherwise, <c>false</c>. /// </returns> public Task <bool> IncrementAchievement(string achievementCode, int progress) { var tcs = new TaskCompletionSource <bool>(); UIKit.UIApplication.SharedApplication.InvokeOnMainThread(async() => { var currentAchievement = new GKAchievement(achievementCode); var finalPercent = currentAchievement.PercentComplete + progress; var result = await InternalUpdateAchievement(currentAchievement, finalPercent); tcs.TrySetResult(result); }); return(tcs.Task); }
public AchievementCollection GetAchievements() { if (IsSignedInToLive) { if (gamerAchievements == null) { gamerAchievements = new AchievementCollection(); } GKAchievementDescription.LoadAchievementDescriptions(delegate(GKAchievementDescription[] achievements, NSError error) { if (achievements != null) { foreach (GKAchievementDescription a in achievements) { gamerAchievements.Add(new Achievement() { Name = a.Title, Key = a.Identifier, Description = a.AchievedDescription, HowToEarn = a.UnachievedDescription, DisplayBeforeEarned = !a.Hidden }); } } }); GKAchievement.LoadAchievements(delegate(GKAchievement[] achievements, NSError error) { if (achievements != null) { foreach (GKAchievement a in achievements) { foreach (Achievement ac in gamerAchievements) { if (ac.Key == a.Identifier) { ac.IsEarned = a.Completed; ac.EarnedDateTime = a.LastReportedDate.NSDateToDateTime(); } } } } }); } return(gamerAchievements); }
void submitAchievementHandleTouchUpInside(object sender, EventArgs e) { if (!GKLocalPlayer.LocalPlayer.Authenticated) { new UIAlertView("Error", "Need sign in Game Center to submit the achievement", null, "OK", null).Show(); GKLocalPlayer.LocalPlayer.Authenticate(authenticatedHandler); return; } //Create the achievement we want to submit. GKAchievement achievement = new GKAchievement("com.appledts.GameCenterSampleApps.achievement"); achievementsPercentageComplete += 25; achievement.PercentComplete = achievementsPercentageComplete; player.submitAchievement(achievement); }
async Task UpdateCurrentTapsLeaderboardAndTapAchievements() { GKLeaderboard averageTapLeaderboard = new GKLeaderboard(new [] { GKLocalPlayer.LocalPlayer.PlayerID }) { Identifier = AverageTapTimeId }; var scores = await averageTapLeaderboard.LoadScoresAsync(); GKScore currentScore; GKScore newScore = new GKScore(AverageTapTimeId); if (scores != null && scores.Length > 1) { currentScore = scores [0]; int oldTaps = (int)currentScore.Context; int oldTime = (int)currentScore.Value * oldTaps; int newTime = oldTime + GameInfo.GameTimeInSeconds * 100; int newTaps = oldTaps + GameInfo.CurrentTaps; newScore.Value = newTime / newTaps; newScore.Context = (ulong)newTaps; } else { newScore.Value = GameInfo.GameTimeInSeconds / Math.Max(GameInfo.CurrentTaps, 1) * 100; newScore.Context = (ulong)GameInfo.CurrentTaps; } GKAchievement playAHundred = new GKAchievement(Tap100Id, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = (float)newScore.Context / 100f * 100f }; GKAchievement playAThousand = new GKAchievement(Tap1000Id, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = (float)newScore.Context / 1000f * 100f }; await GKAchievement.ReportAchievementsAsync(new [] { playAHundred, playAThousand }); await GKScore.ReportScoresAsync(new [] { newScore }); }
public void SubmitAchievement (string identifier, double percentComplete, string achievementName) { if (earnedAchievementCache == null) { GKAchievement.LoadAchievements (new GKCompletionHandler (delegate(GKAchievement[] achievements, NSError error) { NSMutableDictionary tempCache = new NSMutableDictionary (); if (achievements != null) { foreach (var achievement in achievements) { tempCache.Add (new NSString (achievement.Identifier), achievement); } } earnedAchievementCache = tempCache; SubmitAchievement (identifier, percentComplete, achievementName); })); } else { GKAchievement achievement = (GKAchievement)earnedAchievementCache.ValueForKey (new NSString (identifier)); if (achievement != null) { if (achievement.PercentComplete >= 100.0 || achievement.PercentComplete >= percentComplete) achievement = null; else achievement.PercentComplete = percentComplete; } else { achievement = new GKAchievement (identifier) { PercentComplete = percentComplete }; earnedAchievementCache.Add ((NSString)achievement.Identifier, achievement); } if (achievement != null) { achievement.ReportAchievement (error => { if (error == null && achievement != null) { if (percentComplete == 100) ShowAlert ("Achievement Earned", string.Format ("Great job! You earned an achievement: {0}", achievementName)); else if (percentComplete > 0) ShowAlert ("Achievement Progress", string.Format ("Great job! You're {0} % of the way to {1}", percentComplete, achievementName)); } else { ShowAlert ("Achievement submittion failed", string.Format ("Submittion failed because: {0}", error)); } }); } } }
// Submit an achievement to the server and store if submission fails public void submitAchievement(GKAchievement achievement) { if (achievement != null) { //submit the achievement achievement.ReportAchievement(new GKNotificationHandler((error) => { if (error == null) { if (storedAchievements.ContainsKey(new NSString(achievement.Identifier))) { storedAchievements.Remove(new NSString(achievement.Identifier)); } new UIAlertView("Achievement Submitted", "Achievement submitted successfully ", null, "OK", null).Show(); } else { this.storeAchievement(achievement); new UIAlertView("Achievement Stored", "Achievement Stored ", null, "OK", null).Show(); } })); } }
private Task <bool> InternalUpdateAchievement(GKAchievement achievement, double value) { var tcs = new TaskCompletionSource <bool>(); achievement.PercentComplete = value; achievement.ReportAchievement((error) => { var result = true; if (error != null) { result = false; #if DEBUG new UIAlertView("Achievement submittion failed", "Submittion failed because: " + error, null, "OK", null).Show(); #endif } tcs.TrySetResult(result); }); return(tcs.Task); }
public void DoAwardAchievement(string achievementId, double percentageComplete) { if (IsSignedInToLive) { UIApplication.SharedApplication.InvokeOnMainThread(delegate { GKAchievement achievement = new GKAchievement(achievementId); achievement.PercentComplete = percentageComplete; if (!UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { #pragma warning disable 618 // Report achievement for iOS 5 and older achievement.ReportAchievement(delegate(NSError error) { if (error != null) { // Oh oh something went wrong. } }); #pragma warning restore 618 } else { // Report achievement for iOS 6+ GKAchievement.ReportAchievements(new GKAchievement[] { achievement }, delegate(NSError error) { if (error != null) { // Oh oh something went wrong. } }); } }); } }
/// <summary> /// Record that the achievement with the specified id was earned. /// </summary> /// <param name="achievementId">Achievement identifier.</param> public static void RecordAchievement(string achievementId) { // Is this achievement already in the dictionary? If not, it needs to be recorded. if (!KnownAchievements.ContainsKey (achievementId)) { // Remember that this achievement has been earned ... this may need to be synchronized with game center later // if the user is logged into game center right now (see SynchronizeAchievements). KnownAchievements.Add (achievementId, true); // Immediately record with game center if possible if (IsGamePlayerAuthenticated()) { GKAchievement newAchievement = CreateCompleteAchievement (achievementId, true); GKAchievement[] newAchievementArray = new GKAchievement[1]; newAchievementArray [0] = newAchievement; GKAchievement.ReportAchievements (newAchievementArray, (achievementError) => { if (achievementError != null) { Console.WriteLine("Error reporting achievement: " + achievementError.ToString()); } else { Console.WriteLine("Reported achievement " + achievementId); } }); } } }
/// <summary> /// Create a game center achievement object with the specified identifier. /// </summary> /// <returns>The complete achievement.</returns> /// <param name="achievementId">Achievement identifier.</param> /// <param name="showBannerAfterReporting">If set to <c>true</c> show banner after reporting.</param> private static GKAchievement CreateCompleteAchievement(string achievementId, bool showBannerAfterReporting) { GKAchievement newAchievement = new GKAchievement (achievementId); newAchievement.PlayerID = CurrentPlayerId; newAchievement.PercentComplete = 100d; newAchievement.ShowsCompletionBanner = showBannerAfterReporting; return newAchievement; }
/// <summary> /// Reports the progress of an achivement to Game Center. /// The returned GKAchievement can be used for more advanced functionalities such as sending challenges. /// Raises AchievementReported and AchievementReportFailed events on completion. /// </summary> /// <returns>The native achievement.</returns> /// <param name="achievementId">The ID of the achievementId to report to.</param> /// <param name="percentComplete">The progress from 0.0 to 100.0.</param> public static GKAchievement ReportAchievement(string achievementId, double percentComplete) { var achievement = new GKAchievement(achievementId); if (achievement.IsNil) return null; achievement.percentComplete = percentComplete; achievement.ReportAchievement(_AchievementReported); return achievement; }
public void DoAwardAchievement( string achievementId, double percentageComplete ) { GKAchievement a = new GKAchievement(achievementId); a.PercentComplete = percentageComplete; a.ReportAchievement( delegate(NSError error){ if (error != null) { // Retain the achievement object and try again later (not shown). } } ); }
// Submit an achievement to the server and store if submission fails public void submitAchievement (GKAchievement achievement) { if (achievement != null) { //submit the achievement achievement.ReportAchievement (new Action<NSError> ((error) => { if (error == null) { if (storedAchievements.ContainsKey (new NSString (achievement.Identifier))) storedAchievements.Remove (new NSString (achievement.Identifier)); new UIAlertView ("Achievement Submitted", "Achievement submitted successfully ", null, "OK", null).Show (); } else { this.storeAchievement (achievement); new UIAlertView ("Achievement Stored", "Achievement Stored ", null, "OK", null).Show (); } })); } }
public void DoAwardAchievement(string achievementId, double percentageComplete) { if (IsSignedInToLive) { UIApplication.SharedApplication.InvokeOnMainThread(delegate { GKAchievement achievement = new GKAchievement(achievementId); achievement.PercentComplete = percentageComplete; if (!UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) { #pragma warning disable 618 // Report achievement for iOS 5 and older achievement.ReportAchievement(delegate(NSError error) { if (error != null) { // Oh oh something went wrong. } }); #pragma warning restore 618 } else { // Report achievement for iOS 6+ GKAchievement.ReportAchievements(new GKAchievement[] { achievement }, delegate (NSError error) { if (error != null) { // Oh oh something went wrong. } }); } }); } }
public void DoAwardAchievement( string achievementId, double percentageComplete ) { UIApplication.SharedApplication.InvokeOnMainThread(delegate { GKAchievement a = new GKAchievement(achievementId); a.PercentComplete = percentageComplete; GKAchievement.ReportAchievements(new[] { a }, delegate(NSError error) { if (error != null) { // Retain the achievement object and try again later (not shown). } }); }); }
public void unlockAchievement(MainGame.Achievement achieve) { if (!gamer.IsSignedInToLive) { return; } /* * ac = new AchievementCollection(); * ac = gamer.GetAchievements(); * * foreach (Achievement a in ac) * { * if(a.IsEarned) * achievements.Add(a.Key); * } */ GKAchievement.LoadAchievements(delegate(GKAchievement[] achievements, NSError error) { if (error == null) { string code = null; string msg = null; List <string> achievementids = new List <string>(); if (achievements != null) { foreach (GKAchievement a in achievements) { if (a.Completed) { achievementids.Add(a.Identifier); } } } switch (achieve) { case MainGame.Achievement.BabyStep: code = "grp.1shot.babystep"; msg = "Baby Step"; break; case MainGame.Achievement._50Points: code = "grp.1shot.50points"; msg = "50 Points!"; break; case MainGame.Achievement._100Points: code = "grp.1shot.100point"; msg = "A hundred points."; break; case MainGame.Achievement._70Points1Life: code = "grp.1shot.70points1life"; msg = "1 Life, 70 Points."; break; case MainGame.Achievement.OutOfTheEarth: code = "grp.1shot.outoftheearth"; msg = "Out of the Earth"; break; case MainGame.Achievement.Interstellar: code = "grp.1shot.interstellar"; msg = "Interstellar"; break; case MainGame.Achievement.Doge: code = "grp.1shot.doge"; msg = "DogeDogeDogeDoge"; break; } if (code != null) { if (!achievementids.Contains(code)) { gamer.AwardAchievement(code); GKNotificationBanner.Show("Achievement Unlocked!", msg, delegate { }); } } } }); }
async Task UpdateCurrentTapsLeaderboardAndTapAchievements () { GKLeaderboard averageTapLeaderboard = new GKLeaderboard (new [] { GKLocalPlayer.LocalPlayer.PlayerID }) { Identifier = AverageTapTimeId }; var scores = await averageTapLeaderboard.LoadScoresAsync (); GKScore currentScore; GKScore newScore = new GKScore (AverageTapTimeId); if (scores != null && scores.Length > 1) { currentScore = scores [0]; int oldTaps = (int)currentScore.Context; int oldTime = (int)currentScore.Value * oldTaps; int newTime = oldTime + GameInfo.GameTimeInSeconds * 100; int newTaps = oldTaps + GameInfo.CurrentTaps; newScore.Value = newTime / newTaps; newScore.Context = (ulong)newTaps; } else { newScore.Value = GameInfo.GameTimeInSeconds / Math.Max (GameInfo.CurrentTaps, 1) * 100; newScore.Context = (ulong)GameInfo.CurrentTaps; } GKAchievement playAHundred = new GKAchievement (Tap100Id, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = (float) newScore.Context / 100f * 100f }; GKAchievement playAThousand = new GKAchievement (Tap1000Id, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = (float) newScore.Context / 1000f * 100f }; await GKAchievement.ReportAchievementsAsync (new [] { playAHundred, playAThousand }); await GKScore.ReportScoresAsync (new [] { newScore }); }
public async Task<bool> SubmitAchievement (string identifier, double percentComplete, string achievementName) { if (earnedAchievementCache == null) { NSMutableDictionary tempCache = new NSMutableDictionary (); try { var achievements = await GKAchievement.LoadAchievementsAsync (); if (achievements != null) { foreach (var achievement in achievements) { tempCache.Add (new NSString (achievement.Identifier), achievement); } } } catch { } earnedAchievementCache = tempCache; var result = await SubmitAchievement (identifier, percentComplete, achievementName); return result; } else { GKAchievement achievement = (GKAchievement)earnedAchievementCache.ValueForKey (new NSString (identifier)); if (achievement != null) { if (achievement.PercentComplete >= 100.0 || achievement.PercentComplete >= percentComplete) achievement = null; else achievement.PercentComplete = percentComplete; } else { achievement = new GKAchievement (identifier) { PercentComplete = percentComplete }; earnedAchievementCache.Add ((NSString)achievement.Identifier, achievement); } if (achievement != null) { try { await achievement.ReportAchievementAsync (); return true; } catch { return false; } } return false; } }
public async Task<bool> UnlockAchievement (string identifier, string achievementName) { var scoreReporter = new GKAchievement (identifier) { PercentComplete = 100, ShowsCompletionBanner = true }; try { await scoreReporter.ReportAchievementAsync (); return true; } catch { return false; } }
public void Login() { GKLocalPlayer player = GKLocalPlayer.LocalPlayer; if (!player.Authenticated) { player.AuthenticateHandler = (UIViewController controller, NSError error) => { if (controller != null) { UIViewController parent = AppMain.Current.Services.GetService(typeof(UIViewController)) as UIViewController; parent.PresentViewController(controller, true, null); } else { if (GKLocalPlayer.LocalPlayer.Authenticated) { _enabled = true; GKLeaderboard.LoadLeaderboards((GKLeaderboard[] leaderboards, NSError error3) => { if (leaderboards != null && leaderboards.Length > 0 && _leaderboardInfo != null) { LeaderboardInfo[] info = new LeaderboardInfo[leaderboards.Length]; for (int idx = 0; idx < leaderboards.Length; ++idx) { GKLeaderboard lb = leaderboards[idx]; int score = 0; if (lb.LocalPlayerScore != null) { score = (int)lb.LocalPlayerScore.Value; } info[idx] = new LeaderboardInfo(lb.Identifier) { Score = score }; } _leaderboardInfo(info); } }); GKAchievement.LoadAchievements((GKAchievement[] achievements, NSError error2) => { if (achievements != null && achievements.Length > 0 && _achievementInfo != null) { AchievementInfo[] info = new AchievementInfo[achievements.Length]; for (int idx = 0; idx < achievements.Length; ++idx) { GKAchievement ach = achievements[idx]; info[idx] = new AchievementInfo(ach.Identifier) { Completion = ach.Completed ? Achievement.Completed : (int)ach.PercentComplete }; } _achievementInfo(info); } }); } } }; } }
void submitAchievementHandleTouchUpInside (object sender, EventArgs e) { if (!GKLocalPlayer.LocalPlayer.Authenticated) { new UIAlertView ("Error", "Need sign in Game Center to submit the achievement", null, "OK", null).Show(); GKLocalPlayer.LocalPlayer.Authenticate (authenticatedHandler); return; } //Create the achievement we want to submit. NSString identifier = new NSString ("com.appledts.GameCenterSampleApps.achievement"); GKAchievement achievement = new GKAchievement ("com.appledts.GameCenterSampleApps.achievement"); achievementsPercentageComplete += 25; achievement.PercentComplete = achievementsPercentageComplete; player.submitAchievement (achievement); }
async Task ReportScore() { string leaderboardIdentifier = null; string gameTypeString = null; GameTypePlayed gameType = GameTypePlayed.Invalid; if (GameInfo.GameTime == GameTime.Fifteen) { if (GameInfo.GameMode == GameMode.Easy) { gameTypeString = "15secondseasymode"; gameType = GameTypePlayed.Easy15; } else if (GameInfo.GameMode == GameMode.Hard) { gameTypeString = "15secondshardmode"; gameType = GameTypePlayed.Hard15; } } else if (GameInfo.GameTime == GameTime.Thirty) { if (GameInfo.GameMode == GameMode.Easy) { gameTypeString = "30secondseasymode"; gameType = GameTypePlayed.Easy30; } else if (GameInfo.GameMode == GameMode.Hard) { gameTypeString = "30secondshardmode"; gameType = GameTypePlayed.Hard30; } } else if (GameInfo.GameTime == GameTime.FourtyFive) { if (GameInfo.GameMode == GameMode.Easy) { gameTypeString = "45secondseasymode"; gameType = GameTypePlayed.Easy45; } else if (GameInfo.GameMode == GameMode.Hard) { gameTypeString = "45secondshardmode"; gameType = GameTypePlayed.Hard45; } } if (gameTypeString != null) { leaderboardIdentifier = Prefix + gameTypeString; } if (leaderboardIdentifier != null) { GKScore score = new GKScore(leaderboardIdentifier) { Value = GameInfo.CurrentTaps, Context = 0 }; var challenges = GameInfo.Challenge == null ? null : new [] { GameInfo.Challenge }; await GKScore.ReportScoresAsync(new [] { score }, challenges); } if (GKLocalPlayer.LocalPlayer.Authenticated) { if (GameInfo.GameMode == GameMode.Hard) { var playhard = new GKAchievement(PlayHardId, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = 100 }; await GKAchievement.ReportAchievementsAsync(new [] { playhard }); } int playedGameTypesBitField; using (NSUserDefaults defaults = NSUserDefaults.StandardUserDefaults) { playedGameTypesBitField = defaults.IntForKey("playedGameTypes") | (int)gameType; defaults.SetInt(playedGameTypesBitField, "playedGameTypes"); defaults.Synchronize(); } int numTypesPlayed = 0; for (int i = 0; i < 6; i++) { if ((playedGameTypesBitField & 0x01) != 0) { numTypesPlayed++; } playedGameTypesBitField >>= 1; } GKAchievement playAllModesAchievement = new GKAchievement(PlayAllId) { PercentComplete = numTypesPlayed / 6.0 * 100.0 }; await GKAchievement.ReportAchievementsAsync(new [] { playAllModesAchievement }); await UpdateCurrentTapsLeaderboardAndTapAchievements(); } }
async Task ReportScore () { string leaderboardIdentifier = null; string gameTypeString = null; GameTypePlayed gameType = GameTypePlayed.Invalid; if (GameInfo.GameTime == GameTime.Fifteen) { if (GameInfo.GameMode == GameMode.Easy) { gameTypeString = "15secondseasymode"; gameType = GameTypePlayed.Easy15; } else if (GameInfo.GameMode == GameMode.Hard) { gameTypeString = "15secondshardmode"; gameType = GameTypePlayed.Hard15; } } else if (GameInfo.GameTime == GameTime.Thirty) { if (GameInfo.GameMode == GameMode.Easy) { gameTypeString = "30secondseasymode"; gameType = GameTypePlayed.Easy30; } else if (GameInfo.GameMode == GameMode.Hard) { gameTypeString = "30secondshardmode"; gameType = GameTypePlayed.Hard30; } } else if (GameInfo.GameTime == GameTime.FourtyFive) { if (GameInfo.GameMode == GameMode.Easy) { gameTypeString = "45secondseasymode"; gameType = GameTypePlayed.Easy45; } else if (GameInfo.GameMode == GameMode.Hard) { gameTypeString = "45secondshardmode"; gameType = GameTypePlayed.Hard45; } } if (gameTypeString != null) leaderboardIdentifier = Prefix + gameTypeString; if (leaderboardIdentifier != null) { GKScore score = new GKScore (leaderboardIdentifier) { Value = GameInfo.CurrentTaps, Context = 0 }; var challenges = GameInfo.Challenge == null ? null : new [] { GameInfo.Challenge }; await GKScore.ReportScoresAsync (new [] { score }, challenges); } if (GKLocalPlayer.LocalPlayer.Authenticated) { if (GameInfo.GameMode == GameMode.Hard) { var playhard = new GKAchievement (PlayHardId, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = 100 }; await GKAchievement.ReportAchievementsAsync (new [] { playhard }); } int playedGameTypesBitField; using (NSUserDefaults defaults = NSUserDefaults.StandardUserDefaults) { playedGameTypesBitField = (int)defaults.IntForKey ("playedGameTypes") | (int) gameType; defaults.SetInt (playedGameTypesBitField, "playedGameTypes"); defaults.Synchronize (); } int numTypesPlayed = 0; for (int i = 0; i < 6; i++) { if ((playedGameTypesBitField & 0x01) != 0) numTypesPlayed++; playedGameTypesBitField >>= 1; } GKAchievement playAllModesAchievement = new GKAchievement (PlayAllId) { PercentComplete = numTypesPlayed / 6.0 * 100.0 }; await GKAchievement.ReportAchievementsAsync (new [] { playAllModesAchievement }); await UpdateCurrentTapsLeaderboardAndTapAchievements (); } }
public override async void TouchesBegan (NSSet touches, UIEvent evt) { foreach (var touch in touches) { var location = (touch as UITouch).LocationInNode (this); if (button.ContainsPoint (location)) { GameInfo.CurrentTaps ++; if (GameInfo.GameMode == GameMode.Hard) { int x = rand.Next (100) - 50; int y = rand.Next (100) - 50; button.Position = new CGPoint (FrameMidX + x, FrameMidY + y); } } } if (GKLocalPlayer.LocalPlayer.Authenticated) { var tapOnceAchievement = new GKAchievement (TapOnceId, GKLocalPlayer.LocalPlayer.PlayerID) { PercentComplete = 100 }; await GKAchievement.ReportAchievementsAsync (new [] { tapOnceAchievement }, null); } }
/* ************************************************************************ * * Achivements * * ************************************************************************* */ // Create an entry for an achievement that hasn't been submitted to the server void storeAchievement (GKAchievement achievement) { GKAchievement currentStorage = (GKAchievement)storedAchievements.ValueForKey (new NSString (achievement.Identifier)); if (currentStorage == null) { storedAchievements.Add (new NSString (achievement.Identifier), achievement); } else if (currentStorage.PercentComplete < achievement.PercentComplete) { storedAchievements.SetValueForKey (achievement, new NSString (achievement.Identifier)); this.writeStoredAchievements (); } }