initial commit
[browser-rts.git] / src / render / land-development.ts
1 import { BuildQueue } from "../repository/build-queue";
2 import { City } 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: rgb(0,102,0);
10     background: linear-gradient(90deg, rgba(0,102,0,1) 0%, rgba(0,102,0,1) ${percent}%, rgba(0,0,0,1) ${percent}%);">
11     ${percent}%
12     </div>
13     `;
14 }
15
16 export function renderLandDevelopment(city: City, 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="/queue/construction">
20     <table>
21     <tr>
22         <th>Free Land</th>
23         <th>Current</th>
24         <td colspan="2">${(city.totalSpace - city.usedSpace).toLocaleString()} (${Math.ceil(freeSpace/city.totalSpace * 100)}% available)</td>
25     </tr>
26     ${
27         buildings.map(building => {
28             return `
29             <tr>
30                 <th>${building.display}</th>
31                 <td>${city[building.slug]}</td>
32                 <td>
33                     <form hx-post="/build">
34                         <input type="number" name="amount" hx-post="/cost/construction" hx-trigger="change" hx-target="#${building.slug}-cost">
35                         <input type="hidden" name="building_type" value="${building.slug}">
36                         <button type="submit">Build</button>
37                     </form>
38                     <span id="${building.slug}-cost"></span>
39                 </td>
40             </tr>
41             `;
42         }).join("\n")
43     }
44     </table>
45     `;
46
47     const quickFindBuilding = _.keyBy(buildings, 'slug');
48
49     const queues = `
50     <hr>
51     <h2>Build Queues</h2>
52     <table>
53     <tr>
54     <th>Building</th>
55     <th>Amount Expected</th>
56     <th>Progress</th>
57     </tr>
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 }