155 lines
5.2 KiB
JavaScript
155 lines
5.2 KiB
JavaScript
const fs = require('fs')
|
|
const base = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/'
|
|
function patch(file, pairs) {
|
|
const p = base + file
|
|
let c = fs.readFileSync(p, 'utf8')
|
|
for (const [from, to] of pairs) {
|
|
if (!c.includes(from)) {
|
|
console.log('MISS in', file, '::', from.slice(0, 70).replace(/\n/g, '\\n'))
|
|
continue
|
|
}
|
|
c = c.replace(from, to)
|
|
}
|
|
fs.writeFileSync(p, c, 'utf8')
|
|
}
|
|
|
|
// ---- 存档字段:在线时长 / 生日 / 成就 / 日志
|
|
patch('shared/petState.ts', [
|
|
[
|
|
` /** 使用/任务次数统计,key 形如 use:bread / job:wash */
|
|
counters: Record<string, number>
|
|
}`,
|
|
` /** 使用/任务次数统计,key 形如 use:bread / job:wash */
|
|
counters: Record<string, number>
|
|
/** 累计在线时长(毫秒) */
|
|
playtimeMs: number
|
|
/** 主人生日 MM-DD(每月只能改一次) */
|
|
birthday: string
|
|
/** 上次修改生日的时间戳 */
|
|
birthdaySetAt: number
|
|
/** 已解锁成就:id -> 解锁时间戳 */
|
|
achievements: Record<string, number>
|
|
/** 活动日志(最近的在前,最多 60 条) */
|
|
log: string[]
|
|
}`
|
|
],
|
|
[
|
|
` counters: {},`,
|
|
` counters: {},
|
|
playtimeMs: 0,
|
|
birthday: '',
|
|
birthdaySetAt: 0,
|
|
achievements: {},
|
|
log: [],`
|
|
],
|
|
[
|
|
` const m = seconds / 60
|
|
const s = save.stats
|
|
const asleep = save.sleeping`,
|
|
` const m = seconds / 60
|
|
save.playtimeMs += Math.max(0, seconds) * 1000
|
|
const s = save.stats
|
|
const asleep = save.sleeping`
|
|
]
|
|
])
|
|
|
|
// ---- stateStore:迁移 + 日志 + 成就检查 + 生日
|
|
patch('main/stateStore.ts', [
|
|
[
|
|
"import { rollAccident } from '../shared/accidents'",
|
|
"import { rollAccident } from '../shared/accidents'\nimport { ACHIEVEMENTS } from '../shared/achievements'"
|
|
],
|
|
[
|
|
` if (typeof save.accidentForgiven !== 'boolean') save.accidentForgiven = false`,
|
|
` if (typeof save.accidentForgiven !== 'boolean') save.accidentForgiven = false
|
|
if (typeof save.playtimeMs !== 'number') save.playtimeMs = 0
|
|
if (typeof save.birthday !== 'string') save.birthday = ''
|
|
if (typeof save.birthdaySetAt !== 'number') save.birthdaySetAt = 0
|
|
if (!save.achievements) save.achievements = {}
|
|
if (!Array.isArray(save.log)) save.log = []`
|
|
],
|
|
// 成就检查 + 日志 + 回调
|
|
[
|
|
` private readonly configListeners = new Set<(c: PetConfig) => void>()`,
|
|
` private readonly configListeners = new Set<(c: PetConfig) => void>()
|
|
private readonly achievementListeners = new Set<(a: { title: string; desc: string; icon: string }) => void>()`
|
|
],
|
|
[
|
|
` onConfig(cb: (c: PetConfig) => void): () => void {`,
|
|
` onAchievement(cb: (a: { title: string; desc: string; icon: string }) => void): () => void {
|
|
this.achievementListeners.add(cb)
|
|
return () => this.achievementListeners.delete(cb)
|
|
}
|
|
|
|
/** 检查成就,返回本次新解锁的成就 */
|
|
checkAchievements(): Array<{ title: string; desc: string; icon: string }> {
|
|
const unlocked: Array<{ title: string; desc: string; icon: string }> = []
|
|
for (const a of ACHIEVEMENTS) {
|
|
if (this.save.achievements[a.id]) continue
|
|
let ok = false
|
|
try {
|
|
ok = a.check(this.save)
|
|
} catch {
|
|
ok = false
|
|
}
|
|
if (ok) {
|
|
this.save.achievements[a.id] = Date.now()
|
|
unlocked.push({ title: a.title, desc: a.desc, icon: a.icon })
|
|
}
|
|
}
|
|
if (unlocked.length > 0) {
|
|
this.pushLog('解锁成就:' + unlocked.map((a) => a.title).join('、'))
|
|
this.saveNow()
|
|
for (const a of unlocked) for (const cb of this.achievementListeners) cb(a)
|
|
}
|
|
return unlocked
|
|
}
|
|
|
|
pushLog(text: string): void {
|
|
const stamp = new Date()
|
|
const hh = String(stamp.getHours()).padStart(2, '0')
|
|
const mm = String(stamp.getMinutes()).padStart(2, '0')
|
|
this.save.log.unshift(hh + ':' + mm + ' ' + text)
|
|
if (this.save.log.length > 60) this.save.log.length = 60
|
|
}
|
|
|
|
setBirthday(day: string): ActionResult {
|
|
const now = Date.now()
|
|
if (this.save.birthdaySetAt && now - this.save.birthdaySetAt < 30 * 24 * 3600 * 1000) {
|
|
const days = Math.ceil((30 * 24 * 3600 * 1000 - (now - this.save.birthdaySetAt)) / 86400000)
|
|
return { ok: false, message: '一个月只能改一次,还要等 ' + days + ' 天' }
|
|
}
|
|
if (!/^\\d{2}-\\d{2}$/.test(day)) return { ok: false, message: '日期格式不对' }
|
|
this.save.birthday = day
|
|
this.save.birthdaySetAt = now
|
|
this.pushLog('把主人生日设成了 ' + day)
|
|
this.emit()
|
|
this.saveNow()
|
|
return { ok: true, message: '生日已设为 ' + day }
|
|
}
|
|
|
|
onConfig(cb: (c: PetConfig) => void): () => void {`
|
|
],
|
|
// action 后记录日志与成就
|
|
[
|
|
` if (result.ok) {
|
|
this.emit()
|
|
this.saveNow()
|
|
}
|
|
return result`,
|
|
` if (result.ok) {
|
|
if (name === 'use') this.pushLog('使用了 ' + String(payload))
|
|
if (name === 'buy') this.pushLog('购买了 ' + String(payload))
|
|
if (name === 'work') this.pushLog('开始了任务')
|
|
if (name === 'pet') this.pushLog('被摸了摸头')
|
|
if (name === 'sleep') this.pushLog(this.save.sleeping ? '睡着了' : '醒了')
|
|
this.checkAchievements()
|
|
this.emit()
|
|
this.saveNow()
|
|
}
|
|
return result`
|
|
]
|
|
])
|
|
|
|
console.log('save fields + store achievements patched')
|