This commit is contained in:
2014-07-06 17:13:10 +04:00
parent d1d55a64f7
commit 6623a031d0
6 changed files with 151 additions and 128 deletions

View File

@@ -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",
"type":"haxework.gui.ButtonView",
"width":100,
"pHeight":100,
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
"text":"Login",
"onPress":"#listener"
"id":"login", "type":"haxework.gui.HGroupView",
"contentSize":true,
"views":[
{
"type":"haxework.gui.LabelView",
"width":150, "height":25, "text":"Login"
},
{
"id":"connection_state",
"type":"haxework.gui.LabelView",
"width":150,
"pHeight":100,
"text":"Disconnected"
"id":"input", "type":"haxework.gui.InputView",
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffffff"},
"width":200, "height":25, "text":""
}
]
},
{
"id":"nickname",
"id":"password", "type":"haxework.gui.HGroupView",
"contentSize":true,
"views":[
{
"type":"haxework.gui.LabelView",
"width":150,
"pHeight":100,
"text":""
"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":"error",
"type":"haxework.gui.LabelView",
"pWidth":100,
"id":"auth",
"type":"haxework.gui.ButtonView",
"width":100,
"height":30,
"fontColor":"0xff0000",
"text":""
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
"text":"Auth"
}
]
},
{
"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
}
]
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -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<Dynamic>;
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<Dynamic> {
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 {