Skip to content

Commit

Permalink
Merge pull request #84 from Vanessamae23/main
Browse files Browse the repository at this point in the history
Fix bugs with room and fix ui
  • Loading branch information
Vanessamae23 authored Apr 5, 2024
2 parents 587e712 + dfc9b04 commit 293578e
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 41 deletions.
21 changes: 21 additions & 0 deletions TowerForge/Assets.xcassets/menu-button.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "menu-button.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions TowerForge/Assets.xcassets/pause-button.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "pause-button.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions TowerForge/Assets.xcassets/play-button.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "play-button.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ Match</string>
</buttonConfiguration>
<connections>
<action selector="createRoomButtonPressed:" destination="tK4-EO-PjX" eventType="touchUpInside" id="Dmp-hD-vCj"/>
<segue destination="FnT-Eh-Bya" kind="show" identifier="segueToWaitingRoom" id="2av-V7-WsG"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="T8A-hM-ZpD">
Expand Down Expand Up @@ -365,26 +364,25 @@ Match</string>
<color key="backgroundColor" name="AccentColor"/>
</stackView>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="546-UR-FKj">
<rect key="frame" x="401" y="812" width="565" height="127"/>
<rect key="frame" x="351" y="812" width="276" height="127"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<state key="normal" title="Button"/>
<buttonConfiguration key="configuration" style="plain">
<backgroundConfiguration key="background" image="square-button"/>
<attributedString key="attributedTitle">
<fragment content="Leave Room">
<attributes>
<font key="NSFont" size="55" name="Baskerville-Bold"/>
<paragraphStyle key="NSParagraphStyle" alignment="natural" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>
</attributes>
</fragment>
</attributedString>
<color key="baseForegroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<attributedString key="attributedTitle"/>
</buttonConfiguration>
<connections>
<action selector="onLeaveButtonPressed:" destination="FnT-Eh-Bya" eventType="touchUpInside" id="xhU-Xf-OGM"/>
<segue destination="tK4-EO-PjX" kind="showDetail" identifier="segueToGameRoom" id="gCm-LW-wHo"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Leave" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="tSX-Hr-bNY">
<rect key="frame" x="406" y="840" width="168" height="70"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="Nosifer-Regular" family="Nosifer" pointSize="40"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<viewLayoutGuide key="safeArea" id="rUg-Tb-9QP"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
Expand All @@ -396,11 +394,10 @@ Match</string>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="srP-KJ-HWs" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-942" y="2536"/>
<point key="canvasLocation" x="-942.16691068814043" y="2535.9375"/>
</scene>
</scenes>
<inferredMetricsTieBreakers>
<segue reference="gCm-LW-wHo"/>
<segue reference="5Mz-7I-nNc"/>
</inferredMetricsTieBreakers>
<resources>
Expand Down
4 changes: 2 additions & 2 deletions TowerForge/TowerForge/GameModule/GameWorld.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ class GameWorld {
popup.delegate = self
// TODO: Refactor this
popup.zPosition = 10_000
popup.position = CGPoint(x: UIScreen.main.bounds.width / 2,
y: UIScreen.main.bounds.height / 2)
popup.position = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)
// popup.position = CGPoint(x: 0, y: 0)
scene?.add(node: popup, staticOnScreen: false)
}
}
Expand Down
63 changes: 46 additions & 17 deletions TowerForge/TowerForge/Networking/RoomNetwork/GameRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,25 @@ class GameRoom {

// Check if the room name is available
isRoomNameAvailable(roomName: roomName) { isAvailable in

// If the room name is available, try to join the room
guard isAvailable else {
completion(false)
return
}

self.postRoomDataToFirebase { _, _ in
print("Making room ...")
self.makeRoomListener()
if isAvailable {
completion(true)
} else {
completion(false)
}
}
self.makeRoomListener()
}
init(roomName: String, roomState: RoomState? = nil) {
self.roomName = roomName
self.roomState = roomState
self.makeRoomListener()
self.makeRoomChangeListener()
}
deinit {
removeAllListeners()
}
private func postRoomDataToFirebase(completion: ((_ err: Any?, _ result: String?) -> Void)?) {
let roomRef = FirebaseDatabaseReference(.Rooms).child(roomName)
roomRef.updateChildValues(["roomName": roomName as NSString? ]) { err, snap in
roomRef.updateChildValues(["roomName": roomName as? NSString ]) { err, snap in
if err != nil {
completion?(err, nil)
} else {
Expand Down Expand Up @@ -138,6 +134,7 @@ class GameRoom {
roomRef.getData { _, snapshot in
if let snap = snapshot {
if snap.exists() {
print("Finding the room and making it")
let room = GameRoom(roomName: roomName, roomState: .waitingForPlayers)
completion(room)
} else {
Expand All @@ -149,9 +146,7 @@ class GameRoom {
}
}
}
private func makeRoomListener() {
print("Start listening")
roomRef.removeAllObservers()
private func makeRoomChangeListener() {
roomRef.child(roomName).observe(.value) { [weak self] snap in
guard let snapshotValue = snap.value as? [String: Any] else {
return
Expand All @@ -171,17 +166,51 @@ class GameRoom {
self?.gameRoomDelegate?.onRoomChange()
}
}
private func makeRoomDeletionListener() {
roomRef.child(roomName).child("players").observe(.childRemoved) { [weak self] snapshot in
let playerKey = snapshot.key
// Check if the player is playerOne
if let playerOne = self?.playerOne, playerOne.userPlayerId == playerKey {
self?.playerOne = nil

self?.gameRoomDelegate?.onRoomChange()
return
}

// Check if the player is playerTwo
if let playerTwo = self?.playerTwo, playerTwo.userPlayerId == playerKey {
self?.playerTwo = nil
self?.gameRoomDelegate?.onRoomChange()
return
}

}
}
private func isRoomNameAvailable(roomName: String, completion: @escaping (Bool) -> Void?) {
FirebaseDatabaseReference(.Rooms).child(roomName).getData { _, snapshot in
if let snap = snapshot {
if snap.exists() {
completion(false)
} else {
self.postRoomDataToFirebase { _, _ in
self.makeRoomChangeListener()
self.makeRoomDeletionListener()
completion(true)
}
}
completion(true)

} else {
completion(true)
self.postRoomDataToFirebase { _, _ in
self.makeRoomChangeListener()
self.makeRoomDeletionListener()
completion(true)
}
}
}

}
private func removeAllListeners() {
roomRef.child(roomName).removeAllObservers()

}
}
10 changes: 5 additions & 5 deletions TowerForge/TowerForge/Nodes/StatePopupNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class StatePopupNode: TFSpriteNode {
var delegate: StatePopupDelegate?

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

Expand All @@ -33,14 +33,14 @@ class StatePopupNode: TFSpriteNode {
self?.delegate?.onResume()
},
onTouchEnded: {}),
size: CGSize(width: 50,
height: 50),
imageNamed: "circle-button")
size: CGSize(width: 200,
height: 200),
imageNamed: "play-button")
resumeButton.name = "resumeButton"

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

// TODO: Refactor the position into a constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ class GameRoomViewController: UIViewController {
}

let playerOne = GamePlayer(userName: playerName)
print("Attempting to make a room")
gameRoom = GameRoom(roomName: roomName) { success in
if success {
print("Now joining the room \(success)")
self.joinRoom(player: playerOne)
}
}
Expand All @@ -44,9 +46,9 @@ class GameRoomViewController: UIViewController {
if success {
print("Successfully joined the room")
self.currentPlayer = player
DispatchQueue.main.async {
self.performSegue(withIdentifier: "segueToWaitingRoom", sender: self)
}
self.performSegue(withIdentifier: "segueToWaitingRoom",
sender: self)

} else {
print("Failed to join room")
}
Expand All @@ -59,7 +61,7 @@ class GameRoomViewController: UIViewController {
guard let currentPlayer = self.currentPlayer else {
return
}
print("Before segueing \(gameRoom)")
print("Before segueing \(gameRoom?.playerOne)")
destinationVC.currentPlayer = currentPlayer
destinationVC.gameRoom = gameRoom
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GameWaitingRoomViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
gameRoom?.gameRoomDelegate = self
print("Is there game \(gameRoom)")
updatePlayerList()
}
@IBAction private func onLeaveButtonPressed(_ sender: Any) {
Expand All @@ -33,6 +34,7 @@ class GameWaitingRoomViewController: UIViewController {
}

@IBOutlet var ListStackView: UIStackView!

private func updatePlayerList() {
ListStackView.arrangedSubviews.forEach { $0.removeFromSuperview() }

Expand Down

0 comments on commit 293578e

Please sign in to comment.