From cfa1475cf67d0ab8a12995f77048de66492daaa8 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 30 Sep 2019 21:42:33 +0300 Subject: [PATCH] [client] compare versions in updater --- WORK.md | 1 - src/client/haxe/ru/m/tankz/Init.hx | 1 + src/client/haxe/ru/m/tankz/view/MenuFrame.hx | 2 +- src/client/haxe/ru/m/{ => update}/Updater.hx | 14 ++++-- src/client/haxe/ru/m/update/Version.hx | 53 ++++++++++++++++++++ tasks/gulp-publish.js | 12 +++-- 6 files changed, 71 insertions(+), 12 deletions(-) rename src/client/haxe/ru/m/{ => update}/Updater.hx (85%) create mode 100644 src/client/haxe/ru/m/update/Version.hx diff --git a/WORK.md b/WORK.md index 076d5f9..b62400f 100644 --- a/WORK.md +++ b/WORK.md @@ -22,4 +22,3 @@ * ice brick fix * shot delay * boat in tank state - * updater version compare diff --git a/src/client/haxe/ru/m/tankz/Init.hx b/src/client/haxe/ru/m/tankz/Init.hx index 3e495ef..79df69a 100644 --- a/src/client/haxe/ru/m/tankz/Init.hx +++ b/src/client/haxe/ru/m/tankz/Init.hx @@ -30,6 +30,7 @@ import ru.m.tankz.storage.GameStorage; import ru.m.tankz.storage.NetworkStorage; import ru.m.tankz.storage.RecordStorage; import ru.m.tankz.storage.SettingsStorage; +import ru.m.update.Updater; class Init { diff --git a/src/client/haxe/ru/m/tankz/view/MenuFrame.hx b/src/client/haxe/ru/m/tankz/view/MenuFrame.hx index 2d4ab80..fe0992b 100644 --- a/src/client/haxe/ru/m/tankz/view/MenuFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/MenuFrame.hx @@ -2,7 +2,6 @@ package ru.m.tankz.view; import haxework.view.data.DataView; import haxework.view.form.ButtonView; -import haxework.view.form.LabelView; import haxework.view.frame.FrameSwitcher; import haxework.view.frame.FrameView; import ru.m.tankz.bundle.ILevelBundle; @@ -15,6 +14,7 @@ import ru.m.tankz.view.common.PackView; import ru.m.tankz.view.network.RoomFrame; import ru.m.tankz.view.network.RoomListFrame; import ru.m.tankz.view.popup.LoginPopup; +import ru.m.update.Updater; using ru.m.tankz.view.ViewUtil; diff --git a/src/client/haxe/ru/m/Updater.hx b/src/client/haxe/ru/m/update/Updater.hx similarity index 85% rename from src/client/haxe/ru/m/Updater.hx rename to src/client/haxe/ru/m/update/Updater.hx index 9e63caf..4753101 100644 --- a/src/client/haxe/ru/m/Updater.hx +++ b/src/client/haxe/ru/m/update/Updater.hx @@ -1,10 +1,11 @@ -package ru.m; +package ru.m.update; import haxework.net.JsonLoader; import openfl.Lib; import openfl.net.URLRequest; import promhx.Promise; import ru.m.Device; +import ru.m.update.Version; @:enum abstract PackageType(String) from String to String { var APK = "apk"; @@ -19,6 +20,7 @@ typedef PackageInfo = { var path:String; var filename:String; var url:String; + var version:String; } typedef PackagesBundle = { @@ -35,7 +37,7 @@ class Updater { public var bundle(default, null):PackagesBundle; private var url:String; - private var version:String; + private var version:Version; public function new(version:String, url:String) { this.url = url; @@ -61,9 +63,11 @@ class Updater { public function check():Promise { return resolveBundle().then(function(bundle:PackagesBundle):Bool { this.bundle = bundle; - return bundle.version != version && Lambda.exists(bundle.packages, function(item) { - return item.platform == Device.platform && item.type == type; - }); + return Lambda.exists(bundle.packages, function(item) return ( + item.platform == Device.platform && + item.type == type && + version < Version.fromString(item.version) + )); }); } diff --git a/src/client/haxe/ru/m/update/Version.hx b/src/client/haxe/ru/m/update/Version.hx new file mode 100644 index 0000000..5686e11 --- /dev/null +++ b/src/client/haxe/ru/m/update/Version.hx @@ -0,0 +1,53 @@ +package ru.m.update; + +abstract Version(Array) { + + public function new(mayor:Int, minor:Int, patch:Int, snapshot:Bool = false) { + this = [ + mayor, + minor, + patch, + snapshot ? -1 : 0, + ]; + } + + @:arrayAccess public inline function get(key:Int) return this[key]; + + public function compare(other:Version):Int { + if (other == null) { + return 1; + } + for (i in 0...4) { + var d = this[i] - other[i]; + if (d != 0) { + return d; + } + } + return 0; + } + + @:op(A > B) static function gt(a:Version, b:Version):Bool return a.compare(b) > 0; + + @:op(A == B) static function eq(a:Version, b:Version):Bool return a.compare(b) == 0; + + @:op(A < B) static function lt(a:Version, b:Version):Bool return a.compare(b) < 0; + + @:from public static function fromString(value:String):Version { + trace("!fromString", value); + var r = ~/(\d+)\.(\d+)\.(\d+)(-SNAPSHOT)?/; + return if (r.match(value)) { + new Version( + Std.parseInt(r.matched(1)), + Std.parseInt(r.matched(2)), + Std.parseInt(r.matched(3)), + r.matched(4) != null + ); + } else { + new Version(0, 0, 0); + } + } + + @:to public function toString():String { + return '${this[0]}.${this[1]}.${this[2]}${this[3] < 0 ? "-SNAPSHOT" : ""}'; + } +} diff --git a/tasks/gulp-publish.js b/tasks/gulp-publish.js index a702561..2649207 100755 --- a/tasks/gulp-publish.js +++ b/tasks/gulp-publish.js @@ -6,10 +6,11 @@ const through = require('through2'); class Package { - constructor(platform, type, version) { + constructor(platform, type, version, versionInt) { this.platform = platform; this.type = type; this.version = version; + this.versionInt = versionInt; } get key() { @@ -47,9 +48,9 @@ class Package { } static getVersion(filename) { - const m = /(\d+)\.(\d+).(\d+)/.exec(filename); + const m = /(\d+)\.(\d+)\.(\d+)/.exec(filename); if (m) { - return parseInt(m[1]) * 10000 + parseInt(m[2]) * 100 + parseInt(m[3]); + return [m[0], parseInt(m[1]) * 10000 + parseInt(m[2]) * 100 + parseInt(m[3])]; } return null; } @@ -59,7 +60,7 @@ class Package { const type = this.getType(filename); const version = this.getVersion(filename); if (platform && type && version) { - return new Package(platform, type, version); + return new Package(platform, type, version[0], version[1]); } return null; } @@ -86,11 +87,12 @@ module.exports = (name, version, publishDir, publishUrl) => function publish(cb) const basepath = file.path.replace(file.base + path.sep, ''); const pack = Package.parseFilename(file.path); if (pack) { - if (!packages[pack.key] || packages[pack.key].version < pack.version) { + if (!packages[pack.key] || packages[pack.key].versionInt < pack.versionInt) { packages[pack.key] = { platform: pack.platform, type: pack.type, version: pack.version, + versionInt: pack.versionInt, path: basepath, filename: basepath.split(path.sep).pop(), url: `${publishUrl}/${basepath.replace(path.sep, "/")}`,