From 901bea6b4ef9c50e19138aaa8290859e734152cb Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Wed, 14 Oct 2020 11:58:42 +0300 Subject: [PATCH] Add distance condition for auto resource sending --- src/Core/Village.ts | 4 ++++ .../Action/FindSendResourcesPathAction.ts | 5 +++- tests/Core/CoordinatesTest.ts | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/Core/CoordinatesTest.ts diff --git a/src/Core/Village.ts b/src/Core/Village.ts index 6edfce6..a288ecd 100644 --- a/src/Core/Village.ts +++ b/src/Core/Village.ts @@ -19,6 +19,10 @@ export class Coordinates implements CoordinatesInterface { eq(other: CoordinatesInterface): boolean { return this.x === other.x && this.y === other.y; } + + dist(other: CoordinatesInterface): number { + return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2)); + } } export class Village { diff --git a/src/Handler/Action/FindSendResourcesPathAction.ts b/src/Handler/Action/FindSendResourcesPathAction.ts index d9ac537..c8aa0d3 100644 --- a/src/Handler/Action/FindSendResourcesPathAction.ts +++ b/src/Handler/Action/FindSendResourcesPathAction.ts @@ -12,6 +12,8 @@ import { MARKET_ID } from '../../Core/Buildings'; import { registerAction } from '../ActionMap'; import { Task } from '../../Queue/Task'; +const MAX_VILLAGE_DISTANCE = 25; + @registerAction export class FindSendResourcesPathAction extends BaseAction { async run(args: Args, task: Task): Promise { @@ -21,7 +23,8 @@ export class FindSendResourcesPathAction extends BaseAction { const villages = this.villageFactory.getAllVillages(); for (let fromVillage of villages) { for (let toVillage of villages) { - if (fromVillage.id !== toVillage.id) { + const dist = fromVillage.crd.dist(toVillage.crd); + if (fromVillage.id !== toVillage.id && dist < MAX_VILLAGE_DISTANCE) { reports.push(calculator.calc(fromVillage.id, toVillage.id)); } } diff --git a/tests/Core/CoordinatesTest.ts b/tests/Core/CoordinatesTest.ts new file mode 100644 index 0000000..625d0f0 --- /dev/null +++ b/tests/Core/CoordinatesTest.ts @@ -0,0 +1,24 @@ +import { it, describe } from 'mocha'; +import { expect } from 'chai'; + +import { Coordinates } from '../../src/Core/Village'; + +describe('Coordinates', function () { + it('Can compare same', function () { + const a = new Coordinates(1, 1); + const b = new Coordinates(1, 1); + expect(a.eq(b)).is.true; + }); + + it('Can compare different', function () { + const a = new Coordinates(1, 1); + const b = new Coordinates(2, 1); + expect(a.eq(b)).is.false; + }); + + it('Can calc distance', function () { + const a = new Coordinates(0, 0); + const b = new Coordinates(-3, -4); + expect(a.dist(b)).is.equals(5); + }); +});