pet_data/console-demo.js

376 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Console 互動版本 - 可點擊操作所有功能
// 使用方式:在瀏覽器 console 或 Node.js 環境執行
import { PetSystem } from './core/pet-system.js'
import { EventSystem } from './core/event-system.js'
import { TempleSystem } from './core/temple-system.js'
import { ApiService } from './core/api-service.js'
// 創建 API 服務(可切換 mock/real
const apiService = new ApiService({
useMock: true, // 設為 false 可切換到真實 API
baseUrl: 'http://localhost:3000/api',
mockDelay: 100
})
// 全局系統實例
let petSystem, eventSystem, templeSystem
let isRunning = false
// 初始化系統
async function init() {
console.log('=== 虛擬寵物系統初始化 ===\n')
// 創建系統實例
petSystem = new PetSystem(apiService)
eventSystem = new EventSystem(petSystem, apiService)
templeSystem = new TempleSystem(petSystem, apiService)
// 初始化
await petSystem.initialize()
await eventSystem.initialize()
await templeSystem.initialize()
console.log('✅ 系統初始化完成!')
console.log('📝 輸入 help() 查看所有可用命令\n')
// 顯示初始狀態
showStatus()
return { petSystem, eventSystem, templeSystem }
}
// 顯示狀態
function showStatus() {
const state = petSystem.getState()
const buffs = eventSystem.getBuffManager().getActiveBuffs()
const currentDeity = templeSystem.getCurrentDeity()
const favorStars = templeSystem.getFavorStars(state.currentDeityId)
console.log('\n' + '='.repeat(50))
console.log('🐾 寵物狀態')
console.log('='.repeat(50))
console.log(`種類: ${state.speciesId}`)
console.log(`階段: ${state.stage}`)
console.log(`年齡: ${Math.floor(state.ageSeconds)}`)
console.log(`\n📊 基礎數值:`)
console.log(` 飢餓: ${state.hunger.toFixed(1)}/100`)
console.log(` 快樂: ${state.happiness.toFixed(1)}/100`)
console.log(` 健康: ${state.health.toFixed(1)}/100`)
console.log(` 體重: ${state.weight.toFixed(1)}`)
console.log(`\n💪 屬性:`)
console.log(` 力量: ${state.str.toFixed(1)}`)
console.log(` 智力: ${state.int.toFixed(1)}`)
console.log(` 敏捷: ${state.dex.toFixed(1)}`)
console.log(` 運勢: ${state.luck.toFixed(1)}`)
console.log(`\n🎭 狀態:`)
console.log(` 睡覺: ${state.isSleeping ? '是' : '否'}`)
console.log(` 生病: ${state.isSick ? '是' : '否'}`)
console.log(` 死亡: ${state.isDead ? '是' : '否'}`)
console.log(` 便便: ${state.poopCount}/4`)
console.log(`\n🙏 神明:`)
console.log(` 當前: ${currentDeity.name}`)
console.log(` 好感: ${favorStars} (${state.deityFavors[state.currentDeityId]}/100)`)
console.log(` 今日祈福: ${state.dailyPrayerCount}/3`)
console.log(`\n✨ 當前 Buff:`)
if (buffs.length === 0) {
console.log(' (無)')
} else {
buffs.forEach(b => {
const duration = b.durationTicks === Infinity ? '永久' : `${b.currentTicks} ticks`
console.log(` - ${b.name} (${duration})`)
})
}
console.log('='.repeat(50) + '\n')
}
// 啟動遊戲循環
function start() {
if (isRunning) {
console.log('⚠️ 遊戲循環已在運行中')
return
}
isRunning = true
petSystem.startTickLoop((state) => {
console.log(`\n⏰ Tick: ${new Date().toLocaleTimeString()}`)
showStatus()
})
eventSystem.startEventCheck()
console.log('✅ 遊戲循環已啟動(每 3 秒 tick每 10 秒檢查事件)')
console.log('💡 輸入 stop() 停止循環\n')
}
// 停止遊戲循環
function stop() {
if (!isRunning) {
console.log('⚠️ 遊戲循環未運行')
return
}
petSystem.stopTickLoop()
eventSystem.stopEventCheck()
isRunning = false
console.log('⏹️ 遊戲循環已停止')
}
// ========== 互動命令 ==========
// 餵食
async function feed(amount = 20) {
const result = await petSystem.feed(amount)
if (result.success) {
console.log(`✅ 餵食成功!飢餓 +${amount},體重 +${(amount * 0.5).toFixed(1)}`)
} else {
console.log(`${result.message}`)
}
showStatus()
}
// 玩耍
async function play(amount = 15) {
const result = await petSystem.play(amount)
if (result.success) {
console.log(`✅ 玩耍成功!快樂 +${amount},敏捷 +0.5`)
} else {
console.log(`${result.message}`)
}
showStatus()
}
// 清理便便
async function clean() {
const result = await petSystem.cleanPoop()
if (result.success) {
console.log('✅ 清理成功!快樂 +10')
} else {
console.log(`${result.message}`)
}
showStatus()
}
// 治療
async function heal(amount = 20) {
const result = await petSystem.heal(amount)
if (result.success) {
console.log(`✅ 治療成功!健康 +${amount}${result.cured ? ',疾病已治癒' : ''}`)
} else {
console.log(`${result.message}`)
}
showStatus()
}
// 睡覺/起床
async function sleep() {
const result = await petSystem.toggleSleep()
if (result.success) {
console.log(`${result.isSleeping ? '寵物已入睡' : '寵物已醒來'}`)
} else {
console.log(`${result.message}`)
}
showStatus()
}
// 觸發事件(測試用)
async function triggerEvent(eventId) {
const result = await eventSystem.triggerEvent(eventId)
if (result) {
console.log(`✅ 事件 ${eventId} 觸發成功`)
} else {
console.log(`❌ 事件 ${eventId} 觸發失敗或條件不滿足`)
}
showStatus()
}
// 查看事件列表
async function listEvents() {
const events = await apiService.getEvents()
console.log('\n📋 可用事件列表:')
console.log('='.repeat(50))
events.forEach(e => {
console.log(`\n${e.id} (${e.type})`)
console.log(` 權重: ${e.weight}`)
console.log(` 效果數: ${e.effects.length}`)
})
console.log('='.repeat(50) + '\n')
}
// 查看事件歷史
function history() {
const history = eventSystem.getHistory()
console.log('\n📜 事件歷史:')
console.log('='.repeat(50))
if (history.length === 0) {
console.log(' (無)')
} else {
history.forEach((h, i) => {
const time = new Date(h.timestamp).toLocaleTimeString()
console.log(`${i + 1}. [${time}] ${h.eventId} (${h.eventType})`)
})
}
console.log('='.repeat(50) + '\n')
}
// 祈福
async function pray() {
const result = await templeSystem.pray()
if (result.success) {
console.log(`✅ 祈福成功!`)
console.log(` 好感度 +${result.favorIncrease}${result.newFavor}`)
console.log(` ${result.dialogue}`)
} else {
console.log(`${result.message}`)
}
showStatus()
}
// 抽籤
async function drawFortune() {
// 使用 drawLot 方法
const result = await templeSystem.drawLot()
if (result.success) {
console.log('\n🎴 抽籤結果:')
console.log('='.repeat(50))
console.log(`等級: ${result.lot.grade}`)
console.log(`籤詩: ${result.lot.poem1}`)
if (result.lot.poem2) {
console.log(` ${result.lot.poem2}`)
}
console.log(`解釋: ${result.lot.meaning}`)
if (result.needVerification) {
console.log(`\n⚠️ 需要三聖筊驗證才能解籤`)
console.log(`目前: ${result.verificationCount}/${result.requiredHoly} 聖筊`)
}
console.log('='.repeat(50) + '\n')
} else {
console.log(`${result.message}`)
}
}
// 切換神明
async function switchDeity(deityId) {
const result = await templeSystem.switchDeity(deityId)
if (result.success) {
console.log(`✅ 已切換到 ${result.deity.name}`)
} else {
console.log(`${result.message}`)
}
showStatus()
}
// 查看神明列表
function listDeities() {
const deities = templeSystem.getDeities()
console.log('\n🙏 神明列表:')
console.log('='.repeat(50))
deities.forEach(d => {
const state = petSystem.getState()
const favor = state.deityFavors[d.id] || 0
const stars = templeSystem.getFavorStars(d.id)
console.log(`\n${d.id}: ${d.name}`)
console.log(` 個性: ${d.personality}`)
console.log(` 好感: ${stars} (${favor}/100)`)
console.log(` 加成: ${d.buffDescriptions.join(', ')}`)
})
console.log('='.repeat(50) + '\n')
}
// 應用 Buff每 tick 自動執行,也可手動)
async function applyBuffs() {
await eventSystem.applyBuffs()
eventSystem.getBuffManager().tick()
console.log('✅ Buff 已應用並更新')
showStatus()
}
// 幫助
function help() {
console.log('\n' + '='.repeat(50))
console.log('📖 可用命令列表')
console.log('='.repeat(50))
console.log('\n🎮 遊戲控制:')
console.log(' start() - 啟動遊戲循環')
console.log(' stop() - 停止遊戲循環')
console.log(' showStatus() - 顯示當前狀態')
console.log('\n🐾 寵物互動:')
console.log(' feed(amount) - 餵食(預設 +20')
console.log(' play(amount) - 玩耍(預設 +15')
console.log(' clean() - 清理便便')
console.log(' heal(amount) - 治療(預設 +20')
console.log(' sleep() - 睡覺/起床')
console.log('\n🎲 事件系統:')
console.log(' triggerEvent(id) - 手動觸發事件')
console.log(' listEvents() - 查看所有事件')
console.log(' history() - 查看事件歷史')
console.log(' applyBuffs() - 手動應用 Buff')
console.log('\n🙏 神明系統:')
console.log(' pray() - 祈福(每日 3 次)')
console.log(' drawFortune() - 抽籤')
console.log(' switchDeity(id) - 切換神明')
console.log(' listDeities() - 查看神明列表')
console.log('\n💡 提示:')
console.log(' - 所有數值操作都會同步到 APImock 模式使用 localStorage')
console.log(' - 事件每 10 秒自動檢查10% 機率觸發)')
console.log(' - 遊戲循環每 3 秒執行一次 tick')
console.log(' - 輸入 help() 再次查看此列表')
console.log('='.repeat(50) + '\n')
}
// 匯出到全局(瀏覽器環境)
if (typeof window !== 'undefined') {
window.petSystem = petSystem
window.eventSystem = eventSystem
window.templeSystem = templeSystem
window.showStatus = showStatus
window.start = start
window.stop = stop
window.feed = feed
window.play = play
window.clean = clean
window.heal = heal
window.sleep = sleep
window.triggerEvent = triggerEvent
window.listEvents = listEvents
window.history = history
window.pray = pray
window.drawFortune = drawFortune
window.switchDeity = switchDeity
window.listDeities = listDeities
window.applyBuffs = applyBuffs
window.help = help
window.init = init
}
// Node.js 環境自動初始化
if (typeof window === 'undefined') {
init().then(() => {
console.log('\n💡 提示:在瀏覽器環境中,這些函數會自動掛載到 window 物件')
console.log(' 在 Node.js 環境中,請使用 await 呼叫這些函數\n')
})
}
export {
init,
showStatus,
start,
stop,
feed,
play,
clean,
heal,
sleep,
triggerEvent,
listEvents,
history,
pray,
drawFortune,
switchDeity,
listDeities,
applyBuffs,
help
}