Fix computer games for Apocalypse variant
[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() {
e57c4de4
BA
16 const headers = {
17 "Content-Type": "application/json;charset=UTF-8",
18 "X-Requested-With": "XMLHttpRequest"
19 };
f0c68a04
BA
20 fetch(
21 params.serverUrl + "/variants",
e57c4de4
BA
22 {
23 method: "GET",
24 headers: headers
25 }
f0c68a04
BA
26 )
27 .then(res => res.json())
28 .then(json => {
e57c4de4
BA
29 if (!Array.isArray(json.variantArray)) {
30 alert("Variants loading failed: reload the page");
31 return;
32 }
31ccd7e3 33 this.state.variants = json.variantArray
31ccd7e3 34 .sort((v1,v2) => v1.name.localeCompare(v2.name));
6808d7a1 35 });
9ef63965 36 let mysid = localStorage.getItem("mysid");
910d631b 37 // Assign mysid only once (until next time user clear browser data)
6808d7a1 38 if (!mysid) {
98f48579 39 mysid = getRandString();
910d631b 40 localStorage.setItem("mysid", mysid);
98f48579 41 }
5ea8d113 42 // Quick user setup using local storage:
c66a829b 43 this.state.user = {
9ef63965
BA
44 id: localStorage.getItem("myid") || 0,
45 name: localStorage.getItem("myname") || "", //"" for "anonymous"
a7f9f050
BA
46 email: "", //unknown yet
47 notify: false, //email notifications
d9a7a1e4 48 newsRead: localStorage.getItem("newsRead") || 0,
6808d7a1 49 sid: mysid
c66a829b 50 };
5ea8d113
BA
51 // Slow verification through the server:
52 // NOTE: still superficial identity usurpation possible, but difficult.
f0c68a04
BA
53 fetch(
54 params.serverUrl + "/whoami",
55 {
56 method: "GET",
e57c4de4 57 headers: headers,
f0c68a04
BA
58 credentials: params.credentials
59 }
60 )
61 .then(res => res.json())
62 .then(json => {
63 this.state.user.id = json.id;
9ef63965 64 const storedId = localStorage.getItem("myid");
f0c68a04 65 if (json.id > 0 && !storedId)
910d631b 66 // User cleared localStorage
f0c68a04
BA
67 localStorage.setItem("myid", json.id);
68 else if (json.id == 0 && !!storedId)
910d631b 69 // User cleared cookie
9ef63965 70 localStorage.removeItem("myid");
f0c68a04 71 this.state.user.name = json.name;
9ef63965 72 const storedName = localStorage.getItem("myname");
f0c68a04 73 if (!!json.name && !storedName)
910d631b 74 // User cleared localStorage
f0c68a04
BA
75 localStorage.setItem("myname", json.name);
76 else if (!json.name && !!storedName)
910d631b 77 // User cleared cookie
9ef63965 78 localStorage.removeItem("myname");
f0c68a04
BA
79 this.state.user.email = json.email;
80 this.state.user.notify = json.notify;
d9a7a1e4
BA
81 if (!!json.newsRead && json.newsRead > this.state.user.newsRead)
82 this.state.user.newsRead = json.newsRead;
5ea8d113 83 });
c66a829b 84 // Settings initialized with values from localStorage
db1f1f9a
BA
85 const getItemDefaultTrue = (item) => {
86 const value = localStorage.getItem(item);
87 if (!value) return true;
88 return value == "true";
89 };
c66a829b 90 this.state.settings = {
dfeb96ea 91 bcolor: localStorage.getItem("bcolor") || "lichess",
db1f1f9a
BA
92 sound: getItemDefaultTrue("sound"),
93 hints: getItemDefaultTrue("hints"),
f35b9960 94 highlight: getItemDefaultTrue("highlight"),
aae89b49 95 gotonext: getItemDefaultTrue("gotonext"),
f35b9960 96 randomness: parseInt(localStorage.getItem("randomness"))
c66a829b 97 };
f35b9960
BA
98 if (isNaN(this.state.settings.randomness))
99 // Default: random asymmetric
100 this.state.settings.randomness = 2;
6808d7a1 101 const supportedLangs = ["en", "es", "fr"];
dfa6eb76 102 const navLanguage = navigator.language.substr(0,2);
6808d7a1
BA
103 this.state.lang =
104 localStorage["lang"] ||
dfa6eb76 105 (supportedLangs.includes(navLanguage) ? navLanguage : "en");
8418f0d7 106 this.setTranslations();
c66a829b 107 },
dfeb96ea
BA
108 updateSetting: function(propName, value) {
109 this.state.settings[propName] = value;
110 localStorage.setItem(propName, value);
111 },
c66a829b 112 setTranslations: async function() {
7c3cd379 113 // Import translations from "./translations/$lang.js"
c66a829b
BA
114 const tModule = await import("@/translations/" + this.state.lang + ".js");
115 this.state.tr = tModule.translations;
116 },
117 setLanguage(lang) {
118 this.state.lang = lang;
119 this.setTranslations();
6808d7a1 120 }
c66a829b 121};