Commit c5815aaa authored by nanahira's avatar nanahira

timer

parent 6a8271cd
This diff is collapsed.
export class TimerState {
leftMs: [number, number] = [0, 0];
compensatorMs: [number, number] = [0, 0];
backedMs: [number, number] = [0, 0];
runningPos?: number;
startedAtMs = 0;
awaitingConfirm = false;
private timer?: ReturnType<typeof setTimeout>;
reset(initialMs: number) {
this.leftMs = [initialMs, initialMs];
this.compensatorMs = [initialMs, initialMs];
this.backedMs = [initialMs, initialMs];
this.clear();
}
clear(settleElapsed = false) {
if (this.timer) {
clearTimeout(this.timer);
this.timer = undefined;
}
if (settleElapsed && this.runningPos != null) {
const elapsedMs = this.elapsedMs();
if (elapsedMs > 0) {
this.leftMs[this.runningPos] = Math.max(
0,
this.leftMs[this.runningPos] - elapsedMs,
);
}
}
this.runningPos = undefined;
this.startedAtMs = 0;
this.awaitingConfirm = false;
}
elapsedMs() {
if (!this.startedAtMs) {
return 0;
}
return Math.max(0, Date.now() - this.startedAtMs);
}
schedule(
player: number,
delayMs: number,
awaitingConfirm: boolean,
onTimeout: () => void,
) {
this.runningPos = player;
this.startedAtMs = Date.now();
this.awaitingConfirm = awaitingConfirm;
this.timer = setTimeout(onTimeout, delayMs);
}
}
import { OcgcoreCommonConstants } from 'ygopro-msg-encode';
export const canIncreaseTime = (gameMsg: number, response?: Buffer) => {
switch (gameMsg) {
case OcgcoreCommonConstants.MSG_RETRY:
case OcgcoreCommonConstants.MSG_SELECT_UNSELECT_CARD:
return false;
case OcgcoreCommonConstants.MSG_SELECT_CHAIN:
return response != null && response.length >= 4 && response.readInt32LE(0) !== -1;
case OcgcoreCommonConstants.MSG_SELECT_IDLECMD: {
if (response == null || response.length < 4) {
return false;
}
const idleChoice = response.readInt32LE(0) & 0xffff;
return idleChoice <= 5;
}
case OcgcoreCommonConstants.MSG_SELECT_BATTLECMD: {
if (response == null || response.length < 4) {
return false;
}
const battleChoice = response.readInt32LE(0) & 0xffff;
return battleChoice <= 1;
}
default:
return true;
}
};
import { YGOProMsgBase } from 'ygopro-msg-encode';
export const getMessageIdentifier = (message: YGOProMsgBase) =>
((message.constructor as any).identifier as number) ?? 0;
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