[client] update proto
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package ru.m.tankz;
|
||||
|
||||
import flash.events.KeyboardEvent;
|
||||
import flash.text.Font;
|
||||
import openfl.Assets;
|
||||
import ru.m.tankz.frame.StartFrame;
|
||||
import flash.ui.Keyboard;
|
||||
import flash.events.KeyboardEvent;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import flash.text.Font;
|
||||
import haxework.gui.frame.IFrameSwitcher;
|
||||
import haxework.gui.Root;
|
||||
import haxework.gui.VGroupView;
|
||||
@@ -11,10 +15,7 @@ import haxework.log.TraceLogger;
|
||||
import haxework.provider.Provider;
|
||||
import haxework.resources.IResources;
|
||||
import haxework.resources.Resources;
|
||||
import openfl.Assets;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.frame.StartFrame;
|
||||
import ru.m.tankz.PacketBuilder;
|
||||
#if flash import haxework.log.JSLogger; #end
|
||||
#if debug import haxework.log.SocketLogger; #end
|
||||
|
||||
@@ -23,9 +24,7 @@ interface ClientLayout {
|
||||
var switcher(default, null):IFrameSwitcher;
|
||||
}
|
||||
|
||||
@:template('ru/m/tankz/Client.yaml', 'ru/m/tankz/Style.yaml')
|
||||
class Client extends VGroupView implements ClientLayout implements ViewBuilder {
|
||||
|
||||
class Client {
|
||||
private static inline var TAG = 'Tankz';
|
||||
|
||||
public static function main() {
|
||||
@@ -41,29 +40,35 @@ class Client extends VGroupView implements ClientLayout implements ViewBuilder {
|
||||
L.d(TAG, 'Debug: ${Const.DEBUG}');
|
||||
L.i(TAG, 'Version: ${Const.VERSION}');
|
||||
L.i(TAG, 'Build: ${Const.BUILD}');
|
||||
var client:Client = new Client();
|
||||
Root.bind(client);
|
||||
var view:ClientView = new ClientView();
|
||||
Root.bind(view);
|
||||
view.launch();
|
||||
}
|
||||
|
||||
public function init() {
|
||||
Provider.setFactory(IResources, Resources);
|
||||
}
|
||||
|
||||
@:template('ru/m/tankz/Client.yaml', 'ru/m/tankz/Style.yaml')
|
||||
class ClientView extends VGroupView implements ClientLayout implements ViewBuilder {
|
||||
|
||||
private static inline var TAG = 'Tankz';
|
||||
|
||||
public function init():Void {
|
||||
var font:Font = Font.enumerateFonts()[0];
|
||||
Provider.get(IResources).text.put('font', 'Bookman Old Style');
|
||||
Provider.get(IResources).text.put('version', 'v${Const.VERSION} b${Const.BUILD}');
|
||||
|
||||
Provider.set(IPacketBuilder, new PacketBuilder());
|
||||
#if flash
|
||||
Provider.set(IConnection, new ru.m.connect.flash.FlashConnection('localhost', 5001));
|
||||
Provider.set(IConnection, new ru.m.connect.flash.FlashConnection<Request, Response>('localhost', 5001, Response));
|
||||
#elseif html5
|
||||
Provider.set(IConnection, new ru.m.connect.js.JsConnection('localhost', 5001));
|
||||
#else
|
||||
Provider.set(IConnection, new ru.m.connect.fake.FakeConnection());
|
||||
#end
|
||||
|
||||
Provider.get(IConnection).handler.addListener(this);
|
||||
|
||||
Provider.set(IFrameSwitcher, switcher);
|
||||
}
|
||||
|
||||
public function launch():Void {
|
||||
content.stage.stageFocusRect = false;
|
||||
|
||||
content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.m.tankz;
|
||||
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.bundle.ConfigBundle;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import haxework.resources.IResources;
|
||||
import haxework.resources.Resources;
|
||||
import ru.m.tankz.bundle.ILevelBundle;
|
||||
import ru.m.tankz.bundle.LevelBundle;
|
||||
import ru.m.tankz.game.ClassicGame;
|
||||
@@ -17,7 +17,8 @@ import ru.m.tankz.storage.UserStorage;
|
||||
class Init {
|
||||
|
||||
public static function init():Void {
|
||||
Provider.setFactory(IConfigBundle, ConfigBundle);
|
||||
Provider.setFactory(IResources, Resources);
|
||||
Provider.setFactory(ILevelBundle, LevelBundle);
|
||||
Provider.setFactory(ILevelBundle, LevelBundle);
|
||||
Provider.setFactory(SaveStorage, SaveStorage);
|
||||
Provider.setFactory(UserStorage, UserStorage);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package ru.m.tankz.frame;
|
||||
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import flash.events.Event;
|
||||
import haxe.ds.Option;
|
||||
import haxe.Timer;
|
||||
@@ -9,13 +8,11 @@ import haxework.gui.LabelView;
|
||||
import haxework.gui.VGroupView;
|
||||
import haxework.gui.ViewBuilder;
|
||||
import haxework.provider.Provider;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.proto.pack.GameUpdateResponse;
|
||||
import ru.m.tankz.render.Render;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
|
||||
|
||||
@@ -25,7 +22,7 @@ interface GameFrameLayout {
|
||||
}
|
||||
|
||||
@:template("ru/m/tankz/frame/GameFrame.yaml", "ru/m/tankz/Style.yaml")
|
||||
class GameFrame extends VGroupView implements ViewBuilder implements IPacketHandler implements GameFrameLayout {
|
||||
class GameFrame extends VGroupView implements ViewBuilder implements GameFrameLayout {
|
||||
|
||||
private static inline var TAG = "GameFrame";
|
||||
|
||||
@@ -126,11 +123,4 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
private function redraw(_):Void {
|
||||
render.draw(game.engine);
|
||||
}
|
||||
|
||||
public function onGameUpdateResponse(packet:GameUpdateResponse):Void {
|
||||
//engine.updateFromChanges(packet.changes);
|
||||
render.draw(game.engine);
|
||||
}
|
||||
|
||||
public function onPacket(packet:Message):Void {}
|
||||
}
|
||||
|
||||
@@ -2,23 +2,25 @@ package ru.m.tankz.network;
|
||||
|
||||
import haxework.provider.Provider;
|
||||
import hxsignal.impl.Signal1;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.proto.pack.LoginRequest;
|
||||
import ru.m.tankz.proto.pack.LoginResponse;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.storage.UserStorage;
|
||||
|
||||
|
||||
class NetworkManager implements IPacketHandler implements IConnectionHandler {
|
||||
typedef ClientConnection = IConnection<Request, Response>;
|
||||
|
||||
class NetworkManager {
|
||||
|
||||
public var state(default, null):String;
|
||||
public var stateSignal:Signal1<String>;
|
||||
public var user(default, null):User;
|
||||
|
||||
private var connection(get, never):IConnection;
|
||||
private var connection(get, never):ClientConnection;
|
||||
private var storage(get, never):UserStorage;
|
||||
|
||||
inline private function get_connection():IConnection {
|
||||
inline private function get_connection():ClientConnection {
|
||||
return Provider.get(IConnection);
|
||||
}
|
||||
|
||||
@@ -29,8 +31,8 @@ class NetworkManager implements IPacketHandler implements IConnectionHandler {
|
||||
public function new() {
|
||||
stateSignal = new Signal1<String>();
|
||||
updateState('offline');
|
||||
connection.handler.addListener(this);
|
||||
connection.packetHandler.addListener(this);
|
||||
connection.handler.connect(onConnectionEvent);
|
||||
connection.packetHandler.connect(onResponse);
|
||||
user = storage.read();
|
||||
if (user == null) {
|
||||
user = {name: 'User', uuid: null};
|
||||
@@ -44,39 +46,29 @@ class NetworkManager implements IPacketHandler implements IConnectionHandler {
|
||||
|
||||
public function login(name:String):Void {
|
||||
user.name = name;
|
||||
if (!connection.connected) {
|
||||
updateState('connect...');
|
||||
connection.connect();
|
||||
} else {
|
||||
onConnected();
|
||||
updateState('connect...');
|
||||
connection.connect().then(function(c:ClientConnection) {
|
||||
updateState('login...');
|
||||
c.send(new Request().setLogin(
|
||||
new LoginRequest()
|
||||
.setUuid(user.uuid)
|
||||
.setName(user.name)
|
||||
));
|
||||
}).catchError(function(_) {});
|
||||
}
|
||||
|
||||
private function onConnectionEvent(event:ConnectionEvent):Void {
|
||||
updateState(Std.string(event));
|
||||
}
|
||||
|
||||
private function onResponse(packet:Response):Void {
|
||||
if (packet.hasLogin()) {
|
||||
user = {
|
||||
uuid: packet.login.user.uuid,
|
||||
name: packet.login.user.name,
|
||||
};
|
||||
storage.write(user);
|
||||
updateState('online');
|
||||
}
|
||||
}
|
||||
|
||||
public function onConnected():Void {
|
||||
updateState('login...');
|
||||
connection.send(
|
||||
new LoginRequest()
|
||||
.setUuid(user.uuid)
|
||||
.setName(user.name)
|
||||
);
|
||||
}
|
||||
|
||||
public function onDisconnected():Void {
|
||||
updateState('offline');
|
||||
}
|
||||
|
||||
public function onError(error:Dynamic):Void {
|
||||
updateState('error');
|
||||
}
|
||||
|
||||
public function onLoginResponse(packet:LoginResponse):Void {
|
||||
user = {
|
||||
uuid: packet.user.uuid,
|
||||
name: packet.user.name,
|
||||
};
|
||||
storage.write(user);
|
||||
updateState('online');
|
||||
}
|
||||
|
||||
public function onPacket(packet:Message):Void {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user