bugfix: training units overwrites their values
authorxangelo <git@xangelo.ca>
Thu, 26 May 2022 17:34:39 +0000 (13:34 -0400)
committerxangelo <git@xangelo.ca>
Thu, 26 May 2022 17:34:44 +0000 (13:34 -0400)
When training units it was writing all unit values to null resulting in
you being unable to train any units.

src/render/unit-training.ts
src/repository/city.ts

index 351bc7930abb364ea7e77d1a71878af3bcf64592..686b57b16c86bed51b6ba7fc0b74eff256828836 100644 (file)
@@ -27,7 +27,7 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini
     </tr>
     <tr>
         <th>Soldiers</th>
-        <td>${city.population}</td>
+        <td>${city.soldiers}</td>
         <td>
             <form hx-post="/units">
                 <input type="number" name="amount" size="6" max="${city.population}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.soldiers.slug}-cost">
@@ -39,7 +39,7 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini
     </tr>
     <tr>
         <th>Attackers</th>
-        <td>${city.soldiers}</td>
+        <td>${city.attackers}</td>
         <td>
             <form hx-post="/units">
                 <input type="number" name="amount" size="6" max="${city.soldiers}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.attackers.slug}-cost">
@@ -51,7 +51,7 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini
     </tr>
     <tr>
         <th>Defenders</th>
-        <td>${city.soldiers}</td>
+        <td>${city.defenders}</td>
         <td>
             <form hx-post="/units">
                 <input type="number" name="amount" size="6" max="${city.soldiers}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.defenders.slug}-cost">
@@ -63,7 +63,7 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini
     </tr>
     <tr>
         <th>Special Attackers</th>
-        <td>${city.attackers}</td>
+        <td>${city.sp_attackers}</td>
         <td>
             <form hx-post="/units">
                 <input type="number" name="amount" size="6" max="${city.attackers}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.sp_attackers.slug}-cost">
@@ -76,7 +76,7 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini
     </tr>
     <tr>
         <th>Special Defenders</th>
-        <td>${city.defenders}</td>
+        <td>${city.sp_defenders}</td>
         <td>
             <form hx-post="/units">
                 <input type="number" name="amount" size="6" max="${city.defenders}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.sp_defenders.slug}-cost">
index 168053c473b80c942c38f5c0aa4cac31ec6754e8..f49b1db9d09226a8ac8fa46536867c28be588e82 100644 (file)
@@ -265,6 +265,9 @@ where l.sector_id = ?`, [sector_id]);
             throw new InsufficientResourceError('defenders', unit.defenders, city.defenders);
         }
 
+        console.log(city);
+        console.log(unit, amount);
+
         // validate that they have enough of the buildings to support this
 
         // ok they have everything, lets update their city 
@@ -272,10 +275,12 @@ where l.sector_id = ?`, [sector_id]);
 
         city.credits -= unit.credits * amount;
         city.food -= unit.food * amount;
-        city.population -= coalesce(unit.population, 0) * amount;
-        city.soldiers -= coalesce(unit.soldiers, 0) * amount;
-        city.attackers -= coalesce(unit.attackers, 0) * amount;
-        city.defenders -= coalesce(unit.defenders, 0) * amount;
+        city.population -= unit.population * amount;
+        city.soldiers -= unit.soldiers * amount;
+        city.attackers -= unit.attackers * amount;
+        city.defenders -= unit.defenders * amount;
+
+        console.log(city);
 
         await this.save(city);