miao-plugin/models/serv/Serv.js

100 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2023-10-19 09:48:52 +00:00
import { Cfg, Data } from '#miao'
let { diyCfg } = await Data.importCfg('profile')
import ProfileServ from './ProfileServ.js'
import ProfileReq from './ProfileReq.js'
import enkaApi from './api/EnkaApi.js'
import miaoApi from './api/MiaoApi.js'
import mggApi from './api/MggApi.js'
import hutaoApi from './api/HutaoApi.js'
import homoApi from './api/HomoApi.js'
import avocadoApi from './api/AvocadoApi.js'
import enkaHSRApi from './api/EnkaHSRApi.js'
const apis = {
miao: miaoApi,
mgg: mggApi,
enka: enkaApi,
hutao: hutaoApi,
homo: homoApi,
avocado: avocadoApi,
enkaHSR: enkaHSRApi
}
const servs = {}
const Serv = {
// 根据UID获取 ProfileServ
2024-02-04 01:02:58 +00:00
getServ(uid, game = 'gs') {
2023-10-19 09:48:52 +00:00
let token = diyCfg?.miaoApi?.token
let qq = diyCfg?.miaoApi?.qq
let hasToken = !!(qq && token && token.length === 32 && !/^test/.test(token))
let isGs = game === 'gs'
// 根据uid判断当前服务器类型。官服0 B服1 国际2
2024-02-05 23:18:46 +00:00
let servType = { 1: 0, 2: 0, 3: 0, 4: 0, 5: 1, 6: 2, 7: 2, 8: 2, 18: 2, 9: 2 }[String(uid).slice(0, -8)]
2023-10-19 09:48:52 +00:00
// 获取原神、星铁对应服务选择的配置
2023-10-31 20:07:47 +00:00
let servCfg = (Cfg.get(isGs ? 'profileServer' : 'srProfileServer', '0') || '0').toString()
2023-10-19 09:48:52 +00:00
let servIdx = servCfg[servType] || servCfg[0] || '0'
// 设置为自动或1时如果具备token则使用miao
if ((servIdx === '0' || servIdx === '1') && hasToken) {
return Serv.serv('miao')
}
// 如果指定了序号则返回对应服务。0和1已前置判断
// 原神0自动1喵2Enka3Mgg, 4:Hutao
// 星铁0自动1喵2Mihomo3Avocado, 4EnkaHSR
let servKey = isGs ? {
2: 'enka',
3: 'mgg',
4: 'hutao'
} : {
2: 'homo',
3: 'avocado',
4: 'enkaHSR'
}
if (servKey[servIdx]) {
return Serv.serv(servKey[servIdx])
}
// 设置为0或无token使用返回默认的serv。官服0 B服1 国际2
let defServKey = isGs ? ['mgg', 'mgg', 'enka'] : ['homo', 'homo', 'homo']
return Serv.serv(defServKey[servType])
},
// 根据key获取ProfileServ
2024-02-04 01:02:58 +00:00
serv(key) {
2023-10-19 09:48:52 +00:00
if (!servs[key]) {
servs[key] = new ProfileServ(apis[key])
}
return servs[key]
},
// 发起请求
2024-02-04 01:02:58 +00:00
async req(e, player) {
2023-10-19 09:48:52 +00:00
let req = ProfileReq.create(e, player.game)
if (!req) {
return false
}
2023-11-12 21:06:03 +00:00
let serv = Serv.getServ(e.uid || player.uid, player.game)
2023-10-19 09:48:52 +00:00
let { uid } = player
try {
player._update = []
await req.requestProfile(player, serv, player.game)
return player._update?.length || 0
} catch (err) {
if (!e._isReplyed) {
e.reply(`UID:${uid}更新面板失败,更新服务:${serv.name}`)
}
console.log(err)
return false
}
}
}
export default Serv