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>()
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)
}
}
- 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)
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)
}
}
+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
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) {
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 "
}
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
}
}
ShipAttacker.Bombers -> {
- if (owner == LocalSide.RED)
- +"our "
+ +if (owner == LocalSide.RED)
+ "our "
else
- +"enemy "
+ "enemy "
+"bombers"
}
}