[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.system.Capabilities;
class Const {
public static var FPS:Int;
public static var BUILD:String;

View File

@@ -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<Request, Response>;
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<Request, Response>(host, 5000, Response));
connection = new ru.m.connect.flash.FlashConnection<Request, Response>(host, 5000, Response);
#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
Provider.set(IConnection, new ru.m.connect.fake.FakeConnection<Request, Response>(Response));
connection = new ru.m.connect.fake.FakeConnection<Request, Response>(Response);
#end
networkManager = new NetworkManager();
}
}

View File

@@ -7,11 +7,12 @@ import ru.m.tankz.Type;
import yaml.Parser;
import yaml.Yaml;
class ConfigBundle implements IConfigBundle {
private var _cache:Map<GameType, Config> = 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());

View File

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

View File

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

View File

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

View File

@@ -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<Int>;
@@ -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<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

View File

@@ -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<Request, Response>;
@@ -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<String>();
@@ -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;

View File

@@ -118,10 +118,9 @@ class BrickBreakingItem extends RenderItem<Brick, Shape> {
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);
}
}

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;
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<ActionConfig> {
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<Int, ActionConfig> = [
private static var defaults:Map<Int, ActionConfig> = [
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},

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

View File

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

View File

@@ -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<TeamId, Team>();
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]);