Skip to content

Commit

Permalink
fix: invalid check
Browse files Browse the repository at this point in the history
  • Loading branch information
noyyyy committed Apr 18, 2024
1 parent 81c0b98 commit db11489
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 34 deletions.
32 changes: 21 additions & 11 deletions packages/client/src/dojo/opRender/opCommitPrepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const opCommitPrepare = async (
Piece,
LocalPiecesChangeTrack,
LocalPiece,
Player,
InningBattle,
GameStatus,
} = clientComponents;
Expand Down Expand Up @@ -51,20 +52,29 @@ export const opCommitPrepare = async (
.filter(Boolean) as PieceChange[];

// validate piece change, it guarantees that the hero idx was not break
const initIndex = new Set(
changes.map((c) => {
const entity = getEntityIdFromKeys([BigInt(c.gid)]);
const remote = getComponentValueStrict(Piece, entity);
return remote.idx;
})
const playerValue = getComponentValueStrict(Player, playerEntity);

const initIdxArr = Array.from(
new Array(playerValue.level),
(v: number, idx: number) => idx + 1
);

const changedIndex = new Set(changes.map((c) => c.idx));
changes.forEach((c) => {
const entity = getEntityIdFromKeys([BigInt(c.gid)]);
const remote = getComponentValueStrict(Piece, entity);

if (!isEqual(initIndex, changedIndex)) {
logDebug("invalid change, block commit");
return;
}
if (remote.idx === 0) {
initIdxArr[c.idx - 1] = c.idx;
} else {
initIdxArr[remote.idx - 1] = c.idx;
}
});

const sortedChangedIndexArr = initIdxArr
.filter(Boolean)
.sort((a, b) => a - b);

logDebug("sortedChangedIndexArr: ", sortedChangedIndexArr);

const { processBattleLogs } = processBattle(clientComponents);

Expand Down
4 changes: 2 additions & 2 deletions packages/client/src/phaser/systems/battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const battle = (layer: PhaserLayer) => {
// ignore stale update
if (
v.round < status?.currentRound &&
v.currentMatch == status.currentMatch
v.currentMatch === status.currentMatch
) {
console.warn("stale inning battle update");
return;
Expand All @@ -68,7 +68,7 @@ export const battle = (layer: PhaserLayer) => {
updateComponent(GameStatus, zeroEntity, {
status: GameStatusEnum.Prepare,
});
} else if (Boolean(v.end) === true) {
} else if (Boolean(v.end) === true && v.winner === 0n) {
updateComponent(GameStatus, zeroEntity, {
status: GameStatusEnum.InBattle,
});
Expand Down
2 changes: 0 additions & 2 deletions packages/client/src/phaser/systems/local/syncSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ export function syncSystem(layer: PhaserLayer) {
if (preV?.owner !== 0n && v.owner === 0n) {
// TODO: remove gids when user sell this piece

console.log("remove: ", v.gid);

if (piecesTrack) {
const gids = piecesTrack.gids;
updateComponent(LocalPiecesChangeTrack, playerEntity, {
Expand Down
2 changes: 0 additions & 2 deletions packages/client/src/phaser/systems/mapSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export function mapSystem(layer: PhaserLayer) {
},
} = layer;

console.log("tiles: ", size());

// for (let x = 0; x < 8; x++) {
// for (let y = 0; y < 8; y++) {
// // const coord = { x, y };
Expand Down
16 changes: 0 additions & 16 deletions packages/client/src/phaser/systems/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export const prepare = (layer: PhaserLayer) => {

// spawn players piece
for (let i = 1; i <= player.heroesCount; i++) {
console.log("trrr");
spawnPiece(player.player, BigInt(i), true);
}

Expand Down Expand Up @@ -127,19 +126,4 @@ export const prepare = (layer: PhaserLayer) => {
}
}
);

// defineSystem(world, [Has(Piece)], ({ entity, type }) => {
// const gameStatus = getComponentValue(GameStatus, zeroEntity);

// if (!gameStatus) {
// return;
// }

// if (
// type === UpdateType.Enter &&
// gameStatus.status === GameStatusEnum.Prepare
// ) {
// spawnPreparedPiece(entity as Entity);
// }
// });
};
2 changes: 1 addition & 1 deletion packages/client/src/ui/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getComponentValueUtilNotNull<
let value = getComponentValue<S, T>(component, entity);
while (!value) {
await sleep(1000);
logDebug(`fetch`, component, "with entity", entity, "again");
logDebug(`fetch`, component.schema, "with entity", entity, "again");
value = getComponentValue<S, T>(component, entity);
}

Expand Down

0 comments on commit db11489

Please sign in to comment.