+ 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) {
+ // Ask running corr games first
+ ajax(
+ "/runninggames",
+ "GET",
+ {
+ credentials: true,
+ success: (res) => {
+ // These games are garanteed to not be deleted
+ this.corrGames = res.games;
+ this.corrGames.forEach(g => {
+ g.type = "corr";
+ g.score = "*";
+ });
+ this.decorate(this.corrGames);
+ // Now ask completed games (partial list)
+ ajax(
+ "/completedgames",
+ "GET",
+ {
+ credentials: true,
+ data: { cursor: this.cursor },
+ success: (res2) => {
+ if (res2.games.length > 0) {
+ const L = res2.games.length;
+ this.cursor = res2.games[L - 1].created;
+ let completedGames = res2.games;
+ completedGames.forEach(g => g.type = "corr");
+ this.decorate(completedGames);
+ this.corrGames = this.corrGames.concat(completedGames);
+ adjustAndSetDisplay();
+ }
+ }
+ }
+ );
+ }
+ }
+ );
+ } else adjustAndSetDisplay();
+ });