Skip to content

Commit

Permalink
refactor: remove schaledb related dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
U1805 committed Jan 26, 2025
1 parent efe287a commit 55535e5
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 102 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ This project is inspired by:

Character metadata and assets from:

- [lonqie/SchaleDB](https://github.com/lonqie/SchaleDB)
- [bluearchive.fandom](https://bluearchive.fandom.com)
- [kivo.wiki](https://kivo.wiki/)
- [ba.gamekee](https://ba.gamekee.com/)
- [bluearchive.fandom](https://bluearchive.fandom.com)

## Copyrights

Expand Down
3 changes: 1 addition & 2 deletions docs/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@

キャラクターのメタデータとアセットは以下を用いています:

- [lonqie/SchaleDB](https://github.com/lonqie/SchaleDB)
- [bluearchive.fandom](https://bluearchive.fandom.com)
- [kivo.wiki](https://kivo.wiki/)
- [ba.gamekee](https://ba.gamekee.com/)
- [bluearchive.fandom](https://bluearchive.fandom.com)

## Copyrights

Expand Down
3 changes: 1 addition & 2 deletions docs/README-zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@

角色数据来源:

- [lonqie/SchaleDB](https://github.com/lonqie/SchaleDB)
- [bluearchive.fandom](https://bluearchive.fandom.com)
- [kivo.wiki](https://kivo.wiki/)
- [ba.gamekee](https://ba.gamekee.com/)
- [bluearchive.fandom](https://bluearchive.fandom.com)

## 版权

Expand Down
3 changes: 1 addition & 2 deletions docs/README-zh_tw.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@

角色數據來源:

- [lonqie/SchaleDB](https://github.com/lonqie/SchaleDB)
- [bluearchive.fandom](https://bluearchive.fandom.com)
- [kivo.wiki](https://kivo.wiki/)
- [ba.gamekee](https://ba.gamekee.com/)
- [bluearchive.fandom](https://bluearchive.fandom.com)

## 版權聲明

Expand Down
4 changes: 2 additions & 2 deletions src/assets/chatUtils/play.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { sendText } from './send'
import { getId, getRole } from './role'
import { baseStudent, Talk } from '../utils/interface'
import { getMessage, getSchaleImg } from '../utils/request'
import { getMessage, getAvatarImg } from '../utils/request'
import { waitClick, waitTime } from '../utils/wait'
import { store } from '../storeUtils/store'
import { talkHistory } from '../storeUtils/talkHistory'
Expand All @@ -21,7 +21,7 @@ const play = async (
talklist.setAttribute('style', 'height:100%')
talklist.className = talklist.className.replace("show-action", "hidden-action")

const student = getRole(data[0][storyLng], getSchaleImg(data[0]['CharacterId']))
const student = getRole(data[0][storyLng], getAvatarImg(data[0]['CharacterId']))
let item = data[1]
talkHistory.resetData()
let text: string = '', selected: baseStudent | number
Expand Down
8 changes: 0 additions & 8 deletions src/assets/utils/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,4 @@ export class Resource {
const response = await axios.get(this.proxy(file))
return response.data
}

@memorize
getSchale() {
if (!this.config) {
throw new Error('Config not loaded. Call loadConfig before using getSchale.')
}
return this.config.schale
}
}
25 changes: 5 additions & 20 deletions src/assets/utils/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,20 @@ interface studentInfo {
cnt: number
}

type KeysOfType<T, U> = {
[K in keyof T]: T[K] extends U ? K : never
}[keyof T]

interface LocalStudent {
Id: number
Bio: Record<string, string>
Name: Record<string, string>
Birthday: string
Avatar: string[]
Bio: Record<string, string>
Nickname: string[]
Fixed: Array<{
ItemName: KeysOfType<studentInfo, string>
ItemValue: string
ItemLanguage?: Array<'zh' | 'tw' | 'jp' | 'kr' | 'en'>
}>
School: string
Related: {
ItemId: number
ItemType: string
} | null
}

interface SchaleStudent {
Id: number
Name: string
Birthday: string
PathName: string
School: string
}

interface Talk extends baseStudent {
type: number // 0: student| 1: sensei| 2: story| 3: choice| 4:system
content: string
Expand All @@ -51,9 +37,8 @@ interface Talk extends baseStudent {
}

interface ProxyConfig {
schale: string
domain: Record<string, string>
proxy: Record<string, { domain: string; param: string }>
}

export { baseStudent, studentInfo, LocalStudent, SchaleStudent, Talk, ProxyConfig }
export { baseStudent, studentInfo, LocalStudent, Talk, ProxyConfig }
119 changes: 55 additions & 64 deletions src/assets/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,52 @@
import { Resource } from './cache'
import { studentInfo, LocalStudent, SchaleStudent } from './interface'
import { studentInfo, LocalStudent } from './interface'
import { Traditionalized } from './tw_cn'

const resourceInstance = new Resource()
await resourceInstance.loadConfig()
const getData = <T>(file: string): Promise<T> => resourceInstance.getData(file)
const proxy1 = (url: string) => resourceInstance.proxy(url)
const proxy = (url: string[]) => resourceInstance.proxy(url)
const schale_url = resourceInstance.getSchale()

const MONTHS_EN = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]

type SupportedLanguage = 'zh' | 'tw' | 'jp' | 'kr' | 'en'

const getOrdinalSuffix = (day: number): string => {
if (day > 3 && day < 21) return 'th'
switch (day % 10) {
case 1: return 'st'
case 2: return 'nd'
case 3: return 'rd'
default: return 'th'
}
}

const DATE_FORMATS = (birthday: string, lng: SupportedLanguage) => {
const TOOL = {
zh: (month: number, day: number) => `${month}${day}日`,
tw: (month: number, day: number) => `${month}${day}日`,
jp: (month: number, day: number) => `${month}${day}日`,
kr: (month: number, day: number) => `${month}${day}일`,
en: (month: number, day: number) =>
`${MONTHS_EN[month - 1]} ${day}${getOrdinalSuffix(day)}`
}
const [month, day] = birthday.split('/').map(Number)
return TOOL[lng](month, day)
}

/*
数据请求方法
*/
const getSchaleImg = (student: number) => {
return `${schale_url}/images/student/collection/${student}.webp`
const getAvatarImg = (student: number) => {
return proxy1(`/api/Avatars/Kivo/Released/${student}.webp`)
}

const getSchoolIcon = (school: string) => {
return `${schale_url}/images/schoolicon/${school}.png`
return proxy1(`/api/Schools/${school}.png`)
}

const getMessage = async (student: string, storyid: string) => {
Expand All @@ -29,90 +59,51 @@ const getStickers = async (student: number) => {

const getStudentsPart1 = async (lng: string) => {
const tools = {
getOrderedKeys: () => {
const orderedIds = local.map((item) => item.Id)
const schaleIds = Object.values(schale).map((studentInfo) => studentInfo.Id)
const localIdSet = new Set(orderedIds)
const schaleSet = new Set(schaleIds)

const filteredLocalIds = orderedIds.filter((id) => schaleSet.has(id))
const additionalIds = schaleIds.filter((id) => !localIdSet.has(id))

return [...filteredLocalIds, ...additionalIds]
},

initStudentObject: (schaleItem: SchaleStudent): studentInfo => ({
Id: schaleItem.Id,
Name: schaleItem.Name,
Birthday: schaleItem.Birthday,
Avatars: [getSchaleImg(schaleItem.Id)],
Bio: '',
Nickname: [schaleItem.PathName.replace('_', ' ')],
School: schaleItem.School,
initStudentObject: (localItem: LocalStudent): studentInfo => ({
Id: localItem.Id,
Name: tools.fixStudentField(localItem, "Name"),
Birthday: DATE_FORMATS(localItem.Birthday, lng as SupportedLanguage),
Avatars: proxy(localItem.Avatar),
Bio: tools.fixStudentField(localItem, "Bio"),
Nickname: localItem.Nickname,
School: localItem.School || "ETC",
cnt: 0
}),

fixStudentFields: (student: studentInfo, localItem: LocalStudent) => {
// filling avatar
student.Avatars.push(...localItem.Avatar)
student.Avatars = proxy(student.Avatars)

// filling empty bio
if (localItem.Bio[lng]) student.Bio = localItem.Bio[lng]
if (lng == 'en' && !localItem.Bio['en']) student.Bio = localItem.Bio['jp']
if (lng == 'tw' && !localItem.Bio['tw'])
student.Bio = Traditionalized(localItem.Bio['zh'])

// fixing item fileds
type ValidLanguage = 'en' | 'zh' | 'tw' | 'jp' | 'kr'
for (const fixeditem of localItem.Fixed) {
if (
!fixeditem.ItemLanguage ||
fixeditem.ItemLanguage.includes(lng as ValidLanguage)
) {
student[fixeditem.ItemName] = fixeditem.ItemValue
}
}
fixStudentField: (localItem: LocalStudent, field: "Name" | "Bio") => {
// filling empty name
if (lng == 'en' && !localItem[field]['en']) return localItem[field]['jp']
if (lng == 'tw' && !localItem[field]['tw'])
return Traditionalized(localItem[field]['zh'])
return localItem[field][lng]
},

fillNickname: (student: studentInfo, localItem: LocalStudent) => {
student.Nickname.push(...localItem.Nickname)

if (localItem.Related && prefixTable[localItem.Related.ItemType]) {
const relatedId = localItem.Related.ItemId
const relatedSchaleItem = schale[relatedId]
const relatedLocalItem = local.find((ele) => ele.Id === relatedId)
const nicknames = relatedLocalItem?.Nickname || []
const prefixes = prefixTable[localItem.Related.ItemType]
for (const prefix of prefixes) {
student.Nickname.push(
prefix + relatedSchaleItem.Name,
prefix + relatedSchaleItem.PathName,
localItem.Nickname[0] + " " + localItem.Related.ItemType,
prefix + tools.fixStudentField(localItem, "Name"),
...nicknames.map((nickname) => prefix + nickname)
)
}
}
}
}

const [local, schale, prefixTable] = await Promise.all([
const [local, prefixTable] = await Promise.all([
getData<LocalStudent[]>('/api/Momotalk/students.json'),
getData<Record<string, SchaleStudent>>(
`${schale_url}/data/${lng}/students.min.json`
),
getData<Record<string, string[]>>('/api/Momotalk/prefixTable.json')
])

const orderedKeys = tools.getOrderedKeys()

return orderedKeys.map((key) => {
const schaleItem = schale[key.toString()]
const localItem = local.find((ele) => ele.Id === key)
const newStudent = tools.initStudentObject(schaleItem)

if (!localItem) return newStudent

tools.fixStudentFields(newStudent, localItem)
return local.map((localItem) => {
const newStudent = tools.initStudentObject(localItem)
tools.fillNickname(newStudent, localItem)
return newStudent
})
Expand Down Expand Up @@ -143,4 +134,4 @@ const getStudents = async (lng: string) => {
return [data1, data2]
}

export { getStudents, getMessage, getSchaleImg, getSchoolIcon, getStickers, proxy }
export { getStudents, getMessage, getSchoolIcon, getAvatarImg, getStickers, proxy }

0 comments on commit 55535e5

Please sign in to comment.