diff --git a/package.json b/package.json index 0d6f8a2..bdf5681 100644 --- a/package.json +++ b/package.json @@ -40,4 +40,4 @@ "android" ] } -} +} \ No newline at end of file diff --git a/www/js/main.js b/www/js/main.js index a98c2af..fd0af0b 100644 --- a/www/js/main.js +++ b/www/js/main.js @@ -55,6 +55,52 @@ router.on("pageInit", function (pagedata) { }); }); +/** + * Check if the server is alive. + * @param {function} callback + * @returns {undefined} + */ +function isServerAlive(callback) { + callAPIRawResponse("ping", { + username: localStorage.getItem("username"), + password: localStorage.getItem("password") + }, function (data) { + callback(true); + }, function (msg, xhr) { + app.preloader.hide(); + if (xhr.status == 401) { + callback(true); + } else if (xhr.status == 400) { + callback(true); + } else { + callback(false); + } + }); +} + +/** + * 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; +} + /** * Check if the saved username and password are valid. * @param {function} callback A function that will be called with a boolean argument indicating validity. @@ -79,22 +125,39 @@ function checkLogin(callback) { if (localStorage.getItem("configured") == null) { router.navigate("/signin"); } else { - checkLogin(function (valid) { - if (valid) { - callAPI("playerexists", { + isServerAlive(function (yes) { + if (yes) { + callAPI("version", { username: localStorage.getItem("username"), password: localStorage.getItem("password") - }, function (resp) { - if (resp.exists == true) { - router.navigate("/home"); - } else { - router.navigate("/chooseteam"); - } - }, function (msg) { - router.navigate("/signin"); + }, function (data) { + $.getJSON("package.json", {}, function (local) { + if (compareVersions(local.version, data.version) < 0) { + router.navigate("/upgrade"); + } else { + checkLogin(function (valid) { + if (valid) { + callAPI("playerexists", { + username: localStorage.getItem("username"), + password: localStorage.getItem("password") + }, function (resp) { + if (resp.exists == true) { + router.navigate("/home"); + } else { + router.navigate("/chooseteam"); + } + }, function (msg) { + router.navigate("/signin"); + }); + } else { + router.navigate("/signin"); + } + }); + } + }); }); } else { - router.navigate("/signin"); + router.navigate("/connection"); } }); } \ No newline at end of file diff --git a/www/package.json b/www/package.json index 3ac8106..585a142 100644 --- a/www/package.json +++ b/www/package.json @@ -1,13 +1,13 @@ { - "name": "TerranQuest-Web", - "version": "2.0.0", - "main": "index.js", - "license": "MPL-2.0", - "dependencies": { - "@fortawesome/fontawesome-free": "^5.8.1", - "framework7": "^4.3.0", - "jquery": "^3.4.0", - "mapbox-gl": "^0.54.0", - "three": "^0.104.0" - } + "name": "TerranQuest-Web", + "version": "2.0.0", + "main": "index.js", + "license": "MPL-2.0", + "dependencies": { + "@fortawesome/fontawesome-free": "^5.8.1", + "framework7": "^4.3.0", + "jquery": "^3.4.0", + "mapbox-gl": "^0.54.0", + "three": "^0.104.0" + } } diff --git a/www/pages/connection.html b/www/pages/connection.html new file mode 100644 index 0000000..beffd43 --- /dev/null +++ b/www/pages/connection.html @@ -0,0 +1,25 @@ + + +
+ + + + +
+ +
+

TerranQuest can't connect to the game server. +
+ Try Again +

+
+ +
+ +
\ No newline at end of file diff --git a/www/pages/upgrade.html b/www/pages/upgrade.html new file mode 100644 index 0000000..c64685e --- /dev/null +++ b/www/pages/upgrade.html @@ -0,0 +1,23 @@ + + +
+ + + + +
+ +
+

This version of TerranQuest is too old to work properly with the + game server.

+
+ +
+ +
\ No newline at end of file diff --git a/www/routes.js b/www/routes.js index 7c6c363..401cc86 100644 --- a/www/routes.js +++ b/www/routes.js @@ -57,6 +57,16 @@ var routes = [ } } }, + { + path: '/upgrade', + url: './pages/upgrade.html', + name: 'upgrade' + }, + { + path: '/connection', + url: './pages/connection.html', + name: 'connection' + }, { path: '/settings', name: 'settings',