进一步增加hook

This commit is contained in:
kuangshen04 2024-03-05 19:46:37 +08:00
parent 1936eb4cff
commit d138916705
2 changed files with 97 additions and 87 deletions

View File

@ -1881,12 +1881,12 @@ export class Game extends Uninstantable {
config: objectConfig config: objectConfig
} }
try{ try{
if (precontent) { if (precontent) {
_status.extension = name; _status.extension = name;
await (gnc.is.generatorFunc(precontent) ? gnc.of(precontent) : precontent).call(object, config); await (gnc.is.generatorFunc(precontent) ? gnc.of(precontent) : precontent).call(object, config);
delete _status.extension; delete _status.extension;
} }
}catch(e1){ }catch(e1){
console.log(`加载《${name}》扩展的precontent时出现错误。`,e1); console.log(`加载《${name}》扩展的precontent时出现错误。`,e1);
if(!lib.config.extension_alert) alert(`加载《${name}》扩展的precontent时出现错误。\n该错误本身可能并不影响扩展运行。您可以在“设置→通用→无视扩展报错”中关闭此弹窗。\n${decodeURI(e1.stack)}`); if(!lib.config.extension_alert) alert(`加载《${name}》扩展的precontent时出现错误。\n该错误本身可能并不影响扩展运行。您可以在“设置→通用→无视扩展报错”中关闭此弹窗。\n${decodeURI(e1.stack)}`);
@ -5891,19 +5891,10 @@ export class Game extends Uninstantable {
game.Check.skill(event); game.Check.skill(event);
player.node.equips.classList.remove('popequip');
if (event.filterCard && lib.config.popequip && !_status.nopopequip && get.is.phoneLayout() &&
typeof event.position === 'string' && event.position.includes('e') &&
player.node.equips.querySelector('.card.selectable')) {
player.node.equips.classList.add('popequip');
auto_confirm = false;
}
_status.multitarget = false; _status.multitarget = false;
const skillinfo = get.info(_status.event.skill); const skillinfo = get.info(event.skill)||{};
if (_status.event.multitarget) _status.multitarget = true; if (_status.event.multitarget) _status.multitarget = true;
else if (_status.event.name === 'chooseToUse' && skillinfo) { else if (_status.event.name === 'chooseToUse') {
if (skillinfo.multitarget && !skillinfo.multiline) _status.multitarget = true; if (skillinfo.multitarget && !skillinfo.multiline) _status.multitarget = true;
if (skillinfo.viewAs && typeof skillinfo.viewAs !== 'function') { if (skillinfo.viewAs && typeof skillinfo.viewAs !== 'function') {
const cardinfo = get.info(get.card()); const cardinfo = get.info(get.card());
@ -5913,6 +5904,13 @@ export class Game extends Uninstantable {
} }
} }
player.node.equips.classList.remove('popequip');
if (event.filterCard && lib.config.popequip && !_status.nopopequip && get.is.phoneLayout() &&
typeof event.position === 'string' && event.position.includes('e') &&
player.node.equips.querySelector('.card.selectable')) {
player.node.equips.classList.add('popequip');
auto_confirm = false;
}
if (event.isMine() && game.chess && get.config('show_distance') && game.me) { if (event.isMine() && game.chess && get.config('show_distance') && game.me) {
const players = game.players.slice(); const players = game.players.slice();
@ -5928,7 +5926,22 @@ export class Game extends Uninstantable {
}); });
} }
if (event.isMine()) game.Check.confirm(event, { ok, auto, auto_confirm }); let confirm = '';
if (ok && (!event.filterOk || event.filterOk())) confirm += 'o';
if (!event.forced && !event.fakeforce && get.noSelected()) confirm += 'c';
if (event.isMine()) game.Check.confirm(event, confirm);
game.callHook("checkEnd", [event]);
if (event.isMine() && confirm.includes('o')
&& auto && (auto_confirm || skillinfo.direct) && !_status.touchnocheck
&& !_status.mousedown && (!_status.mousedragging || !_status.mouseleft)) {
if (ui.confirm) ui.confirm.close();
if (event.skillDialog === true) event.skillDialog = false;
ui.click.ok();
_status.mousedragging = null;
if (skillinfo.preservecancel) ui.create.confirm('c');
}
// if (ui.confirm && ui.confirm.lastChild.link == 'cancel') { // if (ui.confirm && ui.confirm.lastChild.link == 'cancel') {
// if (_status.event.type == 'phase' && !_status.event.skill) { // if (_status.event.type == 'phase' && !_status.event.skill) {
@ -5938,12 +5951,10 @@ export class Game extends Uninstantable {
// ui.confirm.lastChild.innerHTML = '取消'; // ui.confirm.lastChild.innerHTML = '取消';
// } // }
// } // }
game.callHook("checkEnd", [event]);
return ok; return ok;
} }
static Check = class extends Uninstantable { static Check = class extends Uninstantable {
static processSelection({ type, items, event, useCache, isSelectable, custom }) { static processSelection({ type, items, event, useCache, isSelectable }) {
let ok = true, auto; let ok = true, auto;
let selectableItems = false; let selectableItems = false;
const uppercaseType = (type) => type[0].toUpperCase() + type.slice(1); const uppercaseType = (type) => type[0].toUpperCase() + type.slice(1);
@ -5997,7 +6008,7 @@ export class Game extends Uninstantable {
if (item.classList.contains('selectable')) selectableItems = true; if (item.classList.contains('selectable')) selectableItems = true;
else if (item.classList.contains('selected')) item.classList.add('selectable'); else if (item.classList.contains('selected')) item.classList.add('selectable');
if (custom) custom(item); game.callHook(`check${uppercaseType(type)}`, [item, event]);
}); });
if (event[`${type}Required`] && uiSelected.length === 0) ok = false; if (event[`${type}Required`] && uiSelected.length === 0) ok = false;
@ -6010,7 +6021,7 @@ export class Game extends Uninstantable {
static button(event, useCache) { static button(event, useCache) {
const player = event.player; const player = event.player;
const buttons = event.dialog.buttons; const buttons = event.dialog.buttons;
const isSelectable = button => { const isSelectable = (button, event) => {
if (!lib.filter.buttonIncluded(button)) return false; if (!lib.filter.buttonIncluded(button)) return false;
if (button.classList.contains('unselectable')) return false; if (button.classList.contains('unselectable')) return false;
return event.filterButton(button, player); return event.filterButton(button, player);
@ -6019,21 +6030,14 @@ export class Game extends Uninstantable {
} }
static card(event, useCache) { static card(event, useCache) {
const player = event.player; const player = event.player;
const players = game.players.slice();
if (event.deadTarget) players.addArray(game.dead);
const cards = player.getCards(event.position); const cards = player.getCards(event.position);
const isSelectable = card => { const isSelectable = (card, event) => {
if (card.classList.contains('uncheck')) return false; if (card.classList.contains('uncheck')) return false;
if (player.isOut()) return false; if (player.isOut()) return false;
if (!lib.filter.cardRespondable(card, player)) return false; if (!lib.filter.cardRespondable(card, player)) return false;
return event.filterCard(card, player); return event.filterCard(card, player);
} }
const custom = lib.config.cardtempname === 'off' ? null : card => { return game.Check.processSelection({ type: 'card', items: cards, event, useCache, isSelectable });
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');
}
return game.Check.processSelection({ type: 'card', items: cards, event, useCache, isSelectable, custom });
} }
static target(event, useCache) { static target(event, useCache) {
const player = event.player; const player = event.player;
@ -6045,17 +6049,7 @@ export class Game extends Uninstantable {
if (target.isOut()) return false; if (target.isOut()) return false;
return event.filterTarget(card, player, target); return event.filterTarget(card, player, target);
} }
const custom = target => { return game.Check.processSelection({ type: 'target', items: targets, event, useCache, isSelectable });
if (!target.instance) return;
['selected', 'selectable'].forEach(className => {
if (target.classList.contains(className)) {
target.instance.classList.add(className);
} else {
target.instance.classList.remove(className);
}
});
}
return game.Check.processSelection({ type: 'target', items: targets, event, useCache, isSelectable, custom });
} }
static skill(event) { static skill(event) {
if (ui.skills) ui.skills.close(); if (ui.skills) ui.skills.close();
@ -6081,53 +6075,36 @@ export class Game extends Uninstantable {
if (globalSkills.length) ui.create.skills2(globalSkills); if (globalSkills.length) ui.create.skills2(globalSkills);
if (equipSkills.length) ui.create.skills3(equipSkills); if (equipSkills.length) ui.create.skills3(equipSkills);
} }
static confirm(event, { ok, auto, auto_confirm }) { static confirm(event, confirm) {
const skillinfo = get.info(event.skill) || {}; ui.arena.classList.add('selecting');
if (ok && (!event.filterOk || event.filterOk()) if (event.filterTarget && (!event.filterCard || !event.position || (typeof event.position == 'string' && !event.position.includes('e')))) {
&& auto && (auto_confirm || skillinfo.direct) ui.arena.classList.add('tempnoe');
&& (!_status.mousedragging || !_status.mouseleft) && !_status.mousedown && !_status.touchnocheck) {
if (ui.confirm && !skillinfo.preservecancel) {
ui.confirm.close();
}
if (!ui.confirm && skillinfo.preservecancel) {
ui.create.confirm('c');
}
if (event.skillDialog === true) event.skillDialog = false;
ui.click.ok();
_status.mousedragging = null;
} }
else { game.countChoose();
ui.arena.classList.add('selecting'); if (!_status.noconfirm && !_status.event.noconfirm && (_status.mouseleft || !_status.mousedown)) {
if (event.filterTarget && (!event.filterCard || !event.position || (typeof event.position == 'string' && !event.position.includes('e')))) { ui.create.confirm(confirm);
ui.arena.classList.add('tempnoe');
}
game.countChoose();
if (!_status.noconfirm && !_status.event.noconfirm
&& (_status.mouseleft || !_status.mousedown)) {
let str = '';
if (ok && (!event.filterOk || event.filterOk())) str += 'o';
if (!event.forced && !event.fakeforce && get.noSelected()) str += 'c';
ui.create.confirm(str);
}
} }
} }
} }
static uncheck(...args) { static uncheck(...args) {
if (args.length === 0) args = ['button', 'card', 'target'];
const event = _status.event; const event = _status.event;
const players = game.players.slice(); const players = game.players.slice();
if (_status.event.deadTarget) players.addArray(game.dead); if (_status.event.deadTarget) players.addArray(game.dead);
game.callHook("uncheckBegin", [event]);
game.callHook("uncheckBegin", [event, args]);
if (game.chess) { if (game.chess) {
let shadows = ui.chessContainer.getElementsByClassName('playergrid temp'); const shadows = Array.from(ui.chessContainer.querySelectorAll('.playergrid.temp'));
while (shadows.length) shadows[0].remove(); shadows.forEach(i => i.remove());
} }
if (args.length === 0) args = ['button', 'card', 'target']; if(event.player) event.player.node.equips.classList.remove('popequip');
_status.event.player.node.equips.classList.remove('popequip');
if (args.includes('button') && event.dialog && event.dialog.buttons) { if (args.includes('button') && event.dialog && event.dialog.buttons) {
event.dialog.buttons.forEach(button => { event.dialog.buttons.forEach(button => {
button.classList.remove('selectable'); button.classList.remove('selectable');
button.classList.remove('selected'); button.classList.remove('selected');
game.callHook("uncheckButton", [button, event]);
}); });
ui.selected.buttons.length = 0; ui.selected.buttons.length = 0;
} }
@ -6136,11 +6113,8 @@ export class Game extends Uninstantable {
cards.forEach(card => { cards.forEach(card => {
card.classList.remove('selected'); card.classList.remove('selected');
card.classList.remove('selectable'); card.classList.remove('selectable');
if (card._tempName) { card.updateTransform(false);
card._tempName.delete(); game.callHook("uncheckCard", [card, event]);
delete card._tempName;
}
card.updateTransform();
}); });
ui.selected.cards.length = 0; ui.selected.cards.length = 0;
} }
@ -6148,10 +6122,7 @@ export class Game extends Uninstantable {
players.forEach(target => { players.forEach(target => {
target.classList.remove('selected'); target.classList.remove('selected');
target.classList.remove('selectable'); target.classList.remove('selectable');
if (target.instance) { game.callHook("uncheckTarget", [target, event]);
target.instance.classList.remove('selected');
target.instance.classList.remove('selectable');
}
}); });
ui.selected.targets.length = 0; ui.selected.targets.length = 0;
} }
@ -6172,7 +6143,8 @@ export class Game extends Uninstantable {
_status.dragline.forEach(i => i && i.remove()); _status.dragline.forEach(i => i && i.remove());
_status.dragline.length = 0; _status.dragline.length = 0;
ui.arena.classList.remove('dragging'); ui.arena.classList.remove('dragging');
game.callHook("uncheckEnd", [event]);
game.callHook("uncheckEnd", [event, args]);
} }
/** /**
* @param { Player } player1 * @param { Player } player1

View File

@ -330,11 +330,47 @@ export class Library extends Uninstantable {
game.dynamicStyle.addObject(result2); game.dynamicStyle.addObject(result2);
} }
}], }],
//game.check美化 //game.check
checkBegin: [], checkBegin: [],
checkEnd: [], checkEnd: [],
checkButton: [],
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');
},
],
checkTarget: [
function updateInstance(target, event) {
if (!target.instance) return;
['selected', 'selectable'].forEach(className => {
if (target.classList.contains(className)) {
target.instance.classList.add(className);
} else {
target.instance.classList.remove(className);
}
});
},
],
uncheckBegin: [], uncheckBegin: [],
uncheckEnd: [], uncheckEnd: [],
uncheckButton: [],
uncheckCard: [
function removeTempname(card, event) {
if (!card._tempName) return;
card._tempName.delete();
delete card._tempName;
},
],
uncheckTarget: [
function removeInstance(target, event) {
if (!target.instance) return;
target.instance.classList.remove('selected');
target.instance.classList.remove('selectable');
},
],
}; };
/** /**
@ -7926,7 +7962,9 @@ export class Library extends Uninstantable {
let type; let type;
try { try {
type = typeof obj[text]; type = typeof obj[text];
} catch {} } catch {
void 0;
}
if (type == 'function') { if (type == 'function') {
className += 'function'; className += 'function';
} }