feat: add copy constructor.
This commit is contained in:
parent
e24b14b676
commit
f5aba85f22
|
@ -15,6 +15,11 @@ export class Button extends HTMLDivElement {
|
||||||
*/
|
*/
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
constructor(item, type, position, noClick, button) {
|
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);
|
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);
|
else if (ui.create.buttonPresets[type]) button = ui.create.buttonPresets[type](item, type, position, noClick, button);
|
||||||
if (button) {
|
if (button) {
|
||||||
|
@ -25,6 +30,8 @@ export class Button extends HTMLDivElement {
|
||||||
const intro = button.querySelector('.intro');
|
const intro = button.querySelector('.intro');
|
||||||
if (intro) intro.remove();
|
if (intro) intro.remove();
|
||||||
}
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
button._args = [item, type, position, noClick, button];
|
||||||
return button;
|
return button;
|
||||||
} else {
|
} else {
|
||||||
console.error([item, type, position, noClick, button]);
|
console.error([item, type, position, noClick, button]);
|
||||||
|
|
|
@ -13,6 +13,11 @@ export class Card extends HTMLDivElement {
|
||||||
*/
|
*/
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
constructor(position, info, noclick) {
|
constructor(position, info, noclick) {
|
||||||
|
if (position instanceof Card) {
|
||||||
|
const other = position;
|
||||||
|
// @ts-ignore
|
||||||
|
[position, info, noclick] = other._args;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @type {this}
|
* @type {this}
|
||||||
*/
|
*/
|
||||||
|
@ -20,6 +25,8 @@ export class Card extends HTMLDivElement {
|
||||||
const card = ui.create.div('.card', position);
|
const card = ui.create.div('.card', position);
|
||||||
Object.setPrototypeOf(card, Card.prototype);
|
Object.setPrototypeOf(card, Card.prototype);
|
||||||
card.build(info, noclick);
|
card.build(info, noclick);
|
||||||
|
// @ts-ignore
|
||||||
|
card._args = [position, info, noclick];
|
||||||
return card;
|
return card;
|
||||||
}
|
}
|
||||||
build(info, noclick) {
|
build(info, noclick) {
|
||||||
|
|
|
@ -8,9 +8,10 @@ import { GNC as gnc } from '../../gnc/index.js';
|
||||||
|
|
||||||
export class Client {
|
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) {
|
constructor(ws) {
|
||||||
|
if (ws instanceof Client) throw new Error('Client cannot copy.')
|
||||||
this.ws = ws;
|
this.ws = ws;
|
||||||
/**
|
/**
|
||||||
* @type { string }
|
* @type { string }
|
||||||
|
|
|
@ -8,9 +8,15 @@ import { GNC as gnc } from '../../gnc/index.js';
|
||||||
|
|
||||||
export class Control extends HTMLDivElement {
|
export class Control extends HTMLDivElement {
|
||||||
// @ts-ignore
|
// @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 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}
|
* @type {this}
|
||||||
*/
|
*/
|
||||||
|
@ -54,6 +60,8 @@ export class Control extends HTMLDivElement {
|
||||||
}
|
}
|
||||||
|
|
||||||
ui.updatec();
|
ui.updatec();
|
||||||
|
// @ts-ignore
|
||||||
|
control._args = args;
|
||||||
return control;
|
return control;
|
||||||
}
|
}
|
||||||
open() {
|
open() {
|
||||||
|
|
|
@ -7,7 +7,13 @@ import { UI as ui } from '../../ui/index.js';
|
||||||
|
|
||||||
export class Dialog extends HTMLDivElement {
|
export class Dialog extends HTMLDivElement {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
constructor() {
|
constructor(...args) {
|
||||||
|
if (args[0] instanceof Dialog) {
|
||||||
|
const other = args[0];
|
||||||
|
// @ts-ignore
|
||||||
|
args = other._args;
|
||||||
|
}
|
||||||
|
|
||||||
let hidden = false;
|
let hidden = false;
|
||||||
let noTouchScroll = false;
|
let noTouchScroll = false;
|
||||||
let forceButton = false;
|
let forceButton = false;
|
||||||
|
@ -21,7 +27,7 @@ export class Dialog extends HTMLDivElement {
|
||||||
dialog.bar1 = ui.create.div('.bar.top', dialog);
|
dialog.bar1 = ui.create.div('.bar.top', dialog);
|
||||||
dialog.bar2 = ui.create.div('.bar.bottom', dialog);
|
dialog.bar2 = ui.create.div('.bar.bottom', dialog);
|
||||||
dialog.buttons = [];
|
dialog.buttons = [];
|
||||||
Array.from(arguments).forEach(argument => {
|
Array.from(args).forEach(argument => {
|
||||||
if (typeof argument == 'boolean') dialog.static = argument;
|
if (typeof argument == 'boolean') dialog.static = argument;
|
||||||
else if (argument == 'hidden') hidden = true;
|
else if (argument == 'hidden') hidden = true;
|
||||||
else if (argument == 'notouchscroll') noTouchScroll = true;
|
else if (argument == 'notouchscroll') noTouchScroll = true;
|
||||||
|
@ -42,6 +48,8 @@ export class Dialog extends HTMLDivElement {
|
||||||
dialog.forcebutton = true;
|
dialog.forcebutton = true;
|
||||||
dialog.classList.add('forcebutton');
|
dialog.classList.add('forcebutton');
|
||||||
}
|
}
|
||||||
|
// @ts-ignore
|
||||||
|
dialog._args = args;
|
||||||
return dialog;
|
return dialog;
|
||||||
}
|
}
|
||||||
add(item, noclick, zoom) {
|
add(item, noclick, zoom) {
|
||||||
|
|
|
@ -10,15 +10,21 @@ export class GameEvent {
|
||||||
/** @type { GameEventPromise } */
|
/** @type { GameEventPromise } */
|
||||||
#promise;
|
#promise;
|
||||||
/**
|
/**
|
||||||
* @param {string} [name]
|
* @param {string | GameEvent} [name]
|
||||||
* @param {false} [trigger]
|
* @param {false} [trigger]
|
||||||
*/
|
*/
|
||||||
constructor(name, trigger) {
|
constructor(name, trigger) {
|
||||||
|
if (name instanceof GameEvent) {
|
||||||
|
const other = name;
|
||||||
|
[name, trigger] = other.__args;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof name == 'string') {
|
if (typeof name == 'string') {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
const gameEvent = get.event();
|
const gameEvent = get.event();
|
||||||
if (gameEvent) {
|
if (gameEvent) {
|
||||||
const type = `onNext${name[0].toUpperCase()}${name.slice(1)}`;
|
const type = `onNext${name[0].toUpperCase()}${name.slice(1)}`;
|
||||||
|
// @ts-ignore
|
||||||
if (gameEvent.hasHandler(type)) this.pushHandler(...gameEvent.getHandler(type));
|
if (gameEvent.hasHandler(type)) this.pushHandler(...gameEvent.getHandler(type));
|
||||||
}
|
}
|
||||||
game.globalEventHandlers.addHandlerToEvent(this);
|
game.globalEventHandlers.addHandlerToEvent(this);
|
||||||
|
@ -50,6 +56,7 @@ export class GameEvent {
|
||||||
**/
|
**/
|
||||||
this.resolve = null;
|
this.resolve = null;
|
||||||
if (trigger !== false && !game.online) this._triggered = 0;
|
if (trigger !== false && !game.online) this._triggered = 0;
|
||||||
|
this.__args = [name, trigger];
|
||||||
}
|
}
|
||||||
static initialGameEvent() {
|
static initialGameEvent() {
|
||||||
return new GameEvent().finish().toPromise();
|
return new GameEvent().finish().toPromise();
|
||||||
|
|
|
@ -37,9 +37,15 @@ export class GameEventPromise extends Promise {
|
||||||
}
|
}
|
||||||
#event;
|
#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 => {
|
super(resolve => {
|
||||||
// 设置为异步事件
|
// 设置为异步事件
|
||||||
event.async = true;
|
event.async = true;
|
||||||
|
|
|
@ -8,10 +8,10 @@ import { GNC as gnc } from '../../gnc/index.js';
|
||||||
|
|
||||||
export class NodeWS {
|
export class NodeWS {
|
||||||
/**
|
/**
|
||||||
* @param {string} id
|
* @param {string | NodeWS} id
|
||||||
*/
|
*/
|
||||||
constructor(id) {
|
constructor(id) {
|
||||||
this.wsid = id;
|
this.wsid = (id instanceof NodeWS) ? id.wsid : id;
|
||||||
}
|
}
|
||||||
send(message) {
|
send(message) {
|
||||||
game.send('server', 'send', this.wsid, message);
|
game.send('server', 'send', this.wsid, message);
|
||||||
|
|
|
@ -12,6 +12,11 @@ export class Player extends HTMLDivElement {
|
||||||
*/
|
*/
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
constructor(position, noclick) {
|
constructor(position, noclick) {
|
||||||
|
if (position instanceof Player) {
|
||||||
|
const other = position;
|
||||||
|
[position, noclick] = other._args;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {this}
|
* @type {this}
|
||||||
*/
|
*/
|
||||||
|
@ -19,6 +24,8 @@ export class Player extends HTMLDivElement {
|
||||||
const player = ui.create.div('.player', position);
|
const player = ui.create.div('.player', position);
|
||||||
Object.setPrototypeOf(player, Player.prototype);
|
Object.setPrototypeOf(player, Player.prototype);
|
||||||
player.build(noclick);
|
player.build(noclick);
|
||||||
|
// @ts-ignore
|
||||||
|
player._args = [position, noclick];
|
||||||
return player;
|
return player;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -13,6 +13,11 @@ export class VCard {
|
||||||
* @param { string } [nature]
|
* @param { string } [nature]
|
||||||
*/
|
*/
|
||||||
constructor(suitOrCard, numberOrCards, name, nature) {
|
constructor(suitOrCard, numberOrCards, name, nature) {
|
||||||
|
if (suitOrCard instanceof VCard) {
|
||||||
|
const other = suitOrCard;
|
||||||
|
[suitOrCard, numberOrCards, name, nature] = other._args;
|
||||||
|
}
|
||||||
|
|
||||||
if (Array.isArray(suitOrCard)) {
|
if (Array.isArray(suitOrCard)) {
|
||||||
/**
|
/**
|
||||||
* @type {string}
|
* @type {string}
|
||||||
|
@ -91,6 +96,8 @@ export class VCard {
|
||||||
if (typeof nature == 'string') this.nature = nature;
|
if (typeof nature == 'string') this.nature = nature;
|
||||||
if (!this.storage) this.storage = {};
|
if (!this.storage) this.storage = {};
|
||||||
if (!this.cards) this.cards = [];
|
if (!this.cards) this.cards = [];
|
||||||
|
|
||||||
|
this._args = [suitOrCard, numberOrCards, name, nature];
|
||||||
}
|
}
|
||||||
sameSuitAs(card) {
|
sameSuitAs(card) {
|
||||||
return get.suit(this) == get.suit(card);
|
return get.suit(this) == get.suit(card);
|
||||||
|
|
Loading…
Reference in New Issue