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