1 import { HttpServer } from './lib/server';
2 import * as config from './config';
3 import { AccountRepository } from './repository/accounts';
4 import { CityRepository } from './repository/city';
5 import { MailRepository } from './repository/mail';
6 import {BadInputError, ERROR_CODE, NotFoundError} from './errors';
7 import { renderKingomOverview } from './render/kingdom-overview';
8 import { renderLandDevelopment } from './render/land-development';
9 import { tick } from './tasks/tick';
10 import { construction } from './tasks/construction';
11 import { unitTraining } from './tasks/unit-training';
12 import { fight } from './tasks/fight';
13 import { renderUnitTraining } from './render/unit-training';
14 import { launchOffensive, listOperations, renderOverworldMap } from './render/map';
15 import { createBullBoard } from '@bull-board/api';
16 import { BullAdapter } from '@bull-board/api/bullAdapter';
17 import _ from 'lodash';
18 import { renderCost } from './render/costs';
19 import {renderMailroom, renderMessage} from './render/mail';
20 import {topbar} from './render/topbar';
21 import {renderPublicChatMessage} from './render/chat-message';
24 const server = new HttpServer(config.API_PORT);
25 const accountRepo = new AccountRepository();
26 const cityRepo = new CityRepository();
27 const mailRepo = new MailRepository();
29 const msgBuffer: string[] = [];
33 new BullAdapter(tick.queue),
34 new BullAdapter(construction.queue),
35 new BullAdapter(unitTraining.queue),
36 new BullAdapter(fight.queue)
38 serverAdapter: server.bullAdapter,
42 body: {username: string, password: string}},
45 ('/accounts', async (req) => {
46 const { username, password} = req.body;
47 if(!username || !password || username.length < 3 || password.length < 3) {
48 throw new BadInputError('Invalid username or password', ERROR_CODE.INVALID_USERNAME);
50 const acct = await accountRepo.create(username, password);
52 // lets create the city!
53 await cityRepo.create(acct.id);
55 return `<div class="alert success">You are all signed up! You can go ahead and log in</div>`;
58 server.post<{body: {username: string, password: string}}, void>('/login', async (req, raw, res) => {
59 const { username, password} = req.body;
60 if(!username || !password || username.length < 3 || password.length < 3) {
61 throw new BadInputError('Invalid username or password', ERROR_CODE.INVALID_USERNAME);
63 const {account, session} = await accountRepo.login(username, password);
65 throw new NotFoundError('Invalid username or password', ERROR_CODE.USER_NOT_FOUND);
68 res.setHeader('hx-redirect', `/game.html?token=${session.id}&id=${session.account_id}`);
78 }}, string>('/attack-power', async req => {
79 const power = await cityRepo.power(req.body);
81 return power.toLocaleString();
85 server.get<{params: { cityId: string }}, string>('/city/:cityId', async req => {
86 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
87 const yourCity = await cityRepo.getUsersCity(account.id);
88 const city = await cityRepo.findById(req.params.cityId);
89 const acct = await accountRepo.FindOne({id: city.owner});
92 return await launchOffensive(city, acct || {
96 }, yourCity, account);
99 server.get<{}, string>('/poll/overview', async req => {
100 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
101 const city = await cityRepo.getUsersCity(account.id);
104 foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
105 foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
106 energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
107 energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
110 return renderKingomOverview({
113 }, account) + topbar({...city, ...usage});
116 server.get<{}, string>('/poll/construction', async req => {
117 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
118 const city = await cityRepo.getUsersCity(account.id);
119 const buildings = await cityRepo.buildingRepository.list();
121 const buildQueues = await cityRepo.getBuildQueues(account.id);
123 foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
124 foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
125 energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
126 energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
128 return renderLandDevelopment(city, buildings, buildQueues) + topbar({...city, ...usage});
131 server.get<{}, string>('/poll/unit-training', async req => {
132 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
133 const city = await cityRepo.getUsersCity(account.id);
135 const unitTrainingQueues = await cityRepo.getUnitTrainingQueues(account.id);
136 const units = await cityRepo.unitRepository.list();
138 foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
139 foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
140 energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
141 energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
144 return renderUnitTraining(city, units, unitTrainingQueues) + topbar({
150 server.post<{body: {sector: string}}, string>('/poll/map', async req => {
151 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
152 const city = await cityRepo.getUsersCity(account.id);
154 let sector = city.sector_id;
155 if(req.body.sector) {
157 sector = parseInt(req.body.sector);
160 sector = city.sector_id;
165 foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
166 foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
167 energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
168 energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
171 return renderOverworldMap(await cityRepo.findAllInSector(sector), city, sector) + topbar({
177 server.get<{}, string>('/poll/mailroom', async req => {
178 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
179 const city = await cityRepo.getUsersCity(account.id);
182 foodUsagePerTick: await cityRepo.foodUsagePerTick(city),
183 foodProductionPerTick: await cityRepo.foodProductionPerTick(city),
184 energyUsagePerTick: await cityRepo.energyUsagePerTick(city),
185 energyProductionPerTick: await cityRepo.energyProductionPerTick(city)
188 return renderMailroom(await mailRepo.listReceivedMessages(account.id)) + topbar({
198 building_type: string
200 }, string>('/cost/construction', async req => {
201 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
202 const city = await cityRepo.getUsersCity(account.id);
204 const amount = parseInt(req.body.amount.trim(), 10);
206 if(isNaN(amount) || amount < 1) {
209 const building = await cityRepo.buildingRepository.findBySlug(req.body.building_type);
212 throw new NotFoundError(`Invalid building type ${req.body.building_type}`, ERROR_CODE.INVALID_BUILDING);
216 credits: building.credits * amount,
217 alloys: building.alloys * amount,
218 energy: building.energy * amount,
219 land: building.land * amount,
223 return renderCost(cost, city);
231 }, string>('/cost/training', async req => {
232 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
233 const city = await cityRepo.getUsersCity(account.id);
234 const amount = parseInt(req.body.amount, 10);
236 if(isNaN(amount) || amount < 1) {
240 const unit = await cityRepo.unitRepository.findBySlug(req.body.type);
242 throw new NotFoundError(`Invalid unit type ${req.body.type}`, ERROR_CODE.INVALID_UNIT);
246 population: unit.population * amount,
247 soldiers: unit.soldiers * amount,
248 attackers: unit.attackers * amount,
249 defenders: unit.defenders * amount,
250 credits: unit.credits * amount,
251 food: unit.food * amount,
252 time: unit.time * amount
259 building_type: string,
261 }, void>('/build', async req => {
262 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
263 const city = await cityRepo.getUsersCity(account.id);
265 const amount = parseInt(req.body.amount, 10);
267 throw new BadInputError('Please specify an amount > 0', ERROR_CODE.INVALID_AMOUNT);
269 const building = await cityRepo.buildingRepository.findBySlug(req.body.building_type);
272 throw new NotFoundError(`Invalid building type ${req.body.building_type}`, ERROR_CODE.INVALID_BUILDING);
275 const queueData = await cityRepo.buildBuilding(building, amount, city);
277 construction.trigger(queueData, { delay: queueData.due - Date.now() });
278 }, 'reload-construction-queue');
287 >('/units', async req => {
288 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
289 const city = await cityRepo.getUsersCity(acct.id);
291 const amount = parseInt(req.body.amount, 10) || 0;
292 console.log('request amount?!', amount);
294 throw new BadInputError('Please specify an amount > 0', ERROR_CODE.INVALID_AMOUNT);
296 const unit = await cityRepo.unitRepository.findBySlug(req.body.type);
299 throw new NotFoundError(`Invalid unit type ${req.body.type}`, ERROR_CODE.INVALID_UNIT);
302 const queueData = await cityRepo.train(unit, amount, city);
303 unitTraining.trigger(queueData, { delay: queueData.due - Date.now() });
305 }, 'reload-unit-training');
313 sp_attackers: string,
318 >('/attack', async req => {
319 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
320 const city = await cityRepo.getUsersCity(acct.id);
321 const attackedCity = await cityRepo.findById(req.body.city);
324 soldiers: parseInt(req.body.soldiers),
325 attackers: parseInt(req.body.attackers),
326 defenders: parseInt(req.body.defenders),
327 sp_attackers: parseInt(req.body.sp_attackers),
328 sp_defenders: parseInt(req.body.sp_defenders)
331 const armyQueue = await cityRepo.attack(city, attackedCity, army);
333 fight.trigger(armyQueue, {
334 delay: armyQueue.due - Date.now()
336 }, 'reload-outgoing-attacks');
338 server.get<void, string>('/messages', async req => {
339 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
340 const msgs = await mailRepo.listReceivedMessages(acct.id);
342 return JSON.stringify(msgs);
345 server.get<{params: {id: string}}, string>('/messages/:id', async req => {
346 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
347 const msg = await mailRepo.getMessage(req.params.id, acct.id);
350 throw new NotFoundError('No such message', ERROR_CODE.DUPLICATE_CACHE_KEY);
353 await mailRepo.markAsRead(msg.id, msg.to_account);
355 return renderMessage(msg);
358 server.get<void, string>('/attacks/outgoing', async req => {
359 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
360 const city = await cityRepo.getUsersCity(acct.id);
361 const attacks = await cityRepo.armyRepository.listOutgoing(city.id);
363 return listOperations(attacks);
366 server.post<{body: {message: string}}, void>('/chat', async req => {
367 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
368 const now = Date.now();
370 if(!_.isEmpty(req.body.message)) {
371 const msg = renderPublicChatMessage(acct.username, req.body.message);
372 server.ws.emit('/chat-message', msg);
373 msgBuffer.unshift(msg);
374 while(msgBuffer.length > 30) {
382 server.get<void, string>('/server-stats', async req => {
383 const date = new Date();
385 <div class="text-right">
386 <span class="success-text">${(await server.ws.allSockets()).size} Online</span><br>
388 Server Time: ${date.getHours()}:${date.getMinutes()}
393 server.ws.on('connection', async socket => {
394 const auth = server.authFromUrl(socket.request.headers['referer']);
395 const acct = await accountRepo.validate(auth.authInfo.accountId, auth.authInfo.token);
397 server.ws.emit('/chat-message', msgBuffer.join("\n"));
399 server.ws.emit('/chat-message', renderPublicChatMessage('Server', `${acct.username} logged in`));