building_type: string
}
}, string>('/cost/construction', async req => {
+ const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
+ const city = await cityRepo.getUsersCity(account.id);
+
const amount = parseInt(req.body.amount.trim(), 10);
if(isNaN(amount) || amount < 1) {
time: building.time
};
- return renderCost(cost);
+ return renderCost(cost, city);
});
server.post<{
type: string;
}
}, string>('/cost/training', async req => {
+ const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
+ const city = await cityRepo.getUsersCity(account.id);
const amount = parseInt(req.body.amount, 10);
if(isNaN(amount) || amount < 1) {
credits: unit.credits * amount,
food: unit.food * amount,
time: unit.time * amount
- });
+ }, city);
});
server.post<{
+import {City} from "../repository/city";
+
type Cost = {
credits: number,
time: number,
defenders?: number
}
-function lineItem(display: string, value: number): string {
+function lineItem(display: string, value: number, available: number): string {
+ let className = '';
+
+ if(available <= value && available >= 0) {
+ className = 'danger-text';
+ }
+ else if(available >= value) {
+ className = 'success-text';
+ }
return `
<div>
<b>${display}:</b>
- <span class="text">${value.toLocaleString()}</span>
+ <span class="text ${className}">${value.toLocaleString()}</span>
</div>
`;
}
-export function renderCost(cost: Cost): string {
+export function renderCost(cost: Cost, city: City): string {
const costAsArray = [];
- costAsArray.push(lineItem('Credits', cost.credits));
- costAsArray.push(lineItem('Time', cost.time));
+ costAsArray.push(lineItem('Credits', cost.credits, city.credits));
+ costAsArray.push(lineItem('Time', cost.time, -1));
if(cost.alloys) {
- costAsArray.push(lineItem('Alloys', cost.alloys));
+ costAsArray.push(lineItem('Alloys', cost.alloys, city.alloys));
}
if(cost.food) {
- costAsArray.push(lineItem('Food', cost.food));
+ costAsArray.push(lineItem('Food', cost.food, city.food));
}
if(cost.energy) {
- costAsArray.push(lineItem('Energy', cost.energy));
+ costAsArray.push(lineItem('Energy', cost.energy, city.energy));
}
if(cost.land) {
- costAsArray.push(lineItem('Space', cost.land));
+ costAsArray.push(lineItem('Space', cost.land, city.totalSpace - city.usedSpace));
}
if(cost.population) {
- costAsArray.push(lineItem('Pop', cost.population));
+ costAsArray.push(lineItem('Pop', cost.population, city.population));
}
if(cost.soldiers) {
- costAsArray.push(lineItem('Soldiers', cost.soldiers));
+ costAsArray.push(lineItem('Soldiers', cost.soldiers, city.soldiers));
}
if(cost.attackers) {
- costAsArray.push(lineItem('Attackers', cost.attackers));
+ costAsArray.push(lineItem('Attackers', cost.attackers, city.attackers));
}
if(cost.defenders) {
- costAsArray.push(lineItem('Defenders', cost.defenders));
+ costAsArray.push(lineItem('Defenders', cost.defenders, city.defenders));
}
<td>${city.soldiers}</td>
<td>
<form hx-post="/units">
- <input type="number" name="amount" size="6" max="${city.soldiers}" hx-trigger="change" hx-post="/cost/training" hx-target="#${unit.attackers.slug}-cost">
+ <input type="number" name="amount" size="6" max="${city.soldiers}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.attackers.slug}-cost">
<input type="hidden" name="type" value="${unit.attackers.slug}">
<button type="submit" ${city.soldiers ? '' : 'disabled'}>Train</button>
</form>
<td>${city.soldiers}</td>
<td>
<form hx-post="/units">
- <input type="number" name="amount" size="6" max="${city.soldiers}" hx-trigger="change" hx-post="/cost/training" hx-target="#${unit.defenders.slug}-cost">
+ <input type="number" name="amount" size="6" max="${city.soldiers}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.defenders.slug}-cost">
<input type="hidden" name="type" value="${unit.defenders.slug}">
<button type="submit" ${city.soldiers ? '' : 'disabled'}>Train</button>
</form>
<td>${city.attackers}</td>
<td>
<form hx-post="/units">
- <input type="number" name="amount" size="6" max="${city.attackers}" hx-trigger="change" hx-post="/cost/training" hx-target="#${unit.sp_attackers.slug}-cost">
+ <input type="number" name="amount" size="6" max="${city.attackers}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.sp_attackers.slug}-cost">
<input type="hidden" name="type" value="${unit.sp_attackers.slug}">
<button type="submit" ${city.attackers ? '': 'disabled'}>Train</button>
</form>
<td>${city.defenders}</td>
<td>
<form hx-post="/units">
- <input type="number" name="amount" size="6" max="${city.defenders}" hx-trigger="change" hx-post="/cost/training" hx-target="#${unit.sp_defenders.slug}-cost">
+ <input type="number" name="amount" size="6" max="${city.defenders}" hx-trigger="keyup" hx-post="/cost/training" hx-target="#${unit.sp_defenders.slug}-cost">
<input type="hidden" name="type" value="${unit.sp_defenders.slug}">
<button type="submit" ${city.defenders ? '': 'disabled'}>Train</button>
</form>