allow cancelling of training queues
[browser-rts.git] / src / api.ts
index 4509bec431ad8d616bc0946d2fa2c0933268b2da..66381943a40e55598146862be56975ebd26dbbad 100644 (file)
@@ -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<City> = {
+    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<void, string>('/server-stats', async req => {
   const date = new Date();
   return `