From c4333057e71e4b0439e48eccd0a2a57399475a56 Mon Sep 17 00:00:00 2001 From: David Neilsen Date: Sat, 5 Sep 2020 00:22:01 +1200 Subject: [PATCH] Fix tpyes --- src/lib/randomMove.ts | 3 ++- src/server/snakes/aldo.ts | 2 +- src/server/snakes/base-snake.ts | 5 +++-- src/server/snakes/dunno.ts | 3 ++- src/server/snakes/keep-away.ts | 2 +- src/server/snakes/look-ahead.ts | 2 +- src/server/snakes/project-z-2.ts | 2 +- src/server/snakes/project-z.ts | 2 +- src/server/snakes/rando.ts | 2 +- src/server/snakes/tak.ts | 5 +++-- src/server/snakes/work-it-out.ts | 24 ++++++++++++------------ 11 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/lib/randomMove.ts b/src/lib/randomMove.ts index 75bd269..26f81e0 100644 --- a/src/lib/randomMove.ts +++ b/src/lib/randomMove.ts @@ -4,7 +4,7 @@ import { BTData, BTRequest } from '../types/BTData'; import { MoveDirection } from '../types/MoveDirection'; import { shuffle } from './shuffle'; -export function randomMove(request: BTRequest) { +export function randomMove(request: BTRequest): MoveDirection { let direction, x, y; const directions = [MoveDirection.LEFT, MoveDirection.RIGHT, MoveDirection.UP, MoveDirection.DOWN]; shuffle(directions); @@ -36,4 +36,5 @@ export function randomMove(request: BTRequest) { } } log('randomMove', 'no-options'); + return direction; } diff --git a/src/server/snakes/aldo.ts b/src/server/snakes/aldo.ts index 2fc3e29..0270b9f 100644 --- a/src/server/snakes/aldo.ts +++ b/src/server/snakes/aldo.ts @@ -20,7 +20,7 @@ export class Aldo extends BaseSnake implements ISnake { public headType = HeadType.TONGUE; public tailType = TailType.SKINNY; - public move(request: BTRequest): ServerMoveResponse { + public move(request: BTRequest): ServerMoveResponse | null { // if (!request.storage.xDirection) { // if (y % 2 === 0) { // request.storage.xDirection = request.storage.xDirection || MoveDirection.RIGHT; diff --git a/src/server/snakes/base-snake.ts b/src/server/snakes/base-snake.ts index c1e8e8f..8dbddfa 100644 --- a/src/server/snakes/base-snake.ts +++ b/src/server/snakes/base-snake.ts @@ -1,4 +1,4 @@ -import { Server } from '../Server'; +import { Server, ServerMoveResponse } from '../Server'; import { BTRequest } from '../../types/BTData'; import { MoveDirection } from '../../types/MoveDirection'; @@ -22,7 +22,7 @@ export abstract class BaseSnake { public start(request: BTRequest): void { } - public move(request: BTRequest) { + public move(request: BTRequest): ServerMoveResponse | null { let direction; for (const state of this.states) { if (direction = state(request)) { @@ -33,6 +33,7 @@ export abstract class BaseSnake { }; } } + return null; } public get name() { diff --git a/src/server/snakes/dunno.ts b/src/server/snakes/dunno.ts index 0d68732..4b06e53 100644 --- a/src/server/snakes/dunno.ts +++ b/src/server/snakes/dunno.ts @@ -11,6 +11,7 @@ import { BaseSnake } from './base-snake'; import { moveTowardsKill } from '../../lib/moveTowardsKill'; import { lookAhead } from '../../lib/lookAhead'; import { ISnake } from './snake-interface'; +import { ServerMoveResponse } from '../Server'; export class Dunno extends BaseSnake implements ISnake { public port: number = 9007; @@ -18,7 +19,7 @@ export class Dunno extends BaseSnake implements ISnake { public headType = HeadType.SAND_WORM; public tailType = TailType.PIXEL; - public move(request: BTRequest): ServerMoveResponse { + public move(request: BTRequest): ServerMoveResponse | null { let direction; direction = moveTowardsFoodPf(request); if (!direction) { diff --git a/src/server/snakes/keep-away.ts b/src/server/snakes/keep-away.ts index 0cf0048..cad3a8b 100644 --- a/src/server/snakes/keep-away.ts +++ b/src/server/snakes/keep-away.ts @@ -18,7 +18,7 @@ export class KeepAway extends BaseSnake implements ISnake { public headType = HeadType.DEAD; public tailType = TailType.CURLED; - public move(request: BTRequest): ServerMoveResponse { + public move(request: BTRequest): ServerMoveResponse | null { let direction; if (request.body.you.health < 10) { direction = moveTowardsFoodPf(request); diff --git a/src/server/snakes/look-ahead.ts b/src/server/snakes/look-ahead.ts index e2dd522..a199930 100644 --- a/src/server/snakes/look-ahead.ts +++ b/src/server/snakes/look-ahead.ts @@ -14,7 +14,7 @@ export class LookAhead extends BaseSnake implements ISnake { public headType = HeadType.EVIL; public tailType = TailType.SHARP; - public move(request: BTRequest): ServerMoveResponse { + public move(request: BTRequest): ServerMoveResponse | null { return { move: lookAhead(request.body), }; diff --git a/src/server/snakes/project-z-2.ts b/src/server/snakes/project-z-2.ts index b946be6..c17cde5 100644 --- a/src/server/snakes/project-z-2.ts +++ b/src/server/snakes/project-z-2.ts @@ -20,7 +20,7 @@ export class ProjectZ2 extends BaseSnake implements ISnake { public headType = HeadType.SILLY; public tailType = TailType.ROUND_BUM; - public move(request: BTRequest): ServerMoveResponse { + public move(request: BTRequest): ServerMoveResponse | null { let direction; direction = moveTowardsFoodPf(request); if (!direction) { diff --git a/src/server/snakes/project-z.ts b/src/server/snakes/project-z.ts index 6491b74..4a8fbe4 100644 --- a/src/server/snakes/project-z.ts +++ b/src/server/snakes/project-z.ts @@ -17,7 +17,7 @@ export class ProjectZ extends BaseSnake implements ISnake { public headType = HeadType.BELUGA; public tailType = TailType.BLOCK_BUM; - public move(request: BTRequest): ServerMoveResponse { + public move(request: BTRequest): ServerMoveResponse | null { let direction; // Only go for food if smaller than other snake -2 direction = moveTowardsFoodPf(request); diff --git a/src/server/snakes/rando.ts b/src/server/snakes/rando.ts index 502bc2a..e1c720b 100644 --- a/src/server/snakes/rando.ts +++ b/src/server/snakes/rando.ts @@ -17,7 +17,7 @@ export class Rando extends BaseSnake implements ISnake { public headType = HeadType.REGULAR; public tailType = TailType.FAT_RATTLE; - public move(request: BTRequest): ServerMoveResponse { + public move(request: BTRequest): ServerMoveResponse | null { let direction; if (request.body.you.health < 20) { direction = moveTowardsFoodPf(request); diff --git a/src/server/snakes/tak.ts b/src/server/snakes/tak.ts index 36e91a9..6f1be5c 100644 --- a/src/server/snakes/tak.ts +++ b/src/server/snakes/tak.ts @@ -10,6 +10,7 @@ import { BaseSnake } from './base-snake'; import { ISnake } from './snake-interface'; import { isEnemy } from '../../lib/isEnemy'; import { ServerMoveResponse } from '../Server'; +import { MoveDirection } from '../../types/MoveDirection'; export class Tak extends BaseSnake implements ISnake { public port: number = 9004; @@ -18,8 +19,8 @@ export class Tak extends BaseSnake implements ISnake { public headType = HeadType.FANG; public tailType = TailType.FRECKLED; - public move(request: BTRequest): ServerMoveResponse { - let direction; + public move(request: BTRequest): ServerMoveResponse | null { + let direction: MoveDirection; let biggestSnake = 0; for (const snake of request.body.board.snakes) { if (!isEnemy(request.body.you, snake)) { diff --git a/src/server/snakes/work-it-out.ts b/src/server/snakes/work-it-out.ts index 484aff6..1ba1e9b 100644 --- a/src/server/snakes/work-it-out.ts +++ b/src/server/snakes/work-it-out.ts @@ -45,16 +45,16 @@ export class WorkItOut extends BaseSnake implements ISnake { // }; // } - public move(request: BTRequest): ServerMoveResponse { - let direction; - for (const op of this.ops) { - direction = op(request); - if (direction) { - break; - } - } - return { - move: direction, - }; - } + // public move(request: BTRequest): ServerMoveResponse | null { + // let direction; + // for (const op of this.ops) { + // direction = op(request); + // if (direction) { + // break; + // } + // } + // return { + // move: direction, + // }; + // } }