From e1a5f0e2bd236e1c9076b7e3423e6f3fe3d6a0dd Mon Sep 17 00:00:00 2001 From: shmyga Date: Sun, 6 Jul 2014 21:25:57 +0400 Subject: [PATCH] - --- proto/base.proto | 8 +++ res/layout/main.json | 25 ++++++++-- .../haxe/ru/m/armageddon/client/Client.hx | 2 + .../ru/m/armageddon/client/data/GameData.hx | 9 ++++ .../m/armageddon/client/frames/AuthFrame.hx | 4 +- .../m/armageddon/client/frames/PersonFrame.hx | 50 +++++++++++++++++++ .../client/frames/person/PersonRenderer.hx | 16 ++++++ .../client/frames/person/PersonView.hx | 29 +++++++++++ .../armageddon/core/connect/PacketBuilder.hx | 6 ++- .../core/connect/flash/FlashConnection.hx | 1 + .../haxe/ru/m/armageddon/server/db/Db.hx | 15 +++++- .../ru/m/armageddon/server/session/Session.hx | 12 +++++ 12 files changed, 170 insertions(+), 7 deletions(-) create mode 100755 src/client/haxe/ru/m/armageddon/client/data/GameData.hx create mode 100755 src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx create mode 100755 src/client/haxe/ru/m/armageddon/client/frames/person/PersonRenderer.hx create mode 100755 src/client/haxe/ru/m/armageddon/client/frames/person/PersonView.hx diff --git a/proto/base.proto b/proto/base.proto index 3e3b08e..4445f9e 100755 --- a/proto/base.proto +++ b/proto/base.proto @@ -20,6 +20,14 @@ message LoginResponse { required Account account = 1; } +message PersonSelectRequest { + required int32 person_id = 1; +} + +message PersonSelectResponse { + required Person person = 1; +} + message ErrorResponse { required int32 code = 1; required string message = 2; diff --git a/res/layout/main.json b/res/layout/main.json index e82c1f9..89cf194 100755 --- a/res/layout/main.json +++ b/res/layout/main.json @@ -51,9 +51,28 @@ { - "id":"person", "type":"haxework.gui.VGroupView", - "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ff00"}, - "pWidth":100, "pHeight":100, "layoutMargin":3 + "id":"person", "type":"ru.m.armageddon.client.frames.PersonFrame", + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"}, + "pWidth":100, "pHeight":100, "layoutMargin":3, "paddings":10, + "views":[ + { + "id":"list", "type":"haxework.gui.list.VListView", + "renderer":{ + "type":"ru.m.armageddon.client.frames.person.PersonRenderer" + }, + "scroll":{ + "type":"haxework.gui.list.VScrollView", + "skin":{"type":"haxework.gui.list.VScrollSkin"} + }, + "pWidth":100, "pHeight":100, "layoutMargin":5 + } + ] + }, + + { + "id":"game", "type":"haxework.gui.SpriteView", + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xa0a0a0"}, + "pWidth":100, "pHeight":100 } ] } \ No newline at end of file diff --git a/src/client/haxe/ru/m/armageddon/client/Client.hx b/src/client/haxe/ru/m/armageddon/client/Client.hx index 6d82c8d..0a4bf2a 100755 --- a/src/client/haxe/ru/m/armageddon/client/Client.hx +++ b/src/client/haxe/ru/m/armageddon/client/Client.hx @@ -1,5 +1,6 @@ package ru.m.armageddon.client; +import ru.m.armageddon.client.data.GameData; import haxework.frame.IFrameSwitcher; import haxework.provider.Provider; import ru.m.armageddon.client.frames.AuthFrame; @@ -31,6 +32,7 @@ class Client { switcher = GuiBuilder.build(form, {listener:this}); new Root(switcher); + Provider.setFactory(GameData, GameData); Provider.set(IFrameSwitcher, switcher); Provider.set(IConnection, new FlashConnection("localhost", 5000)); diff --git a/src/client/haxe/ru/m/armageddon/client/data/GameData.hx b/src/client/haxe/ru/m/armageddon/client/data/GameData.hx new file mode 100755 index 0000000..0127077 --- /dev/null +++ b/src/client/haxe/ru/m/armageddon/client/data/GameData.hx @@ -0,0 +1,9 @@ +package ru.m.armageddon.client.data; + +import ru.m.armageddon.proto.Person; +import ru.m.armageddon.proto.Account; + +class GameData { + public var account:Account; + public var person:Person; +} diff --git a/src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx b/src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx index 981a32a..1db174f 100755 --- a/src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx +++ b/src/client/haxe/ru/m/armageddon/client/frames/AuthFrame.hx @@ -1,5 +1,6 @@ package ru.m.armageddon.client.frames; +import ru.m.armageddon.client.data.GameData; import flash.net.SharedObject; import ru.m.armageddon.proto.ErrorResponse; import protohx.Message; @@ -62,7 +63,8 @@ class AuthFrame extends VGroupView implements IPacketHandler { so.setProperty("login", loginInput.text); so.setProperty("password", passwordInput.text); so.flush(); - Provider.get(IFrameSwitcher).change("person"); + Provider.get(GameData).account = packet.account; + Provider.get(IFrameSwitcher).change(PersonFrame.ID); } public function onErrorResponse(packet:ErrorResponse):Void { diff --git a/src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx b/src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx new file mode 100755 index 0000000..8bb6354 --- /dev/null +++ b/src/client/haxe/ru/m/armageddon/client/frames/PersonFrame.hx @@ -0,0 +1,50 @@ +package ru.m.armageddon.client.frames; + +import haxework.frame.IFrameSwitcher; +import protohx.MessageUtils; +import ru.m.armageddon.proto.PersonSelectResponse; +import ru.m.armageddon.proto.PersonSelectRequest; +import haxework.gui.list.ListView; +import protohx.Message; +import ru.m.armageddon.core.connect.IConnection; +import ru.m.armageddon.client.frames.person.PersonView; +import ru.m.armageddon.client.data.GameData; +import haxework.provider.Provider; +import ru.m.armageddon.proto.Person; +import haxework.gui.list.VListView; +import haxework.gui.HGroupView; +import ru.m.armageddon.client.frames.person.PersonRenderer; + +class PersonFrame extends HGroupView implements IPacketHandler implements ListViewListener { + + private static inline var TAG = "PersonFrame"; + + public static inline var ID = "person"; + + private var list:VListView; + + public function new() { + super(); + } + + public function init() { + list = findViewById("list"); + list.dispatcher.addListener(this); + } + + public function onShow() { + list.data = Provider.get(GameData).account.persons; + Provider.get(IConnection).packetHandler = this; + } + + public function onListItemClick(item:ListItem):Void { + Provider.get(IConnection).send(new PersonSelectRequest().setPersonId(item.data.id)); + } + + public function onPersonSelectResponse(packet:PersonSelectResponse):Void { + Provider.get(GameData).person = packet.person; + Provider.get(IFrameSwitcher).change("game"); + } + + public function onPacket(packet:Message):Void {} +} diff --git a/src/client/haxe/ru/m/armageddon/client/frames/person/PersonRenderer.hx b/src/client/haxe/ru/m/armageddon/client/frames/person/PersonRenderer.hx new file mode 100755 index 0000000..af48c93 --- /dev/null +++ b/src/client/haxe/ru/m/armageddon/client/frames/person/PersonRenderer.hx @@ -0,0 +1,16 @@ +package ru.m.armageddon.client.frames.person; + +import ru.m.armageddon.proto.Person; +import haxework.gui.list.VListView; +import haxework.gui.list.ListView; + +class PersonRenderer implements IRenderer, PersonView, Person> { + + public function factory():ListItem { + return {view:new PersonView()}; + } + + public function render(list:VListView, item:ListItem):Void { + item.view.person = item.data; + } +} \ No newline at end of file diff --git a/src/client/haxe/ru/m/armageddon/client/frames/person/PersonView.hx b/src/client/haxe/ru/m/armageddon/client/frames/person/PersonView.hx new file mode 100755 index 0000000..1b57f21 --- /dev/null +++ b/src/client/haxe/ru/m/armageddon/client/frames/person/PersonView.hx @@ -0,0 +1,29 @@ +package ru.m.armageddon.client.frames.person; + +import haxework.gui.LabelView; +import ru.m.armageddon.proto.Person; +import haxework.gui.skin.ColorSkin; +import haxework.gui.HGroupView; + +class PersonView extends HGroupView { + + public var person(default,set):Person; + + private var nameLabel:LabelView; + + public function new() { + super(); + pWidth = 100; + height = 50; + skin = new ColorSkin(0xffffff); + + nameLabel = new LabelView(); + addView(nameLabel); + } + + private function set_person(value:Person):Person { + this.person = value; + nameLabel.text = this.person.name; + return this.person; + } +} \ No newline at end of file diff --git a/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx b/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx index 228a25c..3998bd6 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/PacketBuilder.hx @@ -4,6 +4,8 @@ import ru.m.armageddon.core.connect.IConnection; import protohx.Message; import ru.m.armageddon.proto.LoginRequest; import ru.m.armageddon.proto.LoginResponse; +import ru.m.armageddon.proto.PersonSelectRequest; +import ru.m.armageddon.proto.PersonSelectResponse; import ru.m.armageddon.proto.ErrorResponse; class PacketBuilder implements IPacketBuilder { @@ -14,7 +16,9 @@ class PacketBuilder implements IPacketBuilder { ], 0x01 => [ 0x0001 => LoginRequest, - 0x0002 => LoginResponse + 0x0002 => LoginResponse, + 0x0003 => PersonSelectRequest, + 0x0004 => PersonSelectResponse ] ]; diff --git a/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx index dc68520..b0e37d8 100755 --- a/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx +++ b/src/common/haxe/ru/m/armageddon/core/connect/flash/FlashConnection.hx @@ -70,6 +70,7 @@ class FlashConnection extends BaseConnection { } private function onSocketData(_):Void { + if (socket.bytesAvailable == 0) return; var family = socket.readByte(); var id = socket.readByte(); var length = socket.readShort(); 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 c0b2209..b678985 100755 --- a/src/server/haxe/ru/m/armageddon/server/db/Db.hx +++ b/src/server/haxe/ru/m/armageddon/server/db/Db.hx @@ -6,6 +6,7 @@ import sys.db.Mysql; import sys.db.Connection; typedef DbPerson = { + @:optional var a_id:Int; var p_id:Int; var p_name:String; } @@ -26,13 +27,23 @@ class Db { } 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 + "'"); + var rset = db.request("SELECT a.id AS a_id, p.id AS p_id, p.name AS p_name FROM account AS a LEFT OUTER JOIN person AS p ON(a.id=p.account_id) WHERE a.login='" + login + "' AND a.password='" + password + "'"); if (!rset.hasNext()) return null; 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)); + account.setId(data.a_id); + if (data.p_name != null) { + account.addPersons(new Person().setId(data.p_id).setName(data.p_name)); + } } return account; } + + public function getPerson(account_id:Int, person_id:Int):Null { + var rset = db.request("SELECT id AS p_id, name AS p_name FROM person WHERE id=" + person_id + " AND account_id=" + account_id); + if (!rset.hasNext()) return null; + var data:DbPerson = rset.next(); + return new Person().setId(data.p_id).setName(data.p_name); + } } \ 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 6be89b6..de0ee9f 100755 --- a/src/server/haxe/ru/m/armageddon/server/session/Session.hx +++ b/src/server/haxe/ru/m/armageddon/server/session/Session.hx @@ -1,5 +1,7 @@ package ru.m.armageddon.server.session; +import ru.m.armageddon.proto.PersonSelectResponse; +import ru.m.armageddon.proto.PersonSelectRequest; import ru.m.armageddon.proto.Account; import ru.m.armageddon.core.connect.neko.NekoConnection; import ru.m.armageddon.proto.ErrorResponse; @@ -45,6 +47,16 @@ class Session implements IConnectionHandler implements IPacketHandler { } } + public function onPersonSelectRequest(packet:PersonSelectRequest):Void { + var db = new Db(); + var person = db.getPerson(account.id, packet.personId); + if (person != null) { + connection.send(new PersonSelectResponse().setPerson(person)); + } else { + connection.send(new ErrorResponse().setCode(404).setMessage("Person not found")); + } + } + public function onPacket(packet:Message):Void { trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); }