diff --git a/proto/base.proto b/proto/base.proto index 398020d..3e3b08e 100755 --- a/proto/base.proto +++ b/proto/base.proto @@ -1,9 +1,14 @@ package ru.m.armageddon.proto; -message User { +message Person { + required int32 id = 1; + required string name = 2; +} + +message Account { required int32 id = 1; required string login = 2; - required string nickname = 3; + repeated Person persons = 3; } message LoginRequest { @@ -12,7 +17,7 @@ message LoginRequest { } message LoginResponse { - required User user = 1; + required Account account = 1; } message ErrorResponse { diff --git a/res/layout/main.json b/res/layout/main.json index ff0199f..49747cb 100755 --- a/res/layout/main.json +++ b/res/layout/main.json @@ -40,11 +40,20 @@ ] }, + { + "id":"error", + "type":"haxework.gui.LabelView", + "pWidth":100, + "height":30, + "fontColor":"0xff0000", + "text":"" + }, + { "type":"haxework.gui.SpriteView", "pWidth":100, "pHeight":100, - "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x000000"} + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x505050"} } ] } \ No newline at end of file diff --git a/sql/init.sql b/sql/init.sql index fea84d3..a1ee6fd 100755 --- a/sql/init.sql +++ b/sql/init.sql @@ -6,10 +6,23 @@ CREATE DATABASE IF NOT EXISTS armageddon; GRANT ALL ON armageddon.* TO 'shmyga'@'localhost' IDENTIFIED BY 'xkbp8jh9z2'; -DROP TABLE IF EXISTS armageddon.users; -CREATE TABLE IF NOT EXISTS armageddon.users ( +DROP TABLE IF EXISTS armageddon.account; +CREATE TABLE IF NOT EXISTS armageddon.account ( id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, login VARCHAR(255) UNIQUE, password VARCHAR(32) ); -INSERT INTO armageddon.users (login,password) VALUES('shmyga', 'd48cc4eb42c058869ae90daef9606e43'); \ No newline at end of file + +DROP TABLE IF EXISTS armageddon.person; +CREATE TABLE IF NOT EXISTS armageddon.account ( + id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, + account_id INT UNSIGNED NOT NULL, + name VARCHAR(255), + + CONSTRAINT person_2_account FOREIGN KEY (account_id) REFERENCES armageddon.account(id) + MATCH SIMPLE ON DELETE CASCADE ON UPDATE NO ACTION +); + +INSERT INTO armageddon.account (id,login,password) VALUES(1,'shmyga', 'd48cc4eb42c058869ae90daef9606e43'); +INSERT INTO armageddon.person (id,account_id,name) VALUES(1,1,'-=Shmyga=-'); + diff --git a/src/client/haxe/ru/m/armageddon/client/Client.hx b/src/client/haxe/ru/m/armageddon/client/Client.hx index 2a9bc09..ba6f2f5 100755 --- a/src/client/haxe/ru/m/armageddon/client/Client.hx +++ b/src/client/haxe/ru/m/armageddon/client/Client.hx @@ -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(); } diff --git a/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx index c932bbf..410dd73 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/neko/NekoConnection.hx @@ -28,6 +28,7 @@ class NekoConnection extends BaseConnection { try { receive(packet); } catch (error:Dynamic) { + trace(error); handler.onError(error); } } diff --git a/src/server/haxe/ru/m/armageddon/server/Server.hx b/src/server/haxe/ru/m/armageddon/server/Server.hx index 3de4391..82962a9 100755 --- a/src/server/haxe/ru/m/armageddon/server/Server.hx +++ b/src/server/haxe/ru/m/armageddon/server/Server.hx @@ -14,12 +14,12 @@ class Server extends ThreadServer { 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) { diff --git a/src/server/haxe/ru/m/armageddon/server/db/Db.hx b/src/server/haxe/ru/m/armageddon/server/db/Db.hx index f9a81e6..c0b2209 100755 --- a/src/server/haxe/ru/m/armageddon/server/db/Db.hx +++ b/src/server/haxe/ru/m/armageddon/server/db/Db.hx @@ -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 { + 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; } } \ No newline at end of file diff --git a/src/server/haxe/ru/m/armageddon/server/session/Session.hx b/src/server/haxe/ru/m/armageddon/server/session/Session.hx index d50d8d4..44fff83 100755 --- a/src/server/haxe/ru/m/armageddon/server/session/Session.hx +++ b/src/server/haxe/ru/m/armageddon/server/session/Session.hx @@ -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")); } }