Commit 382ebb8f authored by nanahira's avatar nanahira

add sliceOcgcore

parent 6fd9668d
...@@ -1172,7 +1172,7 @@ export class Room { ...@@ -1172,7 +1172,7 @@ export class Room {
get turnPos() { get turnPos() {
return this.getIngameDuelPosByDuelPos(this.turnIngamePos); return this.getIngameDuelPosByDuelPos(this.turnIngamePos);
} }
phase = undefined; phase: number | undefined = undefined;
timerState = new TimerState(); timerState = new TimerState();
lastResponseRequestMsg?: YGOProMsgResponseBase; lastResponseRequestMsg?: YGOProMsgResponseBase;
isRetrying = false; isRetrying = false;
...@@ -1501,9 +1501,30 @@ export class Room { ...@@ -1501,9 +1501,30 @@ export class Room {
return this.advance(); return this.advance();
} }
private async onNewTurn(tp: number) { setNewTurn(tp: number) {
if (tp & 0x2) {
return;
}
tp = tp & 0x1;
++this.turnCount; ++this.turnCount;
this.turnIngamePos = tp; this.turnIngamePos = tp;
return this;
}
setNewPhase(phase: number) {
this.phase = phase;
return this;
}
setLastResponseRequestMsg(message: YGOProMsgResponseBase) {
this.lastResponseRequestMsg = message;
this.isRetrying = false;
this.responsePos = this.getIngameDuelPosByDuelPos(message.responsePlayer());
return this;
}
private async onNewTurn(tp: number) {
this.setNewTurn(tp);
if (!this.hasTimeLimit) { if (!this.hasTimeLimit) {
return; return;
} }
...@@ -1516,7 +1537,7 @@ export class Room { ...@@ -1516,7 +1537,7 @@ export class Room {
} }
private async onNewPhase(phase: number) { private async onNewPhase(phase: number) {
this.phase = phase; this.setNewPhase(phase);
} }
getIngameOperatingPlayer(ingameDuelPos: number): Client | undefined { getIngameOperatingPlayer(ingameDuelPos: number): Client | undefined {
...@@ -1691,11 +1712,7 @@ export class Room { ...@@ -1691,11 +1712,7 @@ export class Room {
} }
if (message instanceof YGOProMsgResponseBase) { if (message instanceof YGOProMsgResponseBase) {
this.lastResponseRequestMsg = message; this.setLastResponseRequestMsg(message);
this.isRetrying = false;
this.responsePos = this.getIngameDuelPosByDuelPos(
message.responsePlayer(),
);
await this.sendWaitingToNonOperator(message.responsePlayer()); await this.sendWaitingToNonOperator(message.responsePlayer());
await this.setResponseTimer(this.responsePos); await this.setResponseTimer(this.responsePos);
return; return;
...@@ -1804,7 +1821,7 @@ export class Room { ...@@ -1804,7 +1821,7 @@ export class Room {
); );
} }
private canAdvance() { canAdvance() {
return this.duelStage === DuelStage.Dueling && !!this.ocgcore; return this.duelStage === DuelStage.Dueling && !!this.ocgcore;
} }
......
import {
YGOProMsgNewPhase,
YGOProMsgNewTurn,
YGOProMsgResponseBase,
YGOProMsgRetry,
} from 'ygopro-msg-encode';
import { Room } from '../room';
export const sliceOcgcore = async (room: Room, i: number) => {
if (
!room.lastDuelRecord ||
!room.ocgcore ||
!(await room.createOcgcore(room.lastDuelRecord))
) {
throw new Error('Failed to create ocgcore');
}
room.resetDuelState();
const useResponses = room.lastDuelRecord.responses.slice(0, i);
let messagePointer = 0; // 1st message is MSG_START and we skip it
while (useResponses.length) {
for await (const { message, status, raw } of room.ocgcore!.advance()) {
if (!message) {
if (status) {
throw new Error(
`Got empty message but non-advance status: ${status}`,
);
}
continue;
}
if (message instanceof YGOProMsgRetry) {
// no retry here
throw new Error('Unexpected retry message');
}
const expectedMessage = room.lastDuelRecord.messages[++messagePointer];
if (!expectedMessage) {
throw new Error(
`No more expected messages but got ${message.constructor.name} with payload ${Buffer.from(raw).toString('hex')}`,
);
}
if (!Buffer.from(raw).equals(Buffer.from(expectedMessage.toPayload()))) {
throw new Error(
`Message mismatch at position ${messagePointer - 1}: expected ${expectedMessage.constructor.name} with payload ${Buffer.from(expectedMessage.toPayload()).toString('hex')}, got ${message.constructor.name} with payload ${Buffer.from(raw).toString('hex')}`,
);
}
if (message instanceof YGOProMsgNewTurn) {
room.setNewTurn(message.player);
} else if (message instanceof YGOProMsgNewPhase) {
room.setNewPhase(message.phase);
} else if (message instanceof YGOProMsgResponseBase) {
room.setLastResponseRequestMsg(
expectedMessage as YGOProMsgResponseBase, // use exact same reference as the one in the record to avoid issues in response matching
);
}
}
const response = useResponses.shift();
if (!response) {
break;
}
await room.ocgcore!.setResponse(response);
}
room.lastDuelRecord.responsesWithPos.splice(i);
room.lastDuelRecord.messages.splice(messagePointer + 1);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment