feat: add patch size estimation for game updates
- HEAD request to game-patches.hytale.com for content-length - Track lastPatchId in state to calculate patch delta - Display formatted size (~55.4 MiB) in update notifications Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
a7d4df6986
commit
96eb5f754d
2 changed files with 41 additions and 4 deletions
|
|
@ -1,7 +1,20 @@
|
|||
import type { PatchesUpdate } from './tracker.js'
|
||||
|
||||
function formatBytes(bytes: number): string {
|
||||
const units = ['B', 'KiB', 'MiB', 'GiB']
|
||||
let size = bytes
|
||||
let unitIndex = 0
|
||||
|
||||
while (size >= 1024 && unitIndex < units.length - 1) {
|
||||
size /= 1024
|
||||
unitIndex++
|
||||
}
|
||||
|
||||
return `${size.toFixed(1)} ${units[unitIndex]}`
|
||||
}
|
||||
|
||||
export function formatPatchesUpdate(update: PatchesUpdate): string {
|
||||
const { patchline, version, previousVersion, patchId } = update
|
||||
const { patchline, version, previousVersion, patchId, patchSize } = update
|
||||
const emoji = patchline === 'release' ? '🎮' : '🧪'
|
||||
|
||||
return `<b>${emoji} Hytale Game Patch Update</b>
|
||||
|
|
@ -9,5 +22,6 @@ export function formatPatchesUpdate(update: PatchesUpdate): string {
|
|||
<b>Patchline:</b> <code>${patchline}</code>
|
||||
<b>New version:</b> <code>${version}</code>
|
||||
${previousVersion !== 'unknown' ? `<b>Previous:</b> <code>${previousVersion}</code>` : ''}
|
||||
${patchId ? `<b>Patch ID:</b> <code>${patchId}</code>` : ''}`
|
||||
${patchId ? `<b>Patch ID:</b> <code>${patchId}</code>` : ''}
|
||||
${patchSize ? `<b>Size:</b> ~<code>${formatBytes(patchSize)}</code>` : ''}`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ export interface PatchesUpdate {
|
|||
version: string
|
||||
previousVersion: string
|
||||
patchId: number
|
||||
patchSize?: number
|
||||
}
|
||||
|
||||
const ENDPOINT = 'https://account-data.hytale.com/my-account/get-launcher-data?arch=amd64&os=windows'
|
||||
|
|
@ -22,10 +23,26 @@ const STATE_KEY = 'hytale-patches'
|
|||
export interface PatchesState {
|
||||
[patchline: string]: {
|
||||
lastVersion: string
|
||||
lastPatchId: number
|
||||
lastCheck: string
|
||||
}
|
||||
}
|
||||
|
||||
async function getPatchSize(patchline: string, fromPatch: number, toPatch: number): Promise<number | undefined> {
|
||||
const url = `https://game-patches.hytale.com/patches/windows/amd64/${patchline}/${fromPatch}/${toPatch}.pwr`
|
||||
try {
|
||||
const response = await fetch(url, { method: 'HEAD' })
|
||||
if (response.ok) {
|
||||
const contentLength = response.headers.get('content-length')
|
||||
return contentLength ? Number.parseInt(contentLength, 10) : undefined
|
||||
}
|
||||
}
|
||||
catch {
|
||||
// Silently fail if HEAD request fails
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
export async function checkPatchesUpdate(
|
||||
stateStore: StateStore,
|
||||
patchlines: string[],
|
||||
|
|
@ -57,18 +74,24 @@ export async function checkPatchesUpdate(
|
|||
|
||||
if (lastState?.lastVersion === currentVersion) {
|
||||
// No update
|
||||
state[patchline] = { lastVersion: currentVersion, lastCheck: new Date().toISOString() }
|
||||
state[patchline] = { lastVersion: currentVersion, lastPatchId: patchId, lastCheck: new Date().toISOString() }
|
||||
continue
|
||||
}
|
||||
|
||||
// Update detected
|
||||
state[patchline] = { lastVersion: currentVersion, lastCheck: new Date().toISOString() }
|
||||
state[patchline] = { lastVersion: currentVersion, lastPatchId: patchId, lastCheck: new Date().toISOString() }
|
||||
|
||||
// Fetch patch size
|
||||
const patchSize = lastState?.lastPatchId !== undefined
|
||||
? await getPatchSize(patchline, lastState.lastPatchId, patchId)
|
||||
: undefined
|
||||
|
||||
updates.push({
|
||||
patchline,
|
||||
version: currentVersion,
|
||||
previousVersion: lastState?.lastVersion ?? 'unknown',
|
||||
patchId,
|
||||
patchSize,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue