From e032c25e1ac4bfd5008c4f94db89ba2b350e7245 Mon Sep 17 00:00:00 2001 From: shmyga Date: Thu, 29 Aug 2019 17:43:05 +0300 Subject: [PATCH] [client] add Updater --- src/client/haxe/ru/m/Updater.hx | 72 +++++++++++++++++++ src/client/haxe/ru/m/tankz/Init.hx | 9 ++- src/client/haxe/ru/m/tankz/view/MenuFrame.hx | 12 ++++ .../haxe/ru/m/tankz/view/MenuFrame.yaml | 4 ++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/client/haxe/ru/m/Updater.hx diff --git a/src/client/haxe/ru/m/Updater.hx b/src/client/haxe/ru/m/Updater.hx new file mode 100644 index 0000000..1912b06 --- /dev/null +++ b/src/client/haxe/ru/m/Updater.hx @@ -0,0 +1,72 @@ +package ru.m; + +import haxework.net.JsonLoader; +import openfl.Lib; +import openfl.net.URLRequest; +import promhx.Promise; + +typedef PackageInfo = { + var type:String; + var path:String; + var filename:String; + var url:String; +} + +typedef PackagesBundle = { + var name:String; + var version:String; + var packages:Array; +} + +class Updater { + + private static inline var TAG = "Update"; + + public var type(get, null):String; + public var bundle(default, null):PackagesBundle; + + private var url:String; + private var version:String; + + public function new(version:String, url:String) { + this.url = url; + this.version = version; + } + + private function get_type():String { + #if android + return "android"; + #elseif linux + return "debian"; + #else + return null; + #end + } + + private function resolveBundle():Promise { + return bundle != null ? Promise.promise(bundle) : new JsonLoader().GET(url).then(function(bundle) { + this.bundle = bundle; + return this.bundle; + }); + } + + 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.type == type); + }); + } + + public function download():Promise { + return resolveBundle().then(function(bundle:PackagesBundle) { + for (item in bundle.packages) { + if (item.type == type) { + L.i(TAG, 'download: ${item.url}'); + Lib.getURL(new URLRequest(item.url)); + return true; + } + } + return false; + }); + } +} diff --git a/src/client/haxe/ru/m/tankz/Init.hx b/src/client/haxe/ru/m/tankz/Init.hx index 9110267..0d31cf8 100644 --- a/src/client/haxe/ru/m/tankz/Init.hx +++ b/src/client/haxe/ru/m/tankz/Init.hx @@ -1,12 +1,14 @@ package ru.m.tankz; -import haxework.view.theme.ITheme; import flash.Lib; import haxework.animate.FadeAnimate; import haxework.animate.UnFadeAnimate; +import haxework.net.manage.ILoaderManager; +import haxework.net.manage.LoaderManager; import haxework.resources.IResources; import haxework.resources.Resources; import haxework.view.popup.PopupManager; +import haxework.view.theme.ITheme; import lime.ui.Gamepad; import lime.ui.Joystick; import ru.m.connect.IConnection; @@ -42,6 +44,8 @@ class Init { @:provide static var popupManager:PopupManager; @:provide static var connection:IConnection; @:provide static var bus:IControlBus; + @:provide static var loaderManager:ILoaderManager; + @:provide static var updater:Updater; private static function buildConnection():IConnection { var host:String = CompilationOption.get("host"); @@ -77,6 +81,9 @@ class Init { bus = new ControlBus(); bus.connect(new KeyboardDevice(Lib.current.stage)); + loaderManager = new LoaderManager(); + updater = new Updater(Const.VERSION, "https://shmyga.ru/repo/tankz/packages.json"); + for (device in Gamepad.devices) { trace('gamepad', device); } diff --git a/src/client/haxe/ru/m/tankz/view/MenuFrame.hx b/src/client/haxe/ru/m/tankz/view/MenuFrame.hx index 9d134d7..04fe352 100644 --- a/src/client/haxe/ru/m/tankz/view/MenuFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/MenuFrame.hx @@ -19,10 +19,12 @@ import ru.m.tankz.view.popup.LoginPopup; @:provide var gameInit:GameInit; @:provide var switcher:FrameSwitcher; @:provide var network:NetworkManager; + @:provide var appUpdater:Updater; @:view var username:LabelView; @:view("login") var loginButton:ButtonView; @:view("logout") var logoutButton:ButtonView; + @:view("update") var updateButton:ButtonView; public function new() { super(ID); @@ -32,6 +34,12 @@ import ru.m.tankz.view.popup.LoginPopup; super.onShow(data); network.stateSignal.connect(onConnectionState); onConnectionState(network.state); + appUpdater.check().then(function(update:Bool) { + updateButton.visible = update; + if (update) { + updateButton.text = 'Update ${appUpdater.bundle.version}'; + } + }); } override public function onHide():Void { @@ -93,4 +101,8 @@ import ru.m.tankz.view.popup.LoginPopup; private function logout():Void { network.logout(); } + + private function appUpdate():Void { + appUpdater.download(); + } } diff --git a/src/client/haxe/ru/m/tankz/view/MenuFrame.yaml b/src/client/haxe/ru/m/tankz/view/MenuFrame.yaml index 1c3dfc3..8ef3937 100644 --- a/src/client/haxe/ru/m/tankz/view/MenuFrame.yaml +++ b/src/client/haxe/ru/m/tankz/view/MenuFrame.yaml @@ -72,3 +72,7 @@ views: visible: false - $type: haxework.view.SpriteView geometry.width: 50% + - id: update + $type: haxework.view.form.ButtonView + +onPress: ~appUpdate() + visible: false