From c19c4e764a103688eb92968bad5d2f8c3c22eb62 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Thu, 30 May 2013 18:56:02 -0500 Subject: [PATCH] Redirect expired direct battle links to replays --- js/client.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/js/client.js b/js/client.js index ea352ffb6..3c8c6e0d6 100644 --- a/js/client.js +++ b/js/client.js @@ -719,7 +719,9 @@ // Just open the main menu. fragment = ''; } - this.tryJoinRoom(fragment||''); + if (!fragment) fragment = ''; + if (this.initialFragment === undefined) this.initialFragment = fragment; + this.tryJoinRoom(fragment); }, /** * Send to sim server @@ -760,9 +762,12 @@ } else { this.joinRoom(roomid, roomType, true); } - } else if (data.substr(0,8) === '|deinit|' || data.substr(0,8) === '|noinit|') { - this.removeRoom(roomid); - if (this.curRoom) this.navigate(this.curRoom.id, {replace: true}); + } else if ((data+'|').substr(0,8) === '|deinit|' || (data+'|').substr(0,8) === '|noinit|') { + if (data.charAt(1) === 'd') { // deinit + this.removeRoom(roomid); + } else { // noinit + this.unjoinRoom(roomid); + } data = data.substr(8); var pipeIndex = data.indexOf('|'); if (pipeIndex >= 0) { @@ -848,8 +853,8 @@ break; case 'roomerror': - this.removeRoom(parts[1]); - if (this.curRoom) this.navigate(this.curRoom.id, {replace: true}); + // deprecated; use |deinit| or |noinit| + this.unjoinRoom(parts[1]); this.addPopupMessage(parts.slice(2).join('|')); break; @@ -1000,6 +1005,22 @@ this.focusRoom(id); return room; }, + /** + * We tried to join a room but it didn't exist + */ + unjoinRoom: function(id, noinit) { + if (Config.server.id && this.rooms[id] && this.rooms[id].type === 'battle') { + if (id === this.initialFragment) { + // you were direct-linked to this nonexistent room + var replayid = id.substr(7); + if (Config.server.id !== 'showdown') replayid = Config.server.id+'-'+replayid; + document.location.replace('http://pokemonshowdown.com/replay/'+replayid); + return; + } + } + this.removeRoom(id); + if (this.curRoom) this.navigate(this.curRoom.id, {replace: true}); + }, tryJoinRoom: function(id) { this.joinRoom(id); },