Skip to content

Commit

Permalink
Finalise bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessamae23 committed Apr 5, 2024
1 parent c89e2f5 commit dfc9b04
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
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
34 changes: 16 additions & 18 deletions TowerForge/TowerForge/Networking/RoomNetwork/GameRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,13 @@ 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 {
print("Room is taken")
completion(false)
return
}

self.postRoomDataToFirebase { _, _ in
print("Making room ...")
self.makeRoomChangeListener()
self.makeRoomDeletionListener()
if isAvailable {
completion(true)
} else {
completion(false)
}
}
self.makeRoomChangeListener()
self.makeRoomDeletionListener()
}
init(roomName: String, roomState: RoomState? = nil) {
self.roomName = roomName
Expand Down Expand Up @@ -144,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 @@ -156,8 +147,6 @@ class GameRoom {
}
}
private func makeRoomChangeListener() {
print("Start listening")
// roomRef.removeAllObservers()
roomRef.child(roomName).observe(.value) { [weak self] snap in
guard let snapshotValue = snap.value as? [String: Any] else {
return
Expand All @@ -178,7 +167,6 @@ class GameRoom {
}
}
private func makeRoomDeletionListener() {
print("Start Deletion listening")
roomRef.child(roomName).child("players").observe(.childRemoved) { [weak self] snapshot in
let playerKey = snapshot.key
// Check if the player is playerOne
Expand All @@ -203,10 +191,20 @@ class GameRoom {
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)
}
}
}

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 Down

0 comments on commit dfc9b04

Please sign in to comment.