Experimental symmetric randomness + deterministic option
[vchess.git] / client / src / store.js
CommitLineData
f0c68a04
BA
1// NOTE: do not use ajax() here because ajax.js require the store for translations
2import params from "./parameters"; //for server URL
c66a829b 3import { getRandString } from "./utils/alea";
c66a829b 4
4486a21e 5// Global store: see https://medium.com/fullstackio/managing-state-in-vue-js-23a0352b1c87
6808d7a1 6export const store = {
c66a829b
BA
7 state: {
8 variants: [],
9 tr: {},
10 user: {},
c66a829b 11 settings: {},
6808d7a1 12 lang: ""
c66a829b 13 },
cdb34c93 14 socketCloseListener: null,
8418f0d7 15 initialize() {
f0c68a04
BA
16 fetch(
17 params.serverUrl + "/variants",
18 {method: "GET"},
19 )
20 .then(res => res.json())
21 .then(json => {
22 this.state.variants = json.variantArray.sort(
e9b0b504 23 (v1,v2) => v1.name.localeCompare(v2.name));
6808d7a1 24 });
9ef63965 25 let mysid = localStorage.getItem("mysid");
910d631b 26 // Assign mysid only once (until next time user clear browser data)
6808d7a1 27 if (!mysid) {
98f48579 28 mysid = getRandString();
910d631b 29 localStorage.setItem("mysid", mysid);
98f48579 30 }
5ea8d113 31 // Quick user setup using local storage:
c66a829b 32 this.state.user = {
9ef63965
BA
33 id: localStorage.getItem("myid") || 0,
34 name: localStorage.getItem("myname") || "", //"" for "anonymous"
a7f9f050
BA
35 email: "", //unknown yet
36 notify: false, //email notifications
6808d7a1 37 sid: mysid
c66a829b 38 };
5ea8d113
BA
39 // Slow verification through the server:
40 // NOTE: still superficial identity usurpation possible, but difficult.
f0c68a04
BA
41 fetch(
42 params.serverUrl + "/whoami",
43 {
44 method: "GET",
45 credentials: params.credentials
46 }
47 )
48 .then(res => res.json())
49 .then(json => {
50 this.state.user.id = json.id;
9ef63965 51 const storedId = localStorage.getItem("myid");
f0c68a04 52 if (json.id > 0 && !storedId)
910d631b 53 // User cleared localStorage
f0c68a04
BA
54 localStorage.setItem("myid", json.id);
55 else if (json.id == 0 && !!storedId)
910d631b 56 // User cleared cookie
9ef63965 57 localStorage.removeItem("myid");
f0c68a04 58 this.state.user.name = json.name;
9ef63965 59 const storedName = localStorage.getItem("myname");
f0c68a04 60 if (!!json.name && !storedName)
910d631b 61 // User cleared localStorage
f0c68a04
BA
62 localStorage.setItem("myname", json.name);
63 else if (!json.name && !!storedName)
910d631b 64 // User cleared cookie
9ef63965 65 localStorage.removeItem("myname");
f0c68a04
BA
66 this.state.user.email = json.email;
67 this.state.user.notify = json.notify;
5ea8d113 68 });
c66a829b 69 // Settings initialized with values from localStorage
db1f1f9a
BA
70 const getItemDefaultTrue = (item) => {
71 const value = localStorage.getItem(item);
72 if (!value) return true;
73 return value == "true";
74 };
c66a829b 75 this.state.settings = {
dfeb96ea 76 bcolor: localStorage.getItem("bcolor") || "lichess",
db1f1f9a
BA
77 sound: getItemDefaultTrue("sound"),
78 hints: getItemDefaultTrue("hints"),
79 highlight: getItemDefaultTrue("highlight")
c66a829b 80 };
6808d7a1 81 const supportedLangs = ["en", "es", "fr"];
dfa6eb76 82 const navLanguage = navigator.language.substr(0,2);
6808d7a1
BA
83 this.state.lang =
84 localStorage["lang"] ||
dfa6eb76 85 (supportedLangs.includes(navLanguage) ? navLanguage : "en");
8418f0d7 86 this.setTranslations();
c66a829b 87 },
dfeb96ea
BA
88 updateSetting: function(propName, value) {
89 this.state.settings[propName] = value;
90 localStorage.setItem(propName, value);
91 },
c66a829b 92 setTranslations: async function() {
7c3cd379 93 // Import translations from "./translations/$lang.js"
c66a829b
BA
94 const tModule = await import("@/translations/" + this.state.lang + ".js");
95 this.state.tr = tModule.translations;
96 },
97 setLanguage(lang) {
98 this.state.lang = lang;
99 this.setTranslations();
6808d7a1 100 }
c66a829b 101};