Skip to content
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
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = {
eqeqeq: "warn",
"tsdoc/syntax": "warn",
"import/no-cycle": "error",
"@typescript-eslint/consistent-type-imports": "error"
"@typescript-eslint/consistent-type-imports": "error",
},
settings: {
"import/parsers": {
Expand Down
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"name": "utd-chess-bots",
"private": true,
"version": "0.0.0",
"workspaces": [
"src/client",
"src/server"
],
"workspaces": ["src/client", "src/server"],
"scripts": {
"dev": "ts-node src/server/main.ts",
"devup": "nodemon src/server/main.ts",
Expand Down
14 changes: 3 additions & 11 deletions src/server/api/game-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,10 @@ export class ComputerGameManager extends GameManager {
}

// Ensure MINIMUM_DELAY before responding
const startTime = Date.now();
// previous code didn't wait for robots and caused issues where you could move and make robots collide
const move = this.chess.calculateAiMove(this.difficulty);
const elapsedTime = Date.now() - startTime;

// If elapsed time is less than minimum delay, timeout is set to 1ms
const delay = Math.max(1, this.MINIMUM_DELAY - elapsedTime);

setTimeout(async () => {
this.socketManager.sendToAll(new MoveMessage(move));
await this.executeRobotMovement(move);
}, delay);

await this.executeRobotMovement(move); // wait for robots to finish moving
this.socketManager.sendToAll(new MoveMessage(move)); // send move to clients after robots finish moving
if (this.isGameEnded()) {
SaveManager.endGame(id, "ai");
}
Expand Down
3 changes: 2 additions & 1 deletion src/server/command/move-piece.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export class MovePiece extends SequentialCommandGroup {
constructor(
public setupMoves: ReversibleRobotCommand[],
public mainMove: Command,
public noReverse: boolean = false,
) {
super([
new ParallelCommandGroup(setupMoves),
mainMove,
new ParallelCommandGroup(
setupMoves
.map(
(command) => command.reverse(),
(command) => (!noReverse ? command.reverse() : command),
// .then(new RotateToStartCommand(command.robotId)), // TODO have rotatetostart at end of pathmat
)
.reverse(),
Expand Down
25 changes: 17 additions & 8 deletions src/server/robot/path-materializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { MovePiece } from "../command/move-piece";
import { Position } from "./position";
import { GridIndices } from "./grid-indices";
import type { Robot } from "./robot";
import { error } from "console";
//import { error } from "console"; replaced with Error
import { robotManager } from "./robot-manager";
import { gameManager } from "../api/managers";

Expand Down Expand Up @@ -339,6 +339,7 @@ function constructFinalCommand(
rotateCommands: ReversibleRobotCommand[],
collisionType: CollisionType,
numCollisions: number,
noReverse: boolean = false,
): MovePiece {
const from = move.from;
const robotAtFrom = robotManager.getRobotAtIndices(from);
Expand Down Expand Up @@ -371,7 +372,7 @@ function constructFinalCommand(
mainDrive3,
]);
setupCommands.push(...rotateCommands, mainTurn1, ...driveCommands);
return new MovePiece(setupCommands, mainDrive);
return new MovePiece(setupCommands, mainDrive, noReverse);
} else if (
collisionType === CollisionType.VERTICAL &&
numCollisions > 1
Expand All @@ -398,24 +399,31 @@ function constructFinalCommand(
mainDrive3,
]);
setupCommands.push(...rotateCommands, mainTurn1, ...driveCommands);
return new MovePiece(setupCommands, mainDrive);
return new MovePiece(setupCommands, mainDrive, noReverse);
} else {
const pos = new Position(to.i + 0.5, to.j + 0.5);
const mainDrive = constructDriveCommand(mainPiece, pos, null);
const mainTurn = constructRotateCommand(mainPiece, pos, null);
const setupCommands: ReversibleRobotCommand[] = [];
setupCommands.push(...rotateCommands, mainTurn, ...driveCommands);
return new MovePiece(setupCommands, mainDrive);
return new MovePiece(setupCommands, mainDrive, noReverse);
}
} else {
console.log("no main piece");
return new MovePiece(rotateCommands, new SequentialCommandGroup([]));
return new MovePiece(
rotateCommands,
new SequentialCommandGroup([]),
noReverse,
);
}
}

// Takes in a move, and generates the commands required to get the main piece to it's destination
// If there are pieces in the way, it shimmy's them out, and move them back after main piece passes
export function moveMainPiece(move: GridMove): MovePiece {
export function moveMainPiece(
move: GridMove,
noReverse: boolean = false,
): MovePiece {
const driveCommands: DriveCommand[] = [];
const rotateCommands: ReversibleRobotCommand[] = [];
const collisionType = calcCollisionType(move);
Expand All @@ -432,6 +440,7 @@ export function moveMainPiece(move: GridMove): MovePiece {
rotateCommands,
collisionType,
collisions.length,
noReverse,
);
}

Expand Down Expand Up @@ -513,7 +522,7 @@ function returnToHome(from: GridIndices, id: string): SequentialCommandGroup {
//const capturedPiece: GridIndices = GridIndices.squareToGrid(from);
const home: GridIndices = robotManager.getRobot(id).homeIndices;
const fastestMoveToDeadzone = moveToDeadZone(from);
const toDeadzone = moveMainPiece(fastestMoveToDeadzone);
const toDeadzone = moveMainPiece(fastestMoveToDeadzone, true);

const startInDeadzone = fastestMoveToDeadzone.to;
let finalDestination: GridIndices | undefined;
Expand All @@ -538,7 +547,7 @@ function returnToHome(from: GridIndices, id: string): SequentialCommandGroup {
}
}
if (!finalDestination) {
throw new error("WHERE THE HELL ARE YOU GOING");
throw new Error("WHERE THE HELL ARE YOU GOING"); // real
}
const startInArray = findGridIndicesInArray(
arrayOfDeadzone,
Expand Down
Loading