From 135ad0d9923602618a2de72c0b8907c37390aff7 Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 29 Mar 2019 11:50:11 +0300 Subject: [PATCH] [client] update storages --- src/client/haxe/ru/m/tankz/Const.hx | 23 +++++------ src/client/haxe/ru/m/tankz/Init.hx | 40 ++++++++++++------- .../haxe/ru/m/tankz/bundle/ConfigBundle.hx | 3 +- .../haxe/ru/m/tankz/bundle/LevelBundle.hx | 3 +- .../haxe/ru/m/tankz/control/ActionConfig.hx | 14 ------- .../m/tankz/control/ClientControlFactory.hx | 2 + .../haxe/ru/m/tankz/control/HumanControl.hx | 5 +-- .../haxe/ru/m/tankz/network/NetworkManager.hx | 10 ++--- .../haxe/ru/m/tankz/render/RenderItem.hx | 5 +-- .../ru/m/tankz/storage/MultiplayerStorage.hx | 21 ++++++++++ .../ru/m/tankz/storage/SettingsStorage.hx | 39 ++++++------------ .../haxe/ru/m/tankz/storage/UserStorage.hx | 27 ------------- src/client/haxe/ru/m/tankz/view/ClientView.hx | 4 +- .../m/tankz/view/settings/SettingsEditor.hx | 9 ++--- src/common/haxe/ru/m/tankz/game/Game.hx | 3 +- 15 files changed, 90 insertions(+), 118 deletions(-) create mode 100644 src/client/haxe/ru/m/tankz/storage/MultiplayerStorage.hx delete mode 100644 src/client/haxe/ru/m/tankz/storage/UserStorage.hx diff --git a/src/client/haxe/ru/m/tankz/Const.hx b/src/client/haxe/ru/m/tankz/Const.hx index 878726e..9c5ec77 100755 --- a/src/client/haxe/ru/m/tankz/Const.hx +++ b/src/client/haxe/ru/m/tankz/Const.hx @@ -3,17 +3,16 @@ package ru.m.tankz; import flash.Lib; import flash.system.Capabilities; - class Const { - public static var FPS:Int; - public static var BUILD:String; - public static var VERSION:String; - public static var DEBUG:Bool; + public static var FPS:Int; + public static var BUILD:String; + public static var VERSION:String; + public static var DEBUG:Bool; - public static function init():Void { - FPS = Std.parseInt(Lib.current.stage.application.meta.get("fps")); - BUILD = CompilationOption.get("build"); - VERSION = Lib.current.stage.application.meta.get("version"); - DEBUG = Capabilities.isDebugger; - } -} \ No newline at end of file + public static function init():Void { + FPS = Std.parseInt(Lib.current.stage.application.meta.get("fps")); + BUILD = CompilationOption.get("build"); + VERSION = Lib.current.stage.application.meta.get("version"); + DEBUG = Capabilities.isDebugger; + } +} diff --git a/src/client/haxe/ru/m/tankz/Init.hx b/src/client/haxe/ru/m/tankz/Init.hx index 83fb9bf..932cc6c 100644 --- a/src/client/haxe/ru/m/tankz/Init.hx +++ b/src/client/haxe/ru/m/tankz/Init.hx @@ -1,8 +1,8 @@ package ru.m.tankz; -import haxework.view.popup.PopupManager; +import ru.m.tankz.storage.MultiplayerStorage; import ru.m.tankz.storage.SettingsStorage; -import haxework.provider.Provider; +import haxework.view.popup.PopupManager; import haxework.resources.IResources; import haxework.resources.Resources; import ru.m.connect.IConnection; @@ -16,7 +16,6 @@ import ru.m.tankz.network.NetworkManager; import ru.m.tankz.proto.pack.Request; import ru.m.tankz.proto.pack.Response; import ru.m.tankz.sound.SoundManager; -import ru.m.tankz.storage.UserStorage; #if flash import flash.Lib; #elseif html5 @@ -25,6 +24,17 @@ import js.Browser; class Init { + @:provide static var resources:IResources; + @:provide static var levelBundle:ILevelBundle; + @:provide static var configBundle:IConfigBundle; + @:provide static var settingsStorage:SettingsStorage; + @:provide static var multiplayerStorage:MultiplayerStorage; + @:provide static var soundManager:SoundManager; + @:provide static var networkManager:NetworkManager; + @:provide static var controlFactory:IControlFactory; + @:provide static var popupManager:PopupManager; + @:provide static var connection:IConnection; + private static function getHost():String { #if flash var url = Lib.current.loaderInfo.url; @@ -42,24 +52,24 @@ class Init { } public static function init():Void { - Provider.setFactory(IResources, Resources); - Provider.setFactory(ILevelBundle, LevelBundle); - Provider.setFactory(IConfigBundle, ConfigBundle); - Provider.setFactory(UserStorage, UserStorage); - Provider.setFactory(SettingsStorage, SettingsStorage); - Provider.setFactory(SoundManager, SoundManager); - Provider.setFactory(NetworkManager, NetworkManager); - Provider.setFactory(IControlFactory, ClientControlFactory); - Provider.setFactory(PopupManager, PopupManager); + resources = new Resources(); + levelBundle = new LevelBundle(); + configBundle = new ConfigBundle(); + settingsStorage = new SettingsStorage(); + multiplayerStorage = new MultiplayerStorage(); + soundManager = new SoundManager(); + controlFactory = new ClientControlFactory(); + popupManager = new PopupManager(); var host:String = getHost(); L.d('Init', 'host: ${host}'); #if flash - Provider.set(IConnection, new ru.m.connect.flash.FlashConnection(host, 5000, Response)); + connection = new ru.m.connect.flash.FlashConnection(host, 5000, Response); #elseif html5 - Provider.set(IConnection, new ru.m.connect.js.JsConnection(host, 5000, Response)); + connection = new ru.m.connect.js.JsConnection(host, 5000, Response); #else - Provider.set(IConnection, new ru.m.connect.fake.FakeConnection(Response)); + connection = new ru.m.connect.fake.FakeConnection(Response); #end + networkManager = new NetworkManager(); } } diff --git a/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx b/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx index 34d84fc..865510b 100644 --- a/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx +++ b/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx @@ -7,11 +7,12 @@ import ru.m.tankz.Type; import yaml.Parser; import yaml.Yaml; - class ConfigBundle implements IConfigBundle { private var _cache:Map = new Map(); + public function new() {} + public function get(type:GameType):Config { if (!_cache.exists(type)) { var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects()); diff --git a/src/client/haxe/ru/m/tankz/bundle/LevelBundle.hx b/src/client/haxe/ru/m/tankz/bundle/LevelBundle.hx index abe8a44..0cc7103 100644 --- a/src/client/haxe/ru/m/tankz/bundle/LevelBundle.hx +++ b/src/client/haxe/ru/m/tankz/bundle/LevelBundle.hx @@ -5,9 +5,10 @@ import ru.m.tankz.config.Config; import ru.m.tankz.Type; import ru.m.tankz.util.LevelUtil; - class LevelBundle implements ILevelBundle { + public function new() {} + public function get(type:GameType, config:Config, level:Int):LevelConfig { var data:String = Assets.getText('resources/${type}/levels/level${LevelUtil.formatLevel(level)}.txt'); return LevelUtil.loads(config, data); diff --git a/src/client/haxe/ru/m/tankz/control/ActionConfig.hx b/src/client/haxe/ru/m/tankz/control/ActionConfig.hx index 6796cf0..e0d6ccf 100644 --- a/src/client/haxe/ru/m/tankz/control/ActionConfig.hx +++ b/src/client/haxe/ru/m/tankz/control/ActionConfig.hx @@ -1,7 +1,5 @@ package ru.m.tankz.control; -import haxe.Serializer; -import haxe.Unserializer; import ru.m.tankz.control.Control.TankAction; typedef ActionItem = { @@ -26,16 +24,4 @@ class ActionConfig { } return result; } - - public function clone():ActionConfig { - return loads(dumps()); - } - - public function dumps():String { - return Serializer.run(this); - } - - public static function loads(value:String):ActionConfig { - return new Unserializer(value).unserialize(); - } } diff --git a/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx b/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx index cb946d2..93ac0dc 100644 --- a/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx +++ b/src/client/haxe/ru/m/tankz/control/ClientControlFactory.hx @@ -7,6 +7,8 @@ import ru.m.tankz.Type; class ClientControlFactory implements IControlFactory { + public function new() {} + public function build(id:PlayerId, controller:Controller):Control { return switch controller { case HUMAN(index): new HumanControl(id, index); diff --git a/src/client/haxe/ru/m/tankz/control/HumanControl.hx b/src/client/haxe/ru/m/tankz/control/HumanControl.hx index 4a31ecf..1ce540b 100644 --- a/src/client/haxe/ru/m/tankz/control/HumanControl.hx +++ b/src/client/haxe/ru/m/tankz/control/HumanControl.hx @@ -10,10 +10,9 @@ import ru.m.tankz.control.Control; import ru.m.tankz.storage.SettingsStorage; import ru.m.tankz.Type; - class HumanControl extends Control { - @:provide var settings: SettingsStorage; + @:provide var storage:SettingsStorage; private var keyBinding:KeyBinding; private var moveQueue:Array; @@ -21,7 +20,7 @@ class HumanControl extends Control { public function new(playerId:PlayerId, controlIndex:Int) { super(playerId); - this.keyBinding = settings.read(controlIndex).asKeyBinding(); + this.keyBinding = storage.getActionConfig(controlIndex).asKeyBinding(); moveQueue = new Array(); Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); diff --git a/src/client/haxe/ru/m/tankz/network/NetworkManager.hx b/src/client/haxe/ru/m/tankz/network/NetworkManager.hx index 85ff38c..612ede0 100644 --- a/src/client/haxe/ru/m/tankz/network/NetworkManager.hx +++ b/src/client/haxe/ru/m/tankz/network/NetworkManager.hx @@ -1,5 +1,7 @@ package ru.m.tankz.network; +import ru.m.tankz.storage.MultiplayerStorage; +import haxework.storage.IStorage; import haxework.signal.Signal; import ru.m.tankz.proto.pack.GameRequest; import ru.m.tankz.proto.core.GameProto; @@ -17,8 +19,6 @@ import ru.m.tankz.proto.pack.ListGameRequest; import ru.m.tankz.proto.pack.LoginRequest; import ru.m.tankz.proto.pack.Request; import ru.m.tankz.proto.pack.Response; -import ru.m.tankz.storage.UserStorage; - typedef ClientConnection = IConnection; @@ -33,7 +33,7 @@ class NetworkManager { public var game(default, set):NetworkGame; @:provide private var connection:ClientConnection; - @:provide private var storage:UserStorage; + @:provide private var storage:MultiplayerStorage; public function new() { stateSignal = new Signal(); @@ -43,7 +43,7 @@ class NetworkManager { updateState('offline'); connection.handler.connect(onConnectionEvent); connection.receiveHandler.connect(onResponse); - user = storage.read(); + user = storage.user; if (user == null) { user = {name: 'User', uuid: null}; } @@ -127,7 +127,7 @@ class NetworkManager { uuid: packet.login.user.uuid, name: packet.login.user.name, }; - storage.write(user); + storage.user = user; updateState('online'); } else if (packet.hasLogout()) { user = null; diff --git a/src/client/haxe/ru/m/tankz/render/RenderItem.hx b/src/client/haxe/ru/m/tankz/render/RenderItem.hx index 02358d5..b628935 100644 --- a/src/client/haxe/ru/m/tankz/render/RenderItem.hx +++ b/src/client/haxe/ru/m/tankz/render/RenderItem.hx @@ -118,10 +118,9 @@ class BrickBreakingItem extends RenderItem { if (value.destroyed) return; if (value.config.index > 0) { g.beginBitmapFill(image); - g.drawRect(0, 0, value.rect.width, value.rect.height); + //g.drawRect(0, 0, value.rect.width, value.rect.height); for (c in value.cells) { - if (c.destroyed) { - g.beginFill(0x000000); + if (!c.destroyed) { g.drawRect(c.rect.x - value.rect.x, c.rect.y - value.rect.y, c.rect.width, c.rect.height); } } diff --git a/src/client/haxe/ru/m/tankz/storage/MultiplayerStorage.hx b/src/client/haxe/ru/m/tankz/storage/MultiplayerStorage.hx new file mode 100644 index 0000000..5fb7860 --- /dev/null +++ b/src/client/haxe/ru/m/tankz/storage/MultiplayerStorage.hx @@ -0,0 +1,21 @@ +package ru.m.tankz.storage; + +import haxework.storage.SharedObjectStorage; + +class MultiplayerStorage extends SharedObjectStorage { + + public var user(get, set):User; + + public function new() { + super("multiplayer"); + } + + private inline function get_user():User { + return read("user"); + } + + private inline function set_user(value:User):User { + write("user", value); + return value; + } +} diff --git a/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx b/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx index 20b6868..29064b8 100644 --- a/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx +++ b/src/client/haxe/ru/m/tankz/storage/SettingsStorage.hx @@ -1,46 +1,31 @@ package ru.m.tankz.storage; -import flash.net.SharedObject; import flash.ui.Keyboard; +import haxework.storage.SharedObjectStorage; +import haxework.utils.ObjectUtil; import ru.m.geom.Direction; import ru.m.tankz.control.ActionConfig; import ru.m.tankz.control.Control.TankAction; - -class SettingsStorage { - - private static var TAG(default, never):String = 'SettingsStorage'; - - private var so:SharedObject; +class SettingsStorage extends SharedObjectStorage { public function new() { - so = SharedObject.getLocal('settings'); + super("settings"); } - public function read(index: Int):Null { - var data:String = Reflect.getProperty(so.data, Std.string(index)); - L.d(TAG, 'read: ${data}'); - if (data != null) { - try { - return ActionConfig.loads(data); - } catch (error:Dynamic) { - L.w(TAG, "read", error); - } - } - return getDefault(index); + public function getActionConfig(index:Int):ActionConfig { + return exists('action:$index') ? read('action:$index') : getDefaultActionConfig(index); } - public function write(index: Int, data: ActionConfig):Void { - L.d(TAG, 'write: ${data}'); - so.setProperty(Std.string(index), data.dumps()); - so.flush(); + public function setActionConffig(index:Int, value:ActionConfig) { + write('action:$index', value); } - public static function getDefault(index: Int): ActionConfig { - return (defaults.exists(index) ? defaults.get(index) : empty).clone(); + public static function getDefaultActionConfig(index:Int):ActionConfig { + return ObjectUtil.clone(defaults.exists(index) ? defaults.get(index) : empty); } - private static var defaults: Map = [ + private static var defaults:Map = [ 0 => new ActionConfig([ {action:TankAction.MOVE(Direction.TOP), key:Keyboard.W}, {action:TankAction.MOVE(Direction.LEFT), key:Keyboard.A}, @@ -57,7 +42,7 @@ class SettingsStorage { ]), ]; - private static var empty: ActionConfig = new ActionConfig([ + private static var empty:ActionConfig = new ActionConfig([ {action:TankAction.MOVE(Direction.TOP), key:-1}, {action:TankAction.MOVE(Direction.LEFT), key:-1}, {action:TankAction.MOVE(Direction.BOTTOM), key:-1}, diff --git a/src/client/haxe/ru/m/tankz/storage/UserStorage.hx b/src/client/haxe/ru/m/tankz/storage/UserStorage.hx deleted file mode 100644 index d708a14..0000000 --- a/src/client/haxe/ru/m/tankz/storage/UserStorage.hx +++ /dev/null @@ -1,27 +0,0 @@ -package ru.m.tankz.storage; - -import flash.net.SharedObject; - - -class UserStorage { - - private static var TAG(default, never):String = 'UserStorage'; - - private var so:SharedObject; - - public function new() { - so = SharedObject.getLocal('user'); - } - - public function read():Null { - var user:User = Reflect.getProperty(so.data, 'user'); - L.d(TAG, 'read: ${user}'); - return user; - } - - public function write(user:User):Void { - L.d(TAG, 'write: ${user}'); - so.setProperty('user', user); - so.flush(); - } -} diff --git a/src/client/haxe/ru/m/tankz/view/ClientView.hx b/src/client/haxe/ru/m/tankz/view/ClientView.hx index f89bcc7..7bf4caf 100644 --- a/src/client/haxe/ru/m/tankz/view/ClientView.hx +++ b/src/client/haxe/ru/m/tankz/view/ClientView.hx @@ -1,10 +1,9 @@ package ru.m.tankz.view; import flash.events.KeyboardEvent; -import flash.text.Font; import flash.ui.Keyboard; -import haxework.view.frame.FrameSwitcher; import haxework.resources.IResources; +import haxework.view.frame.FrameSwitcher; @:template class ClientView extends FrameSwitcher { @@ -12,7 +11,6 @@ import haxework.resources.IResources; @:provide var switcher:FrameSwitcher; public function init():Void { - var font:Font = Font.enumerateFonts()[0]; resources.text.put('version', '${Const.VERSION}'); switcher = this; } diff --git a/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx b/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx index 6f5ed3d..0c820b0 100644 --- a/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx +++ b/src/client/haxe/ru/m/tankz/view/settings/SettingsEditor.hx @@ -5,7 +5,6 @@ import haxework.view.DataView; import haxework.view.LabelView; import haxework.view.VGroupView; import promhx.Promise; -import ru.m.tankz.control.ActionConfig.ActionItem; import ru.m.tankz.control.ActionConfig; import ru.m.tankz.storage.SettingsStorage; @@ -19,12 +18,12 @@ import ru.m.tankz.storage.SettingsStorage; @:view var clear:ButtonView; @:view var reset:ButtonView; - @:provide var storage: SettingsStorage; + @:provide var storage:SettingsStorage; private function set_controlIndex(value: Int): Int { this.controlIndex = value; label.text = 'Player ${controlIndex+1}'; - list.data = storage.read(controlIndex).data; + list.data = storage.getActionConfig(controlIndex).data; return this.controlIndex; } @@ -67,12 +66,12 @@ import ru.m.tankz.storage.SettingsStorage; } private function _reset():Void { - list.data = SettingsStorage.getDefault(controlIndex).data; + list.data = DefaultSettings.get(controlIndex).data; list.toUpdate(); _save(); } private function _save():Void { - storage.write(controlIndex, new ActionConfig(list.data)); + storage.setActionConffig(controlIndex, new ActionConfig(list.data)); } } diff --git a/src/common/haxe/ru/m/tankz/game/Game.hx b/src/common/haxe/ru/m/tankz/game/Game.hx index 583896f..bfa669c 100644 --- a/src/common/haxe/ru/m/tankz/game/Game.hx +++ b/src/common/haxe/ru/m/tankz/game/Game.hx @@ -2,7 +2,6 @@ package ru.m.tankz.game; import haxe.ds.Option; import haxe.Timer; -import haxework.provider.Provider; import haxework.signal.Signal; import ru.m.geom.Point; import ru.m.tankz.bundle.IConfigBundle; @@ -64,6 +63,7 @@ class Game extends GameDispatcher { @:provide var configBundle:IConfigBundle; @:provide var levelBundle:ILevelBundle; + @:provide var controlFactory:IControlFactory; public function new(type:GameType) { super(); @@ -113,7 +113,6 @@ class Game extends GameDispatcher { points = level.points != null ? level.points : config.points; engine.map.setData(level.data); teams = new Map(); - var controlFactory:IControlFactory = Provider.build(IControlFactory); for (teamConfig in state.preset.teams) { var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id); var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);