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');
}
export function createBuildButton(onClickHandler: (buildTypeId: number) => void) {
export function createBuildButton(onClickHandler: (buildTypeId: number, resources: Resources) => void) {
const $els = jQuery('[id^=contract_building]');
$els.each((idx, 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 resElement = $el.find('.resourceWrapper .resource');
const resources = grabResourcesFromList(resElement);
$el.append(`<div style="padding: 8px"><a id="${btnId}" href="#">Построить</a></div>`);
jQuery(`#${btnId}`).on('click', evt => {
evt.preventDefault();
onClickHandler(id);
onClickHandler(buildTypeId, resources);
});
});
}
@ -43,14 +45,15 @@ export function clickUpgradeButton() {
btn.trigger('click');
}
export function createUpgradeButton(onClickHandler: () => void) {
export function createUpgradeButton(onClickHandler: (resources: Resources) => void) {
const id = uniqId();
jQuery('.upgradeButtonsContainer .section1').append(
`<div style="padding: 8px"><a id="${id}" href="#">В очередь</a></div>`
);
const resources = grabContractResources();
jQuery(`#${id}`).on('click', evt => {
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 { Scheduler } from '../Scheduler';
import { TrainTroopTask } from '../Task/TrainTroopTask';
import { grabActiveVillageId } from './VillageBlock';
import { ConsoleLogger, Logger } from '../Logger';
import { createBuildButton, createTrainTroopButtons, createUpgradeButton, grabContractResources } from './BuildingPage';
import { ConsoleLogger } from '../Logger';
import { createBuildButton, createTrainTroopButtons, createUpgradeButton } from './BuildingPage';
import { BuildBuildingTask } from '../Task/BuildBuildingTask';
import { Resources } from '../Game';
@ -34,8 +34,11 @@ export class BuildingPageController {
const buildTypeId = this.attributes.buildTypeId;
this.logger.log('BUILD PAGE DETECTED', 'ID', this.attributes.buildId, 'TYPE', buildTypeId);
createBuildButton(buildTypeId => this.onScheduleBuildBuilding(buildTypeId));
createUpgradeButton(() => this.onScheduleUpgradeBuilding());
if (buildTypeId) {
createUpgradeButton(res => this.onScheduleUpgradeBuilding(res));
} else {
createBuildButton((buildTypeId, res) => this.onScheduleBuildBuilding(buildTypeId, res));
}
if (buildTypeId === QUARTERS_ID) {
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 categoryId = this.attributes.categoryId;
const villageId = grabActiveVillageId();
const resources = grabContractResources();
this.scheduler.scheduleTask(BuildBuildingTask.name, { villageId, buildId, categoryId, buildTypeId, resources });
notify(`Building ${buildId} scheduled`);
}
private onScheduleUpgradeBuilding() {
private onScheduleUpgradeBuilding(resources: Resources) {
const buildId = this.attributes.buildId;
const villageId = grabActiveVillageId();
const resources = grabContractResources();
this.scheduler.scheduleTask(UpgradeBuildingTask.name, { villageId, buildId, resources });
notify(`Upgrading ${buildId} scheduled`);
}

View File

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