From a5bb6fbca9b4df3534539e7293f3863b241e094e Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 13 Sep 2016 14:37:51 -0600 Subject: [PATCH] Add minimum client version check code (#2), modify critical error handling, bump version to 1.5.2 --- www/js/main.js | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/www/js/main.js b/www/js/main.js index 69aa620..c549bfe 100644 --- a/www/js/main.js +++ b/www/js/main.js @@ -40,16 +40,51 @@ function onDeviceReady() { if (navigator.network.connection.type === Connection.NONE) { navigator.notification.alert("You need an Internet connection to continue.", function () { navigator.app.exitApp(); - }, "No network", 'Dismiss'); + }, "No network", 'OK'); + clientProblemsDialog("You need an Internet connection to continue."); + return; } + $.getJSON(mkApiUrl("minclientversion"), function (data) { + if (data.status == "OK") { + if (compareVersions(window.cordova.plugins.version.getAppVersion(), data.version) < 0) { + navigator.notification.alert("Your game client is too old. You must update the app before playing.", function () { + navigator.app.exitApp(); + }, "Outdated app", 'OK'); + clientProblemsDialog("Your game client is too old. You must update the app before playing."); + } + } + }); } -function serverProblemsDialog(errmsg) { - $('#content-zone').load("screens/servererror.html", function () { - if (typeof errmsg !== 'undefined') { - $('#serverproblemmsg').text(errmsg); +/** + * Compare two version strings. + * http://stackoverflow.com/a/16187766/2534036 + * @param {string} a + * @param {string} b + * @returns a number < 0 if a < b, a number > 0 if a > b, 0 if a = b + */ +function compareVersions(a, b) { + var i, diff; + var regExStrip0 = /(\.0+)+$/; + var segmentsA = a.replace(regExStrip0, '').split('.'); + var segmentsB = b.replace(regExStrip0, '').split('.'); + var l = Math.min(segmentsA.length, segmentsB.length); + + for (i = 0; i < l; i++) { + diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10); + if (diff) { + return diff; } - }); + } + return segmentsA.length - segmentsB.length; +} + +function serverProblemsDialog(errmsg) { + window.location = "servererror.html?errmsg=" + errmsg; +} + +function clientProblemsDialog(errmsg) { + window.location = "clienterror.html?errmsg=" + errmsg; } function mkApiUrl(action, server) {