-
Notifications
You must be signed in to change notification settings - Fork 8
/
EasyGameCenter.swift
executable file
·1556 lines (1241 loc) · 55.3 KB
/
EasyGameCenter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// GameCenter.swift
//
// Created by Yannick Stephan DaRk-_-D0G on 19/12/2014.
// YannickStephan.com
//
// iOS 7.0+ & iOS 8.0+ & iOS 9.0+ & TvOS 9.0+
//
// The MIT License (MIT)
// Copyright (c) 2015 Red Wolf Studio & Yannick Stephan
// http://www.redwolfstudio.fr
// http://yannickstephan.com
// Version 2.3 for Swift 2.0
import Foundation
import GameKit
import SystemConfiguration
/**
TODO List
- REMEMBER report plusieur score pour plusieur leaderboard en array
*/
// Protocol Easy Game Center
@objc public protocol EGCDelegate:NSObjectProtocol {
/**
Authentified, Delegate Easy Game Center
*/
optional func EGCAuthentified(authentified:Bool)
/**
Not Authentified, Delegate Easy Game Center
*/
//optional func EGCNotAuthentified()
/**
Achievementes in cache, Delegate Easy Game Center
*/
optional func EGCInCache()
/**
Method called when a match has been initiated.
*/
optional func EGCMatchStarted()
/**
Method called when the device received data about the match from another device in the match.
- parameter match: GKMatch
- parameter didReceiveData: NSData
- parameter fromPlayer: String
*/
optional func EGCMatchRecept(match: GKMatch, didReceiveData: NSData, fromPlayer: String)
/**
Method called when the match has ended.
*/
optional func EGCMatchEnded()
/**
Cancel match
*/
optional func EGCMatchCancel()
}
// MARK: - Public Func
extension EGC {
/**
CheckUp Connection the new
- returns: Bool Connection Validation
*/
static var isConnectedToNetwork: Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
guard let defaultRouteReachability = withUnsafePointer(&zeroAddress, {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
}) else {
return false
}
var flags : SCNetworkReachabilityFlags = []
if SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) == false {
return false
}
let isReachable = flags.contains(.Reachable)
let needsConnection = flags.contains(.ConnectionRequired)
return (isReachable && !needsConnection)
}
}
/// Easy Game Center Swift
public class EGC: NSObject, GKGameCenterControllerDelegate, GKMatchmakerViewControllerDelegate, GKMatchDelegate, GKLocalPlayerListener {
/*####################################################################################################*/
/* Private Instance */
/*####################################################################################################*/
/// Achievements GKAchievement Cache
private var achievementsCache:[String:GKAchievement] = [String:GKAchievement]()
/// Achievements GKAchievementDescription Cache
private var achievementsDescriptionCache = [String:GKAchievementDescription]()
/// Save for report late when network working
private var achievementsCacheShowAfter = [String:String]()
/// Checkup net and login to GameCenter when have Network
private var timerNetAndPlayer:NSTimer?
/// Debug mode for see message
private var debugModeGetSet:Bool = false
static var showLoginPage:Bool = true
/// The match object provided by GameKit.
private var match: GKMatch?
private var playersInMatch = Set<GKPlayer>()
public var invitedPlayer: GKPlayer?
public var invite: GKInvite?
////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
/*####################################################################################################*/
/* Singleton Public Instance */
/*####################################################################################################*/
override init() {
super.init()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(EGC.authenticationChanged), name: GKPlayerAuthenticationDidChangeNotificationName, object: nil)
}
/**
Static EGC
*/
struct Static {
/// Async EGC
static var onceToken: dispatch_once_t = 0
/// Instance of EGC
static var instance: EGC? = nil
/// Delegate of UIViewController
static weak var delegate: UIViewController? = nil
}
/**
Start Singleton GameCenter Instance
*/
public class func sharedInstance(delegate:UIViewController)-> EGC {
if Static.instance == nil {
dispatch_once(&Static.onceToken) {
Static.instance = EGC()
Static.delegate = delegate
Static.instance!.loginPlayerToGameCenter()
}
}
return Static.instance!
}
/// Delegate UIViewController
class var delegate: UIViewController {
get {
do {
let delegateInstance = try EGC.sharedInstance.getDelegate()
return delegateInstance
} catch {
EGCError.NoDelegate.errorCall()
fatalError("Dont work\(error)")
}
}
set {
guard newValue != EGC.delegate else {
return
}
Static.delegate = EGC.delegate
//EGC.printLogEGC("New delegate UIViewController is \(_stdlib_getDemangledTypeName(newValue))\n")
}
}
/*####################################################################################################*/
/* Public Func / Object */
/*####################################################################################################*/
public class var debugMode:Bool {
get {
return EGC.sharedInstance.debugModeGetSet
}
set {
EGC.sharedInstance.debugModeGetSet = newValue
}
}
/**
If player is Identified to Game Center
- returns: Bool is identified
*/
public static var isPlayerIdentified: Bool {
get {
return GKLocalPlayer.localPlayer().authenticated
}
}
/**
Get local player (GKLocalPlayer)
- returns: Bool True is identified
*/
static var localPayer: GKLocalPlayer {
get {
return GKLocalPlayer.localPlayer()
}
}
// class func getLocalPlayer() -> GKLocalPlayer { }
/**
Get local player Information (playerID,alias,profilPhoto)
:completion: Tuple of type (playerID:String,alias:String,profilPhoto:UIImage?)
*/
class func getlocalPlayerInformation(completion completionTuple: (playerInformationTuple:(playerID:String,alias:String,profilPhoto:UIImage?)?) -> ()) {
guard EGC.isConnectedToNetwork else {
completionTuple(playerInformationTuple: nil)
EGCError.NoConnection.errorCall()
return
}
guard EGC.isPlayerIdentified else {
completionTuple(playerInformationTuple: nil)
EGCError.NotLogin.errorCall()
return
}
EGC.localPayer.loadPhotoForSize(GKPhotoSizeNormal, withCompletionHandler: {
(image, error) in
var playerInformationTuple:(playerID:String,alias:String,profilPhoto:UIImage?)
playerInformationTuple.profilPhoto = nil
playerInformationTuple.playerID = EGC.localPayer.playerID!
playerInformationTuple.alias = EGC.localPayer.alias!
if error == nil { playerInformationTuple.profilPhoto = image }
completionTuple(playerInformationTuple: playerInformationTuple)
})
}
/*####################################################################################################*/
/* Public Func Show */
/*####################################################################################################*/
/**
Show Game Center
- parameter completion: Viod just if open Game Center Achievements
*/
public class func showGameCenter(completion: ((isShow:Bool) -> Void)? = nil) {
guard EGC.isConnectedToNetwork else {
if completion != nil { completion!(isShow:false) }
EGCError.NoConnection.errorCall()
return
}
guard EGC.isPlayerIdentified else {
if completion != nil { completion!(isShow:false) }
EGCError.NotLogin.errorCall()
return
}
EGC.printLogEGC("Show Game Center")
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = Static.instance
#if !os(tvOS)
gc.viewState = GKGameCenterViewControllerState.Default
#endif
var delegeteParent:UIViewController? = EGC.delegate.parentViewController
if delegeteParent == nil {
delegeteParent = EGC.delegate
}
delegeteParent!.presentViewController(gc, animated: true, completion: {
if completion != nil { completion!(isShow:true) }
})
}
/**
Show Game Center Player Achievements
- parameter completion: Viod just if open Game Center Achievements
*/
public class func showGameCenterAchievements(completion: ((isShow:Bool) -> Void)? = nil) {
guard EGC.isConnectedToNetwork else {
if completion != nil { completion!(isShow:false) }
EGCError.NoConnection.errorCall()
return
}
guard EGC.isPlayerIdentified else {
if completion != nil { completion!(isShow:false) }
EGCError.NotLogin.errorCall()
return
}
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = Static.instance
#if !os(tvOS)
gc.viewState = GKGameCenterViewControllerState.Achievements
#endif
var delegeteParent:UIViewController? = EGC.delegate.parentViewController
if delegeteParent == nil {
delegeteParent = EGC.delegate
}
delegeteParent!.presentViewController(gc, animated: true, completion: {
if completion != nil { completion!(isShow:true) }
})
}
/**
Show Game Center Leaderboard
- parameter leaderboardIdentifier: Leaderboard Identifier
- parameter completion: Viod just if open Game Center Leaderboard
*/
public class func showGameCenterLeaderboard(leaderboardIdentifier leaderboardIdentifier :String, completion: ((isShow:Bool) -> Void)? = nil) {
guard leaderboardIdentifier != "" else {
EGCError.Empty.errorCall()
if completion != nil { completion!(isShow:false) }
return
}
guard EGC.isConnectedToNetwork else {
EGCError.NoConnection.errorCall()
if completion != nil { completion!(isShow:false) }
return
}
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
if completion != nil { completion!(isShow:false) }
return
}
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = Static.instance
#if !os(tvOS)
gc.leaderboardIdentifier = leaderboardIdentifier
gc.viewState = GKGameCenterViewControllerState.Leaderboards
#endif
var delegeteParent:UIViewController? = EGC.delegate.parentViewController
if delegeteParent == nil {
delegeteParent = EGC.delegate
}
delegeteParent!.presentViewController(gc, animated: true, completion: {
if completion != nil { completion!(isShow:true) }
})
}
/**
Show Game Center Challenges
- parameter completion: Viod just if open Game Center Challenges
*/
public class func showGameCenterChallenges(completion: ((isShow:Bool) -> Void)? = nil) {
guard EGC.isConnectedToNetwork else {
if completion != nil { completion!(isShow:false) }
EGCError.NoConnection.errorCall()
return
}
guard EGC.isPlayerIdentified else {
if completion != nil { completion!(isShow:false) }
EGCError.NotLogin.errorCall()
return
}
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = Static.instance
#if !os(tvOS)
gc.viewState = GKGameCenterViewControllerState.Challenges
#endif
var delegeteParent:UIViewController? = EGC.delegate.parentViewController
if delegeteParent == nil {
delegeteParent = EGC.delegate
}
delegeteParent!.presentViewController(gc, animated: true, completion: {
() -> Void in
if completion != nil { completion!(isShow:true) }
})
}
/**
Show banner game center
- parameter title: title
- parameter description: description
- parameter completion: When show message
*/
public class func showCustomBanner(title title:String, description:String,completion: (() -> Void)? = nil) {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return
}
GKNotificationBanner.showBannerWithTitle(title, message: description, completionHandler: completion)
}
/**
Show page Authentication Game Center
- parameter completion: Viod just if open Game Center Authentication
*/
public class func showGameCenterAuthentication(completion: ((result:Bool) -> Void)? = nil) {
if completion != nil {
completion!(result: UIApplication.sharedApplication().openURL(NSURL(string: "gamecenter:")!))
}
}
/*####################################################################################################*/
/* Public Func LeaderBoard */
/*####################################################################################################*/
/**
Get Leaderboards
- parameter completion: return [GKLeaderboard] or nil
*/
public class func getGKLeaderboard(completion completion: ((resultArrayGKLeaderboard:Set<GKLeaderboard>?) -> Void)) {
guard EGC.isConnectedToNetwork else {
completion(resultArrayGKLeaderboard: nil)
EGCError.NoConnection.errorCall()
return
}
guard EGC.isPlayerIdentified else {
completion(resultArrayGKLeaderboard: nil)
EGCError.NotLogin.errorCall()
return
}
GKLeaderboard.loadLeaderboardsWithCompletionHandler {
(leaderboards, error) in
guard EGC.isPlayerIdentified else {
completion(resultArrayGKLeaderboard: nil)
EGCError.NotLogin.errorCall()
return
}
guard let leaderboardsIsArrayGKLeaderboard = leaderboards as [GKLeaderboard]? else {
completion(resultArrayGKLeaderboard: nil)
EGCError.Error(error?.localizedDescription).errorCall()
return
}
completion(resultArrayGKLeaderboard: Set(leaderboardsIsArrayGKLeaderboard))
}
}
/**
Reports a score to Game Center
- parameter The: score Int
- parameter Leaderboard: identifier
- parameter completion: (bool) when the score is report to game center or Fail
*/
public class func reportScoreLeaderboard(leaderboardIdentifier leaderboardIdentifier:String, score: Int) {
guard EGC.isConnectedToNetwork else {
EGCError.NoConnection.errorCall()
return
}
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return
}
let gkScore = GKScore(leaderboardIdentifier: leaderboardIdentifier)
gkScore.value = Int64(score)
gkScore.shouldSetDefaultLeaderboard = true
GKScore.reportScores([gkScore], withCompletionHandler: nil)
}
/**
Get High Score for leaderboard identifier
- parameter leaderboardIdentifier: leaderboard ID
- parameter completion: Tuple (playerName: String, score: Int, rank: Int)
*/
public class func getHighScore(
leaderboardIdentifier leaderboardIdentifier:String,
completion:((playerName:String, score:Int,rank:Int)? -> Void)
) {
EGC.getGKScoreLeaderboard(leaderboardIdentifier: leaderboardIdentifier, completion: {
(resultGKScore) in
guard let valGkscore = resultGKScore else {
completion(nil)
return
}
let rankVal = valGkscore.rank
let nameVal = EGC.localPayer.alias!
let scoreVal = Int(valGkscore.value)
completion((playerName: nameVal, score: scoreVal, rank: rankVal))
})
}
/**
Get GKScoreOfLeaderboard
- parameter completion: GKScore or nil
*/
public class func getGKScoreLeaderboard(leaderboardIdentifier leaderboardIdentifier:String, completion:((resultGKScore:GKScore?) -> Void)) {
guard leaderboardIdentifier != "" else {
EGCError.Empty.errorCall()
completion(resultGKScore:nil)
return
}
guard EGC.isConnectedToNetwork else {
EGCError.NoConnection.errorCall()
completion(resultGKScore: nil)
return
}
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
completion(resultGKScore: nil)
return
}
let leaderBoardRequest = GKLeaderboard()
leaderBoardRequest.identifier = leaderboardIdentifier
leaderBoardRequest.loadScoresWithCompletionHandler {
(resultGKScore, error) in
guard error == nil && resultGKScore != nil else {
completion(resultGKScore: nil)
return
}
completion(resultGKScore: leaderBoardRequest.localPlayerScore)
}
}
/*####################################################################################################*/
/* Public Func Achievements */
/*####################################################################################################*/
/**
Get Tuple ( GKAchievement , GKAchievementDescription) for identifier Achievement
- parameter achievementIdentifier: Identifier Achievement
- returns: (gkAchievement:GKAchievement,gkAchievementDescription:GKAchievementDescription)?
*/
public class func getTupleGKAchievementAndDescription(achievementIdentifier achievementIdentifier:String,completion completionTuple: ((tupleGKAchievementAndDescription:(gkAchievement:GKAchievement,gkAchievementDescription:GKAchievementDescription)?) -> Void)) {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
completionTuple(tupleGKAchievementAndDescription: nil)
return
}
let achievementGKScore = EGC.sharedInstance.achievementsCache[achievementIdentifier]
let achievementGKDes = EGC.sharedInstance.achievementsDescriptionCache[achievementIdentifier]
guard let aGKS = achievementGKScore, let aGKD = achievementGKDes else {
completionTuple(tupleGKAchievementAndDescription: nil)
return
}
completionTuple(tupleGKAchievementAndDescription: (aGKS,aGKD))
}
/**
Get Achievement
- parameter identifierAchievement: Identifier achievement
- returns: GKAchievement Or nil if not exist
*/
public class func getAchievementForIndentifier(identifierAchievement identifierAchievement : NSString) -> GKAchievement? {
guard identifierAchievement != "" else {
EGCError.Empty.errorCall()
return nil
}
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return nil
}
guard let achievementFind = EGC.sharedInstance.achievementsCache[identifierAchievement as String] else {
return nil
}
return achievementFind
}
/**
Add progress to an achievement
- parameter progress: Progress achievement Double (ex: 10% = 10.00)
- parameter achievementIdentifier: Achievement Identifier
- parameter showBannnerIfCompleted: if you want show banner when now or not when is completed
- parameter completionIsSend: Completion if is send to Game Center
*/
public class func reportAchievement( progress progress : Double, achievementIdentifier : String, showBannnerIfCompleted : Bool = true ,addToExisting: Bool = false) {
guard achievementIdentifier != "" else {
EGCError.Empty.errorCall()
return
}
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return
}
guard !EGC.isAchievementCompleted(achievementIdentifier: achievementIdentifier) else {
EGC.printLogEGC("Achievement is already completed")
return
}
guard let achievement = EGC.getAchievementForIndentifier(identifierAchievement: achievementIdentifier) else {
EGC.printLogEGC("No Achievement for identifier")
return
}
let currentValue = achievement.percentComplete
let newProgress: Double = !addToExisting ? progress : progress + currentValue
achievement.percentComplete = newProgress
/* show banner only if achievement is fully granted (progress is 100%) */
if achievement.completed && showBannnerIfCompleted {
EGC.printLogEGC("Achievement \(achievementIdentifier) completed")
if EGC.isConnectedToNetwork {
achievement.showsCompletionBanner = true
} else {
//oneAchievement.showsCompletionBanner = true << Bug For not show two banner
// Force show Banner when player not have network
EGC.getTupleGKAchievementAndDescription(achievementIdentifier: achievementIdentifier, completion: {
(tupleGKAchievementAndDescription) -> Void in
if let tupleIsOK = tupleGKAchievementAndDescription {
let title = tupleIsOK.gkAchievementDescription.title
let description = tupleIsOK.gkAchievementDescription.achievedDescription
EGC.showCustomBanner(title: title!, description: description!)
}
})
}
}
if achievement.completed && !showBannnerIfCompleted {
EGC.sharedInstance.achievementsCacheShowAfter[achievementIdentifier] = achievementIdentifier
}
EGC.sharedInstance.reportAchievementToGameCenter(achievement: achievement)
}
/**
Get GKAchievementDescription
- parameter completion: return array [GKAchievementDescription] or nil
*/
public class func getGKAllAchievementDescription(completion completion: ((arrayGKAD:Set<GKAchievementDescription>?) -> Void)){
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return
}
guard EGC.sharedInstance.achievementsDescriptionCache.count > 0 else {
EGCError.NoAchievement.printError()
return
}
var tempsEnvoi = Set<GKAchievementDescription>()
for achievementDes in EGC.sharedInstance.achievementsDescriptionCache {
tempsEnvoi.insert(achievementDes.1)
}
completion(arrayGKAD: tempsEnvoi)
}
/**
If achievement is Completed
- parameter Achievement: Identifier
:return: (Bool) if finished
*/
public class func isAchievementCompleted(achievementIdentifier achievementIdentifier: String) -> Bool{
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return false
}
guard let achievement = EGC.getAchievementForIndentifier(identifierAchievement: achievementIdentifier)
where achievement.completed || achievement.percentComplete == 100.00 else {
return false
}
return true
}
/**
Get Achievements Completes during the game and banner was not showing
- returns: [String : GKAchievement] or nil
*/
public class func getAchievementCompleteAndBannerNotShowing() -> [GKAchievement]? {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return nil
}
let achievements : [String:String] = EGC.sharedInstance.achievementsCacheShowAfter
var achievementsTemps = [GKAchievement]()
if achievements.count > 0 {
for achievement in achievements {
if let achievementExtract = EGC.getAchievementForIndentifier(identifierAchievement: achievement.1) {
if achievementExtract.completed && achievementExtract.showsCompletionBanner == false {
achievementsTemps.append(achievementExtract)
}
}
}
return achievementsTemps
}
return nil
}
/**
Show all save achievement Complete if you have ( showBannerAchievementWhenComplete = false )
- parameter completion: if is Show Achievement banner
(Bug Game Center if you show achievement by showsCompletionBanner = true when you report and again you show showsCompletionBanner = false is not show)
*/
public class func showAllBannerAchievementCompleteForBannerNotShowing(completion: ((achievementShow:GKAchievement?) -> Void)? = nil) {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
if completion != nil { completion!(achievementShow: nil) }
return
}
guard let achievementNotShow: [GKAchievement] = EGC.getAchievementCompleteAndBannerNotShowing() else {
if completion != nil { completion!(achievementShow: nil) }
return
}
for achievement in achievementNotShow {
EGC.getTupleGKAchievementAndDescription(achievementIdentifier: achievement.identifier!, completion: {
(tupleGKAchievementAndDescription) in
guard let tupleOK = tupleGKAchievementAndDescription else {
if completion != nil { completion!(achievementShow: nil) }
return
}
//oneAchievement.showsCompletionBanner = true
let title = tupleOK.gkAchievementDescription.title
let description = tupleOK.gkAchievementDescription.achievedDescription
EGC.showCustomBanner(title: title!, description: description!, completion: {
if completion != nil { completion!(achievementShow: achievement) }
})
})
}
EGC.sharedInstance.achievementsCacheShowAfter.removeAll(keepCapacity: false)
}
/**
Get progress to an achievement
- parameter Achievement: Identifier
- returns: Double or nil (if not find)
*/
public class func getProgressForAchievement(achievementIdentifier achievementIdentifier:String) -> Double? {
guard achievementIdentifier != "" else {
EGCError.Empty.errorCall()
return nil
}
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return nil
}
if let achievementInArrayInt = EGC.sharedInstance.achievementsCache[achievementIdentifier]?.percentComplete {
return achievementInArrayInt
} else {
EGCError.Error("No Achievement for achievementIdentifier : \(achievementIdentifier)").errorCall()
EGCError.NoAchievement.errorCall()
return nil
}
}
/**
Remove All Achievements
completion: return GKAchievement reset or Nil if game center not work
sdsds
*/
public class func resetAllAchievements( completion: ((achievementReset:GKAchievement?) -> Void)? = nil) {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
if completion != nil { completion!(achievementReset: nil) }
return
}
GKAchievement.resetAchievementsWithCompletionHandler({
(error:NSError?) in
guard error == nil else {
EGC.printLogEGC("Couldn't Reset achievement (Send data error)")
return
}
for lookupAchievement in Static.instance!.achievementsCache {
let achievementID = lookupAchievement.0
let achievementGK = lookupAchievement.1
achievementGK.percentComplete = 0
achievementGK.showsCompletionBanner = false
if completion != nil { completion!(achievementReset:achievementGK) }
EGC.printLogEGC("Reset achievement (\(achievementID))")
}
})
}
/*####################################################################################################*/
/* Mutliplayer */
/*####################################################################################################*/
/**
Find player By number
- parameter minPlayers: Int
- parameter maxPlayers: Max
*/
public class func findMatchWithMinPlayers(minPlayers: Int, maxPlayers: Int) {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return
}
do {
let delegatVC = try EGC.sharedInstance.getDelegate()
EGC.disconnectMatch()
let request = GKMatchRequest()
request.minPlayers = minPlayers
request.maxPlayers = maxPlayers
let controlllerGKMatch = GKMatchmakerViewController(matchRequest: request)
controlllerGKMatch!.matchmakerDelegate = EGC.sharedInstance
var delegeteParent:UIViewController? = delegatVC.parentViewController
if delegeteParent == nil {
delegeteParent = delegatVC
}
delegeteParent!.presentViewController(controlllerGKMatch!, animated: true, completion: nil)
} catch EGCError.NoDelegate {
EGCError.NoDelegate.errorCall()
} catch {
fatalError("Dont work\(error)")
}
}
/**
Get Player in match
- returns: Set<GKPlayer>
*/
public class func getPlayerInMatch() -> Set<GKPlayer>? {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return nil
}
guard EGC.sharedInstance.match != nil && EGC.sharedInstance.playersInMatch.count > 0 else {
EGC.printLogEGC("No Match")
return nil
}
return EGC.sharedInstance.playersInMatch
}
/**
Deconnect the Match
*/
public class func disconnectMatch() {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return
}
guard let match = EGC.sharedInstance.match else {
return
}
EGC.printLogEGC("Disconnect from match")
match.disconnect()
EGC.sharedInstance.match = nil
(self.delegate as? EGCDelegate)?.EGCMatchEnded?()
}
/**
Get match
- returns: GKMatch or nil if haven't match
*/
public class func getMatch() -> GKMatch? {
guard EGC.isPlayerIdentified else {
EGCError.NotLogin.errorCall()
return nil
}
guard let match = EGC.sharedInstance.match else {
EGC.printLogEGC("No Match")
return nil
}
return match
}
/**
player in net
*/
@available(iOS 8.0, *)
private func lookupPlayers() {
guard let match = EGC.sharedInstance.match else {
EGC.printLogEGC("No Match")
return
}
let playerIDs = match.players.map { $0.playerID }
guard let hasePlayerIDS = playerIDs as? [String] else {
EGC.printLogEGC("No Player")
return
}
/* Load an array of player */
GKPlayer.loadPlayersForIdentifiers(hasePlayerIDS) {
(players, error) in
guard error == nil else {
EGC.printLogEGC("Error retrieving player info: \(error!.localizedDescription)")
EGC.disconnectMatch()
return
}
guard let players = players else {
EGC.printLogEGC("Error retrieving players; returned nil")
return
}