Commit 50dfa86d authored by nanahira's avatar nanahira

add DuelRecord.toPlayback

parent 382ebb8f
......@@ -427,7 +427,7 @@ export class CloudReplayService {
duelRecord: DuelRecord,
viewMode: ReplayWatchViewMode = 'default',
) {
return duelRecord.toObserverPlayback(
return duelRecord.toPlayback(
viewMode === 'default'
? (msg) => msg
: viewMode === 'observer'
......
......@@ -5,6 +5,7 @@ import {
NetPlayerType,
YGOProMsgBase,
YGOProMsgResponseBase,
YGOProMsgStart,
YGOProMsgWin,
YGOProStocGameMsg,
} from 'ygopro-msg-encode';
......@@ -118,14 +119,21 @@ export class DuelRecord {
return yrp;
}
*toObserverPlayback(
*toPlayback(
cb: (msg: YGOProMsgBase) => YGOProMsgBase | undefined = (msg) => msg,
options: {
includeResponse?: boolean;
includeNonObserver?: boolean;
msgStartPos?: number;
} = {},
): Generator<YGOProStocGameMsg, void, unknown> {
let recordedWinMsg: YGOProMsgWin | undefined;
for (const message of this.messages) {
for (let message of this.messages) {
if (message instanceof YGOProMsgResponseBase) {
continue;
if (!options.includeResponse) {
continue;
}
}
if (message instanceof YGOProMsgWin) {
if (!recordedWinMsg) {
......@@ -133,9 +141,18 @@ export class DuelRecord {
}
continue;
}
if (!message.getSendTargets().includes(NetPlayerType.OBSERVER)) {
if (
!options.includeNonObserver &&
!message.getSendTargets().includes(NetPlayerType.OBSERVER)
) {
continue;
}
if (options.msgStartPos != null && message instanceof YGOProMsgStart) {
message = new YGOProMsgStart().fromPartial({
...message,
playerType: options.msgStartPos,
});
}
const mappedMsg = cb(message);
if (!mappedMsg) {
continue;
......
......@@ -354,7 +354,7 @@ export class Room {
: this.duelRecords;
if (previousDuels.length) {
for (const duelRecord of previousDuels) {
for (const message of duelRecord.toObserverPlayback((msg) =>
for (const message of duelRecord.toPlayback((msg) =>
msg.observerView(),
)) {
await client.send(message);
......@@ -375,7 +375,7 @@ export class Room {
await client.send(new YGOProStocWaitingSide());
} else if (this.duelStage === DuelStage.Dueling) {
// Dueling 阶段不发 DeckCount,直接发送观战消息
for (const message of this.lastDuelRecord?.toObserverPlayback((msg) =>
for (const message of this.lastDuelRecord?.toPlayback((msg) =>
msg.observerView(),
) || []) {
await client.send(message);
......
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