From 6623a031d0265bdbb982991909a35b5db24c1579 Mon Sep 17 00:00:00 2001 From: shmyga Date: Sun, 6 Jul 2014 17:13:10 +0400 Subject: [PATCH] - --- res/layout/main.json | 82 +++++++++--------- .../haxe/ru/m/armageddon/client/Client.hx | 83 +++---------------- .../m/armageddon/client/frames/AuthFrame.hx | 58 +++++++++++++ .../armageddon/core/connect/BaseConnection.hx | 18 ++-- .../m/armageddon/core/connect/IConnection.hx | 11 ++- .../core/connect/flash/FlashConnection.hx | 27 ++++-- 6 files changed, 151 insertions(+), 128 deletions(-) create mode 100755 src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx diff --git a/res/layout/main.json b/res/layout/main.json index 49747cb..e82c1f9 100755 --- a/res/layout/main.json +++ b/res/layout/main.json @@ -1,59 +1,59 @@ { - "type":"haxework.gui.VGroupView", - "paddings":10, - "layoutMargin":10, + "type":"haxework.frame.FrameSwitcher", "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x000000"}, "views":[ { - "id":"panel", - "type":"haxework.gui.HGroupView", - "layoutHAlign":"LEFT", - "pWidth":100, - "height":30, - "paddings":3, - "layoutMargin":3, - "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x505050"}, + "id":"auth", "type":"ru.m.armageddon.client.frames.AuthFrame", + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"}, + "pWidth":100, "pHeight":100, "layoutMargin":3, "views":[ { - "id":"login", + "id":"login", "type":"haxework.gui.HGroupView", + "contentSize":true, + "views":[ + { + "type":"haxework.gui.LabelView", + "width":150, "height":25, "text":"Login" + }, + { + "id":"input", "type":"haxework.gui.InputView", + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffffff"}, + "width":200, "height":25, "text":"" + } + ] + }, + { + "id":"password", "type":"haxework.gui.HGroupView", + "contentSize":true, + "views":[ + { + "type":"haxework.gui.LabelView", + "width":150, "height":25, "text":"Password" + }, + { + "id":"input", "type":"haxework.gui.InputView", + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffffff"}, + "width":200, "height":25, "text":"" + } + ] + }, + + { + "id":"auth", "type":"haxework.gui.ButtonView", "width":100, - "pHeight":100, + "height":30, "skin":{"type":"haxework.gui.skin.ButtonColorSkin"}, - "text":"Login", - "onPress":"#listener" - }, - { - "id":"connection_state", - "type":"haxework.gui.LabelView", - "width":150, - "pHeight":100, - "text":"Disconnected" - }, - { - "id":"nickname", - "type":"haxework.gui.LabelView", - "width":150, - "pHeight":100, - "text":"" + "text":"Auth" } ] }, - { - "id":"error", - "type":"haxework.gui.LabelView", - "pWidth":100, - "height":30, - "fontColor":"0xff0000", - "text":"" - }, { - "type":"haxework.gui.SpriteView", - "pWidth":100, - "pHeight":100, - "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x505050"} + "id":"person", "type":"haxework.gui.VGroupView", + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ff00"}, + "pWidth":100, "pHeight":100, "layoutMargin":3 } ] } \ No newline at end of file diff --git a/src/client/haxe/ru/m/armageddon/client/Client.hx b/src/client/haxe/ru/m/armageddon/client/Client.hx index ba6f2f5..6d82c8d 100755 --- a/src/client/haxe/ru/m/armageddon/client/Client.hx +++ b/src/client/haxe/ru/m/armageddon/client/Client.hx @@ -1,24 +1,18 @@ package ru.m.armageddon.client; -import ru.m.armageddon.proto.ErrorResponse; -import ru.m.armageddon.proto.Account; -import haxework.gui.LabelView; -import haxework.gui.ButtonView; +import haxework.frame.IFrameSwitcher; +import haxework.provider.Provider; +import ru.m.armageddon.client.frames.AuthFrame; +import haxework.frame.FrameSwitcher; import haxework.gui.Root; import haxework.gui.GuiBuilder; -import flash.display.Sprite; -import haxework.gui.IGroupView; import haxe.Json; import openfl.Assets; -import ru.m.armageddon.proto.LoginResponse; -import protohx.Message; -import haxe.crypto.Md5; import ru.m.armageddon.core.connect.flash.FlashConnection; import ru.m.armageddon.core.connect.IConnection; import haxework.log.TraceLogger; -import ru.m.armageddon.proto.LoginRequest; -class Client implements IConnectionHandler { +class Client { private static inline var TAG = "Armageddon"; @@ -29,72 +23,17 @@ class Client implements IConnectionHandler { } - private var connection:IConnection; - private var account:Account; - - private var loginButton:ButtonView; - private var connectionStateLabel:LabelView; - private var nicknameLabel:LabelView; - private var errorLabel:LabelView; + private var switcher:FrameSwitcher; public function new() { var bytes = Assets.getBytes("res/layout/main.json"); var form:Dynamic = Json.parse(bytes.readUTFBytes(bytes.bytesAvailable)); - var v:IGroupView = GuiBuilder.build(form, {listener:this}); - new Root(v); - loginButton = v.findViewById("panel:login"); - connectionStateLabel = v.findViewById("panel:connection_state"); - nicknameLabel = v.findViewById("panel:nickname"); - errorLabel = v.findViewById("error"); + switcher = GuiBuilder.build(form, {listener:this}); + new Root(switcher); - connection = new FlashConnection("localhost", 5000, this); - } + Provider.set(IFrameSwitcher, switcher); + Provider.set(IConnection, new FlashConnection("localhost", 5000)); - private function refreshUI():Void { - connectionStateLabel.text = connection.connected ? "Connected" : "Disconnected"; - nicknameLabel.text = account == null ? "" : account.login; - loginButton.disabled = connection.connected && account != null; - } - - public function onPress(view:ButtonView):Void { - switch (view.id) { - case "login": - errorLabel.text = ""; - connection.connect(); - } - } - - public function onConnected():Void { - refreshUI(); - connection.send( - new LoginRequest() - .setLogin("shmyga") - .setPassword(Md5.encode("xkbp8jh9z2")) - ); - } - - public function onDisconnected():Void { - account = null; - refreshUI(); - } - - public function onError(error:Dynamic):Void { - account = null; - errorLabel.text = Std.string(error); - refreshUI(); - } - - public function onLoginResponse(packet:LoginResponse):Void { - account = packet.account; - refreshUI(); - } - - public function onErrorResponse(packet:ErrorResponse):Void { - errorLabel.text = packet.message; - refreshUI(); - } - - public function onPacket(packet:Message):Void { - L.d(TAG, "Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); + switcher.change(AuthFrame.ID); } } diff --git a/src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx b/src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx new file mode 100755 index 0000000..88db0a3 --- /dev/null +++ b/src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx @@ -0,0 +1,58 @@ +package ru.m.armageddon.client.frames; + +import ru.m.armageddon.proto.ErrorResponse; +import protohx.Message; +import haxework.frame.IFrameSwitcher; +import ru.m.armageddon.proto.LoginRequest; +import ru.m.armageddon.proto.LoginResponse; +import ru.m.armageddon.core.connect.IConnection; +import haxework.provider.Provider; +import haxe.crypto.Md5; +import haxework.gui.InputView; +import haxework.gui.ButtonView; +import haxework.gui.VGroupView; + +class AuthFrame extends VGroupView implements IPacketHandler { + + private static inline var TAG = "AuthFrame"; + + public static inline var ID = "auth"; + + public function new() { + super(); + } + + public function init():Void { + findViewById("auth", ButtonView).onPress = this; + findViewById("password:input", InputView).textField.displayAsPassword = true; + } + + public function onShow():Void { + Provider.get(IConnection).packetHandler = this; + } + + public function onPress(view:ButtonView):Void { + var login:String = findViewById("login:input", InputView).text; + var password:String = Md5.encode(findViewById("password:input", InputView).text); + var connection:IConnection = Provider.get(IConnection); + connection.connect() + .success(function(_) { + connection.send(new LoginRequest().setLogin(login).setPassword(password)); + }) + .fail(function(error) { + L.e(TAG, "Auth", error); + }); + + } + + public function onLoginResponse(packet:LoginResponse):Void { + Provider.get(IFrameSwitcher).change("person"); + } + + public function onErrorResponse(packet:ErrorResponse):Void { + L.e(TAG, packet.message); + } + + public function onPacket(packet:Message):Void {} + +} \ No newline at end of file diff --git a/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx index 81d57c0..02cc22d 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/BaseConnection.hx @@ -1,33 +1,39 @@ package ru.m.armageddon.core.connect; +import flash.errors.Error; +import haxework.net.callback.ICallback; import haxe.io.Bytes; import protohx.Message; import ru.m.armageddon.core.connect.IConnection; class BaseConnection implements IConnection { + public var handler(default,default):IConnectionHandler; + public var packetHandler(default,default):IPacketHandler; public var connected(default, null):Bool; private var builder:IPacketBuilder; - private var handler:IConnectionHandler; - public function new(handler:IConnectionHandler) { + public function new(?handler:IConnectionHandler = null) { this.builder = new PacketBuilder(); this.handler = handler; } - public function connect():Void {} + public function connect():ICallback { + throw new Error("Not implemented"); + } public function pushData(bytes:Bytes):Void {} public function send(packet:Message):Void {} private function receive(packet:Message):Void { + if (packetHandler == null) return; var name = "on" + Type.getClassName(Type.getClass(packet)).split(".").pop(); - var method = Reflect.field(handler, name); + var method = Reflect.field(packetHandler, name); if (method != null && Reflect.isFunction(method)) { - Reflect.callMethod(handler, method, [packet]); + Reflect.callMethod(packetHandler, method, [packet]); } else { - handler.onPacket(packet); + packetHandler.onPacket(packet); } } } \ No newline at end of file diff --git a/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx index a6f44e4..f51dc0b 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/IConnection.hx @@ -1,15 +1,17 @@ package ru.m.armageddon.core.connect; +import haxework.net.callback.ICallback; import haxe.io.Bytes; import protohx.Message; interface IConnection { - public var connected(default, null):Bool; + public var connected(default,null):Bool; + public var handler(default,default):IConnectionHandler; + public var packetHandler(default,default):IPacketHandler; private var builder:IPacketBuilder; - private var handler:IConnectionHandler; - public function connect():Void; + public function connect():ICallback; public function send(packet:Message):Void; public function pushData(bytes:Bytes):Void; private function receive(packet:Message):Void; @@ -19,6 +21,9 @@ interface IConnectionHandler { public function onConnected():Void; public function onDisconnected():Void; public function onError(error:Dynamic):Void; +} + +interface IPacketHandler { public function onPacket(packet:Message):Void; } diff --git a/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx index ff00f17..0a9a5f8 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx @@ -1,5 +1,7 @@ package ru.m.armageddon.core.connect.flash; +import haxework.net.callback.Callback; +import haxework.net.callback.ICallback; import ru.m.armageddon.core.connect.IConnection.IConnectionHandler; import flash.utils.Endian; import haxe.io.BytesOutput; @@ -18,7 +20,9 @@ class FlashConnection extends BaseConnection { private var port:Int; private var socket:Socket; - public function new(host:String, port:Int, handler:IConnectionHandler) { + private var callback:ICallback; + + public function new(host:String, port:Int, ?handler:IConnectionHandler = null) { super(handler); this.host = host; this.port = port; @@ -30,28 +34,39 @@ class FlashConnection extends BaseConnection { socket.addEventListener(Event.CONNECT, onConnect); socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); socket.endian = Endian.LITTLE_ENDIAN; - //socket.connect(host, port); } - override public function connect():Void { + override public function connect():ICallback { + callback = Callback.build(); socket.connect(host, port); + return callback; } private function onError(event:ErrorEvent):Void { socket.close(); connected = false; - handler.onError(event); + if (handler != null) handler.onError(event); + if (callback != null) { + var c = callback; + callback = null; + c.callFail(event); + } } private function onConnect(_):Void { connected = true; - handler.onConnected(); + if (handler != null) handler.onConnected(); + if (callback != null) { + var c = callback; + callback = null; + c.callSuccess(null); + } } private function onClose(_):Void { socket.close(); connected = false; - handler.onDisconnected(); + if (handler != null) handler.onDisconnected(); } private function onSocketData(_):Void {