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