Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/clean code #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/RoomEntities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export default class RoomEntities {
public readonly room: Room;
public readonly spawns: StructureSpawn[];
Expand Down
3 changes: 1 addition & 2 deletions src/actionnableEntities/ActionnableEntity.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export abstract class ActionnableEntity<T, baseClassType> {

constructor(baseObject: baseClassType) {
constructor(public readonly baseObject: baseClassType) {
this.extendParentPrototype(baseObject);
}

Expand Down
22 changes: 22 additions & 0 deletions src/actionnableEntities/ActionnableEntityMutationFacility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default class ActionnableEntityMutationFacility {
public static mutateStructureLinkIntoLink() {
const prototype = StructureLink.prototype as any;
prototype.do = function (task: string, target: Link): boolean {
switch (task) {
case 'transferEnergy':
return this.doTransferEnergy(target);
default:
return false;
}
};
prototype.isEmpty = function (): boolean { return !this.energy; };
prototype.isFull = function (): boolean { return this.energy === this.energyCapacity; };
// private methods
prototype.doTransferEnergy = function (target: Link): boolean {
if (this.isFull) {
return this.transferEnergy(target) === OK;
}
return false;
};
}
}
30 changes: 0 additions & 30 deletions src/actionnableEntities/Link.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import RoomStrategy from "strategies/RoomStrategy";
import { ErrorMapper } from "utils/ErrorMapper";
import ActionnableEntityMutationFacility from "actionnableEntities/ActionnableEntityMutationFacility";

// When compiling TS to JS and bundling with rollup, the line numbers and file names in error messages change
// This utility uses source maps to get the line numbers and file names of the original, TS source code
export const loop = ErrorMapper.wrapLoop(() => {
initMemory();
cleanMemory();

ActionnableEntityMutationFacility.mutateStructureLinkIntoLink();
incrementWallHitsTarget();
const roomStrategy = new RoomStrategy();
roomStrategy.execute();
Expand Down
20 changes: 9 additions & 11 deletions src/strategies/EnergyTeleportationStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import Link from "actionnableEntities/Link";
import RoomEntities from "RoomEntities";
import ActionnableEntityMutationFacility from "actionnableEntities/ActionnableEntityMutationFacility";

export default class EnergyTeleportationStrategy implements Strategy {
private storageAdjacentLink?: StructureLink;
private sourceAdjacentLinks: StructureLink[];
private storageAdjacentLink?: Link;
private sourceAdjacentLinks: Link[];

constructor({ storageAdjacentLink, sourceAdjacentLinks }: RoomEntities) {
this.storageAdjacentLink = storageAdjacentLink;
this.sourceAdjacentLinks = sourceAdjacentLinks;
this.storageAdjacentLink = storageAdjacentLink as Link;
this.sourceAdjacentLinks = sourceAdjacentLinks as Link[];
}

public execute() {
if (this.storageAdjacentLink) {
const storageAdjacentLink = new Link(this.storageAdjacentLink);

if (storageAdjacentLink.isEmpty) {
this.sourceAdjacentLinks.forEach((structuredLink) => {
const storageAdjacentLink = this.storageAdjacentLink;
if (storageAdjacentLink) {
if (storageAdjacentLink.isEmpty()) {
this.sourceAdjacentLinks.forEach((link) => {
// todo: if storage is not full then we shall ask links to transfert
const link = new Link(structuredLink);
link.do('transferEnergy', storageAdjacentLink);
});
}
Expand Down
11 changes: 11 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,14 @@ type Tasks = Task[];
interface Strategy {
execute(): void;
}

interface DecoratedBuilderCreep extends Creep, ActionnableEntity<ConstructionSite<BuildableStructureConstant>> { }

interface Link extends StructureLink, ActionnableEntity<Link> {
isEmpty(): boolean;
isFull(): boolean;
}

interface ActionnableEntity<T> {
do(task: string, target: T): boolean;
}