Skip to content

Commit

Permalink
feat: domain replacements #37
Browse files Browse the repository at this point in the history
  • Loading branch information
U1805 committed Aug 17, 2024
1 parent 2ff6bc5 commit 82b7b45
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 79 deletions.
25 changes: 17 additions & 8 deletions src/assets/utils/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ interface studentInfo {
cnt: number
}

// "Id": 10000,
// "Name": "阿露",
// "Birthday": "3月12日",
// "Avatar": ["https://static.wikia.nocookie.net/blue-archive/images/d/de/Aru_Icon.png"],
// "Bio": "什么都能解决哦!",
// "Nickname": ["Aru", "社长", "亚瑠", "阿鲁"],
// "cnt": 0
/* Aru: studentInfo = {
"Id": 10000,
"Name": "阿露",
"Birthday": "3月12日",
"Avatar": ["https://static.wikia.nocookie.net/blue-archive/images/d/de/Aru_Icon.png"],
"Bio": "什么都能解决哦!",
"Nickname": ["Aru", "社长", "亚瑠", "阿鲁"],
"cnt": 0
}*/


interface Talk extends baseStudent {
type: number // 0: student| 1: sensei| 2: story| 3: choice| 4:system
Expand All @@ -30,4 +33,10 @@ interface Talk extends baseStudent {
flag: number
}

export { baseStudent, studentInfo, Talk }
interface ProxyConfig {
domainReplacements: { [key: string]: string }
proxyDomains: { [key: string]: string }
proxyParams: string
}

export { baseStudent, studentInfo, Talk, ProxyConfig }
136 changes: 103 additions & 33 deletions src/assets/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,90 @@
import axios from 'axios'
import { studentInfo } from './interface'
import { studentInfo, ProxyConfig } from './interface'
import { Traditionalized } from './tw_cn'

function proxy(url: string): string
function proxy(url: string[]): string[]

function proxy(url: string | string[]) {
if (typeof url === 'string') {
if (url.indexOf('nocookie') !== -1 || url.indexOf('kivo.wiki') !== -1)
return 'https://wsrv.nl/?url=' + url + '&output=webp'
else
return url.replace('/api', 'https://BlueArcbox.github.io/resources')
} else {
return url.map((ele) => proxy(ele))
/*
缓存请求结果
*/
function memorize(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value
const cache = new Map()

descriptor.value = function (...args: any[]) {
const key = JSON.stringify(args)
if (cache.has(key)) {
return cache.get(key)
}
const result = originalMethod.apply(this, args)
cache.set(key, result)
return result
}

return descriptor
}

class Resource {
private config: ProxyConfig | null = null

@memorize
async loadConfig() {
let configUrl = '/api/Momotalk/proxyConfig.json'
configUrl = configUrl.replace('/api', 'https://BlueArcbox.github.io/resources')

const response = await axios.get<ProxyConfig>(configUrl)
return response.data
}

proxy(url: string): string
proxy(url: string[]): string[]

@memorize
proxy(url: string | string[]): string | string[] {
if (!this.config) {
throw new Error('Config not loaded. Call loadConfig before using proxy.')
}

if (Array.isArray(url)) {
return url.map((u) => this.proxy(u) as string)
}

// 替换域名
for (const [oldDomain, newDomain] of Object.entries(
this.config.domainReplacements
)) {
if (url.startsWith(oldDomain)) {
url = url.replace(oldDomain, newDomain)
}
}

// 添加代理
for (const [targetDomain, proxyDomain] of Object.entries(
this.config.proxyDomains
)) {
if (url.indexOf(targetDomain) !== -1) {
url = `${proxyDomain}${encodeURIComponent(url)}${this.config.proxyParams}`
}
}

return url
}

@memorize
async getData(file: string) {
if (!this.config) {
this.config = await this.loadConfig()
}
const response = await axios.get(this.proxy(file))
return response.data
}
}

const resourceInstance = new Resource()
const getData = (file: string) => resourceInstance.getData(file)
const proxy = (url: string[]) => resourceInstance.proxy(url)

/*
数据请求方法
*/
const getSchaleImg = (collection: string) => {
return `https://schale.gg/images/student/collection/${collection}.webp`
}
Expand All @@ -24,16 +93,24 @@ const getSchaleSchoolIcon = (school: string) => {
return `https://schale.gg/images/schoolicon/School_Icon_${school.toUpperCase()}_W.png`
}

const getData = async (file: string) => {
let data: any
await axios.get(proxy(file)).then((res) => (data = res.data))
return data
const getMessage = async (storyid: string, story: string) => {
const res = (await getData(`/api/Stories/${storyid}/${story}.json`)) as any[]
return res
}

const getStickers = async (student: number) => {
const res = (await getData(`/api/Stories/${student}/Stickers.json`)) as any[]
return res
}

const getSchale = async (lng: string) => {
const schale = await getData(`https://schale.gg/data/${lng}/students.min.json`) as any []
const local = await getData('/api/Momotalk/students.json') as any[]
const prefixTable = await getData('/api/Momotalk/prefixTable.json') as { [key: string]: string[] }
const schale = (await getData(
`https://schale.gg/data/${lng}/students.min.json`
)) as any[]
const local = (await getData('/api/Momotalk/students.json')) as any[]
const prefixTable = (await getData('/api/Momotalk/prefixTable.json')) as {
[key: string]: string[]
}
const results: studentInfo[] = []
for (const schaleItem of schale) {
const localItem = local.find((ele) => ele.Id === schaleItem.Id)
Expand All @@ -58,11 +135,14 @@ const getSchale = async (lng: string) => {
if (localItem && lng == 'tw' && !localItem.Bio['tw'])
newStudent.Bio = Traditionalized(localItem.Bio['zh'])
// fix zh name
if (localItem && lng == 'zh' && localItem.Name)
newStudent.Name = localItem.Name
if (localItem && lng == 'zh' && localItem.Name) newStudent.Name = localItem.Name

// generating nicknames: add prefix
if (localItem && localItem.related && Object.prototype.hasOwnProperty.call(prefixTable, localItem.related[1])) {
if (
localItem &&
localItem.related &&
Object.prototype.hasOwnProperty.call(prefixTable, localItem.related[1])
) {
const relatedInfo: [number, string] = localItem.related
const relatedItem = results.find((ele) => ele.Id === relatedInfo[0])!
const prefixs = prefixTable[relatedInfo[1]]
Expand All @@ -79,7 +159,7 @@ const getSchale = async (lng: string) => {
}

const getLocal = async (lng: string) => {
const local = await getData('/api/Momotalk/students2.json') as any[]
const local = (await getData('/api/Momotalk/students2.json')) as any[]
const results: studentInfo[] = []
for (const localItem of local) {
const newStudent: studentInfo = {
Expand All @@ -106,14 +186,4 @@ const getStudents = async (lng: string) => {
return [data1, data2]
}

const getMessage = async (storyid: string, story: string) => {
const res = await getData(`/api/Stories/${storyid}/${story}.json`) as any[]
return res
}

const getStickers = async (student: number) => {
const res = await getData(`/api/Stories/${student}/Stickers.json`) as any[]
return res
}

export { getStudents, getMessage, getSchaleImg, getSchaleSchoolIcon, getStickers, proxy }
60 changes: 30 additions & 30 deletions src/assets/utils/stickers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,37 @@ export const stickers = [
]

export const stickers2 = [
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_53_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_83_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_84_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_85_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_86_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_87_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_88_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_89_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_90_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_91_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_92_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_93_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_94_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_95_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_96_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_100_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_103_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_104_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_105_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_106_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_107_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_109_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_110_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_111_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_112_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_141_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_142_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_143_Jp.png',
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_144_Jp.png'
'/kivo/gallery/16/ClanChat_Emoji_53_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_83_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_84_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_85_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_86_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_87_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_88_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_89_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_90_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_91_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_92_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_93_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_94_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_95_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_96_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_100_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_103_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_104_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_105_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_106_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_107_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_109_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_110_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_111_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_112_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_141_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_142_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_143_Jp.png',
'/kivo/gallery/16/ClanChat_Emoji_144_Jp.png'
]

export const stickers3 = [
'https://api.kivo.wiki/assets/images/gallery/16/ClanChat_Emoji_83_Jp.png'
'/kivo/gallery/16/ClanChat_Emoji_83_Jp.png'
]
16 changes: 8 additions & 8 deletions src/assets/utils/tw_cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ function Traditionalized(cc: string[]): string[]

function Traditionalized(cc: string | string[]) {
if (typeof cc === 'string') {
var str = ''
var ss = JTPYStr()
var tt = FTPYStr()
for (var i = 0; i < cc.length; i++) {
let str = ''
const ss = JTPYStr()
const tt = FTPYStr()
for (let i = 0; i < cc.length; i++) {
if (cc.charCodeAt(i) > 10000 && ss.indexOf(cc.charAt(i)) != -1)
str += tt.charAt(ss.indexOf(cc.charAt(i)))
else str += cc.charAt(i)
Expand All @@ -29,10 +29,10 @@ function Simplized(cc: string[]): string[]

function Simplized(cc: string | string[]) {
if (typeof cc === 'string') {
var str = ''
var ss = JTPYStr()
var tt = FTPYStr()
for (var i = 0; i < cc.length; i++) {
let str = ''
const ss = JTPYStr()
const tt = FTPYStr()
for (let i = 0; i < cc.length; i++) {
if (cc.charCodeAt(i) > 10000 && tt.indexOf(cc.charAt(i)) != -1)
str += ss.charAt(tt.indexOf(cc.charAt(i)))
else str += cc.charAt(i)
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"forceConsistentCasingInFileNames": true,
"useDefineForClassFields": true,
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"baseUrl": ".",
"paths": {
"@/*": [
Expand Down

0 comments on commit 82b7b45

Please sign in to comment.