feat: add `Game#parseResourcePath` && replace `Game#playAudio` for test.

This commit is contained in:
Rintim 2024-05-12 19:14:25 +08:00
parent e19eb4693e
commit 215ead7e7b
No known key found for this signature in database
GPG Key ID: BE9E1EA615BACFCF
1 changed files with 59 additions and 51 deletions

View File

@ -1381,9 +1381,6 @@ export class Game {
} else if (typeof argument == "function") onError = argument; } else if (typeof argument == "function") onError = argument;
if (_status.video) break; if (_status.video) break;
} }
if (path.startsWith("ext:")) path = path.replace(/^ext:/, "extension/");
else if (!["db:", "blob:", "data:"].some(prefix => path.startsWith(prefix))) path = `audio/${path}`;
if (!lib.config.repeat_audio && _status.skillaudio.includes(path)) return;
} }
const audio = document.createElement("audio"); const audio = document.createElement("audio");
audio.autoplay = true; audio.autoplay = true;
@ -1400,13 +1397,10 @@ export class Game {
audio.remove(); audio.remove();
if (onError) onError(event); if (onError) onError(event);
}; };
new Promise((resolve, reject) => {
if (path.startsWith("db:")) game.getDB("image", path.slice(3)).then(octetStream => resolve(get.objectURL(octetStream)), reject); game.parseResourcePath(path, path => (lib.path.extname(path) ? `audio/${path}` : `audio/${path}.mp3`)).then(path => {
else if (lib.path.extname(path)) resolve(`${lib.assetURL}${path}`); audio.src = path.href;
else if (URL.canParse(path)) resolve(path); if (!lib.config.repeat_audio && _status.skillaudio.includes(path.href)) return;
else resolve(`${lib.assetURL}${path}.mp3`);
}).then(resolvedPath => {
audio.src = resolvedPath;
ui.window.appendChild(audio); ui.window.appendChild(audio);
}); });
return audio; return audio;
@ -1461,7 +1455,10 @@ export class Game {
* @returns { string[] } 语音地址列表 * @returns { string[] } 语音地址列表
*/ */
parseSkillText(skill, player, skillInfo) { parseSkillText(skill, player, skillInfo) {
return game.parseSkillTextMap(skill, player, skillInfo).map(data => data.text).filter(Boolean); return game
.parseSkillTextMap(skill, player, skillInfo)
.map(data => data.text)
.filter(Boolean);
} }
/** /**
* 根据skill中的audio,audioname,audioname2和player来获取技能台词列表及其对应的源文件名 * 根据skill中的audio,audioname,audioname2和player来获取技能台词列表及其对应的源文件名
@ -1487,12 +1484,15 @@ export class Game {
const getName = filter => { const getName = filter => {
const name = (player.tempname || []).find(i => filter(i)); const name = (player.tempname || []).find(i => filter(i));
if (name) return name; if (name) return name;
return [player.name, player.name1, player.name2].reduce((result, name) => { return [player.name, player.name1, player.name2].reduce(
(result, name) => {
if (result) return result; if (result) return result;
if (!name) return result; if (!name) return result;
if (filter(name)) return name; if (filter(name)) return name;
return get.character(name).tempname.find(i => filter(i)) || result; return get.character(name).tempname.find(i => filter(i)) || result;
}, void 0); },
void 0
);
}; };
const getTextMap = (path, name, ext) => ({ const getTextMap = (path, name, ext) => ({
@ -1529,8 +1529,8 @@ export class Game {
} }
const map = {}; const map = {};
audioInfo.forEach((i) => { audioInfo.forEach(i => {
parseAudio(skill, options, i).forEach(data => map[data.name] = data); parseAudio(skill, options, i).forEach(data => (map[data.name] = data));
}); });
return Object.values(map); return Object.values(map);
} }
@ -1539,12 +1539,13 @@ export class Game {
if (audioInfo === false) return []; if (audioInfo === false) return [];
if (typeof audioInfo === "string") { if (typeof audioInfo === "string") {
if (["data:", "blob:"].some(prefix => audioInfo.startsWith(prefix))) return [getTextMap("", audioInfo, "")]; if (["data:", "blob:"].some(prefix => audioInfo.startsWith(prefix))) return [getTextMap("", audioInfo, "")];
if(checkSkill(audioInfo, history)) return getAudioList(audioInfo, options); if (checkSkill(audioInfo, history)) return getAudioList(audioInfo, options);
} }
audioInfo = String(audioInfo); audioInfo = String(audioInfo);
const list = audioInfo.match(/(?:(.*):|^)(true|\d+)(?::(.*)|$)/); // [path, number|true, ext] const list = audioInfo.match(/(?:(.*):|^)(true|\d+)(?::(.*)|$)/); // [path, number|true, ext]
if (!list) { if (!list) {
let path = "", ext = ""; let path = "",
ext = "";
if (!/^db:|^ext:|\//.test(audioInfo)) path = "skill/"; if (!/^db:|^ext:|\//.test(audioInfo)) path = "skill/";
if (!/\.\w+$/.test(audioInfo)) ext = ".mp3"; if (!/\.\w+$/.test(audioInfo)) ext = ".mp3";
if (path && ext) return parseAudio(audioInfo, options, [true, 2]); if (path && ext) return parseAudio(audioInfo, options, [true, 2]);
@ -1574,51 +1575,49 @@ export class Game {
* @param { string | Player } player 角色名 * @param { string | Player } player 角色名
* @returns { any[] } 语音地址列表 * @returns { any[] } 语音地址列表
*/ */
parseDieTextMap(player){ parseDieTextMap(player) {
let name, rawName; let name, rawName;
if (typeof player === "string") { if (typeof player === "string") {
name = player; name = player;
rawName = name; rawName = name;
} } else if (get.itemtype(player) === "player") {
else if (get.itemtype(player) === "player") {
// @ts-ignore // @ts-ignore
name = player.skin.name || player.name; name = player.skin.name || player.name;
rawName = player.name; rawName = player.name;
} }
const info = get.character(name), datas = []; const info = get.character(name),
datas = [];
let dieAudios; let dieAudios;
if(info && info.dieAudios.length > 0){ if (info && info.dieAudios.length > 0) {
dieAudios = info.dieAudios; dieAudios = info.dieAudios;
} }
//@mengxinzxz写的屎山 //@mengxinzxz写的屎山
else if(rawName !== name && lib.characterSubstitute[rawName] && lib.characterSubstitute[rawName].some((i) => i[0] == name)){ else if (rawName !== name && lib.characterSubstitute[rawName] && lib.characterSubstitute[rawName].some(i => i[0] == name)) {
const trashes = lib.characterSubstitute[rawName].find((i) => i[0] == name)[1]; const trashes = lib.characterSubstitute[rawName].find(i => i[0] == name)[1];
const newCharacter = get.convertedCharacter(['','',0,[],trashes]); const newCharacter = get.convertedCharacter(["", "", 0, [], trashes]);
dieAudios = newCharacter.dieAudios; dieAudios = newCharacter.dieAudios;
} }
if(dieAudios && dieAudios.length > 0){ if (dieAudios && dieAudios.length > 0) {
dieAudios.forEach(item => { dieAudios.forEach(item => {
let key, file; let key, file;
if(item.startsWith("ext:")){ if (item.startsWith("ext:")) {
key = item.slice(4).split("/")[1]; key = item.slice(4).split("/")[1];
file = item; file = item;
} } else {
else {
key = item; key = item;
file = `die/${item}.mp3`; file = `die/${item}.mp3`;
} }
const data = {key, file} const data = { key, file };
if(lib.translate[`#${key}:die`]) data.text = lib.translate[`#${key}:die`]; if (lib.translate[`#${key}:die`]) data.text = lib.translate[`#${key}:die`];
datas.push(data); datas.push(data);
}); });
} } else {
else {
const data = { const data = {
key: name, key: name,
file: `die/${name}.mp3`, file: `die/${name}.mp3`,
isDefault: true, isDefault: true,
} };
if(lib.translate[`#${name}:die`]) data.text = lib.translate[`#${name}:die`]; if (lib.translate[`#${name}:die`]) data.text = lib.translate[`#${name}:die`];
datas.push(data); datas.push(data);
} }
return datas; return datas;
@ -3332,8 +3331,7 @@ export class Game {
}; };
// player.removeGaintag.apply(player, content); // player.removeGaintag.apply(player, content);
checkMatch(content[1], player.getCards("h")); checkMatch(content[1], player.getCards("h"));
} } else player.removeGaintag(content);
else player.removeGaintag(content);
} else { } else {
console.log(player); console.log(player);
} }
@ -6998,7 +6996,7 @@ export class Game {
for (let i = 0; i < event.config.size; i++) { for (let i = 0; i < event.config.size; i++) {
ui.window.appendChild(event.nodes[i]); ui.window.appendChild(event.nodes[i]);
} }
"step 1"; ("step 1");
let rand1 = event.config.first; let rand1 = event.config.first;
if (rand1 == "rand") { if (rand1 == "rand") {
rand1 = Math.random() < 0.5; rand1 = Math.random() < 0.5;
@ -7035,7 +7033,7 @@ export class Game {
} }
game.delay(); game.delay();
lib.init.onfree(); lib.init.onfree();
"step 2"; ("step 2");
if (event.checkredo()) return; if (event.checkredo()) return;
if (event._skiprest) return; if (event._skiprest) return;
if (event.side < 2) { if (event.side < 2) {
@ -7051,7 +7049,7 @@ export class Game {
event.aiMove(); event.aiMove();
game.delay(); game.delay();
} }
"step 3"; ("step 3");
if (typeof event.fast == "number" && get.time() - event.fast <= 1000) { if (typeof event.fast == "number" && get.time() - event.fast <= 1000) {
event.fast = true; event.fast = true;
} else { } else {
@ -7086,7 +7084,7 @@ export class Game {
game.delay(); game.delay();
} }
} }
"step 4"; ("step 4");
if (event.checkredo()) return; if (event.checkredo()) return;
if (event.skipnode) event.skipnode.delete(); if (event.skipnode) event.skipnode.delete();
if (event.replacenode) event.replacenode.delete(); if (event.replacenode) event.replacenode.delete();
@ -7105,7 +7103,7 @@ export class Game {
} }
} }
game.delay(); game.delay();
"step 5"; ("step 5");
event.prompt("选择" + get.cnNumber(event.config.num) + "名出场武将"); event.prompt("选择" + get.cnNumber(event.config.num) + "名出场武将");
event.enemylist = []; event.enemylist = [];
for (let i = 0; i < event.avatars.length; i++) { for (let i = 0; i < event.avatars.length; i++) {
@ -7135,7 +7133,7 @@ export class Game {
event.nodes[i].hide(); event.nodes[i].hide();
} }
game.pause(); game.pause();
"step 6"; ("step 6");
event.promptbar.delete(); event.promptbar.delete();
if (ui.cardPileButton) ui.cardPileButton.style.display = ""; if (ui.cardPileButton) ui.cardPileButton.style.display = "";
lib.onresize.remove(event.resize); lib.onresize.remove(event.resize);
@ -8510,6 +8508,16 @@ export class Game {
await Promise.resolve(asyncFunc(target, i)); await Promise.resolve(asyncFunc(target, i));
} }
} }
/**
* @async
* @param {string | URL} link
* @param {((item: string) => string) | null} [defaultHandle]
* @returns {Promise<URL>}
*/
parseResourcePath(link, defaultHandle = null) {
return (link instanceof URL ? link.href : link).startsWith("db:") ? lib.init.promises.parseResourceAddress(link, defaultHandle, true) : Promise.resolve(lib.init.parseResourceAddress(link, defaultHandle));
}
} }
export let game = new Game(); export let game = new Game();