Add building queue time grabber

This commit is contained in:
Anton Vakhrushev 2020-04-18 13:16:01 +03:00
parent 8001510f0a
commit db16c54137
8 changed files with 54 additions and 19 deletions

View File

@ -2,22 +2,19 @@ import { ActionController, registerAction } from './ActionController';
import { Args } from '../Common';
import { Task } from '../Storage/TaskQueue';
import { BuildingQueueFullError } from '../Errors';
import { grabActiveVillageId } from '../Page/VillageBlock';
import { grabActiveVillageId, grabBuildingQueueInfo } from '../Page/VillageBlock';
@registerAction
export class CheckBuildingRemainingTimeAction extends ActionController {
async run(args: Args, task: Task): Promise<any> {
const timer = jQuery('.buildDuration .timer');
if (timer.length === 1) {
const remainingSeconds = Number(timer.attr('value'));
if (remainingSeconds > 0) {
const info = grabBuildingQueueInfo();
if (info.seconds > 0) {
throw new BuildingQueueFullError(
task.id,
grabActiveVillageId(),
remainingSeconds + 1,
info.seconds + 1,
'Building queue is full'
);
}
}
}
}

View File

@ -71,6 +71,7 @@ export class ControlPanel {
const resources = state.getResources();
const storage = state.getResourceStorage();
const performance = state.getResourcesPerformance();
const buildQueueInfo = state.getBuildingQueueInfo();
return {
id: village.id,
name: village.name,
@ -86,6 +87,7 @@ export class ControlPanel {
crop_hour: performance.crop,
warehouse: storage.warehouse,
granary: storage.granary,
buildRemainingSeconds: buildQueueInfo.seconds,
};
});
for (let village of this.villages) {

View File

@ -42,7 +42,7 @@
<td class="right" v-text="village.granary"></td>
</tr>
<tr class="performance-line">
<td></td>
<td class="right small" v-text="secondsToTime(village.buildRemainingSeconds)"></td>
<td class="right small">+{{ village.lumber_hour }}</td>
<td class="right small">+{{ village.clay_hour }}</td>
<td class="right small">+{{ village.iron_hour }}</td>
@ -108,6 +108,12 @@ export default {
const minutes = Math.round((value - hours) * 60);
return `${hours}:${String(minutes).padStart(2, '0')}`;
},
secondsToTime(value) {
const hours = Math.floor(value / 3600);
const minutes = Math.floor((value % 3600) / 60);
const seconds = value % 60;
return `${hours}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
},
},
};
</script>

View File

@ -90,6 +90,13 @@ export class Village {
export type VillageList = Array<Village>;
export class BuildingQueueInfo {
readonly seconds: number;
constructor(seconds: number) {
this.seconds = seconds;
}
}
export type HeroAllResourcesType = 'all';
export const HeroAllResources: HeroAllResourcesType = 'all';

View File

@ -1,4 +1,4 @@
import { Coordinates, Resources, Village, VillageList } from '../Game';
import { BuildingQueueInfo, Coordinates, Resources, Village, VillageList } from '../Game';
import { GrabError } from '../Errors';
import { getNumber, parseLocation } from '../utils';
@ -59,3 +59,14 @@ export function grabResourcesPerformance(): Resources {
getNumber($nums.get(3).innerText)
);
}
export function grabBuildingQueueInfo(): BuildingQueueInfo {
const timer = jQuery('.buildDuration .timer');
if (timer.length !== 1) {
throw new GrabError();
}
const remainingSeconds = getNumber(timer.attr('value'));
return new BuildingQueueInfo(remainingSeconds);
}

View File

@ -1,6 +1,6 @@
import { StateGrabber } from './StateGrabber';
import { ResourceGrabber } from './ResourceGrabber';
import { ResourcePerformanceGrabber } from './ResourcePerformanceGrabber';
import { VillageOverviewPageGrabber } from './VillageOverviewPageGrabber';
export class StateGrabberManager {
private readonly grabbers: Array<StateGrabber> = [];
@ -8,7 +8,7 @@ export class StateGrabberManager {
constructor() {
this.grabbers = [];
this.grabbers.push(new ResourceGrabber());
this.grabbers.push(new ResourcePerformanceGrabber());
this.grabbers.push(new VillageOverviewPageGrabber());
}
grab() {

View File

@ -1,9 +1,9 @@
import { StateGrabber } from './StateGrabber';
import { grabActiveVillageId, grabResourcesPerformance } from '../Page/VillageBlock';
import { grabActiveVillageId, grabBuildingQueueInfo, grabResourcesPerformance } from '../Page/VillageBlock';
import { VillageState } from './VillageState';
import { parseLocation } from '../utils';
export class ResourcePerformanceGrabber extends StateGrabber {
export class VillageOverviewPageGrabber extends StateGrabber {
grab(): void {
const p = parseLocation();
if (p.pathname !== '/dorf1.php') {
@ -13,5 +13,6 @@ export class ResourcePerformanceGrabber extends StateGrabber {
const villageId = grabActiveVillageId();
const state = new VillageState(villageId);
state.storeResourcesPerformance(grabResourcesPerformance());
state.storeBuildingQueueInfo(grabBuildingQueueInfo());
}
}

View File

@ -1,9 +1,10 @@
import { DataStorage } from '../Storage/DataStorage';
import { Resources, ResourceStorage } from '../Game';
import { BuildingQueueInfo, Resources, ResourceStorage } from '../Game';
const RESOURCES_KEY = 'res';
const CAPACITY_KEY = 'cap';
const PERFORMANCE_KEY = 'perf';
const BUILDING_QUEUE_KEY = 'bq';
export class VillageState {
private storage: DataStorage;
@ -40,4 +41,14 @@ export class VillageState {
let res = new Resources(0, 0, 0, 0);
return Object.assign(res, plain) as Resources;
}
storeBuildingQueueInfo(info: BuildingQueueInfo): void {
this.storage.set(BUILDING_QUEUE_KEY, info);
}
getBuildingQueueInfo(): BuildingQueueInfo {
let plain = this.storage.get(BUILDING_QUEUE_KEY);
let res = new BuildingQueueInfo(0);
return Object.assign(res, plain) as BuildingQueueInfo;
}
}