-
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
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.gui.Root;
|
||||
@@ -8,8 +10,6 @@ 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;
|
||||
@@ -30,11 +30,12 @@ class Client implements IConnectionHandler {
|
||||
|
||||
|
||||
private var connection:IConnection;
|
||||
private var user:User;
|
||||
private var account:Account;
|
||||
|
||||
private var loginButton:ButtonView;
|
||||
private var connectionStateLabel:LabelView;
|
||||
private var nicknameLabel:LabelView;
|
||||
private var errorLabel:LabelView;
|
||||
|
||||
public function new() {
|
||||
var bytes = Assets.getBytes("res/layout/main.json");
|
||||
@@ -44,19 +45,21 @@ class Client implements IConnectionHandler {
|
||||
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);
|
||||
}
|
||||
|
||||
private function refreshUI():Void {
|
||||
connectionStateLabel.text = connection.connected ? "Connected" : "Disconnected";
|
||||
nicknameLabel.text = user == null ? "" : user.nickname;
|
||||
loginButton.disabled = connection.connected && user != null;
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -71,17 +74,23 @@ class Client implements IConnectionHandler {
|
||||
}
|
||||
|
||||
public function onDisconnected():Void {
|
||||
this.user = null;
|
||||
account = null;
|
||||
refreshUI();
|
||||
}
|
||||
|
||||
public function onError(error:Dynamic):Void {
|
||||
this.user = null;
|
||||
account = null;
|
||||
errorLabel.text = Std.string(error);
|
||||
refreshUI();
|
||||
}
|
||||
|
||||
public function onLoginResponse(packet:LoginResponse):Void {
|
||||
this.user = packet.user;
|
||||
account = packet.account;
|
||||
refreshUI();
|
||||
}
|
||||
|
||||
public function onErrorResponse(packet:ErrorResponse):Void {
|
||||
errorLabel.text = packet.message;
|
||||
refreshUI();
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ class NekoConnection extends BaseConnection {
|
||||
try {
|
||||
receive(packet);
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
handler.onError(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,12 +14,12 @@ class Server extends ThreadServer<Session, Bytes> {
|
||||
|
||||
override function clientConnected(s:Socket):Session {
|
||||
var session = new Session(s);
|
||||
Lib.println("client: " + session.user + " / " + s.peer());
|
||||
Lib.println("client: " + s.peer());
|
||||
return session;
|
||||
}
|
||||
|
||||
override function clientDisconnected(session:Session) {
|
||||
Lib.println("client " + Std.string(session.user) + " disconnected");
|
||||
Lib.println("client disconnected");
|
||||
}
|
||||
|
||||
override function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
package ru.m.armageddon.server.db;
|
||||
|
||||
import ru.m.armageddon.proto.Person;
|
||||
import ru.m.armageddon.proto.Account;
|
||||
import sys.db.Mysql;
|
||||
import sys.db.Connection;
|
||||
|
||||
typedef DbPerson = {
|
||||
var p_id:Int;
|
||||
var p_name:String;
|
||||
}
|
||||
|
||||
class Db {
|
||||
|
||||
private var db:Connection;
|
||||
@@ -18,10 +25,14 @@ class Db {
|
||||
});
|
||||
}
|
||||
|
||||
public function getUser(login:String, password:String) {
|
||||
var rset = db.request("SELECT * FROM Users WHERE login='" + login + "' AND password='" + password + "'");
|
||||
public function getAccount(login:String, password:String):Null<Account> {
|
||||
var rset = db.request("SELECT p.id AS p_id, p.name AS p_name FROM account AS a LEFT JOIN person AS p ON(a.id=p.account_id) WHERE a.login='" + login + "' AND a.password='" + password + "'");
|
||||
if (!rset.hasNext()) return null;
|
||||
var user = rset.next();
|
||||
return user;
|
||||
var account = new Account().setLogin(login);
|
||||
while (rset.hasNext()) {
|
||||
var data:DbPerson = rset.next();
|
||||
account.addPersons(new Person().setId(data.p_id).setName(data.p_name));
|
||||
}
|
||||
return account;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package ru.m.armageddon.server.session;
|
||||
|
||||
import ru.m.armageddon.proto.Account;
|
||||
import ru.m.armageddon.core.connect.neko.NekoConnection;
|
||||
import ru.m.armageddon.proto.ErrorResponse;
|
||||
import ru.m.armageddon.server.db.Db;
|
||||
import ru.m.armageddon.proto.User;
|
||||
import ru.m.armageddon.proto.LoginResponse;
|
||||
import ru.m.armageddon.proto.LoginRequest;
|
||||
import protohx.Message;
|
||||
@@ -12,7 +12,7 @@ import sys.net.Socket;
|
||||
|
||||
class Session implements IConnectionHandler {
|
||||
|
||||
public var user(default, null):User;
|
||||
public var account(default, null):Account;
|
||||
public var connection(default, null):IConnection;
|
||||
|
||||
public function new(socket:Socket) {
|
||||
@@ -37,20 +37,11 @@ class Session implements IConnectionHandler {
|
||||
|
||||
public function onLoginRequest(packet:LoginRequest):Void {
|
||||
var db = new Db();
|
||||
var userData = db.getUser(packet.login, packet.password);
|
||||
if (userData != null) {
|
||||
var user = new User();
|
||||
user.login = userData.login;
|
||||
user.nickname = userData.login;
|
||||
this.user = user;
|
||||
var response = new LoginResponse();
|
||||
response.user = user;
|
||||
connection.send(response);
|
||||
account = db.getAccount(packet.login, packet.password);
|
||||
if (account != null) {
|
||||
connection.send(new LoginResponse().setAccount(account));
|
||||
} else {
|
||||
var response = new ErrorResponse();
|
||||
response.code = 403;
|
||||
response.message = "User not found";
|
||||
connection.send(response);
|
||||
connection.send(new ErrorResponse().setCode(404).setMessage("Account not found"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user