[client] compare versions in updater

This commit is contained in:
2019-09-30 21:42:33 +03:00
parent 1e79fcfacb
commit cfa1475cf6
6 changed files with 71 additions and 12 deletions

View File

@@ -22,4 +22,3 @@
* ice brick fix
* shot delay
* boat in tank state
* updater version compare

View File

@@ -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 {

View File

@@ -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;

View File

@@ -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<Bool> {
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)
));
});
}

View File

@@ -0,0 +1,53 @@
package ru.m.update;
abstract Version(Array<Int>) {
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" : ""}';
}
}

View File

@@ -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, "/")}`,