Fix pushing new build tasks

This commit is contained in:
Anton Vakhrushev 2020-04-20 22:21:26 +03:00
parent 4ff55b5850
commit 58b65aff82
3 changed files with 45 additions and 27 deletions

View File

@ -16,16 +16,18 @@ export function clickBuildButton(typeId: number) {
btn.trigger('click'); btn.trigger('click');
} }
export function createBuildButton(onClickHandler: (buildTypeId: number) => void) { export function createBuildButton(onClickHandler: (buildTypeId: number, resources: Resources) => void) {
const $els = jQuery('[id^=contract_building]'); const $els = jQuery('[id^=contract_building]');
$els.each((idx, el) => { $els.each((idx, el) => {
const $el = jQuery(el); const $el = jQuery(el);
const id = getNumber(trimPrefix($el.attr('id') || '', 'contract_building')); const buildTypeId = getNumber(trimPrefix($el.attr('id') || '', 'contract_building'));
const btnId = uniqId(); const btnId = uniqId();
const resElement = $el.find('.resourceWrapper .resource');
const resources = grabResourcesFromList(resElement);
$el.append(`<div style="padding: 8px"><a id="${btnId}" href="#">Построить</a></div>`); $el.append(`<div style="padding: 8px"><a id="${btnId}" href="#">Построить</a></div>`);
jQuery(`#${btnId}`).on('click', evt => { jQuery(`#${btnId}`).on('click', evt => {
evt.preventDefault(); evt.preventDefault();
onClickHandler(id); onClickHandler(buildTypeId, resources);
}); });
}); });
} }
@ -43,14 +45,15 @@ export function clickUpgradeButton() {
btn.trigger('click'); btn.trigger('click');
} }
export function createUpgradeButton(onClickHandler: () => void) { export function createUpgradeButton(onClickHandler: (resources: Resources) => void) {
const id = uniqId(); const id = uniqId();
jQuery('.upgradeButtonsContainer .section1').append( jQuery('.upgradeButtonsContainer .section1').append(
`<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>` `<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>`
); );
const resources = grabContractResources();
jQuery(`#${id}`).on('click', evt => { jQuery(`#${id}`).on('click', evt => {
evt.preventDefault(); evt.preventDefault();
onClickHandler(); onClickHandler(resources);
}); });
} }

View File

@ -1,10 +1,10 @@
import { elClassId, notify, split, uniqId } from '../utils'; import { notify, split } from '../utils';
import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask'; import { UpgradeBuildingTask } from '../Task/UpgradeBuildingTask';
import { Scheduler } from '../Scheduler'; import { Scheduler } from '../Scheduler';
import { TrainTroopTask } from '../Task/TrainTroopTask'; import { TrainTroopTask } from '../Task/TrainTroopTask';
import { grabActiveVillageId } from './VillageBlock'; import { grabActiveVillageId } from './VillageBlock';
import { ConsoleLogger, Logger } from '../Logger'; import { ConsoleLogger } from '../Logger';
import { createBuildButton, createTrainTroopButtons, createUpgradeButton, grabContractResources } from './BuildingPage'; import { createBuildButton, createTrainTroopButtons, createUpgradeButton } from './BuildingPage';
import { BuildBuildingTask } from '../Task/BuildBuildingTask'; import { BuildBuildingTask } from '../Task/BuildBuildingTask';
import { Resources } from '../Game'; import { Resources } from '../Game';
@ -34,8 +34,11 @@ export class BuildingPageController {
const buildTypeId = this.attributes.buildTypeId; const buildTypeId = this.attributes.buildTypeId;
this.logger.log('BUILD PAGE DETECTED', 'ID', this.attributes.buildId, 'TYPE', buildTypeId); this.logger.log('BUILD PAGE DETECTED', 'ID', this.attributes.buildId, 'TYPE', buildTypeId);
createBuildButton(buildTypeId => this.onScheduleBuildBuilding(buildTypeId)); if (buildTypeId) {
createUpgradeButton(() => this.onScheduleUpgradeBuilding()); createUpgradeButton(res => this.onScheduleUpgradeBuilding(res));
} else {
createBuildButton((buildTypeId, res) => this.onScheduleBuildBuilding(buildTypeId, res));
}
if (buildTypeId === QUARTERS_ID) { if (buildTypeId === QUARTERS_ID) {
createTrainTroopButtons((troopId, res, count) => this.onScheduleTrainTroopers(troopId, res, count)); createTrainTroopButtons((troopId, res, count) => this.onScheduleTrainTroopers(troopId, res, count));
@ -50,19 +53,17 @@ export class BuildingPageController {
} }
} }
private onScheduleBuildBuilding(buildTypeId: number) { private onScheduleBuildBuilding(buildTypeId: number, resources: Resources) {
const buildId = this.attributes.buildId; const buildId = this.attributes.buildId;
const categoryId = this.attributes.categoryId; const categoryId = this.attributes.categoryId;
const villageId = grabActiveVillageId(); const villageId = grabActiveVillageId();
const resources = grabContractResources();
this.scheduler.scheduleTask(BuildBuildingTask.name, { villageId, buildId, categoryId, buildTypeId, resources }); this.scheduler.scheduleTask(BuildBuildingTask.name, { villageId, buildId, categoryId, buildTypeId, resources });
notify(`Building ${buildId} scheduled`); notify(`Building ${buildId} scheduled`);
} }
private onScheduleUpgradeBuilding() { private onScheduleUpgradeBuilding(resources: Resources) {
const buildId = this.attributes.buildId; const buildId = this.attributes.buildId;
const villageId = grabActiveVillageId(); const villageId = grabActiveVillageId();
const resources = grabContractResources();
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId, resources }); this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId, resources });
notify(`Upgrading ${buildId} scheduled`); notify(`Upgrading ${buildId} scheduled`);
} }

View File

@ -55,19 +55,10 @@ export class Scheduler {
scheduleTask(name: string, args: Args, ts?: number | undefined): void { scheduleTask(name: string, args: Args, ts?: number | undefined): void {
this.logger.log('PUSH TASK', name, args, ts); this.logger.log('PUSH TASK', name, args, ts);
const villageId = args.villageId; let insertedTs = calculateInsertTime(this.taskQueue.seeItems(), name, args, ts);
let insertedTs = ts; this.taskQueue.push(name, args, insertedTs);
if (villageId && !insertedTs) { if (args.villageId) {
const tasks = this.taskQueue.seeItems(); this.reorderVillageTasks(args.villageId);
const sameNamePred = t => sameVillage(villageId, t.args) && t.name === name;
insertedTs = lastTaskTime(tasks, sameNamePred);
if (insertedTs) {
insertedTs += 1;
}
}
this.taskQueue.push(name, args, insertedTs || timestamp());
if (villageId) {
this.reorderVillageTasks(villageId);
} }
} }
@ -152,6 +143,10 @@ export class Scheduler {
} }
} }
interface TaskNamePredicate {
(name: string): boolean;
}
function isTrainTroopTask(taskName: string) { function isTrainTroopTask(taskName: string) {
return taskName === TrainTroopTask.name; return taskName === TrainTroopTask.name;
} }
@ -160,6 +155,8 @@ function isBuildingTask(taskName: string) {
return taskName === BuildBuildingTask.name || taskName === UpgradeBuildingTask.name; return taskName === BuildBuildingTask.name || taskName === UpgradeBuildingTask.name;
} }
const TASK_TYPE_PREDICATES: Array<TaskNamePredicate> = [isTrainTroopTask, isBuildingTask];
function sameVillage(villageId: number | undefined, args: Args) { function sameVillage(villageId: number | undefined, args: Args) {
return villageId !== undefined && args.villageId === villageId; return villageId !== undefined && args.villageId === villageId;
} }
@ -195,3 +192,20 @@ function findLastIndex(tasks: ImmutableTaskList, predicate: (t: Task) => boolean
} }
return count - 1 - indexInReversed; return count - 1 - indexInReversed;
} }
function calculateInsertTime(tasks: ImmutableTaskList, name: string, args: Args, ts: number | undefined): number {
const villageId = args.villageId;
let insertedTs = ts;
if (villageId && !insertedTs) {
for (let taskTypePred of TASK_TYPE_PREDICATES) {
const sameVillageAndTypePred = t => sameVillage(villageId, t.args) && taskTypePred(t.name);
insertedTs = lastTaskTime(tasks, sameVillageAndTypePred);
if (insertedTs) {
insertedTs += 1;
}
}
}
return insertedTs || timestamp();
}