Skip to content

Commit

Permalink
style: lint + format
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonappah committed Oct 27, 2024
1 parent 281116f commit 375c756
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/client/debug/simulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ function Robot(props: {
className="robot"
style={{
position: "absolute",
left: `${props.pos.position.x * tileSize - 0.25*tileSize}px`,
bottom: `${props.pos.position.y * tileSize - 0.25*tileSize}px`,
left: `${props.pos.position.x * tileSize - 0.25 * tileSize}px`,
bottom: `${props.pos.position.y * tileSize - 0.25 * tileSize}px`,
}}
>
<Tooltip content={`${props.robotId}: ${JSON.stringify(props.pos)}`}>
Expand Down
1 change: 0 additions & 1 deletion src/client/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Home } from "./home";
import { Debug2 } from "./debug/debug2";
import { Simulator } from "./debug/simulator";


export const router = createBrowserRouter([
{
path: "/home",
Expand Down
5 changes: 4 additions & 1 deletion src/common/chess-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ export class ChessEngine {
* @param move - The Move to check.
* @returns The current piece on the square as a it's robot id as a string
*/
getCapturedPieceId(move: Move, robotManager: RobotManager): string | undefined {
getCapturedPieceId(
move: Move,
robotManager: RobotManager,
): string | undefined {
if (this.isEnPassant(move)) {
const y = GridIndices.squareToGrid(move.from).j;
if (y > 6) {
Expand Down
10 changes: 5 additions & 5 deletions src/server/api/game-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export abstract class GameManager {
public abstract handleMessage(
message: Message,
clientType: ClientType,
): Promise<void>
): Promise<void>;
}

export class HumanGameManager extends GameManager {
Expand Down Expand Up @@ -106,14 +106,14 @@ export class HumanGameManager extends GameManager {
if (message instanceof MoveMessage) {
// Call path materializer and send to bots
const command = materializePath(message.move);

this.chess.makeMove(message.move);

console.log("running executor");
console.log(command)
console.log(command);
await executor.execute(command);
console.log("executor done");

if (ids) {
if (currentSave?.host === ids[0]) {
SaveManager.saveGame(
Expand Down
68 changes: 47 additions & 21 deletions src/server/robot/path-materializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { robotManager } from "../api/managers";
import { Move } from "../../common/game-types";
import { gameManager } from "../api/api";
import { Command, SequentialCommandGroup } from "../command/command";
import { AbsoluteMoveCommand, DriveCommand, MoveCommand, RelativeRotateCommand } from "../command/move-command";
import {
AbsoluteMoveCommand,
DriveCommand,
MoveCommand,
RelativeRotateCommand,
} from "../command/move-command";
import { MovePiece, ReversibleRobotCommand } from "../command/move-piece";
import { Position } from "./position";
import { GridIndices } from "./grid-indices";
Expand Down Expand Up @@ -275,7 +280,7 @@ function findShimmyLocation(
);
}
}
return new Position(0, 0)
return new Position(0, 0);
}

function constructDriveCommand(
Expand Down Expand Up @@ -304,17 +309,17 @@ function constructFinalCommand(
rotateCommands: RelativeRotateCommand[],
): MovePiece {
const from = move.from;
console.log(from, robotManager.indicesToIds)
console.log(from, robotManager.indicesToIds);

let mainPiece: string | undefined;

let mainPiece: string | undefined

for (const [key, value] of robotManager.indicesToIds) {
if (key.i === from.i && key.j === from.j) {
mainPiece = value;
break;
}
}

if (mainPiece !== undefined) {
console.log("main piece");
const to = move.to;
Expand Down Expand Up @@ -426,21 +431,24 @@ function directionToEdge(position: GridIndices) {
return DirectionTuple;
}


function returnToHome(from: Square, 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 startInDeadzone = fastestMoveToDeadzone.to;
let finalDestination;

const checkDirections = [new GridIndices(0,1), new GridIndices(1,0), new GridIndices(-1,0), new GridIndices(0,-1)];
const checkDirections = [
new GridIndices(0, 1),
new GridIndices(1, 0),
new GridIndices(-1, 0),
new GridIndices(0, -1),
];

for (const direction of checkDirections) {
if (arrayOfDeadzone.includes(home.add(direction)))
{
if (arrayOfDeadzone.includes(home.add(direction))) {
finalDestination = home.add(direction);
}
}
Expand All @@ -449,30 +457,45 @@ function returnToHome(from: Square, id: string): SequentialCommandGroup {
const endInArray = arrayOfDeadzone.indexOf(finalDestination);
let differenceOfIndex = endInArray - startInArray;

if(differenceOfIndex < 0 )
{
if (differenceOfIndex < 0) {
differenceOfIndex += 36;
}

const botDirectionToHome = differenceOfIndex < 18 ? 1 : -1;

let i = startInArray;
const moveCommands: MoveCommand[] = [];
while (i != endInArray)
{
while (i !== endInArray) {
if (arrayOfCornersIndicies.includes(i)) {
moveCommands.push(new AbsoluteMoveCommand(id, new Position(arrayOfDeadzone[i].i + 0.5, arrayOfDeadzone[i].j + 0.5)));
moveCommands.push(
new AbsoluteMoveCommand(
id,
new Position(
arrayOfDeadzone[i].i + 0.5,
arrayOfDeadzone[i].j + 0.5,
),
),
);
}
i += botDirectionToHome;
}
moveCommands.push(new AbsoluteMoveCommand(id, new Position(arrayOfDeadzone[endInArray].i + 0.5, arrayOfDeadzone[endInArray].j + 0.5)));
moveCommands.push(
new AbsoluteMoveCommand(
id,
new Position(
arrayOfDeadzone[endInArray].i + 0.5,
arrayOfDeadzone[endInArray].j + 0.5,
),
),
);

moveCommands.push(new AbsoluteMoveCommand(id, new Position(home.i + 0.5, home.j + 0.5)));

moveCommands.push(
new AbsoluteMoveCommand(id, new Position(home.i + 0.5, home.j + 0.5)),
);

const goHome: SequentialCommandGroup = new SequentialCommandGroup([
toDeadzone,
...moveCommands
...moveCommands,
]);

return goHome;
Expand All @@ -488,7 +511,10 @@ export function materializePath(move: Move): Command {
if (gameManager?.chess.isEnPassant(move)) {
return new SequentialCommandGroup([]);
} else if (gameManager?.chess.isRegularCapture(move)) {
const capturePiece = gameManager?.chess.getCapturedPieceId(move, robotManager);
const capturePiece = gameManager?.chess.getCapturedPieceId(
move,
robotManager,
);
if (capturePiece !== undefined) {
const captureCommand = returnToHome(move.to, capturePiece);
const mainCommand = moveMainPiece(moveToGridMove(move));
Expand Down
12 changes: 9 additions & 3 deletions src/server/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,15 @@ export const virtualRobots = new Map<string, VirtualRobot>(
id,
new VirtualRobot(
id,
new GridIndices(realRobotConfig.homePosition.x, realRobotConfig.homePosition.y),
new GridIndices(realRobotConfig.defaultPosition.x, realRobotConfig.defaultPosition.y),
getStartHeading((idx < 16) ? Side.WHITE : Side.BLACK),
new GridIndices(
realRobotConfig.homePosition.x,
realRobotConfig.homePosition.y,
),
new GridIndices(
realRobotConfig.defaultPosition.x,
realRobotConfig.defaultPosition.y,
),
getStartHeading(idx < 16 ? Side.WHITE : Side.BLACK),
new Position(
realRobotConfig.defaultPosition.x + 0.5,
realRobotConfig.defaultPosition.y + 0.5,
Expand Down

0 comments on commit 375c756

Please sign in to comment.