This commit is contained in:
2014-07-05 18:47:56 +04:00
parent e207de6eb6
commit 77c0b71ed7
6 changed files with 115 additions and 10 deletions

View File

@@ -1,11 +1,18 @@
package ru.m.armageddon.client;
import haxework.gui.LabelView;
import haxework.gui.ButtonView;
import haxework.gui.Root;
import haxework.gui.GuiBuilder;
import flash.display.Sprite;
import haxework.gui.IGroupView;
import haxe.Json;
import openfl.Assets;
import haxe.Timer;
import ru.m.armageddon.proto.User;
import ru.m.armageddon.proto.LoginResponse;
import protohx.Message;
import haxe.crypto.Md5;
import flash.Lib;
import flash.events.MouseEvent;
import ru.m.armageddon.core.connect.flash.FlashConnection;
import ru.m.armageddon.core.connect.IConnection;
import haxework.log.TraceLogger;
@@ -25,15 +32,37 @@ class Client implements IConnectionHandler {
private var connection:IConnection;
private var user:User;
public function new() {
connection = new FlashConnection("localhost", 5000, this);
private var loginButton:ButtonView;
private var connectionStateLabel:LabelView;
private var nicknameLabel:LabelView;
Lib.current.stage.addEventListener(MouseEvent.CLICK, function(_) {
onConnected();
});
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");
connection = new FlashConnection("localhost", 5000, this);
}
private function refreshUI():Void {
connectionStateLabel.text = connection.connected ? "Connected" : "Disconnected";
nicknameLabel.text = user == null ? "" : user.nickname;
loginButton.disabled = connection.connected && user != null;
}
public function onPress(view:ButtonView):Void {
switch (view.id) {
case "login":
connection.connect();
}
}
public function onConnected():Void {
refreshUI();
connection.send(
new LoginRequest()
.setLogin("shmyga")
@@ -42,16 +71,18 @@ class Client implements IConnectionHandler {
}
public function onDisconnected():Void {
this.user = null;
refreshUI();
}
public function onError(error:Dynamic):Void {
this.user = null;
refreshUI();
}
public function onLoginResponse(packet:LoginResponse):Void {
this.user = packet.user;
L.d(TAG, "Loginned: " + user.nickname);
refreshUI();
}
public function onPacket(packet:Message):Void {