diff --git a/GoldWars/GoldWars/EloHelper.swift b/GoldWars/GoldWars/EloHelper.swift index 0b08cde..ff1d543 100644 --- a/GoldWars/GoldWars/EloHelper.swift +++ b/GoldWars/GoldWars/EloHelper.swift @@ -17,18 +17,19 @@ class EloHelper { static private let LOG = OSLog.init(subsystem: "EloHelper", category: "EloHelper") - static let IDENTIFIER = "de.hft.stuttgart.ip2.goldwars.matchmaking" + static let IDENTIFIER_ALL_ELO = "de.hft.stuttgart.ip2.goldwars.leaderboard" + static let IDENTIFIER_ELO = "de.hft.stuttgart.ip2.goldwars.matchmaking" static func updateEloScore(winner: GKPlayer, hatDenNikoGemacht looser: GKPlayer) { let leaderboard = GKLeaderboard.init(players: [winner, looser]) - leaderboard.identifier = EloHelper.IDENTIFIER + leaderboard.identifier = EloHelper.IDENTIFIER_ELO leaderboard.loadScores{scores, error in // Get Scores - let R_looser = scores?.filter { $0.player == looser }.first ?? GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER, player: looser) - let R_winner = scores?.filter { $0.player == winner }.first ?? GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER, player: winner) + let R_looser = scores?.filter { $0.player == looser }.first ?? GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER_ELO, player: looser) + let R_winner = scores?.filter { $0.player == winner }.first ?? GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER_ELO, player: winner) // Calc ELO let Q_looser = pow(10.0, Double(R_looser.value) / 400.0) @@ -40,21 +41,26 @@ class EloHelper { let R_looser_new = Int64(Double(R_looser.value) + 10.0 * (0.0 - E_looser)) // Update Elo on leaderboard - let winner_new = GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER, player: winner) + let winner_new = GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER_ELO, player: winner) winner_new.value = R_winner_new - let looser_new = GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER, player: looser) + let looser_new = GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER_ELO, player: looser) looser_new.value = R_looser_new let scoreForPeer = winner == GameCenterManager.sharedInstance.localPlayer ? looser_new : winner_new let scoreForHost = winner == GameCenterManager.sharedInstance.localPlayer ? winner_new : looser_new MultiplayerNetwork.sharedInstance.sendEloData(scoreToReport: scoreForPeer) - - GKScore.report([scoreForHost], withCompletionHandler: { error in - os_log("New Scores reported to EloSystem", log: LOG, type: .info) - }) - + reportScore(score: scoreForHost.value) } - + } + + static func reportScore(score: Int64) { + let gkScoreElo = GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER_ELO, player: GameCenterManager.sharedInstance.localPlayer) + gkScoreElo.value = score + let gkScoreAll = GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER_ALL_ELO, player: GameCenterManager.sharedInstance.localPlayer) + gkScoreAll.value = score + GKScore.report([gkScoreAll, gkScoreElo], withCompletionHandler: { error in + os_log("New Scores reported to EloSystem", log: self.LOG, type: .info) + }) } } diff --git a/GoldWars/GoldWars/GameCenterManager.swift b/GoldWars/GoldWars/GameCenterManager.swift index 0b7a26e..fbf0c8d 100644 --- a/GoldWars/GoldWars/GameCenterManager.swift +++ b/GoldWars/GoldWars/GameCenterManager.swift @@ -213,11 +213,7 @@ final class GameCenterManager: NSObject, GKMatchmakerViewControllerDelegate, GKG if let eloData = try? jsonDecoder.decode(EloDataForPeer.self, from: data) { print("Recieved elo data: \(eloData.scoreToReport)") - let score = GKScore(leaderboardIdentifier: EloHelper.IDENTIFIER, player: self.localPlayer) - score.value = eloData.scoreToReport - GKScore.report([score], withCompletionHandler: { error in - os_log("New Scores reported to EloSystem", log: self.LOG, type: .info) - }) + EloHelper.reportScore(score: eloData.scoreToReport) } if let heartbeat = try? jsonDecoder.decode(Heartbeat.self, from: data) { entityManager.getHUD()?.roundTimer.isHeartbeatLocked = false