feat: add copy constructor.

This commit is contained in:
Rintim 2024-01-06 02:00:12 +08:00
parent e24b14b676
commit f5aba85f22
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
10 changed files with 77 additions and 19 deletions

View File

@ -15,6 +15,11 @@ export class Button extends HTMLDivElement {
*/
// @ts-ignore
constructor(item, type, position, noClick, button) {
if (item instanceof Button) {
const other = item;
// @ts-ignore
[item, type, position, noClick, button] = other._args;
}
if (typeof type == 'function') button = type(item, type, position, noClick, button);
else if (ui.create.buttonPresets[type]) button = ui.create.buttonPresets[type](item, type, position, noClick, button);
if (button) {
@ -25,6 +30,8 @@ export class Button extends HTMLDivElement {
const intro = button.querySelector('.intro');
if (intro) intro.remove();
}
// @ts-ignore
button._args = [item, type, position, noClick, button];
return button;
} else {
console.error([item, type, position, noClick, button]);

View File

@ -13,6 +13,11 @@ export class Card extends HTMLDivElement {
*/
// @ts-ignore
constructor(position, info, noclick) {
if (position instanceof Card) {
const other = position;
// @ts-ignore
[position, info, noclick] = other._args;
}
/**
* @type {this}
*/
@ -20,6 +25,8 @@ export class Card extends HTMLDivElement {
const card = ui.create.div('.card', position);
Object.setPrototypeOf(card, Card.prototype);
card.build(info, noclick);
// @ts-ignore
card._args = [position, info, noclick];
return card;
}
build(info, noclick) {

View File

@ -8,9 +8,10 @@ import { GNC as gnc } from '../../gnc/index.js';
export class Client {
/**
* @param {import('../index.js').NodeWS | InstanceType<typeof import('ws').WebSocket>} ws
* @param {import('../index.js').NodeWS | InstanceType<typeof import('ws').WebSocket> | Client} ws
*/
constructor(ws) {
if (ws instanceof Client) throw new Error('Client cannot copy.')
this.ws = ws;
/**
* @type { string }

View File

@ -8,9 +8,15 @@ import { GNC as gnc } from '../../gnc/index.js';
export class Control extends HTMLDivElement {
// @ts-ignore
constructor() {
constructor(...args) {
if (args[0] instanceof Control) {
const other = args[0];
// @ts-ignore
args = other._args;
}
const nc = !ui.control.querySelector('div:not(.removing):not(.stayleft)');
const controls = Array.isArray(arguments[0]) ? arguments[0] : Array.from(arguments);
const controls = Array.isArray(args[0]) ? args[0] : args;
/**
* @type {this}
*/
@ -54,6 +60,8 @@ export class Control extends HTMLDivElement {
}
ui.updatec();
// @ts-ignore
control._args = args;
return control;
}
open() {

View File

@ -7,7 +7,13 @@ import { UI as ui } from '../../ui/index.js';
export class Dialog extends HTMLDivElement {
// @ts-ignore
constructor() {
constructor(...args) {
if (args[0] instanceof Dialog) {
const other = args[0];
// @ts-ignore
args = other._args;
}
let hidden = false;
let noTouchScroll = false;
let forceButton = false;
@ -21,7 +27,7 @@ export class Dialog extends HTMLDivElement {
dialog.bar1 = ui.create.div('.bar.top', dialog);
dialog.bar2 = ui.create.div('.bar.bottom', dialog);
dialog.buttons = [];
Array.from(arguments).forEach(argument => {
Array.from(args).forEach(argument => {
if (typeof argument == 'boolean') dialog.static = argument;
else if (argument == 'hidden') hidden = true;
else if (argument == 'notouchscroll') noTouchScroll = true;
@ -42,6 +48,8 @@ export class Dialog extends HTMLDivElement {
dialog.forcebutton = true;
dialog.classList.add('forcebutton');
}
// @ts-ignore
dialog._args = args;
return dialog;
}
add(item, noclick, zoom) {

View File

@ -10,15 +10,21 @@ export class GameEvent {
/** @type { GameEventPromise } */
#promise;
/**
* @param {string} [name]
* @param {string | GameEvent} [name]
* @param {false} [trigger]
*/
constructor(name, trigger) {
if (name instanceof GameEvent) {
const other = name;
[name, trigger] = other.__args;
}
if (typeof name == 'string') {
this.name = name;
const gameEvent = get.event();
if (gameEvent) {
const type = `onNext${name[0].toUpperCase()}${name.slice(1)}`;
// @ts-ignore
if (gameEvent.hasHandler(type)) this.pushHandler(...gameEvent.getHandler(type));
}
game.globalEventHandlers.addHandlerToEvent(this);
@ -50,6 +56,7 @@ export class GameEvent {
**/
this.resolve = null;
if (trigger !== false && !game.online) this._triggered = 0;
this.__args = [name, trigger];
}
static initialGameEvent() {
return new GameEvent().finish().toPromise();

View File

@ -37,9 +37,15 @@ export class GameEventPromise extends Promise {
}
#event;
/**
* @param { GameEvent } event
* @param { GameEvent | GameEventPromise } arg
*/
constructor(event) {
constructor(arg) {
if (arg instanceof GameEventPromise)
throw new Error("GameEventPromise cannot copy.")
/**
* @type {GameEvent}
*/
const event = arg;
super(resolve => {
// 设置为异步事件
event.async = true;

View File

@ -8,10 +8,10 @@ import { GNC as gnc } from '../../gnc/index.js';
export class NodeWS {
/**
* @param {string} id
* @param {string | NodeWS} id
*/
constructor(id) {
this.wsid = id;
this.wsid = (id instanceof NodeWS) ? id.wsid : id;
}
send(message) {
game.send('server', 'send', this.wsid, message);

View File

@ -12,6 +12,11 @@ export class Player extends HTMLDivElement {
*/
// @ts-ignore
constructor(position, noclick) {
if (position instanceof Player) {
const other = position;
[position, noclick] = other._args;
}
/**
* @type {this}
*/
@ -19,6 +24,8 @@ export class Player extends HTMLDivElement {
const player = ui.create.div('.player', position);
Object.setPrototypeOf(player, Player.prototype);
player.build(noclick);
// @ts-ignore
player._args = [position, noclick];
return player;
}
/**

View File

@ -13,6 +13,11 @@ export class VCard {
* @param { string } [nature]
*/
constructor(suitOrCard, numberOrCards, name, nature) {
if (suitOrCard instanceof VCard) {
const other = suitOrCard;
[suitOrCard, numberOrCards, name, nature] = other._args;
}
if (Array.isArray(suitOrCard)) {
/**
* @type {string}
@ -91,6 +96,8 @@ export class VCard {
if (typeof nature == 'string') this.nature = nature;
if (!this.storage) this.storage = {};
if (!this.cards) this.cards = [];
this._args = [suitOrCard, numberOrCards, name, nature];
}
sameSuitAs(card) {
return get.suit(this) == get.suit(card);