From 23651f2cfe342a32503dec70f56f8ffaf03f2616 Mon Sep 17 00:00:00 2001 From: xangelo Date: Mon, 6 Jun 2022 16:29:08 -0400 Subject: [PATCH] allow cancelling of training queues When cancelling a training queue you end up with a minimum of 20% loss on resources (credits/food/alloy/energy), 0% loss of people. The 20% loss is based on the percentage completion.. after 20% completion, you get the inverse of resources back. IE: 40% completion means you get 60% of your resources back. IE: 99% completion means you get 1% of your resources back --- src/api.ts | 43 +++++++++++++++++++++++++++++++++++++ src/render/unit-training.ts | 9 ++++---- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/api.ts b/src/api.ts index 4509bec..6638194 100644 --- a/src/api.ts +++ b/src/api.ts @@ -423,6 +423,49 @@ server.post<{params: {queueId: string}}, void>('/construction/:queueId/cancel', }, 'reload-construction-queue'); +server.post<{params: {queueId: string}}, void>('/training/:queueId/cancel', async req => { + const acct = await accountRepo.validate(req.authInfo.accountId, req.authInfo.token); + const city = await cityRepo.getUsersCity(acct.id); + + if(!validator.isUUID(req.params.queueId)) { + throw new BadInputError('Invalid queue ID', ERROR_CODE.INVALID_BUILD_QUEUE); + } + + const queue = await cityRepo.unitTrainigQueue.FindOne({owner: city.owner, id: req.params.queueId}); + if(!queue) { + throw new NotFoundError('That queue does not exist', ERROR_CODE.INVALID_BUILD_QUEUE); + } + + const [, unit] = await Promise.all([ + cityRepo.unitTrainigQueue.Delete({ + owner: city.owner, + id: req.params.queueId + }), + cityRepo.unitRepository.FindOne({slug: queue.unit_type}) + ]); + + // now that it's deleted we can give the player back some percentage + // of resources based on how close they are to completion. + const diff = (queue.due - Date.now()) / (queue.due - queue.created); + // force a 20% loss minimum + const finalDiff = diff < 0.2 ? 0.2 : diff; + + const costReturn: Partial = { + id: city.id, + credits: city.credits + Math.floor(unit.credits * queue.amount * diff), + food: city.food + Math.floor(unit.food * queue.amount * diff), + population: city.population + Math.floor(unit.population * queue.amount), + soldiers: city.soldiers + Math.floor(unit.soldiers * queue.amount), + attackers: city.attackers + Math.floor(unit.attackers * queue.amount), + defenders: city.defenders + Math.floor(unit.attackers * queue.amount) + }; + + console.log('update', costReturn) + + await cityRepo.save(costReturn); + +}, 'reload-unit-training'); + server.get('/server-stats', async req => { const date = new Date(); return ` diff --git a/src/render/unit-training.ts b/src/render/unit-training.ts index a1bd4b8..9e64c9c 100644 --- a/src/render/unit-training.ts +++ b/src/render/unit-training.ts @@ -117,7 +117,7 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini Unit Type Amount Expected - Progress + Progress ${sortedTrainingQueue.map(queue => { if(queue.unit_type === 'empty') { @@ -135,10 +135,9 @@ export function renderUnitTraining(city: CityWithLocation, units: Unit[], traini ${queue.display} ${queue.amount} - - - ${progressBar(now, duration)}
-
+ ${progressBar(now, duration)} + + × `; -- 2.25.1