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/fight';
15 import { createBullBoard } from '@bull-board/api';
16 import { BullAdapter } from '@bull-board/api/bullAdapter';
17 import _ from 'lodash';
18 import { renderMailroom, renderMessage } from './render/mail';
20 const server = new HttpServer(config.API_PORT);
22 const accountRepo = new AccountRepository();
23 const cityRepo = new CityRepository();
24 const mailRepo = new MailRepository();
28 new BullAdapter(tick.queue),
29 new BullAdapter(construction.queue),
30 new BullAdapter(unitTraining.queue),
31 new BullAdapter(fight.queue)
33 serverAdapter: server.bullAdapter,
37 body: {username: string, password: string}},
40 ('/accounts', async (req) => {
41 const { username, password} = req.body;
42 if(!username || !password || username.length < 3 || password.length < 3) {
43 throw new BadInputError('Invalid username or password', ERROR_CODE.INVALID_USERNAME);
45 const acct = await accountRepo.create(username, password);
47 // lets create the city!
48 await cityRepo.create(acct.id);
50 return `<div class="alert success">You are all signed up! You can go ahead and log in</div>`;
53 server.post<{body: {username: string, password: string}}, void>('/login', async (req, raw, res) => {
54 const { username, password} = req.body;
55 if(!username || !password || username.length < 3 || password.length < 3) {
56 throw new BadInputError('Invalid username or password', ERROR_CODE.INVALID_USERNAME);
58 const {account, session} = await accountRepo.login(username, password);
60 throw new NotFoundError('Invalid username or password', ERROR_CODE.USER_NOT_FOUND);
63 res.setHeader('hx-redirect', `/game.html?token=${session.id}&id=${session.account_id}`);
73 }}, string>('/attack-power', async req => {
74 const power = await cityRepo.power(req.body);
76 return power.toLocaleString();
80 server.get<{params: { cityId: string }}, string>('/city/:cityId', async req => {
81 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
82 const yourCity = await cityRepo.getUsersCity(account.id);
83 const city = await cityRepo.findById(req.params.cityId);
84 const acct = await accountRepo.FindOne({id: city.owner});
87 return await launchOffensive(city, acct || {
91 }, yourCity, account);
94 server.get<{}, string>('/poll/overview', async req => {
95 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
96 const city = await cityRepo.getUsersCity(account.id);
98 return renderKingomOverview(city, account);
101 server.get<{}, string>('/poll/construction', async req => {
102 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
103 const city = await cityRepo.getUsersCity(account.id);
104 const buildings = await cityRepo.buildingRepository.list();
106 const buildQueues = await cityRepo.getBuildQueues(account.id);
107 return renderLandDevelopment(city, buildings, buildQueues);
110 server.get<{}, string>('/poll/unit-training', async req => {
111 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
112 const city = await cityRepo.getUsersCity(account.id);
114 const unitTrainingQueues = await cityRepo.getUnitTrainingQueues(account.id);
115 const units = await cityRepo.unitRepository.list();
117 return renderUnitTraining(city, units, unitTrainingQueues);
120 server.get<{}, string>('/poll/map', async req => {
121 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
122 const city = await cityRepo.getUsersCity(account.id);
124 return renderOverworldMap(await cityRepo.findAllInSector(city.sector_id), city);
127 server.get<{}, string>('/poll/mailroom', async req => {
128 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
130 return renderMailroom(await mailRepo.listReceivedMessages(account.id));
137 building_type: string
139 }, string>('/cost/construction', async req => {
140 const amount = parseInt(req.body.amount.trim(), 10);
141 console.log('checking amount', amount);
143 if(isNaN(amount) || amount < 1) {
146 const building = await cityRepo.buildingRepository.findBySlug(req.body.building_type);
149 throw new NotFoundError(`Invalid building type ${req.body.building_type}`, ERROR_CODE.INVALID_BUILDING);
152 return JSON.stringify({
153 gold: building.gold * amount,
154 ore: building.ore * amount,
155 logs: building.logs * amount,
156 land: building.land * amount,
166 }, string>('/cost/training', async req => {
167 const amount = parseInt(req.body.amount, 10);
168 const unit = await cityRepo.unitRepository.findBySlug(req.body.type);
171 throw new NotFoundError(`Invalid unit type ${req.body.type}`, ERROR_CODE.INVALID_UNIT);
174 return JSON.stringify({
175 population: unit.population * amount,
176 soldiers: unit.soldiers * amount,
177 attackers: unit.attackers * amount,
178 defenders: unit.defenders * amount,
179 gold: unit.gold * amount,
180 bushels: unit.bushels * amount
187 building_type: string,
189 }, void>('/build', async req => {
190 const account = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
191 const city = await cityRepo.getUsersCity(account.id);
193 const amount = parseInt(req.body.amount, 10);
194 const building = await cityRepo.buildingRepository.findBySlug(req.body.building_type);
197 throw new NotFoundError(`Invalid building type ${req.body.building_type}`, ERROR_CODE.INVALID_BUILDING);
200 const queueData = await cityRepo.buildBuilding(building, amount ,city);
202 construction.trigger(queueData, { delay: queueData.due });
203 }, 'reload-construction-queue');
212 >('/units', async req => {
213 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
214 const city = await cityRepo.getUsersCity(acct.id);
216 const amount = parseInt(req.body.amount, 10);
217 const unit = await cityRepo.unitRepository.findBySlug(req.body.type);
220 throw new NotFoundError(`Invalid unit type ${req.body.type}`, ERROR_CODE.INVALID_UNIT);
223 const queueData = await cityRepo.train(unit, amount, city);
224 unitTraining.trigger(queueData, { delay: queueData.due });
226 }, 'reload-unit-training');
234 sp_attackers: string,
239 >('/attack', async req => {
240 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
241 const city = await cityRepo.getUsersCity(acct.id);
242 const attackedCity = await cityRepo.findById(req.body.city);
245 soldiers: parseInt(req.body.soldiers),
246 attackers: parseInt(req.body.attackers),
247 defenders: parseInt(req.body.defenders),
248 sp_attackers: parseInt(req.body.sp_attackers),
249 sp_defenders: parseInt(req.body.sp_defenders)
252 const armyQueue = await cityRepo.attack(city, attackedCity, army);
254 fight.trigger(armyQueue, {
255 delay: armyQueue.due - Date.now()
257 }, 'reload-outgoing-attacks');
259 server.get<void, string>('/messages', async req => {
260 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
261 const msgs = await mailRepo.listReceivedMessages(acct.id);
263 return JSON.stringify(msgs);
266 server.get<{params: {id: string}}, string>('/messages/:id', async req => {
267 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
268 const msg = await mailRepo.getMessage(req.params.id, acct.id);
271 throw new NotFoundError('No such message', ERROR_CODE.DUPLICATE_CACHE_KEY);
274 await mailRepo.markAsRead(msg.id, msg.to_account);
276 return renderMessage(msg);
279 server.get<void, string>('/attacks/outgoing', async req => {
280 const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token);
281 const city = await cityRepo.getUsersCity(acct.id);
282 const attacks = await cityRepo.armyRepository.listOutgoing(city.id);
284 return listOperations(attacks);