为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 { Get as get } from '../get/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) {
if (get.itemtype(cards) == 'card') {
// @ts-ignore
cards = [cards];
}
// @ts-ignore
cards.forEach(card => card.addKnower(players));
}
//移除牌的所有知情者。
/**
* 移除牌的所有知情者
* @param { import('../library/index.js').Card[] | import('../library/index.js').Card } cards
*/
static clearCardKnowers(cards) {
if (get.itemtype(cards) == 'card') {
// @ts-ignore
cards = [cards];
}
// @ts-ignore
cards.forEach(card => card.clearKnowers());
}
/**
* @param { { [key: string]: any } } [arg]
*/
static loseAsync(arg) {
var next = game.createEvent('loseAsync');
next.forceDie = true;
@ -736,17 +758,27 @@ export class Game extends Uninstantable {
}
return ret;
}
/**
* @param {string} name
*/
static getRarity(name) {
var rank = lib.rank.rarity;
if (rank.legend.contains(name)) return 'legend';
if (rank.epic.contains(name)) return 'epic';
if (rank.rare.contains(name)) return 'rare';
if (get.mode() != 'chess' && rank.junk.contains(name)) return 'junk';
if (rank.legend.includes(name)) return 'legend';
if (rank.epic.includes(name)) return 'epic';
if (rank.rare.includes(name)) return 'rare';
if (get.mode() != 'chess' && rank.junk.includes(name)) return 'junk';
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) {
if (!key) return _status.globalHistory[_status.globalHistory.length - 1];
if (!filter) return _status.globalHistory[_status.globalHistory.length - 1][key];
// md谁写的和getGlobalHistory一样害人
if (!key || !filter) return false;
else {
const history = game.getGlobalHistory(key);
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) {
if (!key) return _status.globalHistory[_status.globalHistory.length - 1];
if (!filter) return _status.globalHistory[_status.globalHistory.length - 1][key];
// md谁写的和getGlobalHistory一样害人
if (!key || !filter) return;
else {
const history = game.getGlobalHistory(key);
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) {
if (!key) return _status.globalHistory[_status.globalHistory.length - 1];
if (!filter) return _status.globalHistory[_status.globalHistory.length - 1][key];
@ -793,13 +844,18 @@ export class Game extends Uninstantable {
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) {
if (!key || !filter) return;
let stopped = false;
_status.globalHistory.forEach(value => {
if (!key || !filter) return false;
return _status.globalHistory.some(value => {
if (value[key]) {
if (last && value[key].includes(last) && !stopped) {
stopped = true;
if (last && value[key].includes(last)) {
const lastIndex = value[key].indexOf(last);
if (value[key].some((event, index) => {
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) {
if (!key || !filter) return;
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) {
const history = [];
_status.globalHistory.forEach(value => {
@ -844,7 +919,7 @@ export class Game extends Uninstantable {
if (filter) {
if (last) {
const lastIndex = history.indexOf(last);
return history.filter(function (event, index) {
return history.filter((event, index) => {
if (index > lastIndex) return false;
return filter(event);
});
@ -853,10 +928,14 @@ export class Game extends Uninstantable {
}
return history;
}
/**
* @param { import('../library/index.js').Card | import('../library/index.js').Card[] } cards
*/
static cardsDiscard(cards) {
var type = get.itemtype(cards);
if (type != 'cards' && type != 'card') return;
var next = game.createEvent('cardsDiscard');
// @ts-ignore
next.cards = type == 'cards' ? cards.slice(0) : [cards];
next.setContent('cardsDiscard');
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])) }
/**
* @param { [number, number] | string | import('../library/index.js').Player[] |
* import('../library/index.js').Card[] | [number, number, number, number] | import('../library/index.js').Button |
* import('../library/index.js').Dialog | import('../library/index.js').Player | import('../library/index.js').Card |
* import('../library/index.js').GameEvent | import('../library/index.js').GameEventPromise } obj
* @overload
* @param { string } obj
* @returns { 'position' | 'natures' | 'nature' }
*/
/**
* @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) {
if (typeof obj == 'string') {
@ -1853,19 +1897,13 @@ export class Get extends Uninstantable {
if (lib.nature.has(obj)) return 'nature';
}
if (Array.isArray(obj) && obj.length > 0) {
if (obj.every(p => p instanceof lib.element.Player)) {
return 'players';
}
if (obj.every(p => p instanceof lib.element.Card)) {
return 'cards';
}
if (obj.every(p => p instanceof lib.element.Player)) return 'players';
if (obj.every(p => p instanceof lib.element.Card)) return 'cards';
if (obj.length == 2) {
if (typeof obj[0] == 'number' && typeof obj[1] == 'number') {
if (obj[0] <= obj[1] || obj[1] <= -1) return 'select';
}
}
if (obj.length == 4) {
if (obj.every(p => typeof p == 'number')) {
return 'divposition';
@ -2172,10 +2210,9 @@ export class Get extends Uninstantable {
* @returns { [number, number] }
*/
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());
else if (typeof select == 'number') return [select, select];
else if (select && get.itemtype(select) == 'select') return select;
return [1, 1];
}
static card(original) {

View File

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

View File

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