[client] update storages

This commit is contained in:
2019-03-29 11:50:11 +03:00
parent 8fb1d6132d
commit 135ad0d992
15 changed files with 90 additions and 118 deletions

View File

@@ -3,7 +3,6 @@ package ru.m.tankz;
import flash.Lib; import flash.Lib;
import flash.system.Capabilities; import flash.system.Capabilities;
class Const { class Const {
public static var FPS:Int; public static var FPS:Int;
public static var BUILD:String; public static var BUILD:String;

View File

@@ -1,8 +1,8 @@
package ru.m.tankz; package ru.m.tankz;
import haxework.view.popup.PopupManager; import ru.m.tankz.storage.MultiplayerStorage;
import ru.m.tankz.storage.SettingsStorage; import ru.m.tankz.storage.SettingsStorage;
import haxework.provider.Provider; import haxework.view.popup.PopupManager;
import haxework.resources.IResources; import haxework.resources.IResources;
import haxework.resources.Resources; import haxework.resources.Resources;
import ru.m.connect.IConnection; 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.Request;
import ru.m.tankz.proto.pack.Response; import ru.m.tankz.proto.pack.Response;
import ru.m.tankz.sound.SoundManager; import ru.m.tankz.sound.SoundManager;
import ru.m.tankz.storage.UserStorage;
#if flash #if flash
import flash.Lib; import flash.Lib;
#elseif html5 #elseif html5
@@ -25,6 +24,17 @@ import js.Browser;
class Init { 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<Request, Response>;
private static function getHost():String { private static function getHost():String {
#if flash #if flash
var url = Lib.current.loaderInfo.url; var url = Lib.current.loaderInfo.url;
@@ -42,24 +52,24 @@ class Init {
} }
public static function init():Void { public static function init():Void {
Provider.setFactory(IResources, Resources); resources = new Resources();
Provider.setFactory(ILevelBundle, LevelBundle); levelBundle = new LevelBundle();
Provider.setFactory(IConfigBundle, ConfigBundle); configBundle = new ConfigBundle();
Provider.setFactory(UserStorage, UserStorage); settingsStorage = new SettingsStorage();
Provider.setFactory(SettingsStorage, SettingsStorage); multiplayerStorage = new MultiplayerStorage();
Provider.setFactory(SoundManager, SoundManager); soundManager = new SoundManager();
Provider.setFactory(NetworkManager, NetworkManager); controlFactory = new ClientControlFactory();
Provider.setFactory(IControlFactory, ClientControlFactory); popupManager = new PopupManager();
Provider.setFactory(PopupManager, PopupManager);
var host:String = getHost(); var host:String = getHost();
L.d('Init', 'host: ${host}'); L.d('Init', 'host: ${host}');
#if flash #if flash
Provider.set(IConnection, new ru.m.connect.flash.FlashConnection<Request, Response>(host, 5000, Response)); connection = new ru.m.connect.flash.FlashConnection<Request, Response>(host, 5000, Response);
#elseif html5 #elseif html5
Provider.set(IConnection, new ru.m.connect.js.JsConnection<Request, Response>(host, 5000, Response)); connection = new ru.m.connect.js.JsConnection<Request, Response>(host, 5000, Response);
#else #else
Provider.set(IConnection, new ru.m.connect.fake.FakeConnection<Request, Response>(Response)); connection = new ru.m.connect.fake.FakeConnection<Request, Response>(Response);
#end #end
networkManager = new NetworkManager();
} }
} }

View File

@@ -7,11 +7,12 @@ import ru.m.tankz.Type;
import yaml.Parser; import yaml.Parser;
import yaml.Yaml; import yaml.Yaml;
class ConfigBundle implements IConfigBundle { class ConfigBundle implements IConfigBundle {
private var _cache:Map<GameType, Config> = new Map(); private var _cache:Map<GameType, Config> = new Map();
public function new() {}
public function get(type:GameType):Config { public function get(type:GameType):Config {
if (!_cache.exists(type)) { if (!_cache.exists(type)) {
var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects()); var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects());

View File

@@ -5,9 +5,10 @@ import ru.m.tankz.config.Config;
import ru.m.tankz.Type; import ru.m.tankz.Type;
import ru.m.tankz.util.LevelUtil; import ru.m.tankz.util.LevelUtil;
class LevelBundle implements ILevelBundle { class LevelBundle implements ILevelBundle {
public function new() {}
public function get(type:GameType, config:Config, level:Int):LevelConfig { public function get(type:GameType, config:Config, level:Int):LevelConfig {
var data:String = Assets.getText('resources/${type}/levels/level${LevelUtil.formatLevel(level)}.txt'); var data:String = Assets.getText('resources/${type}/levels/level${LevelUtil.formatLevel(level)}.txt');
return LevelUtil.loads(config, data); return LevelUtil.loads(config, data);

View File

@@ -1,7 +1,5 @@
package ru.m.tankz.control; package ru.m.tankz.control;
import haxe.Serializer;
import haxe.Unserializer;
import ru.m.tankz.control.Control.TankAction; import ru.m.tankz.control.Control.TankAction;
typedef ActionItem = { typedef ActionItem = {
@@ -26,16 +24,4 @@ class ActionConfig {
} }
return result; 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();
}
} }

View File

@@ -7,6 +7,8 @@ import ru.m.tankz.Type;
class ClientControlFactory implements IControlFactory { class ClientControlFactory implements IControlFactory {
public function new() {}
public function build(id:PlayerId, controller:Controller):Control { public function build(id:PlayerId, controller:Controller):Control {
return switch controller { return switch controller {
case HUMAN(index): new HumanControl(id, index); case HUMAN(index): new HumanControl(id, index);

View File

@@ -10,10 +10,9 @@ import ru.m.tankz.control.Control;
import ru.m.tankz.storage.SettingsStorage; import ru.m.tankz.storage.SettingsStorage;
import ru.m.tankz.Type; import ru.m.tankz.Type;
class HumanControl extends Control { class HumanControl extends Control {
@:provide var settings: SettingsStorage; @:provide var storage:SettingsStorage;
private var keyBinding:KeyBinding; private var keyBinding:KeyBinding;
private var moveQueue:Array<Int>; private var moveQueue:Array<Int>;
@@ -21,7 +20,7 @@ class HumanControl extends Control {
public function new(playerId:PlayerId, controlIndex:Int) { public function new(playerId:PlayerId, controlIndex:Int) {
super(playerId); super(playerId);
this.keyBinding = settings.read(controlIndex).asKeyBinding(); this.keyBinding = storage.getActionConfig(controlIndex).asKeyBinding();
moveQueue = new Array<Int>(); moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

View File

@@ -1,5 +1,7 @@
package ru.m.tankz.network; package ru.m.tankz.network;
import ru.m.tankz.storage.MultiplayerStorage;
import haxework.storage.IStorage;
import haxework.signal.Signal; import haxework.signal.Signal;
import ru.m.tankz.proto.pack.GameRequest; import ru.m.tankz.proto.pack.GameRequest;
import ru.m.tankz.proto.core.GameProto; 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.LoginRequest;
import ru.m.tankz.proto.pack.Request; import ru.m.tankz.proto.pack.Request;
import ru.m.tankz.proto.pack.Response; import ru.m.tankz.proto.pack.Response;
import ru.m.tankz.storage.UserStorage;
typedef ClientConnection = IConnection<Request, Response>; typedef ClientConnection = IConnection<Request, Response>;
@@ -33,7 +33,7 @@ class NetworkManager {
public var game(default, set):NetworkGame; public var game(default, set):NetworkGame;
@:provide private var connection:ClientConnection; @:provide private var connection:ClientConnection;
@:provide private var storage:UserStorage; @:provide private var storage:MultiplayerStorage;
public function new() { public function new() {
stateSignal = new Signal<String>(); stateSignal = new Signal<String>();
@@ -43,7 +43,7 @@ class NetworkManager {
updateState('offline'); updateState('offline');
connection.handler.connect(onConnectionEvent); connection.handler.connect(onConnectionEvent);
connection.receiveHandler.connect(onResponse); connection.receiveHandler.connect(onResponse);
user = storage.read(); user = storage.user;
if (user == null) { if (user == null) {
user = {name: 'User', uuid: null}; user = {name: 'User', uuid: null};
} }
@@ -127,7 +127,7 @@ class NetworkManager {
uuid: packet.login.user.uuid, uuid: packet.login.user.uuid,
name: packet.login.user.name, name: packet.login.user.name,
}; };
storage.write(user); storage.user = user;
updateState('online'); updateState('online');
} else if (packet.hasLogout()) { } else if (packet.hasLogout()) {
user = null; user = null;

View File

@@ -118,10 +118,9 @@ class BrickBreakingItem extends RenderItem<Brick, Shape> {
if (value.destroyed) return; if (value.destroyed) return;
if (value.config.index > 0) { if (value.config.index > 0) {
g.beginBitmapFill(image); 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) { for (c in value.cells) {
if (c.destroyed) { if (!c.destroyed) {
g.beginFill(0x000000);
g.drawRect(c.rect.x - value.rect.x, c.rect.y - value.rect.y, c.rect.width, c.rect.height); g.drawRect(c.rect.x - value.rect.x, c.rect.y - value.rect.y, c.rect.width, c.rect.height);
} }
} }

View File

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

View File

@@ -1,46 +1,31 @@
package ru.m.tankz.storage; package ru.m.tankz.storage;
import flash.net.SharedObject;
import flash.ui.Keyboard; import flash.ui.Keyboard;
import haxework.storage.SharedObjectStorage;
import haxework.utils.ObjectUtil;
import ru.m.geom.Direction; import ru.m.geom.Direction;
import ru.m.tankz.control.ActionConfig; import ru.m.tankz.control.ActionConfig;
import ru.m.tankz.control.Control.TankAction; import ru.m.tankz.control.Control.TankAction;
class SettingsStorage extends SharedObjectStorage {
class SettingsStorage {
private static var TAG(default, never):String = 'SettingsStorage';
private var so:SharedObject;
public function new() { public function new() {
so = SharedObject.getLocal('settings'); super("settings");
} }
public function read(index: Int):Null<ActionConfig> { public function getActionConfig(index:Int):ActionConfig {
var data:String = Reflect.getProperty(so.data, Std.string(index)); return exists('action:$index') ? read('action:$index') : getDefaultActionConfig(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 write(index: Int, data: ActionConfig):Void { public function setActionConffig(index:Int, value:ActionConfig) {
L.d(TAG, 'write: ${data}'); write('action:$index', value);
so.setProperty(Std.string(index), data.dumps());
so.flush();
} }
public static function getDefault(index: Int): ActionConfig { public static function getDefaultActionConfig(index:Int):ActionConfig {
return (defaults.exists(index) ? defaults.get(index) : empty).clone(); return ObjectUtil.clone(defaults.exists(index) ? defaults.get(index) : empty);
} }
private static var defaults: Map<Int, ActionConfig> = [ private static var defaults:Map<Int, ActionConfig> = [
0 => new ActionConfig([ 0 => new ActionConfig([
{action:TankAction.MOVE(Direction.TOP), key:Keyboard.W}, {action:TankAction.MOVE(Direction.TOP), key:Keyboard.W},
{action:TankAction.MOVE(Direction.LEFT), key:Keyboard.A}, {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.TOP), key:-1},
{action:TankAction.MOVE(Direction.LEFT), key:-1}, {action:TankAction.MOVE(Direction.LEFT), key:-1},
{action:TankAction.MOVE(Direction.BOTTOM), key:-1}, {action:TankAction.MOVE(Direction.BOTTOM), key:-1},

View File

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

View File

@@ -1,10 +1,9 @@
package ru.m.tankz.view; package ru.m.tankz.view;
import flash.events.KeyboardEvent; import flash.events.KeyboardEvent;
import flash.text.Font;
import flash.ui.Keyboard; import flash.ui.Keyboard;
import haxework.view.frame.FrameSwitcher;
import haxework.resources.IResources; import haxework.resources.IResources;
import haxework.view.frame.FrameSwitcher;
@:template class ClientView extends FrameSwitcher { @:template class ClientView extends FrameSwitcher {
@@ -12,7 +11,6 @@ import haxework.resources.IResources;
@:provide var switcher:FrameSwitcher; @:provide var switcher:FrameSwitcher;
public function init():Void { public function init():Void {
var font:Font = Font.enumerateFonts()[0];
resources.text.put('version', '${Const.VERSION}'); resources.text.put('version', '${Const.VERSION}');
switcher = this; switcher = this;
} }

View File

@@ -5,7 +5,6 @@ import haxework.view.DataView;
import haxework.view.LabelView; import haxework.view.LabelView;
import haxework.view.VGroupView; import haxework.view.VGroupView;
import promhx.Promise; import promhx.Promise;
import ru.m.tankz.control.ActionConfig.ActionItem;
import ru.m.tankz.control.ActionConfig; import ru.m.tankz.control.ActionConfig;
import ru.m.tankz.storage.SettingsStorage; import ru.m.tankz.storage.SettingsStorage;
@@ -19,12 +18,12 @@ import ru.m.tankz.storage.SettingsStorage;
@:view var clear:ButtonView; @:view var clear:ButtonView;
@:view var reset:ButtonView; @:view var reset:ButtonView;
@:provide var storage: SettingsStorage; @:provide var storage:SettingsStorage;
private function set_controlIndex(value: Int): Int { private function set_controlIndex(value: Int): Int {
this.controlIndex = value; this.controlIndex = value;
label.text = 'Player ${controlIndex+1}'; label.text = 'Player ${controlIndex+1}';
list.data = storage.read(controlIndex).data; list.data = storage.getActionConfig(controlIndex).data;
return this.controlIndex; return this.controlIndex;
} }
@@ -67,12 +66,12 @@ import ru.m.tankz.storage.SettingsStorage;
} }
private function _reset():Void { private function _reset():Void {
list.data = SettingsStorage.getDefault(controlIndex).data; list.data = DefaultSettings.get(controlIndex).data;
list.toUpdate(); list.toUpdate();
_save(); _save();
} }
private function _save():Void { private function _save():Void {
storage.write(controlIndex, new ActionConfig(list.data)); storage.setActionConffig(controlIndex, new ActionConfig(list.data));
} }
} }

View File

@@ -2,7 +2,6 @@ package ru.m.tankz.game;
import haxe.ds.Option; import haxe.ds.Option;
import haxe.Timer; import haxe.Timer;
import haxework.provider.Provider;
import haxework.signal.Signal; import haxework.signal.Signal;
import ru.m.geom.Point; import ru.m.geom.Point;
import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.bundle.IConfigBundle;
@@ -64,6 +63,7 @@ class Game extends GameDispatcher {
@:provide var configBundle:IConfigBundle; @:provide var configBundle:IConfigBundle;
@:provide var levelBundle:ILevelBundle; @:provide var levelBundle:ILevelBundle;
@:provide var controlFactory:IControlFactory;
public function new(type:GameType) { public function new(type:GameType) {
super(); super();
@@ -113,7 +113,6 @@ class Game extends GameDispatcher {
points = level.points != null ? level.points : config.points; points = level.points != null ? level.points : config.points;
engine.map.setData(level.data); engine.map.setData(level.data);
teams = new Map<TeamId, Team>(); teams = new Map<TeamId, Team>();
var controlFactory:IControlFactory = Provider.build(IControlFactory);
for (teamConfig in state.preset.teams) { for (teamConfig in state.preset.teams) {
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id); var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]); var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);