From 2f2f9bbece0f48d15e6567f387d2364c720b322a Mon Sep 17 00:00:00 2001 From: ymmot239 <77180103+ymmot239@users.noreply.github.com> Date: Mon, 2 Dec 2024 21:08:41 -0600 Subject: [PATCH] Parallel and castle fixes Fixed small errors when castling. Forced parallel to work by making it extend sequential groups. It makes parting the red sea look nice, but it may not be the best way to do it. --- src/server/command/command.ts | 36 +++++++++++------------ src/server/command/move-piece.ts | 5 ++-- src/server/robot/path-materializer.ts | 42 +++++++++++++++------------ src/server/simulator.ts | 11 +++---- 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/src/server/command/command.ts b/src/server/command/command.ts index 2859f1d..69ca663 100644 --- a/src/server/command/command.ts +++ b/src/server/command/command.ts @@ -82,24 +82,6 @@ function isReversable(obj): obj is Reversible { return typeof obj.reverse() === "function"; } -/** - * Executes one or more commands in parallel. - */ -export class ParallelCommandGroup extends CommandGroup { - public async execute(): Promise { - const promises = this.commands.map((move) => move.execute()); - return Promise.all(promises).then(null); - } - public async reverse(): Promise { - const promises = this.commands.map((move) => { - if (isReversable(move)) { - move.reverse(); - } - }); - return Promise.all(promises).then(null); - } -} - /** * Executes one or more commands in sequence, one after another. */ @@ -121,3 +103,21 @@ export class SequentialCommandGroup extends CommandGroup { return promise; } } + +/** + * Executes one or more commands in parallel. + */ +export class ParallelCommandGroup extends SequentialCommandGroup { + public async execute(): Promise { + const promises = this.commands.map((move) => move.execute()); + return Promise.all(promises).then(null); + } + public async reverse(): Promise { + const promises = this.commands.map((move) => { + if (isReversable(move)) { + move.reverse(); + } + }); + return Promise.all(promises).then(null); + } +} \ No newline at end of file diff --git a/src/server/command/move-piece.ts b/src/server/command/move-piece.ts index c927599..346f3c4 100644 --- a/src/server/command/move-piece.ts +++ b/src/server/command/move-piece.ts @@ -3,6 +3,7 @@ import { RobotCommand, SequentialCommandGroup, Reversible, + ParallelCommandGroup, } from "./command"; export type ReversibleRobotCommand = RobotCommand & @@ -18,9 +19,9 @@ export class MovePiece extends SequentialCommandGroup { public mainMove: Command, ) { super([ - new SequentialCommandGroup(setupMoves), + new ParallelCommandGroup(setupMoves), mainMove, - new SequentialCommandGroup( + new ParallelCommandGroup( setupMoves.map((command) => command .reverse() diff --git a/src/server/robot/path-materializer.ts b/src/server/robot/path-materializer.ts index 712ef83..f5243cf 100644 --- a/src/server/robot/path-materializer.ts +++ b/src/server/robot/path-materializer.ts @@ -557,38 +557,41 @@ export function materializePath(move: Move): Command { let rookMove1; let rookMove2; let rookMove3; + let rookPiece; if (moveToGridMove(move).from.j === 2) { + rookPiece = robotManager.getRobotAtIndices(new GridIndices(2, 2)); kingMove = new AbsoluteMoveCommand( robotManager.getRobotAtIndices(moveToGridMove(move).from).id, gridIndicesToPosition(new GridIndices(4, 2)), ); rookMove1 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(2, 2)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(2, 1)), ); rookMove2 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(2, 1)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(5, 1)), ); rookMove3 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(5, 1)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(5, 2)), ); } else { + rookPiece = robotManager.getRobotAtIndices(new GridIndices(2, 9)); kingMove = new AbsoluteMoveCommand( robotManager.getRobotAtIndices(moveToGridMove(move).from).id, gridIndicesToPosition(new GridIndices(4, 9)), ); rookMove1 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(2, 9)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(2, 10)), ); rookMove2 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(2, 10)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(5, 10)), ); rookMove3 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(6, 10)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(5, 9)), ); } @@ -602,39 +605,42 @@ export function materializePath(move: Move): Command { let rookMove1; let rookMove2; let rookMove3; + let rookPiece; if (moveToGridMove(move).from.j === 2) { + rookPiece = robotManager.getRobotAtIndices(new GridIndices(9, 2)) kingMove = new AbsoluteMoveCommand( robotManager.getRobotAtIndices(moveToGridMove(move).from).id, - gridIndicesToPosition(new GridIndices(9, 2)), + gridIndicesToPosition(new GridIndices(8, 2)), ); rookMove1 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(9, 2)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(9, 1)), ); rookMove2 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(9, 1)).id, - gridIndicesToPosition(new GridIndices(6, 1)), + rookPiece.id, + gridIndicesToPosition(new GridIndices(7, 1)), ); rookMove3 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(6, 1)).id, - gridIndicesToPosition(new GridIndices(9, 2)), + rookPiece.id, + gridIndicesToPosition(new GridIndices(7, 2)), ); } else { + rookPiece = robotManager.getRobotAtIndices(new GridIndices(9, 9)); kingMove = new AbsoluteMoveCommand( robotManager.getRobotAtIndices(moveToGridMove(move).from).id, - gridIndicesToPosition(new GridIndices(9, 9)), + gridIndicesToPosition(new GridIndices(9, 8)), ); rookMove1 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(9, 9)).id, + rookPiece.id, gridIndicesToPosition(new GridIndices(9, 10)), ); rookMove2 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(9, 10)).id, - gridIndicesToPosition(new GridIndices(6, 10)), + rookPiece.id, + gridIndicesToPosition(new GridIndices(7, 10)), ); rookMove3 = new AbsoluteMoveCommand( - robotManager.getRobotAtIndices(new GridIndices(6, 10)).id, - gridIndicesToPosition(new GridIndices(6, 9)), + rookPiece.id, + gridIndicesToPosition(new GridIndices(7, 9)), ); } return new SequentialCommandGroup([ diff --git a/src/server/simulator.ts b/src/server/simulator.ts index d43b6e4..5732bd8 100644 --- a/src/server/simulator.ts +++ b/src/server/simulator.ts @@ -4,15 +4,16 @@ import { Robot } from "./robot/robot"; import config from "./api/bot-server-config.json"; import { Packet } from "./utils/tcp-packet"; import { Position, ZERO_POSITION } from "./robot/position"; -import path from "path"; +//import path from "path"; import { SimulatorUpdateMessage, - StackFrame, + //StackFrame, } from "../common/message/simulator-message"; import { socketManager } from "./api/managers"; import { GridIndices } from "./robot/grid-indices"; import { getStartHeading, Side } from "../common/game-types"; +/* const srcDir = path.resolve(__dirname, "../"); function getStack(justMyCode = true) { @@ -56,7 +57,7 @@ const parseErrorStack = (stack: string): StackFrame[] => { }); return frames; }; - +*/ export class VirtualBotTunnel extends BotTunnel { connected = true; @@ -95,7 +96,7 @@ export class VirtualBotTunnel extends BotTunnel { } send(packet: Packet) { - const stack = getStack(); + //const stack = getStack(); // NOTE: need to ensure that all the packets which are used in the Robot class (src/server/robot/robot.ts) are also provided with a matching virtual implementation here switch (packet.type) { @@ -135,7 +136,7 @@ export class VirtualBotTunnel extends BotTunnel { headingRadians: this.headingRadians, }, packet, - stack, + [], ); VirtualBotTunnel.messages.push({ ts: new Date(), message }); socketManager.sendToAll(message);