1
0

Fixing bugs

This commit is contained in:
James Ketr 2025-10-10 17:07:20 -07:00
parent 0818145a81
commit b2e5fe4e03
3 changed files with 26 additions and 8 deletions

View File

@ -353,8 +353,13 @@ const distributeResources = (game: Game, roll: number): void => {
}); });
const receives: Received = {} as Received; const receives: Received = {} as Received;
/* Initialize all fields of 'receives' to zero so we can safely increment any
* matched tile */
PLAYER_COLORS.forEach((color) => { PLAYER_COLORS.forEach((color) => {
receives[color] = { wood: 0, brick: 0, sheep: 0, wheat: 0, stone: 0, desert: 0 }; receives[color] = {} as ResourceCount;
RESOURCE_TYPES.forEach((type) => {
receives[color]![type] = 0;
});
}); });
/* Find which corners are on each tile */ /* Find which corners are on each tile */
@ -381,7 +386,11 @@ const distributeResources = (game: Game, roll: number): void => {
const count = active.type === "settlement" ? 1 : 2; const count = active.type === "settlement" ? 1 : 2;
if (!tile.robber) { if (!tile.robber) {
if (resource.type) { if (resource.type) {
receives[active.color][resource.type]! += count; try {
receives[active.color][resource.type]! += count;
} catch (e) {
console.error("Error incrementing resources", { receives, active, resource, count });
}
} }
} else { } else {
const victim = game.players[active.color]; const victim = game.players[active.color];
@ -779,7 +788,9 @@ const loadGame = async (id: string): Promise<Game> => {
// Check if we need to place a settlement (no action or place-settlement action) // Check if we need to place a settlement (no action or place-settlement action)
if (!game.turn.actions || game.turn.actions.length === 0 || game.turn.actions.indexOf("place-settlement") !== -1) { if (!game.turn.actions || game.turn.actions.length === 0 || game.turn.actions.indexOf("place-settlement") !== -1) {
setForSettlementPlacement(game, getValidCorners(game, currentColor)); // During initial placement, settlements may be placed on any valid corner
// (they are not constrained by existing roads), so do not filter by color.
setForSettlementPlacement(game, getValidCorners(game));
console.log( console.log(
`${info}: Set turn limits for settlement placement (${game.turn.limits?.corners?.length || 0} valid corners)` `${info}: Set turn limits for settlement placement (${game.turn.limits?.corners?.length || 0} valid corners)`
); );
@ -2820,7 +2831,9 @@ const placeRoad = (game: Game, session: Session, index: number): string | undefi
name: game.players[nextColor].name, name: game.players[nextColor].name,
color: nextColor, color: nextColor,
} as unknown as Turn; } as unknown as Turn;
setForSettlementPlacement(game, getValidCorners(game, nextColor)); // During initial placement forward pass, allow any valid corner
// (not restricted by the player's existing roads).
setForSettlementPlacement(game, getValidCorners(game));
addChatMessage(game, null, `It is ${game.turn.name}'s turn to place a settlement.`); addChatMessage(game, null, `It is ${game.turn.name}'s turn to place a settlement.`);
} }
} }

View File

@ -105,7 +105,7 @@ export interface PersistentSessionData {
} }
export type PlayerColor = "R" | "B" | "O" | "W" | "robber" | "unassigned"; export type PlayerColor = "R" | "B" | "O" | "W" | "robber" | "unassigned";
export const PLAYER_COLORS = ["R", "B", "O", "W", "robber"] as PlayerColor[]; export const PLAYER_COLORS: PlayerColor[] = ["R", "B", "O", "W", "robber", "unassigned"];
/** /**
* Runtime Session type = Persistent + Transient * Runtime Session type = Persistent + Transient

View File

@ -95,12 +95,16 @@ const getValidCorners = (game: Game, color: PlayerColor = "unassigned", type?: C
// If the corner has a color set and it's not the explicit sentinel // If the corner has a color set and it's not the explicit sentinel
// "unassigned" then it's occupied and should be skipped. // "unassigned" then it's occupied and should be skipped.
if (placement.color !== "unassigned") { // Note: placement.color may be undefined (initial state), treat that
// the same as unassigned.
if (placement.color && placement.color !== "unassigned") {
return; return;
} }
let valid; let valid;
if (!color) { // Treat either a falsy color (""/undefined) or the explicit sentinel
// "unassigned" as meaning "do not filter by player".
if (!color || color === "unassigned") {
valid = true; /* Not filtering based on current player */ valid = true; /* Not filtering based on current player */
} else { } else {
valid = false; valid = false;
@ -132,7 +136,8 @@ const getValidCorners = (game: Game, color: PlayerColor = "unassigned", type?: C
/* There is a settlement within one segment from this /* There is a settlement within one segment from this
* corner, so it is invalid for settlement placement */ * corner, so it is invalid for settlement placement */
const cc = road.corners[c] as number; const cc = road.corners[c] as number;
if (game.placements.corners[cc]!.color !== "unassigned") { const ccColor = game.placements.corners[cc]!.color;
if (ccColor && ccColor !== "unassigned") {
valid = false; valid = false;
} }
} }