+ const adjustAndSetDisplay = () => {
+ // showType is the last type viwed by the user (default)
+ let showType = localStorage.getItem("type-myGames") || "live";
+ // Live games, my turn: highest priority:
+ if (this.liveGames.some(g => !!g.myTurn)) showType = "live";
+ // Then corr games, my turn:
+ else if (this.corrGames.some(g => !!g.myTurn)) showType = "corr";
+ else {
+ // If a listing is empty, try showing the other (if non-empty)
+ const types = ["corr", "live"];
+ for (let i of [0,1]) {
+ if (
+ this[types[i] + "Games"].length > 0 &&
+ this[types[1-i] + "Games"].length == 0
+ ) {
+ showType = types[i];
+ }
+ }
+ }
+ this.setDisplay(showType);
+ };
+ GameStorage.getAll(localGames => {
+ localGames.forEach(g => g.type = "live");
+ this.decorate(localGames);
+ this.liveGames = localGames;
+ if (this.st.user.id > 0) {
+ ajax(
+ "/games",
+ "GET",
+ {
+ data: { uid: this.st.user.id },
+ success: (res) => {
+ let serverGames = res.games.filter(g => {
+ const mySide =
+ g.players[0].uid == this.st.user.id
+ ? "White"
+ : "Black";
+ return !g["deletedBy" + mySide];
+ });
+ serverGames.forEach(g => g.type = "corr");
+ this.decorate(serverGames);
+ this.corrGames = serverGames;
+ adjustAndSetDisplay();
+ }
+ }
+ );
+ } else adjustAndSetDisplay();
+ });