Skip to content

Commit 877c866

Browse files
committed
Now is linted with tslint with recommended rules.
Added tslint to run_game scripts. Updated readme about tslint.
1 parent 5d4e4ae commit 877c866

File tree

20 files changed

+271
-263
lines changed

20 files changed

+271
-263
lines changed

starter_kits/TypeScript/MyBot.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Constants } from './hlt/Constants';
2-
import { Direction } from './hlt/Direction';
3-
import { Logging } from './hlt/Logging';
4-
import { Game } from './hlt/Game';
5-
import { Random } from './hlt/Random';
1+
import { Constants } from "./hlt/Constants";
2+
import { Direction } from "./hlt/Direction";
3+
import { Game } from "./hlt/Game";
4+
import { Logging } from "./hlt/Logging";
5+
import { Random } from "./hlt/Random";
66

77
const firstArgument = process.argv[2];
88
const random = new Random(firstArgument ? Number(firstArgument) : 324231);
@@ -12,7 +12,7 @@ game.initialize().then(async ([gameMap, me]) => {
1212
// At this point "game" variable is populated with initial map data.
1313
// This is a good place to do computationally expensive start-up pre-processing.
1414
// As soon as you call "ready" function below, the 2 second per turn timer will start.
15-
await game.ready('MyTypeScriptBot');
15+
await game.ready("MyTypeScriptBot");
1616

1717
Logging.info(`My Player ID is ${game.myId}.`);
1818

@@ -32,16 +32,17 @@ game.initialize().then(async ([gameMap, me]) => {
3232
const destination = me.shipyard.position;
3333
const safeMove = gameMap.naiveNavigate(ship, destination);
3434
commandQueue.push(ship.move(safeMove));
35-
}
36-
else if (gameMap.get(ship.position).haliteAmount < tooLittleHaliteToKeepHarvestingCoefficient * Constants.MAX_ENERGY) {
35+
} else if (
36+
gameMap.get(ship.position).haliteAmount <
37+
tooLittleHaliteToKeepHarvestingCoefficient * Constants.MAX_ENERGY
38+
) {
3739
// Go harvest to a new place
3840
const getRandomArrayElement = (anArray: any[]) => anArray[Math.floor(anArray.length * random.next())];
3941
const direction = getRandomArrayElement(Direction.getAllCardinals());
4042
const destination = ship.position.directionalOffset(direction);
4143
const safeMove = gameMap.naiveNavigate(ship, destination);
4244
commandQueue.push(ship.move(safeMove));
43-
}
44-
else {
45+
} else {
4546
// Keep still and harvest in place
4647
}
4748
}

starter_kits/TypeScript/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Setup
2-
You should have typescript installed before developing. You can do this globally with `npm install -g typescript`
2+
You should have typescript installed before developing. You can do this globally with `npm install -g tslint typescript`
33
Run `npm install` to get the typescript definitions for node.
44

55
# Development
6-
Don't forget to run `tsc` to compile the typescript sources before using the bot.
6+
The `run_game` scripts will run `tslint` and `tsc` to proof and compile the typescript sources before running the bot.

starter_kits/TypeScript/hlt/Direction.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,54 @@
1-
import { Commands } from './Commands';
1+
import { Commands } from "./Commands";
22

33
export class Direction {
4-
static North = new Direction(0, -1);
5-
static South = new Direction(0, 1);
6-
static East = new Direction(1, 0);
7-
static West = new Direction(-1, 0);
8-
static Still = new Direction(0, 0);
4+
public static North = new Direction(0, -1);
5+
public static South = new Direction(0, 1);
6+
public static East = new Direction(1, 0);
7+
public static West = new Direction(-1, 0);
8+
public static Still = new Direction(0, 0);
9+
10+
public static getAllCardinals() {
11+
return [ Direction.North, Direction.South, Direction.East, Direction.West ];
12+
}
913

1014
constructor(public dx: number, public dy: number) {
1115
this.dx = dx;
1216
this.dy = dy;
1317
}
1418

15-
equals(other: Direction) {
19+
public equals(other: Direction) {
1620
return this.dx === other.dx && this.dy === other.dy;
1721
}
1822

19-
toString() {
23+
public toString() {
2024
return `${this.constructor.name}(${this.dx}, ${this.dy})`;
2125
}
2226

23-
static getAllCardinals() {
24-
return [ Direction.North, Direction.South, Direction.East, Direction.West ];
25-
}
26-
27-
toWireFormat() {
27+
public toWireFormat() {
2828
if (this.equals(Direction.North)) {
2929
return Commands.NORTH;
30-
}
31-
else if (this.equals(Direction.South)) {
30+
} else if (this.equals(Direction.South)) {
3231
return Commands.SOUTH;
33-
}
34-
else if (this.equals(Direction.East)) {
32+
} else if (this.equals(Direction.East)) {
3533
return Commands.EAST;
36-
}
37-
else if (this.equals(Direction.West)) {
34+
} else if (this.equals(Direction.West)) {
3835
return Commands.WEST;
39-
}
40-
else if (this.equals(Direction.Still)) {
36+
} else if (this.equals(Direction.Still)) {
4137
return Commands.STAY_STILL;
4238
}
4339
throw new Error(`Non-cardinal direction cannot be converted to wire format: ${this}`);
4440
}
4541

46-
invert() {
42+
public invert() {
4743
if (this.equals(Direction.North)) {
4844
return Direction.South;
49-
}
50-
else if (this.equals(Direction.South)) {
45+
} else if (this.equals(Direction.South)) {
5146
return Direction.North;
52-
}
53-
else if (this.equals(Direction.East)) {
47+
} else if (this.equals(Direction.East)) {
5448
return Direction.West;
55-
}
56-
else if (this.equals(Direction.West)) {
49+
} else if (this.equals(Direction.West)) {
5750
return Direction.East;
58-
}
59-
else if (this.equals(Direction.Still)) {
51+
} else if (this.equals(Direction.Still)) {
6052
return Direction.Still;
6153
}
6254
throw new Error(`Non-cardinal direction cannot be inverted: ${this}`);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Entity } from './entity';
1+
import { Entity } from "./entity";
22

33
/** Represents a dropoff. */
44
export class Dropoff extends Entity {
5-
}
5+
}

starter_kits/TypeScript/hlt/Game.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Logging } from "./Logging";
1+
import { Dropoff } from "./Dropoff";
22
import { GameMap } from "./GameMap";
3+
import { Logging } from "./Logging";
34
import { Player } from "./Player";
4-
import { Ship } from "./Ship";
55
import { Position } from "./Position";
6-
import { Shipyard } from "./Shipyard";
7-
import { Dropoff } from "./Dropoff";
86
import { ServerCommunication } from "./ServerCommunicaion";
7+
import { Ship } from "./Ship";
8+
import { Shipyard } from "./Shipyard";
99

1010
export class Game {
11-
turnNumber = 0;
12-
server: ServerCommunication = new ServerCommunication();
11+
public turnNumber = 0;
12+
public server: ServerCommunication = new ServerCommunication();
1313

1414
public myId = 0;
1515
public players = new Map<number, Player>();
@@ -22,94 +22,96 @@ export class Game {
2222
* "bot-<bot_id>.log".
2323
* @returns The initialized gameMap and me so we don't have to check if undefined.
2424
*/
25-
async initialize(): Promise<[GameMap, Player]> {
25+
public async initialize(): Promise<[GameMap, Player]> {
2626
const serverData = await this.server.getInitialData();
2727
this.myId = serverData.myId;
2828

2929
Logging.setup(`bot-${this.myId}.log`);
3030

31-
serverData.players.forEach(playerData => {
32-
const player = new Player(playerData.id, new Shipyard(playerData.id, -1, new Position(playerData.x, playerData.y)))
31+
serverData.players.forEach((playerData) => {
32+
const player = new Player(playerData.id,
33+
new Shipyard(playerData.id, -1, new Position(playerData.x, playerData.y)));
3334
this.players.set(player.id, player);
3435
});
3536
this.me = this.players.get(this.myId);
3637

3738
this.gameMap = new GameMap(serverData.cells);
3839

39-
return [<GameMap>this.gameMap, <Player>this.me]; // We cast here because we have just initialized
40+
return [this.gameMap as GameMap, this.me as Player]; // We cast here because we have just initialized
4041
}
4142

4243
/**
4344
* Updates the game object's state.
4445
*/
45-
async updateFrame() {
46+
public async updateFrame() {
4647
const data = await this.server.getUpdateData(this.players.size);
4748
this.turnNumber = data.turn;
48-
Logging.info(`================ TURN ${this.turnNumber.toString().padStart(3, '0')} ================`);
49-
data.players.forEach(playerData => {
50-
const player = <Player>this.players.get(playerData.id);
49+
Logging.info(`================ TURN ${this.turnNumber.toString().padStart(3, "0")} ================`);
50+
data.players.forEach((playerData) => {
51+
const player = this.players.get(playerData.id) as Player;
5152
player.haliteAmount = playerData.halite;
5253

5354
// Process ships
5455
const newShipsData = playerData.ships
55-
.filter(shipData => !player.hasShip(shipData.id));
56-
newShipsData.forEach(newShipData =>
57-
player.addShip(new Ship(player.id, newShipData.id, new Position(newShipData.x, newShipData.y), newShipData.halite)));
56+
.filter((shipData) => !player.hasShip(shipData.id));
57+
newShipsData.forEach((newShipData) =>
58+
player.addShip(new Ship(player.id, newShipData.id,
59+
new Position(newShipData.x, newShipData.y), newShipData.halite)));
5860

5961
const lostShips = player.getShips()
60-
.filter(ship => !playerData.ships.some(shipData => shipData.id === ship.id));
61-
lostShips.forEach(ship => player.loseShip(ship.id));
62+
.filter((ship) => !playerData.ships.some((shipData) => shipData.id === ship.id));
63+
lostShips.forEach((ship) => player.loseShip(ship.id));
6264

63-
player.getShips().forEach(ship => {
65+
player.getShips().forEach((ship) => {
6466
const updatedShipData = playerData.ships
65-
.find(shipData => ship.id === shipData.id);
67+
.find((shipData) => ship.id === shipData.id);
6668
if (updatedShipData) {
67-
[ship.haliteAmount, ship.position.x, ship.position.y] = [updatedShipData.halite, updatedShipData.x, updatedShipData.y];
69+
[ship.haliteAmount, ship.position.x, ship.position.y] =
70+
[updatedShipData.halite, updatedShipData.x, updatedShipData.y];
6871
}
6972
});
7073

7174
// Process dropoffs
7275
const newDropoffsData = playerData.dropoffs
73-
.filter(dropoffData => !player.dropoffs.has(dropoffData.id));
76+
.filter((dropoffData) => !player.dropoffs.has(dropoffData.id));
7477
newDropoffsData.forEach((newDropoffData: { id: number, x: number, y: number }) =>
7578
player.dropoffs.set(newDropoffData.id,
7679
new Dropoff(player.id, newDropoffData.id, new Position(newDropoffData.x, newDropoffData.y))));
7780

7881
const lostDropoffs = Array.from(player.dropoffs.values())
79-
.filter(dropoff => !playerData.dropoffs.some(dropoffData => dropoffData.id === dropoff.id));
80-
lostDropoffs.forEach(lostDropoff => {
82+
.filter((dropoff) => !playerData.dropoffs.some((dropoffData) => dropoffData.id === dropoff.id));
83+
lostDropoffs.forEach((lostDropoff) => {
8184
player.dropoffs.delete(lostDropoff.id);
8285
player.lostDropoffs.set(lostDropoff.id, lostDropoff);
8386
});
8487
});
8588

86-
const gameMap = <GameMap>this.gameMap;
89+
const gameMap = this.gameMap as GameMap;
8790
// Mark all cells as safe
88-
gameMap.cells.forEach(row => row.forEach(cell => cell.markSafe()));
91+
gameMap.cells.forEach((row) => row.forEach((cell) => cell.markSafe()));
8992
// Update cells
90-
data.cells.forEach(cell => gameMap.get(new Position(cell.x, cell.y)).haliteAmount = cell.halite);
93+
data.cells.forEach((cell) => gameMap.get(new Position(cell.x, cell.y)).haliteAmount = cell.halite);
9194
// Mark cells with ships as unsafe for navigation, mark sturctures
9295
for (const player of this.players.values()) {
9396
player.getShips()
94-
.forEach(ship => gameMap.get(ship.position).markUnsafe(ship));
97+
.forEach((ship) => gameMap.get(ship.position).markUnsafe(ship));
9598
player.getDropoffs()
96-
.forEach(dropoff => gameMap.get(dropoff.position).structure = dropoff);
99+
.forEach((dropoff) => gameMap.get(dropoff.position).structure = dropoff);
97100
}
98101
}
99102

100-
/**
101-
* Indicate that your bot is ready to play by sending the bot name.
103+
/**
104+
* Indicate that your bot is ready to play by sending the bot name.
102105
*/
103-
async ready(botName: string) {
106+
public async ready(botName: string) {
104107
await this.server.sendCommands([botName]);
105108
}
106109

107110
/**
108111
* Send all commands to the game engine, effectively ending your
109112
* turn.
110113
*/
111-
async endTurn(commands: string[]) {
114+
public async endTurn(commands: string[]) {
112115
await this.server.sendCommands(commands);
113116
}
114117
}
115-

starter_kits/TypeScript/hlt/MapCell.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Ship } from "./Ship";
1+
import { Dropoff } from "./Dropoff";
22
import { Position } from "./Position";
3+
import { Ship } from "./Ship";
34
import { Shipyard } from "./Shipyard";
4-
import { Dropoff } from "./Dropoff";
55

66
/** A cell on the game map. */
77
export class MapCell {
8-
ship?: Ship;
9-
structure?: Shipyard | Dropoff;
8+
public ship?: Ship;
9+
public structure?: Shipyard | Dropoff;
1010

1111
constructor(public position: Position, public haliteAmount: number) {
1212
}
@@ -35,19 +35,19 @@ export class MapCell {
3535
/**
3636
* Mark this cell as unsafe (occupied) for navigation.
3737
*/
38-
markUnsafe(ship: Ship) {
38+
public markUnsafe(ship: Ship) {
3939
this.ship = ship;
4040
}
4141

42-
markSafe() {
42+
public markSafe() {
4343
this.ship = undefined;
4444
}
4545

46-
equals(other: MapCell) {
46+
public equals(other: MapCell) {
4747
return this.position.equals(other.position);
4848
}
49-
50-
toString() {
49+
50+
public toString() {
5151
return `MapCell(${this.position}, halite=${this.haliteAmount})`;
5252
}
53-
}
53+
}

0 commit comments

Comments
 (0)