First attempt to resource sending

This commit is contained in:
2020-04-24 10:45:05 +03:00
parent 542bc353b0
commit fb4ac6424c
25 changed files with 437 additions and 74 deletions

View File

@ -1,6 +1,7 @@
import { GrabError } from '../Errors';
import { elClassId, getNumber, trimPrefix, uniqId } from '../utils';
import { Resources } from '../Core/Resources';
import { Coordinates } from '../Core/Village';
export function clickBuildButton(typeId: number) {
const section = jQuery(`#contract_building${typeId}`);
@ -102,3 +103,40 @@ export function createTrainTroopButtons(
});
});
}
export function createSendResourcesButton(onClickHandler: (resources: Resources, crd: Coordinates) => void) {
const id = uniqId();
jQuery('#button').before(`<div style="padding: 8px"><a id="${id}" href="#">Отправить</a></div>`);
jQuery(`#${id}`).on('click', evt => {
evt.preventDefault();
const sendSelect = jQuery('#send_select');
const resources = new Resources(
getNumber(sendSelect.find('#r1').val()),
getNumber(sendSelect.find('#r2').val()),
getNumber(sendSelect.find('#r3').val()),
getNumber(sendSelect.find('#r4').val())
);
const crd = new Coordinates(getNumber(jQuery('#xCoordInput').val()), getNumber(jQuery('#yCoordInput').val()));
onClickHandler(resources, crd);
});
}
export function grabMerchantsInfo() {
const available = getNumber(jQuery('.merchantsAvailable').text());
const carry = getNumber(jQuery('.carry b').text());
return { available, carry };
}
export function fillSendResourcesForm(resources: Resources, crd: Coordinates) {
const sendSelect = jQuery('#send_select');
sendSelect.find('#r1').val(resources.lumber);
sendSelect.find('#r2').val(resources.clay);
sendSelect.find('#r3').val(resources.iron);
sendSelect.find('#r4').val(resources.crop);
jQuery('#xCoordInput').val(crd.x);
jQuery('#yCoordInput').val(crd.y);
}
export function clickSendButton() {
jQuery('#enabledButton').trigger('click');
}

View File

@ -4,10 +4,18 @@ import { Scheduler } from '../Scheduler';
import { TrainTroopTask } from '../Task/TrainTroopTask';
import { grabActiveVillageId } from './VillageBlock';
import { ConsoleLogger } from '../Logger';
import { createBuildButton, createTrainTroopButtons, createUpgradeButton } from './BuildingPage';
import {
createBuildButton,
createSendResourcesButton,
createTrainTroopButtons,
createUpgradeButton,
} from './BuildingPage';
import { BuildBuildingTask } from '../Task/BuildBuildingTask';
import { Resources } from '../Core/Resources';
import { Coordinates } from '../Core/Village';
import { SendResourcesTask } from '../Task/SendResourcesTask';
const MARKET_ID = 17;
const QUARTERS_ID = 19;
const HORSE_STABLE_ID = 20;
const EMBASSY_ID = 25;
@ -16,6 +24,7 @@ export interface BuildingPageAttributes {
buildId: number;
buildTypeId: number;
categoryId: number;
sheetId: number;
tabId: number;
}
@ -31,8 +40,8 @@ export class BuildingPageController {
}
run() {
const { buildTypeId } = this.attributes;
this.logger.log('BUILD PAGE DETECTED', 'ID', this.attributes.buildId, 'TYPE', buildTypeId);
const { buildTypeId, sheetId, tabId } = this.attributes;
this.logger.log('BUILD PAGE DETECTED', 'ID', this.attributes.buildId, this.attributes);
if (buildTypeId) {
createUpgradeButton(res => this.onScheduleUpgradeBuilding(res));
@ -48,9 +57,13 @@ export class BuildingPageController {
createTrainTroopButtons((troopId, res, count) => this.onScheduleTrainTroopers(troopId, res, count));
}
if (buildTypeId === EMBASSY_ID && this.attributes.tabId === 1) {
if (buildTypeId === EMBASSY_ID && sheetId === 1) {
createTrainTroopButtons((troopId, res, count) => this.onScheduleTrainTroopers(troopId, res, count));
}
if (buildTypeId === MARKET_ID && tabId === 5) {
createSendResourcesButton((res, crd) => this.onSendResources(res, crd));
}
}
private onScheduleBuildBuilding(buildTypeId: number, resources: Resources) {
@ -69,13 +82,12 @@ export class BuildingPageController {
}
private onScheduleTrainTroopers(troopId: number, resources: Resources, count: number) {
const tabId = this.attributes.tabId;
for (let chunk of split(count)) {
const args = {
villageId: grabActiveVillageId(),
buildId: this.attributes.buildId,
buildTypeId: this.attributes.buildTypeId,
tabId,
sheetId: this.attributes.sheetId,
troopId,
resources: resources.scale(chunk),
trainCount: chunk,
@ -85,4 +97,17 @@ export class BuildingPageController {
}
notify(`Training ${count} troopers scheduled`);
}
private onSendResources(resources: Resources, coordinates: Coordinates) {
const villageId = grabActiveVillageId();
this.scheduler.scheduleTask(SendResourcesTask.name, {
villageId: villageId,
buildTypeId: this.attributes.buildTypeId,
buildId: this.attributes.buildId,
tabId: this.attributes.tabId,
resources,
coordinates,
});
notify(`Send resources ${JSON.stringify(resources)} from ${villageId} to ${JSON.stringify(coordinates)}`);
}
}

View File

@ -4,7 +4,7 @@ import { Resources } from '../Core/Resources';
import { ResourceType } from '../Core/ResourceType';
import { ResourceStorage } from '../Core/ResourceStorage';
export function grabResources(): Resources {
export function grabVillageResources(): Resources {
const lumber = grabResource(ResourceType.Lumber);
const clay = grabResource(ResourceType.Clay);
const iron = grabResource(ResourceType.Iron);
@ -13,7 +13,7 @@ export function grabResources(): Resources {
return new Resources(lumber, clay, iron, crop);
}
export function grabResourceStorage() {
export function grabVillageResourceStorage() {
const warehouse = grabCapacity('warehouse');
const granary = grabCapacity('granary');
return new ResourceStorage(warehouse, granary);