From ed03b8e117a588bf57d480585d280338c6ad2d78 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Thu, 7 Apr 2016 11:22:06 -0300 Subject: [PATCH] Prevent pairing if already in game --- static/js/index.js | 2 ++ static/js/utils.js | 43 +++++++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/static/js/index.js b/static/js/index.js index f98a8b5..f625449 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -108,6 +108,8 @@ function pairPushed() { snackbarLog('Pairing failed'); $('#pairButton')[0].innerHTML = 'Pairing Failed'; document.getElementById('pairingDialogText').innerHTML = 'Error: Pairing failed'; + if (api.currentGame != 0) + snackbarLog(target + ' is already in game. Cannot pair!'); return; } diff --git a/static/js/utils.js b/static/js/utils.js index d48f0dc..bf27073 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -36,24 +36,36 @@ function NvHTTP(address, clientUid) { NvHTTP.prototype = { refreshServerInfo: function () { return sendMessage('openUrl', [ _self._baseUrlHttps + '/serverinfo?' + _self._buildUidStr()]).then(function(ret) { - $xml = _self._parseXML(ret); - $root = $xml.find('root') - - if($root.attr("status_code") == 200) { - _self.paired = $root.find("PairStatus").text().trim() == 1; - _self.currentGame = parseInt($root.find("currentgame").text().trim(), 10); - _self.serverMajorVersion = parseInt($root.find("appversion").text().trim().substring(0, 1), 10); - - // GFE 2.8 started keeping currentgame set to the last game played. As a result, it no longer - // has the semantics that its name would indicate. To contain the effects of this change as much - // as possible, we'll force the current game to zero if the server isn't in a streaming session. - if ($root.find("state").text().trim().endsWith("_SERVER_AVAILABLE")) { - _self.currentGame = 0; - } + if (!_self._parseServerInfo(ret)) { + return sendMessage('openUrl', [ _self._baseUrlHttp + '/serverinfo?' + _self._buildUidStr()]).then(function(retHttp) { + _self._parseServerInfo(retHttp); + }); } }); }, + _parseServerInfo: function(xmlStr) { + $xml = _self._parseXML(xmlStr); + $root = $xml.find('root'); + + if($root.attr("status_code") != 200) { + return false; + } + + _self.paired = $root.find("PairStatus").text().trim() == 1; + _self.currentGame = parseInt($root.find("currentgame").text().trim(), 10); + _self.serverMajorVersion = parseInt($root.find("appversion").text().trim().substring(0, 1), 10); + + // GFE 2.8 started keeping currentgame set to the last game played. As a result, it no longer + // has the semantics that its name would indicate. To contain the effects of this change as much + // as possible, we'll force the current game to zero if the server isn't in a streaming session. + if ($root.find("state").text().trim().endsWith("_SERVER_AVAILABLE")) { + _self.currentGame = 0; + } + + return true; + }, + getAppById: function (appId) { return _self.getAppList().then(function (list) { var retApp = null; @@ -167,6 +179,9 @@ NvHTTP.prototype = { if (_self.paired) return true; + if (_self.currentGame != 0) + return false; + return sendMessage('pair', [_self.serverMajorVersion, _self.address, randomNumber]).then(function (pairStatus) { return sendMessage('openUrl', [_self._baseUrlHttps + '/pair?uniqueid=' + _self.clientUid + '&devicename=roth&updateState=1&phrase=pairchallenge']).then(function (ret) { $xml = _self._parseXML(ret);