126 lines
3.4 KiB
JavaScript
126 lines
3.4 KiB
JavaScript
// 神明系統 - 與 API 整合
|
||
import { apiService } from './api-service.js'
|
||
|
||
export class TempleSystem {
|
||
constructor(petSystem, api = apiService) {
|
||
this.petSystem = petSystem
|
||
this.api = api
|
||
this.deities = []
|
||
}
|
||
|
||
// 初始化(從 API 載入神明資料)
|
||
async initialize() {
|
||
try {
|
||
this.deities = await this.api.getDeities()
|
||
console.log(`[TempleSystem] 載入 ${this.deities.length} 位神明`)
|
||
} catch (error) {
|
||
console.error('[TempleSystem] 載入神明失敗:', error)
|
||
// 降級到本地載入
|
||
const { DEITIES } = await import('../data/deities.js')
|
||
this.deities = DEITIES
|
||
}
|
||
}
|
||
|
||
// 獲取所有神明
|
||
getDeities() {
|
||
return [...this.deities]
|
||
}
|
||
|
||
// 獲取當前神明
|
||
getCurrentDeity() {
|
||
const state = this.petSystem.getState()
|
||
return this.deities.find(d => d.id === state.currentDeityId) || this.deities[0]
|
||
}
|
||
|
||
// 切換神明
|
||
async switchDeity(deityId) {
|
||
const deity = this.deities.find(d => d.id === deityId)
|
||
if (!deity) {
|
||
return { success: false, message: '找不到該神明' }
|
||
}
|
||
|
||
await this.petSystem.updateState({ currentDeityId: deityId })
|
||
return { success: true, deity }
|
||
}
|
||
|
||
// 祈福(每日上限 3 次)
|
||
async pray() {
|
||
const state = this.petSystem.getState()
|
||
|
||
if (state.dailyPrayerCount >= 3) {
|
||
return { success: false, message: '今日祈福次數已用完' }
|
||
}
|
||
|
||
try {
|
||
const result = await this.api.prayToDeity({
|
||
deityId: state.currentDeityId,
|
||
petState: state
|
||
})
|
||
|
||
// 更新好感度
|
||
const currentFavor = state.deityFavors[state.currentDeityId] || 0
|
||
const newFavor = Math.min(100, currentFavor + result.favorIncrease)
|
||
|
||
await this.petSystem.updateState({
|
||
deityFavors: {
|
||
...state.deityFavors,
|
||
[state.currentDeityId]: newFavor
|
||
},
|
||
dailyPrayerCount: state.dailyPrayerCount + 1
|
||
})
|
||
|
||
// 獲取神明對話
|
||
const deity = this.getCurrentDeity()
|
||
const dialogue = deity.dialogues[Math.floor(Math.random() * deity.dialogues.length)]
|
||
|
||
return {
|
||
success: true,
|
||
favorIncrease: result.favorIncrease,
|
||
newFavor,
|
||
dialogue,
|
||
message: result.message
|
||
}
|
||
} catch (error) {
|
||
console.error('[TempleSystem] 祈福失敗:', error)
|
||
return { success: false, message: '祈福失敗' }
|
||
}
|
||
}
|
||
|
||
// 獲取好感度星級(每 20 點一星)
|
||
getFavorStars(deityId) {
|
||
const state = this.petSystem.getState()
|
||
const favor = state.deityFavors[deityId] || 0
|
||
const stars = Math.floor(favor / 20)
|
||
return '★'.repeat(stars) + '☆'.repeat(5 - stars)
|
||
}
|
||
|
||
// 抽籤
|
||
async drawFortune() {
|
||
try {
|
||
const result = await this.api.drawFortune()
|
||
|
||
// 根據籤詩等級應用效果
|
||
const { FORTUNE_LOTS } = await import('../data/fortune-lots.js')
|
||
const lot = FORTUNE_LOTS.find(l => l.grade === result.lot.grade) || FORTUNE_LOTS[0]
|
||
|
||
if (lot.effects?.addBuff) {
|
||
// 應用 Buff(需要透過 eventSystem)
|
||
return {
|
||
success: true,
|
||
lot: result.lot,
|
||
buff: lot.effects.addBuff
|
||
}
|
||
}
|
||
|
||
return {
|
||
success: true,
|
||
lot: result.lot
|
||
}
|
||
} catch (error) {
|
||
console.error('[TempleSystem] 抽籤失敗:', error)
|
||
return { success: false, message: '抽籤失敗' }
|
||
}
|
||
}
|
||
}
|
||
|