935ff74dbdd74868b9dedda76480e209e9467f68
[browser-rts.git] / src / render / land-development.ts
1 import { BuildQueue } from "../repository/build-queue";
2 import { CityWithLocation } from "../repository/city";
3 import { Building } from '../repository/buildings';
4 import _ from "lodash";
5
6 function progressBar(current, max): string {
7     const percent = Math.ceil((current/max) * 100);
8     return `
9     <div class="progress-bar construction" style="background: background: var(--green-bg);
10     background: linear-gradient(90deg, var(--green-bg) 0%, var(--green-bg) ${percent}%, #000 ${percent}%); border-color: var(--green-border); ">
11     ${percent}%
12     </div>
13     `;
14 }
15
16 export function renderLandDevelopment(city: CityWithLocation, buildings: Building[], buildQueues: BuildQueue[]): string {
17     const freeSpace = city.totalSpace - city.usedSpace;
18     let html = `
19     <div hx-trigger="reload-construction-queue, every 600s" hx-get="/poll/construction">
20     <h2 data-augmented-ui="tl-clip bl-clip none">Construction</h2>
21     <table>
22     <tr>
23         <th>Free Land</th>
24         <th>Current</th>
25         <td colspan="2">${(city.totalSpace - city.usedSpace).toLocaleString()} (${Math.ceil(freeSpace/city.totalSpace * 100)}% available)</td>
26     </tr>
27     ${
28         buildings.map(building => {
29             return `
30             <tr>
31                 <th>${building.display}</th>
32                 <td>${city[building.slug]}</td>
33                 <td>
34                     <form hx-post="/build">
35                         <input type="number" size="6" name="amount" hx-post="/cost/construction" hx-trigger="change" hx-target="#${building.slug}-cost">
36                         <input type="hidden" name="building_type" value="${building.slug}">
37                         <button type="submit">Build</button>
38                     </form>
39                     <span id="${building.slug}-cost"></span>
40                 </td>
41             </tr>
42             `;
43         }).join("\n")
44     }
45     </table>
46     `;
47
48     const quickFindBuilding = _.keyBy(buildings, 'slug');
49
50     const queues = `
51     <hr>
52     <h2 data-augmented-ui="tl-clip bl-clip none">Build Queues</h2>
53     <table>
54     <tr>
55     <th>Building</th>
56     <th>Amount Expected</th>
57     <th>Progress</th>
58     </tr>
59     ${buildQueues.sort((a, b) => {
60         return a.due - b.due;
61     }).map(queue => {
62         const now = Date.now() - queue.created;
63         const duration = queue.due - queue.created;
64
65         return `
66         <tr>
67         <td>${quickFindBuilding[queue.building_type].display}</td>
68         <td>${queue.amount}</td>
69         <td>${progressBar(now, duration)}</td>
70         </tr>
71         `;
72     }).join("\n")}
73     </table>
74     </div>
75     `;
76
77     return html + queues;
78 }