为Game类添加部分注释

This commit is contained in:
shijian 2023-12-21 02:17:03 +08:00
parent f9a111dc8b
commit 1acc180c27
4 changed files with 156 additions and 37 deletions

View File

@ -1,3 +1,13 @@
/**
* @typedef {{
* cardMove:(import('../library/index.js').GameEventPromise)[],
* custom: (import('../library/index.js').GameEventPromise)[],
* useCard: (import('../library/index.js').GameEventPromise)[],
* changeHp: (import('../library/index.js').GameEventPromise)[],
* everything: (import('../library/index.js').GameEventPromise)[]
* }} History
*/
import { AI as ai } from '../ai/index.js'; import { AI as ai } from '../ai/index.js';
import { Get as get } from '../get/index.js'; import { Get as get } from '../get/index.js';
import { Library as lib } from '../library/index.js'; import { Library as lib } from '../library/index.js';
@ -652,20 +662,32 @@ export class Game extends Uninstantable {
} }
/** /**
* 为牌添加知情者 * 为牌添加知情者
* @param { import('../library/index.js').Card[] | import('../library/index.js').Card } cards
* @param { import('../library/index.js').Player[] } players
*/ */
static addCardKnower(cards, players) { static addCardKnower(cards, players) {
if (get.itemtype(cards) == 'card') { if (get.itemtype(cards) == 'card') {
// @ts-ignore
cards = [cards]; cards = [cards];
} }
// @ts-ignore
cards.forEach(card => card.addKnower(players)); cards.forEach(card => card.addKnower(players));
} }
//移除牌的所有知情者。 /**
* 移除牌的所有知情者
* @param { import('../library/index.js').Card[] | import('../library/index.js').Card } cards
*/
static clearCardKnowers(cards) { static clearCardKnowers(cards) {
if (get.itemtype(cards) == 'card') { if (get.itemtype(cards) == 'card') {
// @ts-ignore
cards = [cards]; cards = [cards];
} }
// @ts-ignore
cards.forEach(card => card.clearKnowers()); cards.forEach(card => card.clearKnowers());
} }
/**
* @param { { [key: string]: any } } [arg]
*/
static loseAsync(arg) { static loseAsync(arg) {
var next = game.createEvent('loseAsync'); var next = game.createEvent('loseAsync');
next.forceDie = true; next.forceDie = true;
@ -736,17 +758,27 @@ export class Game extends Uninstantable {
} }
return ret; return ret;
} }
/**
* @param {string} name
*/
static getRarity(name) { static getRarity(name) {
var rank = lib.rank.rarity; var rank = lib.rank.rarity;
if (rank.legend.contains(name)) return 'legend'; if (rank.legend.includes(name)) return 'legend';
if (rank.epic.contains(name)) return 'epic'; if (rank.epic.includes(name)) return 'epic';
if (rank.rare.contains(name)) return 'rare'; if (rank.rare.includes(name)) return 'rare';
if (get.mode() != 'chess' && rank.junk.contains(name)) return 'junk'; if (get.mode() != 'chess' && rank.junk.includes(name)) return 'junk';
return 'common'; return 'common';
} }
/**
* @template { keyof History } T
* @param { T } key
* @param { (event: import('../library/index.js').GameEventPromise) => boolean } filter
* @param { import('../library/index.js').GameEventPromise } [last]
* @returns { boolean }
*/
static hasGlobalHistory(key, filter, last) { static hasGlobalHistory(key, filter, last) {
if (!key) return _status.globalHistory[_status.globalHistory.length - 1]; // md谁写的和getGlobalHistory一样害人
if (!filter) return _status.globalHistory[_status.globalHistory.length - 1][key]; if (!key || !filter) return false;
else { else {
const history = game.getGlobalHistory(key); const history = game.getGlobalHistory(key);
if (last) { if (last) {
@ -761,9 +793,16 @@ export class Game extends Uninstantable {
} }
} }
} }
/**
* @template { keyof History } T
* @param { T } key
* @param { (event: import('../library/index.js').GameEventPromise) => boolean } filter
* @param { import('../library/index.js').GameEventPromise } [last]
* @returns { void }
*/
static checkGlobalHistory(key, filter, last) { static checkGlobalHistory(key, filter, last) {
if (!key) return _status.globalHistory[_status.globalHistory.length - 1]; // md谁写的和getGlobalHistory一样害人
if (!filter) return _status.globalHistory[_status.globalHistory.length - 1][key]; if (!key || !filter) return;
else { else {
const history = game.getGlobalHistory(key); const history = game.getGlobalHistory(key);
if (last) { if (last) {
@ -778,6 +817,18 @@ export class Game extends Uninstantable {
} }
} }
} }
/**
* @overload
* @returns { History }
*/
/**
* @template { keyof History } T
* @overload
* @param { T } key
* @param { (event: import('../library/index.js').GameEventPromise) => boolean } [filter]
* @param { import('../library/index.js').GameEventPromise } [last]
* @returns { History[T] }
*/
static getGlobalHistory(key, filter, last) { static getGlobalHistory(key, filter, last) {
if (!key) return _status.globalHistory[_status.globalHistory.length - 1]; if (!key) return _status.globalHistory[_status.globalHistory.length - 1];
if (!filter) return _status.globalHistory[_status.globalHistory.length - 1][key]; if (!filter) return _status.globalHistory[_status.globalHistory.length - 1][key];
@ -793,13 +844,18 @@ export class Game extends Uninstantable {
return history.filter(filter); return history.filter(filter);
} }
} }
/**
* @template { keyof History } T
* @param { T } key
* @param { (event: import('../library/index.js').GameEventPromise) => boolean } filter
* @param { import('../library/index.js').GameEventPromise } [last]
* @returns { boolean }
*/
static hasAllGlobalHistory(key, filter, last) { static hasAllGlobalHistory(key, filter, last) {
if (!key || !filter) return; if (!key || !filter) return false;
let stopped = false; return _status.globalHistory.some(value => {
_status.globalHistory.forEach(value => {
if (value[key]) { if (value[key]) {
if (last && value[key].includes(last) && !stopped) { if (last && value[key].includes(last)) {
stopped = true;
const lastIndex = value[key].indexOf(last); const lastIndex = value[key].indexOf(last);
if (value[key].some((event, index) => { if (value[key].some((event, index) => {
if (index > lastIndex) return false; if (index > lastIndex) return false;
@ -812,6 +868,13 @@ export class Game extends Uninstantable {
} }
}) })
} }
/**
* @template { keyof History } T
* @param { T } key
* @param { (event: import('../library/index.js').GameEventPromise) => boolean } filter
* @param { import('../library/index.js').GameEventPromise } [last]
* @returns { void }
*/
static checkAllGlobalHistory(key, filter, last) { static checkAllGlobalHistory(key, filter, last) {
if (!key || !filter) return; if (!key || !filter) return;
let stopped = false; let stopped = false;
@ -831,6 +894,18 @@ export class Game extends Uninstantable {
} }
}) })
} }
/**
* @overload
* @returns { History[] }
*/
/**
* @template { keyof History } T
* @overload
* @param { T } key
* @param { (event: import('../library/index.js').GameEventPromise) => boolean } [filter]
* @param { import('../library/index.js').GameEventPromise } [last]
* @returns { History[T] }
*/
static getAllGlobalHistory(key, filter, last) { static getAllGlobalHistory(key, filter, last) {
const history = []; const history = [];
_status.globalHistory.forEach(value => { _status.globalHistory.forEach(value => {
@ -844,7 +919,7 @@ export class Game extends Uninstantable {
if (filter) { if (filter) {
if (last) { if (last) {
const lastIndex = history.indexOf(last); const lastIndex = history.indexOf(last);
return history.filter(function (event, index) { return history.filter((event, index) => {
if (index > lastIndex) return false; if (index > lastIndex) return false;
return filter(event); return filter(event);
}); });
@ -853,10 +928,14 @@ export class Game extends Uninstantable {
} }
return history; return history;
} }
/**
* @param { import('../library/index.js').Card | import('../library/index.js').Card[] } cards
*/
static cardsDiscard(cards) { static cardsDiscard(cards) {
var type = get.itemtype(cards); var type = get.itemtype(cards);
if (type != 'cards' && type != 'card') return; if (type != 'cards' && type != 'card') return;
var next = game.createEvent('cardsDiscard'); var next = game.createEvent('cardsDiscard');
// @ts-ignore
next.cards = type == 'cards' ? cards.slice(0) : [cards]; next.cards = type == 'cards' ? cards.slice(0) : [cards];
next.setContent('cardsDiscard'); next.setContent('cardsDiscard');
next.getd = function (player, key, position) { next.getd = function (player, key, position) {

View File

@ -1833,10 +1833,54 @@ export class Get extends Uninstantable {
} }
static xyDistance(from, to) { return Math.sqrt((from[0] - to[0]) * (from[0] - to[0]) + (from[1] - to[1]) * (from[1] - to[1])) } static xyDistance(from, to) { return Math.sqrt((from[0] - to[0]) * (from[0] - to[0]) + (from[1] - to[1]) * (from[1] - to[1])) }
/** /**
* @param { [number, number] | string | import('../library/index.js').Player[] | * @overload
* import('../library/index.js').Card[] | [number, number, number, number] | import('../library/index.js').Button | * @param { string } obj
* import('../library/index.js').Dialog | import('../library/index.js').Player | import('../library/index.js').Card | * @returns { 'position' | 'natures' | 'nature' }
* import('../library/index.js').GameEvent | import('../library/index.js').GameEventPromise } obj */
/**
* @overload
* @param { import('../library/index.js').Player[] } obj
* @returns { 'players' }
*/
/**
* @overload
* @param { import('../library/index.js').Card[] } obj
* @returns { 'cards' }
*/
/**
* @overload
* @param { [number, number] } obj
* @returns { 'select' }
*/
/**
* @overload
* @param { [number, number, number, number] } obj
* @returns { 'divposition' }
*/
/**
* @overload
* @param { import('../library/index.js').Button } obj
* @returns { 'button' }
*/
/**
* @overload
* @param { import('../library/index.js').Card } obj
* @returns { 'card' }
*/
/**
* @overload
* @param { import('../library/index.js').Player } obj
* @returns { 'player' }
*/
/**
* @overload
* @param { import('../library/index.js').Dialog } obj
* @returns { 'dialog' }
*/
/**
* @overload
* @param { import('../library/index.js').GameEvent | import('../library/index.js').GameEventPromise } obj
* @returns { 'event' }
*/ */
static itemtype(obj) { static itemtype(obj) {
if (typeof obj == 'string') { if (typeof obj == 'string') {
@ -1853,19 +1897,13 @@ export class Get extends Uninstantable {
if (lib.nature.has(obj)) return 'nature'; if (lib.nature.has(obj)) return 'nature';
} }
if (Array.isArray(obj) && obj.length > 0) { if (Array.isArray(obj) && obj.length > 0) {
if (obj.every(p => p instanceof lib.element.Player)) { if (obj.every(p => p instanceof lib.element.Player)) return 'players';
return 'players'; if (obj.every(p => p instanceof lib.element.Card)) return 'cards';
}
if (obj.every(p => p instanceof lib.element.Card)) {
return 'cards';
}
if (obj.length == 2) { if (obj.length == 2) {
if (typeof obj[0] == 'number' && typeof obj[1] == 'number') { if (typeof obj[0] == 'number' && typeof obj[1] == 'number') {
if (obj[0] <= obj[1] || obj[1] <= -1) return 'select'; if (obj[0] <= obj[1] || obj[1] <= -1) return 'select';
} }
} }
if (obj.length == 4) { if (obj.length == 4) {
if (obj.every(p => typeof p == 'number')) { if (obj.every(p => typeof p == 'number')) {
return 'divposition'; return 'divposition';
@ -2172,10 +2210,9 @@ export class Get extends Uninstantable {
* @returns { [number, number] } * @returns { [number, number] }
*/ */
static select(select) { static select(select) {
if (typeof select == 'number') return [select, select];
// @ts-ignore
if (get.itemtype(select) == 'select') return select;
if (typeof select == 'function') return get.select(select()); if (typeof select == 'function') return get.select(select());
else if (typeof select == 'number') return [select, select];
else if (select && get.itemtype(select) == 'select') return select;
return [1, 1]; return [1, 1];
} }
static card(original) { static card(original) {

View File

@ -5,7 +5,7 @@
* @typedef {InstanceType<typeof lib.element.Button>} Button * @typedef {InstanceType<typeof lib.element.Button>} Button
* @typedef {InstanceType<typeof lib.element.Dialog>} Dialog * @typedef {InstanceType<typeof lib.element.Dialog>} Dialog
* @typedef {InstanceType<typeof lib.element.GameEvent>} GameEvent * @typedef {InstanceType<typeof lib.element.GameEvent>} GameEvent
* @typedef {InstanceType<typeof lib.element.GameEventPromise>} GameEventPromise * @typedef {InstanceType<typeof lib.element.GameEvent> & InstanceType<typeof lib.element.GameEventPromise> & typeof Promise<lib.element.GameEvent>} GameEventPromise
* @typedef {InstanceType<typeof lib.element.NodeWS>} NodeWS * @typedef {InstanceType<typeof lib.element.NodeWS>} NodeWS
*/ */
import { nonameInitialized, assetURL, userAgent, Uninstantable, GeneratorFunction, AsyncFunction } from "../util/index.js"; import { nonameInitialized, assetURL, userAgent, Uninstantable, GeneratorFunction, AsyncFunction } from "../util/index.js";
@ -31302,7 +31302,7 @@ export class Library extends Uninstantable {
} }
}, },
GameEvent: class { GameEvent: class {
/** @type { Promise<GameEvent> & GameEvent & GameEventPromise } */ /** @type { GameEventPromise } */
#promise; #promise;
/** /**
* @param {string} [name] * @param {string} [name]
@ -31321,11 +31321,11 @@ export class Library extends Uninstantable {
this.step = 0; this.step = 0;
this.finished = false; this.finished = false;
/** /**
* @type {(Promise<GameEvent> & GameEvent & GameEventPromise)[]} * @type {(GameEventPromise)[]}
*/ */
this.next = []; this.next = [];
/** /**
* @type {(Promise<GameEvent> & GameEvent & GameEventPromise)[]} * @type {(GameEventPromise)[]}
*/ */
this.after = []; this.after = [];
this.custom = { this.custom = {
@ -32107,7 +32107,7 @@ export class Library extends Uninstantable {
/** /**
* 事件转为Promise化 * 事件转为Promise化
* *
* @returns { Promise<GameEvent> & GameEvent & GameEventPromise<> } * @returns { GameEventPromise }
*/ */
toPromise() { toPromise() {
if (!this.#promise) { if (!this.#promise) {

View File

@ -6,7 +6,7 @@ export const status = {
clicked: false, clicked: false,
auto: false, auto: false,
/** /**
* @type {import('../library/index.js').GameEventPromise & import('../library/index.js').GameEvent} * @type {import('../library/index.js').GameEventPromise}
*/ */
// @ts-ignore // @ts-ignore
event: null, event: null,
@ -16,6 +16,9 @@ export const status = {
dieClose: [], dieClose: [],
dragline: [], dragline: [],
dying: [], dying: [],
/**
* @type { import('../game/index.js').History[] }
*/
globalHistory: [{ globalHistory: [{
cardMove: [], cardMove: [],
custom: [], custom: [],