Merge pull request #1091 from nofficalfs/Dev-Feat-HooksChange

拆分hooks
This commit is contained in:
Spmario233 2024-03-16 20:31:59 +08:00 committed by GitHub
commit 86a65c2091
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 13660 additions and 13399 deletions

View File

@ -211,8 +211,16 @@ export class Game extends Uninstantable {
game.callHook("addGroup", [id, short, name, config]);
return id;
}
/**
* @typedef {import("../library/hooks/interface.js").NonameHookType} NonameHookType
*/
/**
* 通用的调用钩子函数
*
* @template {NonameHookType} HookType
* @template {keyof HookType} Name
* @param {Name} name
* @param {Parameters<HookType[Name]>} args
*/
static callHook(name, args) {
const callHook = () => {
@ -5946,7 +5954,7 @@ export class Game extends Uninstantable {
if (!event.forced && !event.fakeforce && get.noSelected()) confirm += 'c';
if (event.isMine()) game.Check.confirm(event, confirm);
game.callHook("checkEnd", [event, { ok, auto, auto_confirm }]);
game.callHook("checkEnd", [event, { ok, auto, auto_confirm, autoConfirm: auto_confirm }]);
// if (ui.confirm && ui.confirm.lastChild.link == 'cancel') {
// if (_status.event.type == 'phase' && !_status.event.skill) {

View File

@ -0,0 +1,324 @@
import { lib } from "../index.js"
import { game } from "../../game/index.js"
import { ui } from "../../ui/index.js"
import { get } from "../../get/index.js"
import { _status } from "../../status/index.js"
/**
* @type {(import("./interface.js").NonameHookType["addGroup"])[]}
*/
export const addGroup = [
function addColor(id, _short, _name, config) {
if (typeof config.color != "undefined" && config.color != null) {
let color1, color2, color3, color4
if (typeof config.color == "string" && /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(config.color)) {
let c1 = parseInt(`0x${config.color.slice(1, 3)}`)
let c2 = parseInt(`0x${config.color.slice(3, 5)}`)
let c3 = parseInt(`0x${config.color.slice(5, 7)}`)
color1 = color2 = color3 = color4 = [c1, c2, c3, 1]
}
else if (Array.isArray(config.color) && config.color.length == 4) {
if (config.color.every(item => Array.isArray(item))) {
color1 = config.color[0]
color2 = config.color[1]
color3 = config.color[2]
color4 = config.color[3]
}
else color1 = color2 = color3 = color4 = config.color
}
if (color1 && color2 && color3 && color4) {
const cs = lib.linq.cselector
game.dynamicStyle.addObject({
[cs.group(
cs.of(
cs.class("player ", "identity"),
cs.isAttr("data-color", `"${id}"`)
),
cs.of(
"div",
cs.isAttr("data-nature", `"${id}"`)
),
cs.of(
"span",
cs.isAttr("data-nature", `"${id}"`)
)
)]: {
textShadow: cs.group(
"black 0 0 1px",
`rgba(${color1.join()}) 0 0 2px`,
`rgba(${color2.join()}) 0 0 5px`,
`rgba(${color3.join()}) 0 0 10px`,
`rgba(${color4.join()}) 0 0 10px`
)
},
[cs.group(
cs.of(
"div",
cs.isAttr("data-nature", `"${id}m"`)
),
cs.of(
"span",
cs.isAttr("data-nature", `"${id}m"`)
)
)]: {
textShadow: cs.group(
"black 0 0 1px",
`rgba(${color1.join()}) 0 0 2px`,
`rgba(${color2.join()}) 0 0 5px`,
`rgba(${color3.join()}) 0 0 5px`,
`rgba(${color4.join()}) 0 0 5px`,
"black 0 0 1px"
)
},
[cs.group(
cs.of(
"div",
cs.isAttr("data-nature", `"${id}mm"`)
),
cs.of(
"span",
cs.isAttr("data-nature", `"${id}mm"`)
)
)]: {
textShadow: cs.group(
"black 0 0 1px",
`rgba(${color1.join()}) 0 0 2px`,
`rgba(${color2.join()}) 0 0 2px`,
`rgba(${color3.join()}) 0 0 2px`,
`rgba(${color4.join()}) 0 0 2px`,
"black 0 0 1px"
)
}
})
lib.groupnature[id] = id
}
}
},
function addImage(id, _short, _name, config) {
if (typeof config.image == "string") {
Reflect.defineProperty(lib.card, `group_${id}`, {
configurable: true,
enumerable: false,
writable: true,
value: {
fullskin: true,
image: config.image
}
})
}
}
]
/**
* @type {(import("./interface.js").NonameHookType["addNature"])[]}
*/
export const addNature = [
function addColor(nature, _translation, config) {
if (typeof config != 'object') config = {}
/**
* @type {boolean}
*/
// @ts-ignore
let linked = config.linked
/**
* @type {number}
*/
// @ts-ignore
let order = config.order
/**
* @type {string}
*/
// @ts-ignore
let background = config.background
/**
* @type {number[]}
*/
// @ts-ignore
let lineColor = config.lineColor
if (typeof linked != 'boolean') linked = true
if (typeof order != 'number') order = 0
if (typeof background != 'string') background = ''
if (!Array.isArray(lineColor) || lineColor.length != 3) lineColor = []
else if (background.startsWith('ext:')) {
background = background.replace(/^ext:/, 'extension/')
}
if (linked) lib.linked.add(nature)
if (lineColor.length) lib.lineColor.set(nature, lineColor)
lib.nature.set(nature, order)
if (background.length > 0) lib.natureBg.set(nature, background)
if (config.audio) {
for (let key in config.audio) {
if (!lib.natureAudio[key]) {
lib.natureAudio[key] = config.audio[key]
} else {
for (let key2 in config.audio[key]) {
lib.natureAudio[key][key2] = config.audio[key][key2]
}
}
}
}
let color1, color2
if (typeof config.color == "string" && /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/.test(config.color)) {
let c1 = parseInt(`0x${config.color.slice(1, 3)}`)
let c2 = parseInt(`0x${config.color.slice(3, 5)}`)
let c3 = parseInt(`0x${config.color.slice(5, 7)}`)
color1 = color2 = [c1, c2, c3, 1]
}
else if (Array.isArray(config.color) && config.color.length >= 2 && config.color.length <= 4) {
if (config.color.every(item => Array.isArray(item))) {
color1 = config.color[0]
color2 = config.color[1]
}
else {
let color = config.color.slice()
if (color.length == 3) color.push(1)
color1 = color2 = color
}
}
if (color1 && color2) {
const cs = lib.linq.cselector
const g1 = cs.group(
cs.of(
cs.class("card", "fullskin", `${nature}`),
'>',
cs.class("name")
)
)
let result = {}
result[g1] = {
color: `rgba(${color1.join()})`,
border: cs.merge(
'1px',
'solid',
`rgba(${color2.join()})`
),
}
// @ts-ignore
game.dynamicStyle.addObject(result)
const g2 = cs.group(
cs.of(
cs.class("tempname", `${nature}`),
':not([data-nature])>',
cs.class("span")
)
)
let result2 = {}
result2[g2] = {
color: `rgba(${color1.join()})`,
}
// @ts-ignore
game.dynamicStyle.addObject(result2)
}
}
]
/**
* @type {(import("./interface.js").NonameHookType["checkBegin"])[]}
*/
export const checkBegin = []
/**
* @type {(import("./interface.js").NonameHookType["checkCard"])[]}
*/
export const checkCard = [
function updateTempname(card, event) {
if (lib.config.cardtempname === 'off') return
if (get.name(card) === card.name && get.is.sameNature(get.nature(card), card.nature, true)) return
const node = ui.create.cardTempName(card)
if (lib.config.cardtempname !== 'default') node.classList.remove('vertical')
}
]
/**
* @type {(import("./interface.js").NonameHookType["checkTarget"])[]}
*/
export const checkTarget = [
function updateInstance(target, event) {
// @ts-ignore
if (!target.instance) return;
['selected', 'selectable'].forEach(className => {
if (target.classList.contains(className)) {
// @ts-ignore
target.instance.classList.add(className)
} else {
// @ts-ignore
target.instance.classList.remove(className)
}
})
},
]
/**
* @type {(import("./interface.js").NonameHookType["checkButton"])[]}
*/
export const checkButton = []
/**
* @type {(import("./interface.js").NonameHookType["checkEnd"])[]}
*/
export const checkEnd = [
function autoConfirm(event, { ok, auto, autoConfirm }) {
if (!event.isMine()) return
const skillinfo = get.info(event.skill) || {}
if (ok && auto && (autoConfirm || skillinfo.direct) && !_status.touchnocheck
&& !_status.mousedown && (!_status.mousedragging || !_status.mouseleft)) {
if (ui.confirm) ui.confirm.close()
// @ts-ignore
if (event.skillDialog === true) event.skillDialog = false
ui.click.ok()
_status.mousedragging = null
if (skillinfo.preservecancel) ui.create.confirm('c')
}
}
]
/**
* @type {(import("./interface.js").NonameHookType["uncheckBegin"])[]}
*/
export const uncheckBegin = []
/**
* @type {(import("./interface.js").NonameHookType["uncheckCard"])[]}
*/
export const uncheckCard = [
function removeTempname(card, event) {
// @ts-ignore
if (!card._tempName) return
// @ts-ignore
card._tempName.delete()
// @ts-ignore
delete card._tempName
},
]
/**
* @type {(import("./interface.js").NonameHookType["uncheckTarget"])[]}
*/
export const uncheckTarget = [
function removeInstance(target, event) {
// @ts-ignore
if (!target.instance) return
// @ts-ignore
target.instance.classList.remove('selected')
// @ts-ignore
target.instance.classList.remove('selectable')
},
]
/**
* @type {(import("./interface.js").NonameHookType["uncheckButton"])[]}
*/
export const uncheckButton = []
/**
* @type {(import("./interface.js").NonameHookType["uncheckEnd"])[]}
*/
export const uncheckEnd = []

View File

@ -0,0 +1,51 @@
import * as buildin from "./buildin.js"
/**
* @template {import("./interface.js").NonameHookType} HookType
* @template {keyof HookType} Name
*/
export class NonameHook {
/**
* @type {Name}
*/
#name
/**
* @type {HookType[Name][]}
*/
#methodList
/**
*
* @param {Name} name
*/
constructor(name) {
this.#name = name
this.#methodList = (name in buildin) ? [...buildin[name]] : []
}
get name() {
return this.#name
}
/**
*
* @param {HookType[Name]} method
*/
add(method) {
return this.#methodList.add(method)
}
/**
*
* @param {HookType[Name]} method
*/
push(method) {
return this.#methodList.push(method)
}
*[Symbol.iterator]() {
yield* this.#methodList
}
}

View File

@ -0,0 +1,18 @@
import { NonameHook } from "./hook.js"
export const defaultHooks = {
addGroup: new NonameHook("addGroup"),
addNature: new NonameHook("addNature"),
checkBegin: new NonameHook("checkBegin"),
checkCard: new NonameHook("checkCard"),
checkTarget: new NonameHook("checkTarget"),
checkButton: new NonameHook("checkButton"),
checkEnd: new NonameHook("checkEnd"),
uncheckBegin: new NonameHook("uncheckBegin"),
uncheckCard: new NonameHook("uncheckCard"),
uncheckTarget: new NonameHook("uncheckTarget"),
uncheckButton: new NonameHook("uncheckButton"),
uncheckEnd: new NonameHook("uncheckEnd")
}

92
noname/library/hooks/interface.d.ts vendored Normal file
View File

@ -0,0 +1,92 @@
import { Button, Card, GameEvent, GameEventPromise, Player } from "../element"
export interface NonameHookType {
/**
*
* @param id - id
* @param short -
* @param name -
* @param config -
*/
addGroup(id: string, short: string, name: string, config: Record<string, unknown>)
/**
*
* @param nature - id
* @param translation -
* @param config -
*/
addNature(nature: string, translation: string, config: Record<string, unknown>)
/**
*
* @param event -
*/
checkBegin(event: GameEvent & GameEventPromise)
/**
*
* @param event -
* @param config -
*/
checkEnd(event: GameEvent & GameEventPromise, config: { ok: boolean, auto: boolean, autoConfirm: boolean })
/**
*
* @param button - Button
* @param event -
*/
checkButton(button: Button, event: GameEvent & GameEventPromise)
/**
*
* @param card -
* @param event -
*/
checkCard(card: Card, event: GameEvent & GameEventPromise)
/**
*
* @param target -
* @param event -
*/
checkTarget(target: Player, event: GameEvent & GameEventPromise)
/**
*
* @param event -
* @param args -
*/
uncheckBegin(event: GameEvent & GameEventPromise, args: ("button" | "card" | "target")[] = ["button", "card", "target"])
/**
*
* @param event -
* @param args -
*/
uncheckEnd(event: GameEvent & GameEventPromise, args: ("button" | "card" | "target")[] = ["button", "card", "target"])
/**
*
* @param button - Button
* @param event -
*/
uncheckButton(button: Button, event: GameEvent & GameEventPromise)
/**
*
* @param card -
* @param event -
*/
uncheckCard(card: Card, event: GameEvent & GameEventPromise)
/**
*
* @param target -
* @param event -
*/
uncheckTarget(target: Player, event: GameEvent & GameEventPromise)
}

File diff suppressed because it is too large Load Diff