Complete typed combat command parity

ober

0531590115c8975abda8c390535f1d1a55924c59

diff --git a/templates/original-tactics-parts/MainActivity.kt.ss b/templates/original-tactics-parts/MainActivity.kt.ss
index c1e853b..3dfaceb 100644
--- a/templates/original-tactics-parts/MainActivity.kt.ss
+++ b/templates/original-tactics-parts/MainActivity.kt.ss
@@ -1346,13 +1346,17 @@
           (def (commitAndFire) : Unit (modifiers private)
             (let ((game (maSelfGameOrStatus this)))
               (if (nullable-null? game) (begin)
-                  (maSelfPost this
-                    (maJsonPutObject
-                      (maJsonPutString (maJsonObject) "op" "run-to-fire")
-                      "game" (nullable-get game))
-                    "Committing orders..."
-                    (lambda ((response : JSONObject))
-                      (maSelfHandleResponse this response "Committed and fired" #t #t))))))
+                  (let ((request (maSelfBuildOrders this (nullable-get game))))
+                    (maSelfPost this request "Committing orders..."
+                      (lambda ((response : JSONObject))
+                        (begin
+                          (maSelfHandleResponse this response "Orders staged" #t #t)
+                          (let ((nextGame
+                                  (json-object-opt-json-object response "game")))
+                            (if (nullable-null? nextGame) (begin)
+                                (runGameToFireWindow
+                                  (nullable-get nextGame)
+                                  "Committed and fired"))))))))))
           (def (fireWeapons) : Unit (modifiers private)
             (maSelfPostCurrent this "fire-weapons" "Resolving fire..." "Fire resolved"))
           (def (advanceImpulse) : Unit (modifiers private)
@@ -1363,7 +1367,7 @@
                   (maSelfPost this
                     (maJsonPutInt
                       (maJsonPutObject
-                        (maJsonPutString (maJsonObject) "op" "run-combat")
+                        (maJsonPutString (maJsonObject) "op" "run-api-combat")
                         "game" (nullable-get game))
                       "max-impulses"
                       (maIntFromText runImpulsesInput (int32 8) (int32 1) (int32 128)))
@@ -1966,15 +1970,27 @@
                           (begin)))
                       found)))))
           (def (runToFire) : Unit (modifiers private)
-            (maSelfPostCurrent this "run-to-fire" "Running to fire window..." "At fire window"))
+            (let ((game (maSelfGameOrStatus this)))
+              (if (nullable-null? game) (begin)
+                  (runGameToFireWindow (nullable-get game) "At fire window"))))
           (def (runGameToFireWindow (game : JSONObject) (title : String)) : Unit
             (modifiers private)
-            (maSelfPost this
-              (maJsonPutObject
-                (maJsonPutString (maJsonObject) "op" "run-to-fire") "game" game)
-              title
-              (lambda ((response : JSONObject))
-                (maSelfHandleResponse this response title #t #t))))
+            (let ((current
+                    (json-object-opt-int32-default game "impulse" (int32 0)))
+                  (orders (json-object-opt-json-object game "orders")))
+              (let ((fire (if (nullable-null? orders) (int32 32)
+                              (json-object-opt-int32-default
+                                (nullable-get orders) "fire-impulse" (int32 32)))))
+                (if (and (= current fire)
+                         (not (json-object-opt-bool-default game "fired?" #f)))
+                    (maSelfPost this
+                      (maJsonPutObject
+                        (maJsonPutString (maJsonObject) "op" "fire-weapons")
+                        "game" game)
+                      "Resolving fire..."
+                      (lambda ((response : JSONObject))
+                        (maSelfHandleResponse this response title #t #t)))
+                    (runCombatSteps game (stepsToFireWindow game) title)))))
           (def (stepsToFireWindow (game : JSONObject)) : Int32
             (modifiers private)
             (let ((current (json-object-opt-int32-default game "impulse" (int32 0)))
@@ -1987,14 +2003,22 @@
                   (if (< steps (int32 1)) (int32 1)
                       (if (> steps (int32 64)) (int32 64) steps))))))
           (def (runToTurnEnd) : Unit (modifiers private)
-            (maSelfPostCurrent this "run-to-turn-end" "Running turn..." "Turn complete"))
+            (let ((game (maSelfGameOrStatus this)))
+              (if (nullable-null? game) (begin)
+                  (let ((current
+                          (json-object-opt-int32-default
+                            (nullable-get game) "impulse" (int32 0))))
+                    (runCombatSteps (nullable-get game)
+                      (if (< current (int32 32)) (- (int32 32) current)
+                          (int32 32))
+                      "At turn end")))))
           (def (runCombatSteps (game : JSONObject) (maxImpulses : Int32)
                                (title : String)) : Unit
             (modifiers private)
             (maSelfPost this
               (maJsonPutInt
                 (maJsonPutObject
-                  (maJsonPutString (maJsonObject) "op" "run-combat") "game" game)
+                  (maJsonPutString (maJsonObject) "op" "run-api-combat") "game" game)
                 "max-impulses" maxImpulses)
               title
               (lambda ((response : JSONObject))
@@ -2097,23 +2121,85 @@
           (def (setFireGroup (actor : String) (input : EditText)
                              (group : Int32)) : Unit
             (modifiers private)
-            (begin
-              (maEditSetText input (fireGroupName group))
-              (maSelfSetStatus this
-                (string-append (actorLabel actor)
-                  (string-append " " (fireGroupName group))))))
+            (if (nullable-null? currentGame)
+                (maSelfSetStatus this "Start a new game first")
+                (let ((battle
+                        (json-object-opt-json-object
+                          (nullable-get currentGame) "battle-state")))
+                  (if (nullable-null? battle)
+                      (maSelfSetStatus this "Start a new game first")
+                      (let ((ship
+                              (json-object-opt-json-object
+                                (nullable-get battle) actor)))
+                        (if (nullable-null? ship)
+                            (maSelfSetStatus this "Start a new game first")
+                            (let ((text (fireGroupText (nullable-get ship) group)))
+                              (if (string-blank? text)
+                                  (maSelfSetStatus this
+                                    (string-append (actorLabel actor)
+                                      (string-append " has no "
+                                        (string-append
+                                          (fireGroupName group) " weapons"))))
+                                  (begin
+                                    (maEditSetText input text)
+                                    (let ((check (directFireCheckFor actor)))
+                                      (if (nullable-null? check) (begin)
+                                          (maCheckedSet (nullable-get check) #t)))
+                                    (maSelfSetStatus this
+                                      (string-append (actorLabel actor)
+                                        (string-append " "
+                                          (string-append
+                                            (fireGroupName group) " selected")))))))))))))
           (def (directFireCheckFor (actor : String)) : (Nullable CheckBox)
             (modifiers private)
             (if (equal? actor "alpha") (nullable-some alphaDirectFireCheck)
                 (if (equal? actor "beta") (nullable-some betaDirectFireCheck)
                     (nullable-none CheckBox))))
           (def (fireGroupText (ship : JSONObject) (group : Int32)) : String
-            (modifiers private) (fireGroupName group))
+            (modifiers private)
+            (let ((counter (selectableWeaponCounter ship)))
+              (if (nullable-null? counter) ""
+                  (let ((weapons (nullable-get counter))
+                        (keys (maJsonKeys (nullable-get counter)))
+                        (out (maStringBuilder)))
+                    (var ((first #t))
+                      (begin
+                        (while (maIteratorHasNext keys)
+                          (begin
+                            (let ((weapon (maIteratorNext keys)))
+                              (let ((count
+                                      (json-object-opt-int32-default
+                                        weapons weapon (int32 0))))
+                                (let ((include
+                                        (and (> count (int32 0))
+                                             (if (= group FIRE_GROUP_HEAVY)
+                                                 (not (isPhaserWeapon weapon))
+                                                 (if (= group FIRE_GROUP_PHASER)
+                                                     (isPhaserWeapon weapon) #t)))))
+                                  (if include
+                                      (begin
+                                        (if first (set! first #f)
+                                            (begin (maStringBuilderAppend out " ")
+                                                   (begin)))
+                                        (maStringBuilderAppend out weapon)
+                                        (maStringBuilderAppend out ":1")
+                                        (if (> count (int32 1))
+                                            (begin
+                                              (maStringBuilderAppend out "-")
+                                              (maStringBuilderAppend out
+                                                (int32->string count))
+                                              (begin))
+                                            (begin)))
+                                      (begin)))))
+                            (begin)))
+                        (maStringBuilderText out)))))))
           (def (selectableWeaponCounter (ship : JSONObject)) : (Nullable JSONObject)
             (modifiers private)
             (let ((armed (json-object-opt-json-object ship "armed-weapons")))
               (if (nullable-null? armed)
-                  (json-object-opt-json-object ship "weapons") armed)))
+                  (json-object-opt-json-object ship "weapons")
+                  (if (<= (counterTotal (nullable-get armed)) (int32 0))
+                      (json-object-opt-json-object ship "weapons") armed))))
           (def (counterTotal (counter : JSONObject)) : Int32
             (modifiers private)
             (let ((keys (maJsonKeys counter)))
@@ -3660,7 +3746,7 @@
             (modifiers private)
             (begin
               (setStatus "Creating Andro fleet...")
-              (startPreset "andro-tkc" betaId)))
+              (startPreset "andromedan-tkc" betaId)))
           (def (createNewGameNow (alphaId : String) (betaId : String)) : Unit
             (modifiers private)
             (begin))