Fix bomber-attacks not showing up in chat log
authorTheSaminator <TheSaminator@users.noreply.github.com>
Sun, 6 Mar 2022 15:13:48 +0000 (10:13 -0500)
committerTheSaminator <TheSaminator@users.noreply.github.com>
Sun, 6 Mar 2022 15:13:48 +0000 (10:13 -0500)
src/commonMain/kotlin/starshipfights/game/game_state.kt
src/jsMain/kotlin/starshipfights/game/client_game.kt
src/jsMain/kotlin/starshipfights/game/game_ui.kt

index 167a1ef4b44d93561abe216280d7da500556e422..fb1855ee0f0bbfb106b05268e44c4d5d1e184306 100644 (file)
@@ -25,7 +25,7 @@ data class GameState(
        fun getShipOwner(id: Id<ShipInstance>) = 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<ChatEntry>()
@@ -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)
 
index 0fa1f7cfb3098181a6b260c33bfa32626f3859b5..f379f6bb318e3ce413e43d1431962dc3d7dbb59a 100644 (file)
@@ -157,7 +157,7 @@ private suspend fun GameNetworkInteraction.execute(token: String): String {
        return gameEnd.await()
 }
 
-private fun CoroutineScope.uiResponder(actions: SendChannel<PlayerAction>, errors: SendChannel<String>) = object : GameUIResponder {
+private class GameUIResponderImpl(scope: CoroutineScope, private val actions: SendChannel<PlayerAction>, private val errors: SendChannel<String>) : GameUIResponder, CoroutineScope by scope {
        override fun doAction(action: PlayerAction) {
                launch {
                        actions.send(action)
@@ -180,6 +180,8 @@ private fun CoroutineScope.uiResponder(actions: SendChannel<PlayerAction>, error
        }
 }
 
+private fun CoroutineScope.uiResponder(actions: SendChannel<PlayerAction>, errors: SendChannel<String>) = GameUIResponderImpl(this, actions, errors)
+
 suspend fun gameMain(side: GlobalSide, token: String, state: GameState) {
        interruptExit = true
        
index 34fe5f4ab71ed5ec3f00d0892ad94f2bee30e235..33ced42060532f0daed88114d3e221dbef464b1e 100644 (file)
@@ -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"
                                                                }
                                                        }