Commit 3874f4a5 authored by nanahira's avatar nanahira

just before duel start

parent 76b99077
......@@ -14,6 +14,7 @@ export class DuelRecord {
public seed: number[],
public players: { name: string; deck: YGOProDeck }[],
) {}
date = new Date();
winPosition?: number;
responses: Buffer[] = [];
......@@ -29,6 +30,8 @@ export class DuelRecord {
header.flag |= REPLAY_TAG;
}
header.seedSequence = this.seed;
// Set start_time (stored in hash field) as Unix timestamp in seconds
header.hash = Math.floor(this.date.getTime() / 1000);
// Build YGOProYrp object
// Note: players array is already swapped
......
import 'reflect-metadata';
import { Context } from '../app';
import { getSpecificFields } from '../utility/metadata';
import { RoomMethodOptions } from '../utility/decorators';
import { Room } from './room';
import { Client } from '../client/client';
import { YGOProCtosBase } from 'ygopro-msg-encode';
import { RoomManager } from './room-manager';
import { makeArray } from 'nfkit';
export class RoomEventRegister {
private logger = this.ctx.createLogger('RoomEventRegister');
......@@ -15,7 +17,7 @@ export class RoomEventRegister {
private registerRoomEvents() {
const roomMethods = getSpecificFields('roomMethod', Room);
for (const { key: method } of roomMethods) {
for (const { key: method, metadata } of roomMethods) {
// 获取方法的参数类型
const paramTypes: any[] =
Reflect.getMetadata('design:paramtypes', Room.prototype, method) || [];
......@@ -48,6 +50,9 @@ export class RoomEventRegister {
continue;
}
// 获取方法选项
const options: RoomMethodOptions = metadata;
// 注册 middleware
this.ctx.middleware(ctosParamType, async (msg, client, next) => {
// 如果 client 没有关联的 room,直接跳过
......@@ -62,6 +67,14 @@ export class RoomEventRegister {
return next();
}
// 检查 DuelStage 限制
if (options?.allowInDuelStages) {
const allowedStages = makeArray(options.allowInDuelStages);
if (!allowedStages.includes(room.duelStage)) {
return next();
}
}
// 构造参数数组
const args = new Array(paramTypes.length);
for (let i = 0; i < paramTypes.length; i++) {
......
This diff is collapsed.
import { Metadata } from './metadata';
import { DuelStage } from '../room/duel-stage';
import { MayBeArray } from 'nfkit';
export const RoomMethod = (): MethodDecorator =>
Metadata.set('roomMethod', true, 'roomMethodKeys');
export interface RoomMethodOptions {
allowInDuelStages?: MayBeArray<DuelStage>;
}
export const RoomMethod = (options: RoomMethodOptions = {}): MethodDecorator =>
Metadata.set('roomMethod', options, 'roomMethodKeys');
import { MetadataSetter, Reflector } from 'typed-reflector';
import type { RoomMethodOptions } from './decorators';
interface MetadataMap {
roomMethod: boolean;
roomMethod: RoomMethodOptions;
}
type MetadataArrayMap = {
......
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