From 7437e58faed9f69f3c8de459b49acc10131136fe Mon Sep 17 00:00:00 2001 From: TheSaminator Date: Sun, 6 Mar 2022 10:13:48 -0500 Subject: [PATCH] Fix bomber-attacks not showing up in chat log --- .../kotlin/starshipfights/game/game_state.kt | 13 ++++- .../kotlin/starshipfights/game/client_game.kt | 4 +- .../kotlin/starshipfights/game/game_ui.kt | 55 ++++++++++--------- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/commonMain/kotlin/starshipfights/game/game_state.kt b/src/commonMain/kotlin/starshipfights/game/game_state.kt index 167a1ef..fb1855e 100644 --- a/src/commonMain/kotlin/starshipfights/game/game_state.kt +++ b/src/commonMain/kotlin/starshipfights/game/game_state.kt @@ -25,7 +25,7 @@ data class GameState( fun getShipOwner(id: Id) = destroyedShips[id]?.owner ?: ships.getValue(id).owner } -fun GameState.afterPlayerReady(playerSide: GlobalSide) = if (ready == playerSide.other) { +private fun GameState.afterPhase(): GameState { var newShips = ships val newWrecks = destroyedShips.toMutableMap() val newChatEntries = mutableListOf() @@ -95,7 +95,10 @@ fun GameState.afterPlayerReady(playerSide: GlobalSide) = if (ready == playerSide hits++ when (val impactResult = ship.impact(hits)) { - is ImpactResult.Damaged -> id to impactResult.ship + is ImpactResult.Damaged -> { + newChatEntries += ChatEntry.ShipAttacked(id, ShipAttacker.Bombers, Moment.now, hits, null) + id to impactResult.ship + } is ImpactResult.Destroyed -> { newWrecks[id] = impactResult.ship newChatEntries += ChatEntry.ShipDestroyed(id, Moment.now, ShipAttacker.Bombers) @@ -134,7 +137,11 @@ fun GameState.afterPlayerReady(playerSide: GlobalSide) = if (ready == playerSide } } - copy(phase = phase.next(), ready = null, ships = newShips.mapValues { (_, ship) -> ship.copy(isDoneCurrentPhase = false) }, chatBox = chatBox + newChatEntries) + return copy(phase = phase.next(), ships = newShips.mapValues { (_, ship) -> ship.copy(isDoneCurrentPhase = false) }, destroyedShips = newWrecks, chatBox = chatBox + newChatEntries) +} + +fun GameState.afterPlayerReady(playerSide: GlobalSide) = if (ready == playerSide.other) { + afterPhase().copy(ready = null) } else copy(ready = playerSide) diff --git a/src/jsMain/kotlin/starshipfights/game/client_game.kt b/src/jsMain/kotlin/starshipfights/game/client_game.kt index 0fa1f7c..f379f6b 100644 --- a/src/jsMain/kotlin/starshipfights/game/client_game.kt +++ b/src/jsMain/kotlin/starshipfights/game/client_game.kt @@ -157,7 +157,7 @@ private suspend fun GameNetworkInteraction.execute(token: String): String { return gameEnd.await() } -private fun CoroutineScope.uiResponder(actions: SendChannel, errors: SendChannel) = object : GameUIResponder { +private class GameUIResponderImpl(scope: CoroutineScope, private val actions: SendChannel, private val errors: SendChannel) : GameUIResponder, CoroutineScope by scope { override fun doAction(action: PlayerAction) { launch { actions.send(action) @@ -180,6 +180,8 @@ private fun CoroutineScope.uiResponder(actions: SendChannel, error } } +private fun CoroutineScope.uiResponder(actions: SendChannel, errors: SendChannel) = GameUIResponderImpl(this, actions, errors) + suspend fun gameMain(side: GlobalSide, token: String, state: GameState) { interruptExit = true diff --git a/src/jsMain/kotlin/starshipfights/game/game_ui.kt b/src/jsMain/kotlin/starshipfights/game/game_ui.kt index 34fe5f4..33ced42 100644 --- a/src/jsMain/kotlin/starshipfights/game/game_ui.kt +++ b/src/jsMain/kotlin/starshipfights/game/game_ui.kt @@ -187,37 +187,38 @@ object GameUI { is ChatEntry.ShipEscaped -> { val ship = state.getShipInfo(entry.ship) val owner = state.getShipOwner(entry.ship).relativeTo(mySide) - if (owner == LocalSide.RED) - +"The enemy ship " + +if (owner == LocalSide.RED) + "The enemy ship " else - +"Our ship, the " + "Our ship, the " strong { style = "color:${owner.htmlColor}" +ship.fullName } +" has " - if (owner == LocalSide.RED) - +"fled like a coward from" + +if (owner == LocalSide.RED) + "fled like a coward" else - +"disengaged from" - +" the battlefield!" + "disengaged" + +" from the battlefield!" } is ChatEntry.ShipAttacked -> { val ship = state.getShipInfo(entry.ship) val owner = state.getShipOwner(entry.ship).relativeTo(mySide) - +"The " - if (owner == LocalSide.RED) - +"enemy ship " + +if (owner == LocalSide.RED) + "The enemy ship " + else + "Our ship, the " strong { style = "color:${owner.htmlColor}" +ship.fullName } +" has taken " - if (entry.weapon is ShipWeapon.EmpAntenna) - +"subsystem-disabling" + +if (entry.weapon is ShipWeapon.EmpAntenna) + "subsystem-draining" else - +entry.damageInflicted.toString() + entry.damageInflicted.toString() +" damage from " when (entry.attacker) { @@ -231,14 +232,14 @@ object GameUI { is ShipAttacker.EnemyShip -> { if (entry.weapon != null) { +"the " - when (entry.weapon) { - is ShipWeapon.Cannon -> +"cannons" - is ShipWeapon.Lance -> +"lances" - is ShipWeapon.Hangar -> +"bombers" - is ShipWeapon.Torpedo -> +"torpedoes" - ShipWeapon.MegaCannon -> +"Mega Giga Cannon" - ShipWeapon.RevelationGun -> +"Revelation Gun" - ShipWeapon.EmpAntenna -> +"EMP antenna" + +when (entry.weapon) { + is ShipWeapon.Cannon -> "cannons" + is ShipWeapon.Lance -> "lances" + is ShipWeapon.Hangar -> "bombers" + is ShipWeapon.Torpedo -> "torpedoes" + ShipWeapon.MegaCannon -> "Mega Giga Cannon" + ShipWeapon.RevelationGun -> "Revelation Gun" + ShipWeapon.EmpAntenna -> "EMP antenna" } +" of " } @@ -254,10 +255,10 @@ object GameUI { is ChatEntry.ShipDestroyed -> { val ship = state.getShipInfo(entry.ship) val owner = state.getShipOwner(entry.ship).relativeTo(mySide) - if (owner == LocalSide.RED) - +"The enemy ship " + +if (owner == LocalSide.RED) + "The enemy ship " else - +"Our ship, the " + "Our ship, the " strong { style = "color:${owner.htmlColor}" +ship.fullName @@ -272,10 +273,10 @@ object GameUI { } } ShipAttacker.Bombers -> { - if (owner == LocalSide.RED) - +"our " + +if (owner == LocalSide.RED) + "our " else - +"enemy " + "enemy " +"bombers" } } -- 2.25.1