Skip to content

Commit

Permalink
Merge pull request cs3217-2324#106 from Vanessamae23/main
Browse files Browse the repository at this point in the history
Add leaderboard
  • Loading branch information
Vanessamae23 authored Apr 10, 2024
2 parents e641776 + 41cb394 commit 18326b4
Show file tree
Hide file tree
Showing 13 changed files with 232 additions and 16 deletions.
10 changes: 9 additions & 1 deletion TowerForge/TowerForge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
523E5C4C2BC53F70007444DA /* WaveSpawnEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 523E5C4B2BC53F70007444DA /* WaveSpawnEvent.swift */; };
523E5C512BC60563007444DA /* SurvivalModeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 523E5C502BC60563007444DA /* SurvivalModeTests.swift */; };
523E5C532BC60A15007444DA /* DeathMatchModeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 523E5C522BC60A15007444DA /* DeathMatchModeTests.swift */; };
523E5C552BC63A16007444DA /* LeaderboardViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 523E5C542BC63A16007444DA /* LeaderboardViewController.swift */; };
5240D08F2BAE6D0A004F1486 /* Point.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5240D08E2BAE6D0A004F1486 /* Point.swift */; };
5240D0912BAF3453004F1486 /* Life.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5240D0902BAF3453004F1486 /* Life.swift */; };
5240D0A02BB330B5004F1486 /* GameMode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5240D09F2BB330B4004F1486 /* GameMode.swift */; };
Expand Down Expand Up @@ -165,6 +166,7 @@
52DF5FFB2BA3601400135367 /* HealthComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52DF5FFA2BA3601400135367 /* HealthComponent.swift */; };
52DF5FFF2BA3656500135367 /* ShootingComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52DF5FFE2BA3656500135367 /* ShootingComponent.swift */; };
52F268702BB4B319009599AD /* GameModeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52F2686F2BB4B319009599AD /* GameModeViewController.swift */; };
52F930E72BC63F7F003D11B5 /* LeaderboardSelectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52F930E62BC63F7F003D11B5 /* LeaderboardSelectionViewController.swift */; };
9B04060D2BB875740026E903 /* EventTransformation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B04060C2BB875740026E903 /* EventTransformation.swift */; };
9B0406102BB879990026E903 /* InvulnerabilityPowerUp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B04060F2BB879990026E903 /* InvulnerabilityPowerUp.swift */; };
9B0406122BB889940026E903 /* PowerUpNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B0406112BB889940026E903 /* PowerUpNode.swift */; };
Expand Down Expand Up @@ -301,6 +303,7 @@
523E5C4B2BC53F70007444DA /* WaveSpawnEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WaveSpawnEvent.swift; sourceTree = "<group>"; };
523E5C502BC60563007444DA /* SurvivalModeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SurvivalModeTests.swift; sourceTree = "<group>"; };
523E5C522BC60A15007444DA /* DeathMatchModeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeathMatchModeTests.swift; sourceTree = "<group>"; };
523E5C542BC63A16007444DA /* LeaderboardViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeaderboardViewController.swift; sourceTree = "<group>"; };
5240D08E2BAE6D0A004F1486 /* Point.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Point.swift; sourceTree = "<group>"; };
5240D0902BAF3453004F1486 /* Life.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Life.swift; sourceTree = "<group>"; };
5240D0952BB04E57004F1486 /* Nosifer-Regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "Nosifer-Regular.ttf"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -381,6 +384,7 @@
52DF5FFA2BA3601400135367 /* HealthComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthComponent.swift; sourceTree = "<group>"; };
52DF5FFE2BA3656500135367 /* ShootingComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShootingComponent.swift; sourceTree = "<group>"; };
52F2686F2BB4B319009599AD /* GameModeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameModeViewController.swift; sourceTree = "<group>"; };
52F930E62BC63F7F003D11B5 /* LeaderboardSelectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LeaderboardSelectionViewController.swift; sourceTree = "<group>"; };
9B04060C2BB875740026E903 /* EventTransformation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventTransformation.swift; sourceTree = "<group>"; };
9B04060F2BB879990026E903 /* InvulnerabilityPowerUp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InvulnerabilityPowerUp.swift; sourceTree = "<group>"; };
9B0406112BB889940026E903 /* PowerUpNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PowerUpNode.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -670,6 +674,8 @@
5299D13B2BC3670E003EF746 /* LoginViewController.swift */,
5299D13D2BC36E61003EF746 /* RegisterViewController.swift */,
52DD8F982BC52F8400D96BAB /* LevelPopupViewController.swift */,
523E5C542BC63A16007444DA /* LeaderboardViewController.swift */,
52F930E62BC63F7F003D11B5 /* LeaderboardSelectionViewController.swift */,
);
path = ViewControllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -715,8 +721,8 @@
52A7940E2BBC476B0083C976 /* Networking */ = {
isa = PBXGroup;
children = (
52A794072BBC35E30083C976 /* Constants */,
5299D13F2BC3AA27003EF746 /* RankingNetwork */,
52A794072BBC35E30083C976 /* Constants */,
3CFA72E52BC039740081337F /* Utils */,
3CBECF822BBDC36A005EF39B /* GameNetwork */,
52A7940F2BBC47770083C976 /* RoomNetwork */,
Expand Down Expand Up @@ -1360,6 +1366,7 @@
5240D0A72BB33356004F1486 /* LifeProp.swift in Sources */,
3CA829C42BB70C5E00D8E72A /* ButtonComponent.swift in Sources */,
3CBECF892BBE9797005EF39B /* TFNetworkCoder.swift in Sources */,
523E5C552BC63A16007444DA /* LeaderboardViewController.swift in Sources */,
5299D13E2BC36E61003EF746 /* RegisterViewController.swift in Sources */,
52DF5FFF2BA3656500135367 /* ShootingComponent.swift in Sources */,
BA443D3D2BAD9557009F0FFB /* RemoveSystem.swift in Sources */,
Expand Down Expand Up @@ -1459,6 +1466,7 @@
52A794062BBC32A10083C976 /* FirebaseRepositoryProtocol.swift in Sources */,
9B0406102BB879990026E903 /* InvulnerabilityPowerUp.swift in Sources */,
5299D1412BC3AA3A003EF746 /* GameRankData.swift in Sources */,
52F930E72BC63F7F003D11B5 /* LeaderboardSelectionViewController.swift in Sources */,
9BD669682BAFDE5E00DC8C4C /* GridDelegate.swift in Sources */,
52DF5FEB2BA3400C00135367 /* TFAnimatableNode.swift in Sources */,
3C3CBDFF2BB8708A0001B8A9 /* CGPoint+Extensions.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="32700.99.1234" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="LaC-gc-01p">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="LaC-gc-01p">
<device id="ipad12_9" orientation="landscape" layout="fullscreen" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22684"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22504"/>
<capability name="Named colors" minToolsVersion="9.0"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
Expand Down Expand Up @@ -112,6 +112,21 @@
</objects>
<point key="canvasLocation" x="-684.33382137628109" y="-2.34375"/>
</scene>
<!--Leaderboard Selection View Controller-->
<scene sceneID="SlJ-QI-6Dh">
<objects>
<tabBarController storyboardIdentifier="LeaderboardSelectionViewController" extendedLayoutIncludesOpaqueBars="YES" modalPresentationStyle="fullScreen" useStoryboardIdentifierAsRestorationIdentifier="YES" id="22d-FP-2g3" customClass="LeaderboardSelectionViewController" customModule="TowerForge" customModuleProvider="target" sceneMemberID="viewController">
<navigationItem key="navigationItem" id="LUD-kC-ATN"/>
<tabBar key="tabBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="ecM-56-JFJ">
<rect key="frame" x="0.0" y="0.0" width="393" height="49"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tabBar>
</tabBarController>
<placeholder placeholderIdentifier="IBFirstResponder" id="PkQ-x3-TdK" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-3267" y="558"/>
</scene>
<!--Login View Controller-->
<scene sceneID="e1f-5K-0il">
<objects>
Expand Down Expand Up @@ -568,6 +583,7 @@
<state key="normal" backgroundImage="square-button"/>
<connections>
<action selector="onRankingPressed:" destination="bL0-ag-pcs" eventType="touchUpInside" id="5RS-dy-IkJ"/>
<segue destination="22d-FP-2g3" kind="show" id="mYs-bl-YJS"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Global Ranking" textAlignment="center" lineBreakMode="headTruncation" numberOfLines="2" minimumScaleFactor="0.5" translatesAutoresizingMaskIntoConstraints="NO" id="BQV-da-mJ4" userLabel="Ranking Label">
Expand Down Expand Up @@ -1066,7 +1082,7 @@
</scene>
</scenes>
<inferredMetricsTieBreakers>
<segue reference="22u-0c-ugI"/>
<segue reference="WuO-mG-bY5"/>
</inferredMetricsTieBreakers>
<resources>
<image name="Flag" width="500" height="500"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ class CaptureTheFlagMode: GameMode {
}
}
func getGameResults() -> [GameResult] {
[GameResult(variable: "Life left", value: String(self.currentOwnLife))]
[LocalResult(variable: "Life left", value: String(self.currentOwnLife))]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ class DeathMatchMode: GameMode {
}
}
func getGameResults() -> [GameResult] {
let result = [GameResult(variable: "Total Kill", value: String(self.currentOwnKillCounter)),
GameResult(variable: "Opponent Kill", value: String(self.currentOpponentKillCounter))]
let result: [GameResult] = [
LeaderboardResult(variable: RankType.TotalKill.rawValue,
result: Double(self.currentOwnKillCounter),
value: String(self.currentOwnKillCounter)),
LocalResult(variable: "Opponent Kill",
value: String(self.currentOpponentKillCounter))
]
return result
}

Expand Down
13 changes: 12 additions & 1 deletion TowerForge/TowerForge/GameModule/GameModes/GameMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@

import Foundation

struct GameResult {
protocol GameResult {
var variable: String { get }
var value: String { get }
}

struct LeaderboardResult: GameResult {
var variable: RankType.RawValue
var result: Double
var value: String
}

struct LocalResult: GameResult {
var variable: String
var value: String
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SurvivalGameMode: GameMode {
}

func getGameResults() -> [GameResult] {
[GameResult(variable: "Finished Waves", value: String(currentLevel - 1))]
[LocalResult(variable: "Finished Waves", value: String(currentLevel - 1))]
}

private func generateWaveSpawns(enemyCount: Int) {
Expand Down
24 changes: 22 additions & 2 deletions TowerForge/TowerForge/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import SpriteKit

class GameViewController: UIViewController {
private var gameWorld: GameWorld?
private var playerData: AuthenticationData?
private var gameRankProvider: GameRankProvider?
var gameMode: Mode?
var isPaused = false
var gameRoom: GameRoom?
Expand All @@ -24,7 +26,14 @@ class GameViewController: UIViewController {
super.viewDidLoad()
AchievementManager.incrementTotalGamesStarted()
AudioManager.shared.playBackground()
showGameLevelScene(level: 1) // TODO : Change hardcoded level value
showGameLevelScene()

let auth = AuthenticationProvider()
if auth.isUserLoggedIn() {
auth.getUserDetails { data, _ in
self.playerData = data
}
}
}

override func viewDidDisappear(_ animated: Bool) {
Expand Down Expand Up @@ -79,11 +88,22 @@ extension GameViewController: SceneManagerDelegate {
}
func showGameOverScene(isWin: Bool, results: [GameResult]) {
let gameOverScene = GameOverScene(win: isWin, results: results)
if let data = self.playerData {
for result in results {
if let leaderboardResult = result as? LeaderboardResult {
let rank = GameRankProvider(type: leaderboardResult.variable)
let data = GameRankData(userId: data.userId,
username: data.username ?? "",
score: leaderboardResult.result)
rank.setNewRank(rank: data)
}
}
}
gameOverScene.sceneManagerDelegate = self
gamePopupButton.isHidden = true
showScene(scene: gameOverScene)
}
func showGameLevelScene(level: Int) {
func showGameLevelScene() {
guard let gameScene = GameScene(fileNamed: "GameScene") else {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,41 @@
import Foundation
import FirebaseDatabaseInternal

enum RankType: String, CaseIterable {
case TotalKill
static var allCasesAsString: [String] {
allCases.map { $0.rawValue }
}
}

class GameRankProvider {
private let ranksRef = FirebaseDatabaseReference(.Ranks)
private let ranksRef: DatabaseReference
init(type: String) {
self.ranksRef = FirebaseDatabaseReference(.Ranks).child(type)
}
func setNewRank(rank: GameRankData) {
let userRankData = ["username": rank.username, "score": rank.score] as [String: Any]
ranksRef.child(rank.userId).setValue(userRankData)
self.getHighScore(forPlayer: rank.userId) { result, _ in
guard let oldResult = result else {
let userRankData = ["username": rank.username, "score": rank.score] as [String: Any]
self.ranksRef.child(rank.userId).setValue(userRankData)
return
}
if rank.score > oldResult {
let userRankData = ["username": rank.username, "score": rank.score] as [String: Any]
self.ranksRef.child(rank.userId).setValue(userRankData)
}
}

}
private func getHighScore(forPlayer playerId: String, completion: @escaping (Double?, Error?) -> Void) {
ranksRef.child(playerId).observeSingleEvent(of: .value) { snapshot in
if let userData = snapshot.value as? [String: Any],
let score = userData["score"] as? Double {
completion(score, nil)
} else {
completion(nil, nil)
}
}
}
func getTopRanks(completion: @escaping ([GameRankData]?, Error?) -> Void) {
ranksRef.queryOrdered(byChild: "score").queryLimited(toLast: 10).observeSingleEvent(of: .value) { snapshot in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ protocol SceneManagerDelegate: AnyObject {
func showMenuScene()
func showGameOverScene(isWin: Bool, results: [GameResult])
func showLevelScene()
func showGameLevelScene(level: Int)
func showGameLevelScene()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// LeaderboardSelectionViewController.swift
// TowerForge
//
// Created by Vanessa Mae on 10/04/24.
//

import Foundation
import UIKit

class LeaderboardSelectionViewController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()
let viewControllers = RankType.allCases.map { type -> UINavigationController in
let leaderboardVC = LeaderboardViewController(type: type)
let navigationController = UINavigationController(rootViewController: leaderboardVC)
navigationController.tabBarItem.title = type.rawValue
return navigationController
}

self.setViewControllers(viewControllers, animated: false)
}
}
Loading

0 comments on commit 18326b4

Please sign in to comment.