BlueGit

timfa questward

Files

questward / shared / quests / favoriteQuest.js

favoriteQuest.js

favoriteQuest.js 1.3 KiB Download Raw
1/** Client-only favorite quest pin (one active quest at a time). */
2
3const STORAGE_KEY = "questward-favorite-quest";
4
5/**
6 * @returns {Record<string, string>}
7 */
8function readMap ()
9{
10 try
11 {
12 const raw = localStorage.getItem(STORAGE_KEY);
13 if (!raw)
14 { return {}; }
15 const parsed = JSON.parse(raw);
16 if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
17 { return {}; }
18 /** @type {Record<string, string>} */
19 const out = {};
20 for (const [key, value] of Object.entries(parsed))
21 {
22 if (typeof value === "string" && value.trim())
23 { out[String(key)] = value.trim(); }
24 }
25 return out;
26 }
27 catch
28 { return {}; }
29}
30
31function writeMap (map)
32{
33 try
34 {
35 localStorage.setItem(STORAGE_KEY, JSON.stringify(map));
36 }
37 catch
38 { /* ignore quota / private mode */ }
39}
40
41export function loadFavoriteQuestId (characterId)
42{
43 const id = characterId | 0;
44 if (!id)
45 { return null; }
46 return readMap()[String(id)] ?? null;
47}
48
49export function saveFavoriteQuestId (characterId, questId)
50{
51 const id = characterId | 0;
52 if (!id)
53 { return; }
54 const map = readMap();
55 const next = String(questId ?? "").trim();
56 if (!next)
57 { delete map[String(id)]; }
58 else
59 { map[String(id)] = next; }
60 writeMap(map);
61}
62
63export function clearFavoriteQuestId (characterId)
64{
65 saveFavoriteQuestId(characterId, null);
66}