-
This commit is contained in:
@@ -1,59 +1,59 @@
|
|||||||
{
|
{
|
||||||
"type":"haxework.gui.VGroupView",
|
"type":"haxework.frame.FrameSwitcher",
|
||||||
"paddings":10,
|
|
||||||
"layoutMargin":10,
|
|
||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x000000"},
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x000000"},
|
||||||
"views":[
|
"views":[
|
||||||
{
|
{
|
||||||
"id":"panel",
|
"id":"auth", "type":"ru.m.armageddon.client.frames.AuthFrame",
|
||||||
"type":"haxework.gui.HGroupView",
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"},
|
||||||
"layoutHAlign":"LEFT",
|
"pWidth":100, "pHeight":100, "layoutMargin":3,
|
||||||
"pWidth":100,
|
|
||||||
"height":30,
|
|
||||||
"paddings":3,
|
|
||||||
"layoutMargin":3,
|
|
||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x505050"},
|
|
||||||
"views":[
|
"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",
|
"type":"haxework.gui.ButtonView",
|
||||||
"width":100,
|
"width":100,
|
||||||
"pHeight":100,
|
"height":30,
|
||||||
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
|
"skin":{"type":"haxework.gui.skin.ButtonColorSkin"},
|
||||||
"text":"Login",
|
"text":"Auth"
|
||||||
"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":""
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
|
||||||
"id":"error",
|
|
||||||
"type":"haxework.gui.LabelView",
|
|
||||||
"pWidth":100,
|
|
||||||
"height":30,
|
|
||||||
"fontColor":"0xff0000",
|
|
||||||
"text":""
|
|
||||||
},
|
|
||||||
|
|
||||||
{
|
{
|
||||||
"type":"haxework.gui.SpriteView",
|
"id":"person", "type":"haxework.gui.VGroupView",
|
||||||
"pWidth":100,
|
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ff00"},
|
||||||
"pHeight":100,
|
"pWidth":100, "pHeight":100, "layoutMargin":3
|
||||||
"skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x505050"}
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,18 @@
|
|||||||
package ru.m.armageddon.client;
|
package ru.m.armageddon.client;
|
||||||
|
|
||||||
import ru.m.armageddon.proto.ErrorResponse;
|
import haxework.frame.IFrameSwitcher;
|
||||||
import ru.m.armageddon.proto.Account;
|
import haxework.provider.Provider;
|
||||||
import haxework.gui.LabelView;
|
import ru.m.armageddon.client.frames.AuthFrame;
|
||||||
import haxework.gui.ButtonView;
|
import haxework.frame.FrameSwitcher;
|
||||||
import haxework.gui.Root;
|
import haxework.gui.Root;
|
||||||
import haxework.gui.GuiBuilder;
|
import haxework.gui.GuiBuilder;
|
||||||
import flash.display.Sprite;
|
|
||||||
import haxework.gui.IGroupView;
|
|
||||||
import haxe.Json;
|
import haxe.Json;
|
||||||
import openfl.Assets;
|
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.flash.FlashConnection;
|
||||||
import ru.m.armageddon.core.connect.IConnection;
|
import ru.m.armageddon.core.connect.IConnection;
|
||||||
import haxework.log.TraceLogger;
|
import haxework.log.TraceLogger;
|
||||||
import ru.m.armageddon.proto.LoginRequest;
|
|
||||||
|
|
||||||
class Client implements IConnectionHandler {
|
class Client {
|
||||||
|
|
||||||
private static inline var TAG = "Armageddon";
|
private static inline var TAG = "Armageddon";
|
||||||
|
|
||||||
@@ -29,72 +23,17 @@ class Client implements IConnectionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private var connection:IConnection;
|
private var switcher:FrameSwitcher;
|
||||||
private var account:Account;
|
|
||||||
|
|
||||||
private var loginButton:ButtonView;
|
|
||||||
private var connectionStateLabel:LabelView;
|
|
||||||
private var nicknameLabel:LabelView;
|
|
||||||
private var errorLabel:LabelView;
|
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
var bytes = Assets.getBytes("res/layout/main.json");
|
var bytes = Assets.getBytes("res/layout/main.json");
|
||||||
var form:Dynamic = Json.parse(bytes.readUTFBytes(bytes.bytesAvailable));
|
var form:Dynamic = Json.parse(bytes.readUTFBytes(bytes.bytesAvailable));
|
||||||
var v:IGroupView<Sprite> = GuiBuilder.build(form, {listener:this});
|
switcher = GuiBuilder.build(form, {listener:this});
|
||||||
new Root(v);
|
new Root(switcher);
|
||||||
loginButton = v.findViewById("panel:login");
|
|
||||||
connectionStateLabel = v.findViewById("panel:connection_state");
|
|
||||||
nicknameLabel = v.findViewById("panel:nickname");
|
|
||||||
errorLabel = v.findViewById("error");
|
|
||||||
|
|
||||||
connection = new FlashConnection("localhost", 5000, this);
|
Provider.set(IFrameSwitcher, switcher);
|
||||||
}
|
Provider.set(IConnection, new FlashConnection("localhost", 5000));
|
||||||
|
|
||||||
private function refreshUI():Void {
|
switcher.change(AuthFrame.ID);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
58
src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx
Executable file
58
src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx
Executable 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 {}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,33 +1,39 @@
|
|||||||
package ru.m.armageddon.core.connect;
|
package ru.m.armageddon.core.connect;
|
||||||
|
|
||||||
|
import flash.errors.Error;
|
||||||
|
import haxework.net.callback.ICallback;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
import ru.m.armageddon.core.connect.IConnection;
|
import ru.m.armageddon.core.connect.IConnection;
|
||||||
|
|
||||||
class BaseConnection implements IConnection {
|
class BaseConnection implements IConnection {
|
||||||
|
public var handler(default,default):IConnectionHandler;
|
||||||
|
public var packetHandler(default,default):IPacketHandler;
|
||||||
public var connected(default, null):Bool;
|
public var connected(default, null):Bool;
|
||||||
|
|
||||||
private var builder:IPacketBuilder;
|
private var builder:IPacketBuilder;
|
||||||
private var handler:IConnectionHandler;
|
|
||||||
|
|
||||||
public function new(handler:IConnectionHandler) {
|
public function new(?handler:IConnectionHandler = null) {
|
||||||
this.builder = new PacketBuilder();
|
this.builder = new PacketBuilder();
|
||||||
this.handler = handler;
|
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 pushData(bytes:Bytes):Void {}
|
||||||
|
|
||||||
public function send(packet:Message):Void {}
|
public function send(packet:Message):Void {}
|
||||||
|
|
||||||
private function receive(packet:Message):Void {
|
private function receive(packet:Message):Void {
|
||||||
|
if (packetHandler == null) return;
|
||||||
var name = "on" + Type.getClassName(Type.getClass(packet)).split(".").pop();
|
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)) {
|
if (method != null && Reflect.isFunction(method)) {
|
||||||
Reflect.callMethod(handler, method, [packet]);
|
Reflect.callMethod(packetHandler, method, [packet]);
|
||||||
} else {
|
} else {
|
||||||
handler.onPacket(packet);
|
packetHandler.onPacket(packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,17 @@
|
|||||||
package ru.m.armageddon.core.connect;
|
package ru.m.armageddon.core.connect;
|
||||||
|
|
||||||
|
import haxework.net.callback.ICallback;
|
||||||
import haxe.io.Bytes;
|
import haxe.io.Bytes;
|
||||||
import protohx.Message;
|
import protohx.Message;
|
||||||
|
|
||||||
interface IConnection {
|
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 builder:IPacketBuilder;
|
||||||
private var handler:IConnectionHandler;
|
|
||||||
|
|
||||||
public function connect():Void;
|
public function connect():ICallback<Dynamic>;
|
||||||
public function send(packet:Message):Void;
|
public function send(packet:Message):Void;
|
||||||
public function pushData(bytes:Bytes):Void;
|
public function pushData(bytes:Bytes):Void;
|
||||||
private function receive(packet:Message):Void;
|
private function receive(packet:Message):Void;
|
||||||
@@ -19,6 +21,9 @@ interface IConnectionHandler {
|
|||||||
public function onConnected():Void;
|
public function onConnected():Void;
|
||||||
public function onDisconnected():Void;
|
public function onDisconnected():Void;
|
||||||
public function onError(error:Dynamic):Void;
|
public function onError(error:Dynamic):Void;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IPacketHandler {
|
||||||
public function onPacket(packet:Message):Void;
|
public function onPacket(packet:Message):Void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ru.m.armageddon.core.connect.flash;
|
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 ru.m.armageddon.core.connect.IConnection.IConnectionHandler;
|
||||||
import flash.utils.Endian;
|
import flash.utils.Endian;
|
||||||
import haxe.io.BytesOutput;
|
import haxe.io.BytesOutput;
|
||||||
@@ -18,7 +20,9 @@ class FlashConnection extends BaseConnection {
|
|||||||
private var port:Int;
|
private var port:Int;
|
||||||
private var socket:Socket;
|
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);
|
super(handler);
|
||||||
this.host = host;
|
this.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
@@ -30,28 +34,39 @@ class FlashConnection extends BaseConnection {
|
|||||||
socket.addEventListener(Event.CONNECT, onConnect);
|
socket.addEventListener(Event.CONNECT, onConnect);
|
||||||
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
||||||
socket.endian = Endian.LITTLE_ENDIAN;
|
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);
|
socket.connect(host, port);
|
||||||
|
return callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onError(event:ErrorEvent):Void {
|
private function onError(event:ErrorEvent):Void {
|
||||||
socket.close();
|
socket.close();
|
||||||
connected = false;
|
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 {
|
private function onConnect(_):Void {
|
||||||
connected = true;
|
connected = true;
|
||||||
handler.onConnected();
|
if (handler != null) handler.onConnected();
|
||||||
|
if (callback != null) {
|
||||||
|
var c = callback;
|
||||||
|
callback = null;
|
||||||
|
c.callSuccess(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onClose(_):Void {
|
private function onClose(_):Void {
|
||||||
socket.close();
|
socket.close();
|
||||||
connected = false;
|
connected = false;
|
||||||
handler.onDisconnected();
|
if (handler != null) handler.onDisconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onSocketData(_):Void {
|
private function onSocketData(_):Void {
|
||||||
|
|||||||
Reference in New Issue
Block a user