[msc] make playersettings more efficient

This commit is contained in:
Devin Zuczek
2026-09-30 18:04:56 -04:00
parent 90c808c4a3
commit e00103c6b3
13 changed files with 178 additions and 37 deletions
+8 -11
View File
@@ -11,6 +11,8 @@ import {
getImagesByRoom,
getPlayerFeed,
getSlideshowImages,
mergePlayerSettings,
readPlayerSettings,
SavedImageType,
setImageAccessibility,
setImageCheer,
@@ -100,10 +102,7 @@ async function getPlayerSettings(
env: App['Bindings'],
accountId: number
): Promise<Record<string, string> | null> {
return env.RECFLARE_PLAYER_SETTINGS.get<Record<string, string>>(
`player:${accountId}`,
'json'
).catch(() => null)
return readPlayerSettings(env.RECFLARE_PLAYER_SETTINGS, accountId).catch(() => null)
}
/** The caller's stored photo-tagging preference, or the default when they have none. */
@@ -119,19 +118,17 @@ async function readPhotoTaggingSetting(env: App['Bindings'], accountId: number):
*
* The write MERGES, as the `playersettings` worker's own PUT does: the map holds every
* setting the player has (OOBE state, tutorial mask, …), so storing this one on its own
* would wipe the rest. Read-modify-write on KV isn't atomic, but the same is true there,
* and racing writers here means one player toggling two of their own options at once.
* would wipe the rest. Re-posting the value already stored writes nothing — the client
* does that freely, and KV writes are the cost.
*/
async function writePhotoTaggingSetting(
env: App['Bindings'],
accountId: number,
setting: number
): Promise<void> {
const stored = (await getPlayerSettings(env, accountId)) ?? {}
await env.RECFLARE_PLAYER_SETTINGS.put(
`player:${accountId}`,
JSON.stringify({ ...stored, [PHOTO_TAGGING_KEY]: String(setting) })
)
await mergePlayerSettings(env.RECFLARE_PLAYER_SETTINGS, accountId, {
[PHOTO_TAGGING_KEY]: String(setting),
})
}
/**
@@ -6742,6 +6742,13 @@ describe('images', () => {
// The setting is per-player.
expect(await (await read('710')).text()).toBe('2')
// Re-posting the stored value writes nothing to KV: the raw value is seeded with
// whitespace JSON.stringify never produces, and it survives the PUT untouched.
const padded = '{ "Recroom.OOBE": "77", "playerPhotoTaggingSetting": "1" }'
await env.RECFLARE_PLAYER_SETTINGS.put('player:712', padded)
expect(await (await write('712', { Setting: 1 })).text()).toBe('1')
expect(await env.RECFLARE_PLAYER_SETTINGS.get('player:712', 'text')).toBe(padded)
// A body with no readable Setting leaves the stored value alone rather than writing 0
// — and answers what the player still has.
expect(await (await write('710', { Nothing: true })).text()).toBe('2')