Skip to content

Commit

Permalink
Parallel and castle fixes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ymmot239 committed Dec 3, 2024
1 parent 5ec51bc commit 2f2f9bb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
36 changes: 18 additions & 18 deletions src/server/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,6 @@ function isReversable(obj): obj is Reversible<typeof obj> {
return typeof obj.reverse() === "function";
}

/**
* Executes one or more commands in parallel.
*/
export class ParallelCommandGroup extends CommandGroup {
public async execute(): Promise<void> {
const promises = this.commands.map((move) => move.execute());
return Promise.all(promises).then(null);
}
public async reverse(): Promise<void> {
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.
*/
Expand All @@ -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<void> {
const promises = this.commands.map((move) => move.execute());
return Promise.all(promises).then(null);
}
public async reverse(): Promise<void> {
const promises = this.commands.map((move) => {
if (isReversable(move)) {
move.reverse();
}
});
return Promise.all(promises).then(null);
}
}
5 changes: 3 additions & 2 deletions src/server/command/move-piece.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
RobotCommand,
SequentialCommandGroup,
Reversible,
ParallelCommandGroup,
} from "./command";

export type ReversibleRobotCommand = RobotCommand &
Expand All @@ -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()
Expand Down
42 changes: 24 additions & 18 deletions src/server/robot/path-materializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
);
}
Expand All @@ -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([
Expand Down
11 changes: 6 additions & 5 deletions src/server/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -56,7 +57,7 @@ const parseErrorStack = (stack: string): StackFrame[] => {
});
return frames;
};

*/
export class VirtualBotTunnel extends BotTunnel {
connected = true;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -135,7 +136,7 @@ export class VirtualBotTunnel extends BotTunnel {
headingRadians: this.headingRadians,
},
packet,
stack,
[],
);
VirtualBotTunnel.messages.push({ ts: new Date(), message });
socketManager.sendToAll(message);
Expand Down

0 comments on commit 2f2f9bb

Please sign in to comment.