From cba4e50aeb0b3141ec01759e3111e18297fc6655 Mon Sep 17 00:00:00 2001 From: lorumic Date: Sun, 22 Oct 2023 14:31:29 +0200 Subject: [PATCH] Add queued action feature, apply it to battle -> login -> battle flow --- js/client-mainmenu.js | 2 +- js/client-topbar.js | 22 ++++++++++++++++------ js/client.js | 39 +++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/js/client-mainmenu.js b/js/client-mainmenu.js index d83c7f88c4..ee6cd84720 100644 --- a/js/client-mainmenu.js +++ b/js/client-mainmenu.js @@ -1079,7 +1079,7 @@ } if (!app.user.get('named')) { - app.addPopup(LoginPopup); + app.addPopup(LoginPopup, {nextAction: 'battle'}); return; } diff --git a/js/client-topbar.js b/js/client-topbar.js index 81d77a2078..9390709f22 100644 --- a/js/client-topbar.js +++ b/js/client-topbar.js @@ -42,6 +42,16 @@ } buf += ' '; this.$userbar.html(buf); + + // after userbar update, execute any queued action + switch (app.nextAction) { + case 'battle': + $('button[name=search]').click(); + break; + default: + break; + } + app.nextAction = undefined; }, login: function () { app.addPopup(LoginPopup); @@ -915,7 +925,7 @@ var name = (data.name || ''); if (!name && app.user.get('named')) name = app.user.get('name'); - buf += '

'; + buf += '

' + (data.nextAction ? '' : '') + '

'; if (name) { buf += '

(Others will be able to see your name change. To change name privately, use "Log out")

'; } @@ -944,8 +954,7 @@ }, submit: function (data) { this.close(); - if (!$.trim(data.username)) return; - app.user.rename(data.username); + app.user.rename(data.username, data.nextAction); } }); @@ -1067,7 +1076,7 @@ } buf += '

If this is your account:

'; - buf += '

'; + buf += '

' + (data.nextAction ? '' : '') + '

'; if (data.special === '@gmail') { buf += '
[loading Google log-in button]
'; buf += '

'; @@ -1113,12 +1122,13 @@ } }, login: function () { + var nextAction = this.$('input[name=nextAction]').val(); this.close(); - app.addPopup(LoginPopup); + app.addPopup(LoginPopup, {nextAction: nextAction}); }, submit: function (data) { this.close(); - app.user.passwordRename(data.username, data.password); + app.user.passwordRename(data.username, data.password, '', data.nextAction); } }); diff --git a/js/client.js b/js/client.js index 38212c2476..91f8ea7282 100644 --- a/js/client.js +++ b/js/client.js @@ -237,7 +237,7 @@ function toId() { * `login:noresponse` * triggered if the login server did not return a response */ - finishRename: function (name, assertion) { + finishRename: function (name, assertion, nextAction) { if (assertion.slice(0, 14).toLowerCase() === ''); @@ -250,14 +250,16 @@ function toId() { return; } if (assertion === ';') { - this.trigger('login:authrequired', name); + this.trigger('login:authrequired', name, '', nextAction); } else if (assertion === ';;@gmail') { - this.trigger('login:authrequired', name, '@gmail'); + this.trigger('login:authrequired', name, '@gmail', nextAction); } else if (assertion.substr(0, 2) === ';;') { - this.trigger('login:invalidname', name, assertion.substr(2)); + this.trigger('login:invalidname', name, assertion.substr(2), nextAction); } else if (assertion.indexOf('\n') >= 0 || !assertion) { app.addPopupMessage("Something is interfering with our connection to the login server."); } else { + // login completed, transfer any queued action to app scope + app.nextAction = nextAction; app.trigger('loggedin'); app.send('/trn ' + name + ',0,' + assertion); } @@ -270,7 +272,7 @@ function toId() { * * See `finishRename` above for a list of events this can emit. */ - rename: function (name) { + rename: function (name, nextAction) { // | , ; are not valid characters in names name = name.replace(/[\|,;]+/g, ''); for (var i in this.replaceList) { @@ -279,7 +281,7 @@ function toId() { for (var i in this.normalizeList) { name = name.replace(this.normalizeList[i], i); } - var userid = toUserid(name); + var userid = $.trim(toUserid(name)); if (!userid) { app.addPopupMessage("Usernames must contain at least one letter."); return; @@ -292,13 +294,13 @@ function toId() { userid: userid, challstr: this.challstr }, function (data) { - self.finishRename(name, data); + self.finishRename(name, data, nextAction); }); } else { app.send('/trn ' + name); } }, - passwordRename: function (name, password, special) { + passwordRename: function (name, password, special, nextAction) { var self = this; $.post(this.getActionPHP(), { act: 'login', @@ -309,7 +311,7 @@ function toId() { if (data && data.curuser && data.curuser.loggedin) { // success! self.set('registered', data.curuser); - self.finishRename(name, data.assertion); + self.finishRename(name, data.assertion, nextAction); } else { // wrong password if (special === '@gmail') { @@ -320,7 +322,8 @@ function toId() { app.addPopup(LoginPasswordPopup, { username: name, error: data.error || 'Wrong password.', - special: special + special: special, + nextAction: nextAction }); } }), 'text'); @@ -546,12 +549,20 @@ function toId() { self.addPopup(ReconnectPopup, {cantconnect: true}); }); - this.user.on('login:invalidname', function (name, reason) { - self.addPopup(LoginPopup, {name: name, reason: reason}); + this.user.on('login:invalidname', function (name, reason, nextAction) { + self.addPopup(LoginPopup, { + name: name, + reason: reason, + nextAction: nextAction + }); }); - this.user.on('login:authrequired', function (name, special) { - self.addPopup(LoginPasswordPopup, {username: name, special: special}); + this.user.on('login:authrequired', function (name, special, nextAction) { + self.addPopup(LoginPasswordPopup, { + username: name, + special: special, + nextAction: nextAction + }); }); this.on('loggedin', function () {