182 lines
6.1 KiB
JavaScript
182 lines
6.1 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')
|
|
}
|
|
|
|
// ---- stateStore:迁移 + 每日礼包 + 特价购买
|
|
patch('main/stateStore.ts', [
|
|
[
|
|
" petHead,\n rest,\n startJob,\n useItem,",
|
|
" itemById,\n petHead,\n rest,\n startJob,\n useItem,"
|
|
],
|
|
[
|
|
" if (!save || save.version !== 2 || !save.stats || !save.inventory) throw new Error('存档版本不匹配')",
|
|
" if (!save || save.version !== 2 || !save.stats || !save.inventory) throw new Error('存档版本不匹配')\n if (typeof save.dailyGiftDay !== 'string') save.dailyGiftDay = ''"
|
|
],
|
|
[
|
|
` subscribe(cb: (s: PetSave) => void): () => void {`,
|
|
` claimDaily(): ActionResult {
|
|
const day = new Date().toISOString().slice(0, 10)
|
|
if (this.save.dailyGiftDay === day) return { ok: false, message: '今天的礼包已经领过了' }
|
|
this.save.dailyGiftDay = day
|
|
this.save.money += 30
|
|
this.emit()
|
|
this.saveNow()
|
|
return { ok: true, message: '每日礼包:经费 +30' }
|
|
}
|
|
|
|
buySpecial(itemId: string, price: number): ActionResult {
|
|
const item = itemById(itemId)
|
|
if (!item) return { ok: false, message: '没有这件商品' }
|
|
if (this.save.money < price) return { ok: false, message: '金币不够啦' }
|
|
this.save.money -= price
|
|
this.save.inventory[itemId] = (this.save.inventory[itemId] ?? 0) + 1
|
|
this.emit()
|
|
this.saveNow()
|
|
return { ok: true, message: '领用成功' }
|
|
}
|
|
|
|
subscribe(cb: (s: PetSave) => void): () => void {`
|
|
]
|
|
])
|
|
|
|
// ---- main:失焦联动、送礼台词、每日特价/礼包、资料大窗口
|
|
patch('main/index.ts', [
|
|
[
|
|
"import { MediaStore } from './mediaStore'\nimport { KnowledgeBase } from './knowledge'\nimport { rollAccident } from '../shared/accidents'",
|
|
"import { MediaStore } from './mediaStore'\nimport { KnowledgeBase } from './knowledge'\nimport { rollAccident } from '../shared/accidents'\nimport { ITEMS, itemById } from '../shared/petState'"
|
|
],
|
|
// 失焦:只要菜单或对话框任一有焦点就不收
|
|
[
|
|
` menuWindow.on('blur', () => {
|
|
setTimeout(() => {
|
|
if (menuWindow && !menuWindow.isFocused()) hideMenu()
|
|
}, 250)
|
|
})`,
|
|
` menuWindow.on('blur', () => {
|
|
setTimeout(() => {
|
|
const mf = menuWindow?.isFocused() ?? false
|
|
const cf = chatBar?.isFocused() ?? false
|
|
if (!mf && !cf) hideMenu()
|
|
}, 350)
|
|
})`
|
|
],
|
|
[
|
|
` chatBar.once('ready-to-show', () => {
|
|
if (menuWindow?.isVisible()) chatBar?.showInactive()
|
|
})`,
|
|
` chatBar.once('ready-to-show', () => {
|
|
if (menuWindow?.isVisible()) chatBar?.showInactive()
|
|
})
|
|
chatBar.on('blur', () => {
|
|
setTimeout(() => {
|
|
const mf = menuWindow?.isFocused() ?? false
|
|
const cf = chatBar?.isFocused() ?? false
|
|
if (!mf && !cf) hideMenu()
|
|
}, 350)
|
|
})`
|
|
],
|
|
// 使用礼物时说台词
|
|
[
|
|
` ipcMain.handle('pet:action', (_e, name: string, payload?: unknown) => {
|
|
const result = store.action(String(name), payload)
|
|
if (String(name) === 'sleep' && result.ok) {`,
|
|
` ipcMain.handle('pet:action', (_e, name: string, payload?: unknown) => {
|
|
const result = store.action(String(name), payload)
|
|
if (String(name) === 'use' && result.ok) {
|
|
const item = itemById(String(payload))
|
|
if (item?.category === 'gift') {
|
|
say('(眼睛亮了)这是给我的吗?……谢谢你!(⁄ ⁄•⁄ω⁄•⁄ ⁄)', 6000)
|
|
} else if (item?.liked) {
|
|
say('(开心地眯起眼)这个我最喜欢了 (๑´ڡ\`๑)', 5000)
|
|
}
|
|
}
|
|
if (String(name) === 'sleep' && result.ok) {`
|
|
],
|
|
// 每日特价计算与 IPC
|
|
[
|
|
`async function webSearch(q: string): Promise<string> {`,
|
|
`function dailySpecial(): { itemId: string; price: number; day: string } {
|
|
const day = new Date().toISOString().slice(0, 10)
|
|
let h = 0
|
|
for (const ch of day) h = (h * 31 + ch.charCodeAt(0)) % 100000
|
|
const item = ITEMS[h % ITEMS.length]
|
|
return { itemId: item.id, price: Math.round(item.price * 0.7), day }
|
|
}
|
|
|
|
async function webSearch(q: string): Promise<string> {`
|
|
],
|
|
[
|
|
` ipcMain.handle('pet:kb:list', () => kb.list())`,
|
|
` ipcMain.handle('pet:daily:special', () => {
|
|
const sp = dailySpecial()
|
|
const item = itemById(sp.itemId)
|
|
return { ...sp, name: item?.name ?? '', origin: item?.price ?? 0, claimed: store.snapshot().dailyGiftDay === sp.day }
|
|
})
|
|
ipcMain.handle('pet:daily:gift', () => store.claimDaily())
|
|
ipcMain.handle('pet:daily:buy', () => {
|
|
const sp = dailySpecial()
|
|
return store.buySpecial(sp.itemId, sp.price)
|
|
})
|
|
|
|
ipcMain.handle('pet:kb:list', () => kb.list())`
|
|
],
|
|
// 资料大窗口
|
|
[
|
|
`function positionMenu(): void {`,
|
|
`function createStorageWindow(): void {
|
|
if (storageWindow && !storageWindow.isDestroyed()) {
|
|
storageWindow.show()
|
|
storageWindow.focus()
|
|
return
|
|
}
|
|
storageWindow = new BrowserWindow({
|
|
width: 780,
|
|
height: 660,
|
|
minWidth: 560,
|
|
minHeight: 480,
|
|
show: false,
|
|
frame: false,
|
|
resizable: true,
|
|
backgroundColor: '#0d1017',
|
|
webPreferences: {
|
|
preload: path.join(__dirname, '../preload/index.js'),
|
|
contextIsolation: true,
|
|
nodeIntegration: false,
|
|
sandbox: false
|
|
}
|
|
})
|
|
storageWindow.loadFile(path.join(__dirname, '../renderer/index.html'), {
|
|
query: { view: 'storage' }
|
|
})
|
|
storageWindow.once('ready-to-show', () => storageWindow?.show())
|
|
storageWindow.on('closed', () => {
|
|
storageWindow = null
|
|
})
|
|
}
|
|
|
|
function positionMenu(): void {`
|
|
],
|
|
[
|
|
"let chatBar: BrowserWindow | null = null",
|
|
"let chatBar: BrowserWindow | null = null\nlet storageWindow: BrowserWindow | null = null"
|
|
],
|
|
[
|
|
` ipcMain.on('pet:menu:toggle', () => toggleMenu())`,
|
|
` ipcMain.on('pet:menu:toggle', () => toggleMenu())
|
|
ipcMain.on('pet:storage:open', () => createStorageWindow())`
|
|
]
|
|
])
|
|
|
|
console.log('main rich patched')
|