+ const adjustAndSetDisplay = () => {
+ // showType is the last type viewed 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.getRunning(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)
+ this.loadMore(
+ "live",
+ () => this.loadMore("corr", () => {
+ this.loadMore("import", adjustAndSetDisplay);
+ })
+ );
+ }
+ }
+ );
+ }
+ else {
+ this.loadMore("live", () => {
+ this.loadMore("import", adjustAndSetDisplay);
+ });
+ }
+ });