Skip to content

Commit c2d7b5e

Browse files
committed
Added pause rollback
allowed pause to rollback the robot positions. Also allowed saveGame to load from a saved positions, so hopefully less errors
1 parent 072b3a4 commit c2d7b5e

File tree

9 files changed

+75
-47
lines changed

9 files changed

+75
-47
lines changed

src/client/game/game.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function getMessageHandler(
4242
setGameInterruptedReason: Dispatch<GameInterruptedReason>,
4343
setGameEndedReason: Dispatch<GameEndReason>,
4444
setGameHoldReason: Dispatch<GameHoldReason>,
45-
setPaused : Dispatch<boolean>,
45+
setPaused: Dispatch<boolean>,
4646
): MessageHandler {
4747
return (message) => {
4848
if (message instanceof MoveMessage) {
@@ -65,9 +65,9 @@ function getMessageHandler(
6565
setGameEndedReason(message.reason);
6666
} else if (message instanceof GameHoldMessage) {
6767
setGameHoldReason(message.reason);
68-
if(message.reason === GameHoldReason.GAME_PAUSED){
68+
if (message.reason === GameHoldReason.GAME_PAUSED) {
6969
setPaused(true);
70-
} else if(message.reason === GameHoldReason.GAME_UNPAUSED){
70+
} else if (message.reason === GameHoldReason.GAME_UNPAUSED) {
7171
setPaused(false);
7272
}
7373
}
@@ -161,27 +161,30 @@ export function Game(): JSX.Element {
161161
: null
162162
: null;
163163

164-
const gamePauseDialog =
164+
const gamePauseDialog =
165165
gameHoldReason !== undefined ?
166166
gameHoldReason === GameHoldReason.GAME_PAUSED ?
167167
<PauseDialog />
168-
: null
169-
: null;
168+
: null
169+
: null;
170170

171-
const gameUnpauseDialog =
171+
const gameUnpauseDialog =
172172
gameHoldReason !== undefined ?
173173
gameHoldReason === GameHoldReason.GAME_UNPAUSED ?
174-
<NotificationDialog dialogText="Game Unpaused"/>
175-
: null
176-
: null;
174+
<NotificationDialog dialogText="Game Unpaused" />
175+
: null
176+
: null;
177177

178178
/** make moves by making a copy of the chessboard and sending the move message */
179-
const handleMove = !paused?
179+
const handleMove =
180+
!paused ?
180181
(move: Move): void => {
181182
setChess(chess.copy(move));
182183
sendMessage(new MoveMessage(move));
183184
}
184-
: (move:Move):void => {move;}; //send a do-nothing function if game is paused
185+
: (move: Move): void => {
186+
move;
187+
}; //send a do-nothing function if game is paused
185188

186189
// return the chessboard wrapper, navbar, and potential end dialog
187190
return (

src/server/api/api.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ import {
5151
SpinRadiansCommand,
5252
} from "../command/move-command";
5353
import { GridIndices } from "../robot/grid-indices";
54-
import { puzzles, type PuzzleComponents } from "./puzzles";
5554
import {
5655
moveAllRobotsHomeToDefaultOptimized,
5756
moveAllRobotsToDefaultPositions,
58-
moveAllRobotsFromBoardToHome,
5957
} from "../robot/path-materializer";
58+
import type { PuzzleComponents } from "./puzzles";
59+
import { puzzles } from "./puzzles";
6060
import { tcpServer } from "./tcp-interface";
6161
import { robotManager } from "../robot/robot-manager";
6262
import { executor } from "../command/executor";
@@ -76,7 +76,7 @@ async function setupDefaultRobotPositions(
7676
moveAllRobotsToDefaultPositions(defaultPositionsMap);
7777
await executor.execute(command);
7878
} else {
79-
setAllRobotsToDefaultPositions(defaultPositionsMap);
79+
moveAllRobotsToDefaultPositions(defaultPositionsMap);
8080
}
8181
} else {
8282
if (isMoving) {
@@ -186,6 +186,17 @@ apiRouter.get("/client-information", async (req, res) => {
186186
),
187187
);
188188
}
189+
const robotPos = new Map(
190+
oldSave!.robotPos?.map<[string, GridIndices]>((obj) => [
191+
obj[1],
192+
new GridIndices(
193+
parseInt(obj[0].split(", ")[0]),
194+
parseInt(obj[0].split(", ")[1]),
195+
),
196+
]),
197+
);
198+
console.log(robotPos);
199+
setAllRobotsToDefaultPositions(robotPos);
189200
}
190201
/**
191202
* Note the client currently redirects to home from the game over screen
@@ -285,7 +296,6 @@ apiRouter.post("/start-puzzle-game", async (req, res) => {
285296
const fen = puzzle.fen;
286297
const moves = puzzle.moves;
287298
const difficulty = puzzle.rating;
288-
const tooltip = puzzle.tooltip;
289299

290300
if (puzzle.robotDefaultPositions) {
291301
// Convert puzzle.robotDefaultPositions from Record<string, string> to Map<string, GridIndices>
@@ -322,7 +332,7 @@ apiRouter.post("/start-puzzle-game", async (req, res) => {
322332
new ChessEngine(),
323333
socketManager,
324334
fen,
325-
tooltip,
335+
"",
326336
moves,
327337
difficulty,
328338
),
@@ -331,15 +341,6 @@ apiRouter.post("/start-puzzle-game", async (req, res) => {
331341
return res.send({ message: "success" });
332342
});
333343

334-
/**
335-
* Returns robots to home after a game ends.
336-
*/
337-
apiRouter.post("/return-home", async (_req, res) => {
338-
const command = moveAllRobotsFromBoardToHome();
339-
await executor.execute(command);
340-
return res.send({ message: "success" });
341-
});
342-
343344
/**
344345
* Returns all registered robot ids
345346
*/
@@ -575,6 +576,23 @@ apiRouter.get("/pause-game", (_, res) => {
575576
*/
576577
apiRouter.get("/unpause-game", async (_, res) => {
577578
gamePaused.flag = false;
579+
const ids = clientManager.getIds();
580+
if (ids) {
581+
const oldSave = SaveManager.loadGame(ids[0]);
582+
gameManager?.chess.loadFen(oldSave!.oldPos);
583+
setAllRobotsToDefaultPositions(
584+
new Map(
585+
oldSave!.oldRobotPos?.map<[string, GridIndices]>((obj) => [
586+
obj[1],
587+
new GridIndices(
588+
parseInt(obj[0].split(", ")[0]),
589+
parseInt(obj[0].split(", ")[1]),
590+
),
591+
]),
592+
),
593+
);
594+
socketManager.sendToAll(new SetChessMessage(oldSave!.oldPos));
595+
}
578596
socketManager.sendToAll(new GameHoldMessage(GameHoldReason.GAME_UNPAUSED));
579597
return res.send({ message: "success" });
580598
});

src/server/api/game-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export class HumanGameManager extends GameManager {
220220
SaveManager.endGame(ids[0], ids[1]);
221221
else SaveManager.endGame(ids[1], ids[0]);
222222
}
223-
}
223+
}
224224
}
225225
}
226226

src/server/api/managers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { SocketManager } from "./socket-manager";
99
export const socketManager = new SocketManager({});
1010
export const clientManager = new ClientManager(socketManager);
1111
export let gameManager: GameManager | null = null;
12-
export const gamePaused = {flag:false};
12+
export const gamePaused = { flag: false };
1313

1414
export function setGameManager(manager: GameManager) {
1515
gameManager = manager;

src/server/api/save-manager.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* Modified: 9/20/24
1111
*/
1212

13-
import { Side } from "../../common/game-types";2
13+
import { Side } from "../../common/game-types";
14+
2;
1415

1516
// Save files contain a date in ms and pgn string
1617
export interface iSave {
@@ -19,10 +20,10 @@ export interface iSave {
1920
hostWhite: boolean;
2021
aiDifficulty: number;
2122
game: string;
22-
pos:string;
23-
robotPos: Array<[string,string]>;
23+
pos: string;
24+
robotPos: Array<[string, string]>;
2425
oldPos: string;
25-
oldRobotPos: Array<[string,string]>;
26+
oldRobotPos: Array<[string, string]>;
2627
}
2728

2829
export class SaveManager {
@@ -57,7 +58,7 @@ export class SaveManager {
5758
pos: fen,
5859
robotPos: Array.from(robots),
5960
oldPos: "",
60-
oldRobotPos: Array<[string,string]>(),
61+
oldRobotPos: Array<[string, string]>(),
6162
};
6263
const oldGame = SaveManager.loadGame(hostId + "+" + clientID);
6364
if (oldGame && oldGame.pos !== null) {

src/server/command/command.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ function isReversable(obj): obj is Reversible<typeof obj> {
113113
*/
114114
export class ParallelCommandGroup extends CommandGroup {
115115
public async execute(): Promise<void> {
116-
const promises = this.commands.map((move) => {gamePaused.flag?null:move.execute()});
116+
const promises = this.commands.map((move) => {
117+
gamePaused.flag ? null : move.execute();
118+
});
117119
return Promise.all(promises).then(null);
118120
}
119121
public async reverse(): Promise<void> {
@@ -133,7 +135,9 @@ export class SequentialCommandGroup extends CommandGroup {
133135
public async execute(): Promise<void> {
134136
let promise = Promise.resolve();
135137
for (const command of this.commands) {
136-
promise = promise.then(() => {gamePaused?null:command.execute()});
138+
promise = promise.then(() => {
139+
gamePaused.flag ? null : command.execute();
140+
});
137141
}
138142
return promise;
139143
}

src/server/command/executor.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ export class CommandExecutor {
4343
public async execute(command: Command): Promise<void> {
4444
this.checkRequirements(command);
4545
this.runningCommands.push(command);
46-
if(!gamePaused.flag){
46+
if (!gamePaused.flag) {
4747
return command.execute().finally(() => {
4848
this.oldCommands.unshift(command);
4949
const index = this.runningCommands.indexOf(command);
5050
if (index >= 0) {
5151
this.runningCommands.splice(index, 1);
5252
}
53-
})
53+
});
5454
}
5555
}
5656

@@ -59,19 +59,19 @@ export class CommandExecutor {
5959
* mainly used to finish the backlog from a paused game
6060
* @returns - The command to execute.
6161
*/
62-
public async finishExecution() : Promise<void> {
63-
return this.runningCommands.forEach((command) =>{
64-
command.execute().finally(()=>{
62+
public async finishExecution(): Promise<void> {
63+
return this.runningCommands.forEach((command) => {
64+
command.execute().finally(() => {
6565
this.oldCommands.unshift(command);
6666
const index = this.runningCommands.indexOf(command);
6767
if (index >= 0) {
6868
this.runningCommands.splice(index, 1);
6969
}
70-
})
71-
})
70+
});
71+
});
7272
}
7373

74-
public clearExecution(){
74+
public clearExecution() {
7575
this.runningCommands = [];
7676
}
7777

@@ -83,7 +83,7 @@ export class CommandExecutor {
8383
return this.oldCommands;
8484
}
8585

86-
public clearOldCommands(){
86+
public clearOldCommands() {
8787
this.oldCommands = [];
8888
}
8989
}

src/server/robot/robot-manager.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ export class RobotManager {
100100
indicesToIds.set(indices.toString(), robotId);
101101
}
102102

103-
stopAllRobots(){
104-
Array.from(this.idsToRobots.values()).forEach((robot)=>{robot.sendStopPacket()})
103+
stopAllRobots() {
104+
Array.from(this.idsToRobots.values()).forEach((robot) => {
105+
robot.sendStopPacket();
106+
});
105107
}
106108
}
107109

src/server/robot/robot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class Robot {
187187
});
188188
}
189189

190-
public async sendStopPacket():Promise<void>{
190+
public async sendStopPacket(): Promise<void> {
191191
await this.tunnel!.send({
192192
type: PacketType.ESTOP,
193193
});

0 commit comments

Comments
 (0)