Skip to content

Commit

Permalink
Merge pull request cs3217-2324#87 from zheng-ze/main
Browse files Browse the repository at this point in the history
Update GameRoom to save roomId and Add Unit Tests
  • Loading branch information
Vanessamae23 authored Apr 7, 2024
2 parents 8c286b8 + abdda05 commit 18b8742
Show file tree
Hide file tree
Showing 17 changed files with 753 additions and 34 deletions.
28 changes: 28 additions & 0 deletions TowerForge/TowerForge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
3C07CB1F2BC27FDB00207C38 /* SystemManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C07CB1E2BC27FDB00207C38 /* SystemManagerTests.swift */; };
3C07CB212BC287DD00207C38 /* GameModuleTestsUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C07CB202BC287DD00207C38 /* GameModuleTestsUtils.swift */; };
3C07CB232BC28A2500207C38 /* EntityManagerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C07CB222BC28A2500207C38 /* EntityManagerTest.swift */; };
3C07CB252BC2903000207C38 /* TFEntityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C07CB242BC2903000207C38 /* TFEntityTests.swift */; };
3C07CB272BC2990E00207C38 /* TFComponentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C07CB262BC2990E00207C38 /* TFComponentTests.swift */; };
3C07CB2E2BC2A49200207C38 /* EventManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C07CB2C2BC2A49200207C38 /* EventManagerTests.swift */; };
3C07CB2F2BC2A49200207C38 /* EventTestsUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C07CB2D2BC2A49200207C38 /* EventTestsUtils.swift */; };
3C0B608D2BB2B84000FFECB4 /* ContactComponent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C0B608C2BB2B84000FFECB4 /* ContactComponent.swift */; };
3C3CBDF72BB81D970001B8A9 /* TFCameraNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C3CBDF62BB81D970001B8A9 /* TFCameraNode.swift */; };
3C3CBDF92BB821500001B8A9 /* TFScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C3CBDF82BB821500001B8A9 /* TFScene.swift */; };
Expand Down Expand Up @@ -205,6 +212,13 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
3C07CB1E2BC27FDB00207C38 /* SystemManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemManagerTests.swift; sourceTree = "<group>"; };
3C07CB202BC287DD00207C38 /* GameModuleTestsUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameModuleTestsUtils.swift; sourceTree = "<group>"; };
3C07CB222BC28A2500207C38 /* EntityManagerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManagerTest.swift; sourceTree = "<group>"; };
3C07CB242BC2903000207C38 /* TFEntityTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TFEntityTests.swift; sourceTree = "<group>"; };
3C07CB262BC2990E00207C38 /* TFComponentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TFComponentTests.swift; sourceTree = "<group>"; };
3C07CB2C2BC2A49200207C38 /* EventManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventManagerTests.swift; sourceTree = "<group>"; };
3C07CB2D2BC2A49200207C38 /* EventTestsUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventTestsUtils.swift; sourceTree = "<group>"; };
3C0B608C2BB2B84000FFECB4 /* ContactComponent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactComponent.swift; sourceTree = "<group>"; };
3C3CBDF62BB81D970001B8A9 /* TFCameraNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TFCameraNode.swift; sourceTree = "<group>"; };
3C3CBDF82BB821500001B8A9 /* TFScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TFScene.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -798,6 +812,8 @@
BA443D462BB05C6E009F0FFB /* MoveEventTests.swift */,
BA443D482BB05EF0009F0FFB /* RemoveEventTests.swift */,
BA443D4A2BB05F73009F0FFB /* RequestSpawnEventTests.swift */,
3C07CB2C2BC2A49200207C38 /* EventManagerTests.swift */,
3C07CB2D2BC2A49200207C38 /* EventTestsUtils.swift */,
);
path = EventTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1036,6 +1052,11 @@
isa = PBXGroup;
children = (
BAFFB94F2BB12F9D00D8301F /* GameEngineTests.swift */,
3C07CB1E2BC27FDB00207C38 /* SystemManagerTests.swift */,
3C07CB222BC28A2500207C38 /* EntityManagerTest.swift */,
3C07CB242BC2903000207C38 /* TFEntityTests.swift */,
3C07CB262BC2990E00207C38 /* TFComponentTests.swift */,
3C07CB202BC287DD00207C38 /* GameModuleTestsUtils.swift */,
);
path = GameModuleTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1404,12 +1425,19 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3C07CB252BC2903000207C38 /* TFEntityTests.swift in Sources */,
3C07CB2F2BC2A49200207C38 /* EventTestsUtils.swift in Sources */,
3C07CB232BC28A2500207C38 /* EntityManagerTest.swift in Sources */,
3C07CB1F2BC27FDB00207C38 /* SystemManagerTests.swift in Sources */,
52DF5FC22BA32B2600135367 /* TowerForgeTests.swift in Sources */,
3C07CB2E2BC2A49200207C38 /* EventManagerTests.swift in Sources */,
BA443D472BB05C6E009F0FFB /* MoveEventTests.swift in Sources */,
52DF5FE42BA3391200135367 /* TFTexturesTests.swift in Sources */,
3C07CB272BC2990E00207C38 /* TFComponentTests.swift in Sources */,
BAFFB9502BB12F9D00D8301F /* GameEngineTests.swift in Sources */,
BA443D4B2BB05F73009F0FFB /* RequestSpawnEventTests.swift in Sources */,
BA443D452BB05A3C009F0FFB /* SpawnEventTests.swift in Sources */,
3C07CB212BC287DD00207C38 /* GameModuleTestsUtils.swift in Sources */,
BA443D492BB05EF0009F0FFB /* RemoveEventTests.swift in Sources */,
BA443D422BAD9885009F0FFB /* DamageEventTests.swift in Sources */,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

protocol EventTransformation: Identifiable {
protocol EventTransformation: Identifiable, AnyObject {
var id: UUID { get}
func transformEvent(event: TFEvent) -> TFEvent
}
3 changes: 0 additions & 3 deletions TowerForge/TowerForge/GameModule/Events/TFEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ extension TFEventTypeWrapper: Hashable {
protocol TFEvent {
var timestamp: TimeInterval { get }
var entityId: UUID { get }

/// Execute method returns an optional EventOutput as not all event executions
/// will return an EventOutput
func execute(in target: EventTarget) -> EventOutput
}

Expand Down
17 changes: 12 additions & 5 deletions TowerForge/TowerForge/GameModule/GameWorld.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class GameWorld {
private var grid: Grid
private var renderer: Renderer?
private let worldBounds: CGRect
private var popup: StatePopupNode

unowned var delegate: SceneManagerDelegate?
unowned var statePopupDelegate: StatePopupDelegate?
unowned var statePopupDelegate: StatePopupDelegate? { didSet { popup.delegate = statePopupDelegate } }

init(scene: GameScene?, screenSize: CGRect, mode: Mode,
gameRoom: GameRoom? = nil, currentPlayer: GamePlayer? = nil) {
Expand All @@ -33,6 +34,7 @@ class GameWorld {
selectionNode = UnitSelectionNode()
powerUpSelectionNode = PowerUpSelectionNode(eventManager: gameEngine.eventManager)
grid = Grid(screenSize: worldBounds)
popup = StatePopupNode()
renderer = Renderer(target: self, scene: scene)

setUp()
Expand Down Expand Up @@ -62,6 +64,7 @@ class GameWorld {
setUpGameEngine()
setUpSelectionNode()
setUpPowerUpNode()
setUpStatePopupNode()
}

private func setUpScene() {
Expand Down Expand Up @@ -93,15 +96,19 @@ class GameWorld {
}
}

func presentStatePopup() {
let popup = StatePopupNode()
popup.delegate = statePopupDelegate
// TODO: Refactor this
private func setUpStatePopupNode() {
popup.zPosition = 10_000
popup.name = "popup"
popup.position = CGPoint(x: 0, y: 0)
}

func presentStatePopup() {
scene?.add(node: popup, staticOnScreen: true)
}

func removeStatePopup() {
scene?.remove(node: popup)
}
}

extension GameWorld: Renderable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation
import UIKit
import QuartzCore

class PositionSystem: TFSystem {
var isActive = true
Expand All @@ -27,7 +26,6 @@ class PositionSystem: TFSystem {
continue
}
if playerComponent.player == .ownPlayer && positionComponent.position.x > GameWorld.worldSize.width {
// TODO: Change UIScreen
handleOutOfGame(entity: entity)
} else if playerComponent.player == .oppositePlayer
&& positionComponent.position.x < UIScreen.main.bounds.minX {
Expand Down
3 changes: 2 additions & 1 deletion TowerForge/TowerForge/GameViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ extension GameViewController: SceneManagerDelegate {
// Present the scene
gameScene.sceneManagerDelegate = self
gameScene.updateDelegate = self
gameScene.statePopupDelegate = self
gameScene.statePopupDelegate = self
showScene(scene: gameScene)
setUpGameWorld(scene: gameScene)
}
Expand All @@ -106,5 +106,6 @@ extension GameViewController: StatePopupDelegate {

func onResume() {
isPaused = false
gameWorld?.removeStatePopup()
}
}
7 changes: 4 additions & 3 deletions TowerForge/TowerForge/Networking/RoomNetwork/GameRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ class GameRoom {

private func postRoomDataToFirebase(completion: ((_ err: Any?, _ result: String?) -> Void)?) {
let roomRef = FirebaseDatabaseReference(.Rooms).child(roomName)
roomRef.updateChildValues(["roomName": roomName ]) { err, snap in
let roomId = UUID().uuidString
roomRef.updateChildValues(["roomId": roomId ]) { err, _ in
if err != nil {
completion?(err, nil)
} else {
completion?(nil, snap.key)
completion?(nil, roomId)
}
}
}
Expand Down Expand Up @@ -156,7 +157,7 @@ class GameRoom {
}
print("Finding the room and making it")
let room = GameRoom(roomName: roomName, roomState: .waitingForPlayers)
room.roomId = snap.key
room.roomId = snap.childSnapshot(forPath: "roomId").value as? RoomId
completion(room)
}
}
Expand Down
7 changes: 4 additions & 3 deletions TowerForge/TowerForge/Nodes/StatePopupNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ protocol StatePopupDelegate: AnyObject {

class StatePopupNode: TFSpriteNode {

var delegate: StatePopupDelegate?
var delegate: StatePopupDelegate? { didSet { setupNode() } }

init() {
super.init(color: .clear, size: CGSize(width: 700, height: 300))
setupNode()
}

func setupNode() {
self.isUserInteractionEnabled = true
let background = TFSpriteNode(imageName: "square-button", size: self.size)

// TODO: Refactor into a constant
Expand All @@ -38,11 +38,13 @@ class StatePopupNode: TFSpriteNode {
height: 200),
imageNamed: "play-button")
resumeButton.name = "resumeButton"
resumeButton.isUserInteractionEnabled = true

let menuButton = TFButtonNode(action: TFButtonDelegate(onTouchBegan: { [weak self] in
self?.delegate?.onMenu()
}, onTouchEnded: {}), size: CGSize(width: 200, height: 200), imageNamed: "menu-button")
menuButton.name = "menuButton"
menuButton.isUserInteractionEnabled = true

// TODO: Refactor the position into a constants
resumeButton.position = CGPoint(x: background.position.x + 150, y: background.position.y)
Expand All @@ -55,7 +57,6 @@ class StatePopupNode: TFSpriteNode {
self.add(child: background)
self.add(child: menuButton)
self.add(child: resumeButton)

}

}
6 changes: 6 additions & 0 deletions TowerForge/TowerForge/Scenes/GameScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class GameScene: SKScene {

override func update(_ currentTime: TimeInterval) {
if statePopupDelegate?.isPaused ?? false {
lastUpdatedTimeInterval = currentTime
return
}

if lastUpdatedTimeInterval == TimeInterval(0) {
lastUpdatedTimeInterval = currentTime
}
Expand Down Expand Up @@ -98,6 +100,10 @@ extension GameScene: TFScene {

func remove(node: TFNode) {
node.node.removeFromParent()

if let name = node.name {
cameraNode?.removeChild(withName: name)
}
}

func contains(node: TFNode) -> Bool {
Expand Down
123 changes: 123 additions & 0 deletions TowerForge/TowerForgeTests/EventTests/EventManagerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//
// EventManagerTests.swift
// TowerForgeTests
//
// Created by Zheng Ze on 7/4/24.
//

import XCTest
@testable import TowerForge

final class EventManagerTests: XCTestCase {
func test_initialize() {
let eventManager = EventManager()
XCTAssertEqual(eventManager.eventQueue.count, 0)
XCTAssertEqual(eventManager.eventTransformations.count, 0)
XCTAssertEqual(eventManager.eventHandler.count, 0)
XCTAssertNil(eventManager.remoteEventManager)
}

func test_intialiseWithNetworking() {
let eventManager = EventManager(roomId: "testRoom",
currentPlayer: GamePlayer(userPlayerId: "test", userName: "test"))
XCTAssertEqual(eventManager.eventQueue.count, 0)
XCTAssertEqual(eventManager.eventTransformations.count, 0)
XCTAssertEqual(eventManager.eventHandler.count, 0)
XCTAssertNotNil(eventManager.remoteEventManager)
}

func test_addEvents_eventsShouldGetAdded() {
let eventManager = EventManager()
XCTAssertEqual(eventManager.eventQueue.count, 0)

let event = TestEvent()
eventManager.add(event)

XCTAssertEqual(eventManager.eventQueue.count, 1)
XCTAssertIdentical(eventManager.eventQueue[0] as AnyObject, event)
}

func test_addEventTransformation_transformationAdded() {
let eventManager = EventManager()
XCTAssertEqual(eventManager.eventTransformations.count, 0)

let eventTransformation = TestEventTransformation()
eventManager.addTransformation(eventTransformation: eventTransformation)

XCTAssertEqual(eventManager.eventTransformations.count, 1)
XCTAssertIdentical(eventManager.eventTransformations[0], eventTransformation)
}

func test_removeEventTransformation_transformationRemoved() {
let eventManager = EventManager()
XCTAssertEqual(eventManager.eventTransformations.count, 0)

let eventTransformation = TestEventTransformation()
eventManager.addTransformation(eventTransformation: eventTransformation)

XCTAssertEqual(eventManager.eventTransformations.count, 1)
XCTAssertIdentical(eventManager.eventTransformations[0], eventTransformation)

eventManager.removeTransformation(eventTransformation: eventTransformation)

XCTAssertEqual(eventManager.eventTransformations.count, 0)
}

func test_addRemoteEventWithoutNetworking_eventUnpackedImmediately() {
let eventManager = EventManager()
XCTAssertEqual(eventManager.eventQueue.count, 0)

let event = TestRemoteEvent()
eventManager.add(event)

XCTAssertEqual(eventManager.eventQueue.count, 1)
XCTAssertTrue(eventManager.eventQueue[0] is TestEvent)
}

func test_addRemoteEventWithNetworking_eventNotUnpackedImmediately() {
let eventManager = EventManager(roomId: "testRoom",
currentPlayer: GamePlayer(userPlayerId: "test", userName: "test"))
XCTAssertEqual(eventManager.eventQueue.count, 0)

let event = TestRemoteEvent()
eventManager.add(event)

XCTAssertEqual(eventManager.eventQueue.count, 0)
}

func test_executeEvents_eventsShouldGetExecuted() {
let eventA = TestEvent()
let eventB = TestEvent()
let eventC = TestEvent()
let eventManager = EventManager()

eventManager.add(eventA)
eventManager.add(eventB)
eventManager.add(eventC)

XCTAssertFalse(eventA.didExecute)
XCTAssertFalse(eventB.didExecute)
XCTAssertFalse(eventC.didExecute)

eventManager.executeEvents(in: TestEventTarget())

XCTAssertTrue(eventA.didExecute)
XCTAssertTrue(eventB.didExecute)
XCTAssertTrue(eventC.didExecute)
}

func test_executeEventWithTransformation_eventTransformed() {
let event = TestEvent()
let transformation = TestEventTransformation()
let eventManager = EventManager()

eventManager.addTransformation(eventTransformation: transformation)
eventManager.add(event)

XCTAssertFalse(transformation.didTransformEvent)

eventManager.executeEvents(in: TestEventTarget())

XCTAssertTrue(transformation.didTransformEvent)
}
}
Loading

0 comments on commit 18b8742

Please sign in to comment.