Skip to content

Commit

Permalink
room: state tracking fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hex-agon committed May 12, 2024
1 parent 6f95a89 commit 39dd15b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions game-server/src/main/kotlin/work/fking/pangya/game/room/Room.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import work.fking.pangya.game.room.RoomJoinError.ROOM_FULL
import work.fking.pangya.game.room.match.MatchDirector
import work.fking.pangya.game.room.match.MatchEvent
import work.fking.pangya.game.room.match.MatchState
import work.fking.pangya.networking.protocol.OutboundPacket
import work.fking.pangya.networking.protocol.writeFixedSizeString
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
Expand Down Expand Up @@ -70,6 +71,7 @@ class Room(
player.currentRoom = null
val roomPlayer = findSelf(player)
players.remove(roomPlayer)
nextFreeSlot--

broadcast(RoomReplies.roomCensusRemove(roomPlayer))

Expand Down Expand Up @@ -100,7 +102,7 @@ class Room(
}

fun handleMatchEvent(event: MatchEvent) {
activeMatch?.onMatchEvent(this, event) ?: throw IllegalStateException("Room[$id] Cannot handle match event, no bound handler")
activeMatch?.onMatchEvent(this, event)
}

fun handleUpdates(updates: List<RoomUpdate>) {
Expand All @@ -112,7 +114,7 @@ class Room(
broadcast(RoomReplies.roomSettings(this))
}

fun broadcast(message: Any) {
fun broadcast(message: OutboundPacket) {
playersLock.read {
players.forEach { it.player.writeAndFlush(message) }
}
Expand All @@ -129,8 +131,9 @@ class Room(
)
activeMatch = Match(matchState, settings.type.matchDirector)

playersLock.read {
playersLock.write {
players.forEach { player ->
player.resetGameState()
player.write(MatchReplies.start230())
player.write(MatchReplies.start231())
player.write(MatchReplies.start77())
Expand All @@ -140,6 +143,12 @@ class Room(
}
}

fun endGame() {
state = RoomState.LOBBY
activeMatch = null
playersLock.write { players.forEach { player -> player.resetLobbyState() } }
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand All @@ -152,7 +161,6 @@ class Room(
override fun hashCode(): Int {
return id
}

}

fun ByteBuf.write(room: Room) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ class RoomPlayer(
player.flush()
}

fun resetLobbyState() {
ready = false
away = false
}

fun resetGameState() {
currentHole = 1
finishedHole = false
pang = 0
bonusPang = 0
statistics = PlayerStatistics()
}

private fun computeRoomFlags(roomOwner: Boolean): List<RoomPlayerFlag> {
val playerFlags = mutableListOf<RoomPlayerFlag>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ class PracticeMatchDirector : MatchDirector {
write(MatchReplies.tourneyTimeout())
}
room.broadcast(MatchReplies.tourneyUpdatePlayerProgress(event.player))
room.endGame()
}
}

0 comments on commit 39dd15b

Please sign in to comment.