From c605f843b049dd0f22b7a2433582b44b5f26aaea Mon Sep 17 00:00:00 2001 From: shmyga Date: Wed, 27 Aug 2014 17:34:58 +0400 Subject: [PATCH] - --- proto/base.proto | 8 +++ src/client/haxe/ru/m/tankz/data/GameData.hx | 2 + .../ru/m/tankz/view/frames/GameListFrame.hx | 9 ++- .../ru/m/tankz/view/frames/game/GameView.hx | 4 +- src/common/haxe/ru/m/tankz/PacketBuilder.hx | 5 ++ src/server/haxe/ru/m/tankz/server/Server.hx | 1 + .../haxe/ru/m/tankz/server/session/Session.hx | 67 +++++++++++++++++-- 7 files changed, 87 insertions(+), 9 deletions(-) mode change 100644 => 100755 src/client/haxe/ru/m/tankz/view/frames/GameListFrame.hx mode change 100644 => 100755 src/client/haxe/ru/m/tankz/view/frames/game/GameView.hx diff --git a/proto/base.proto b/proto/base.proto index 964cfe0..f56954a 100755 --- a/proto/base.proto +++ b/proto/base.proto @@ -54,4 +54,12 @@ message CreateGameRequest { message CreateGameResponse { required Game game = 1; +} + +message JoinGameRequest { + required int32 game_id = 1; +} + +message JoinGameResponse { + required Game game = 1; } \ No newline at end of file diff --git a/src/client/haxe/ru/m/tankz/data/GameData.hx b/src/client/haxe/ru/m/tankz/data/GameData.hx index 2891464..9c427a2 100755 --- a/src/client/haxe/ru/m/tankz/data/GameData.hx +++ b/src/client/haxe/ru/m/tankz/data/GameData.hx @@ -1,9 +1,11 @@ package ru.m.tankz.data; +import ru.m.tankz.proto.Game; import ru.m.tankz.proto.Person; import ru.m.tankz.proto.Account; class GameData { public var account:Account; public var person:Person; + public var game:Game; } diff --git a/src/client/haxe/ru/m/tankz/view/frames/GameListFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/GameListFrame.hx old mode 100644 new mode 100755 index ae0fb46..4a9d82a --- a/src/client/haxe/ru/m/tankz/view/frames/GameListFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameListFrame.hx @@ -1,5 +1,7 @@ package ru.m.tankz.view.frames; +import ru.m.tankz.proto.JoinGameResponse; +import ru.m.tankz.proto.JoinGameRequest; import ru.m.tankz.proto.CreateGameResponse; import ru.m.tankz.proto.CreateGameRequest; import ru.m.tankz.view.frames.game.GameView; @@ -44,7 +46,7 @@ class GameListFrame extends VGroupView implements IPacketHandler implements List public function onListItemClick(item:IListItemView):Void { L.d(TAG, "game selected: ", item.data.id); - Provider.get(IFrameSwitcher).change(GameFrame.ID); + Provider.get(IConnection).send(new JoinGameRequest().setGameId(item.data.id)); } public function onGamesResponse(packet:GamesResponse):Void { @@ -56,6 +58,11 @@ class GameListFrame extends VGroupView implements IPacketHandler implements List list.update(); } + public function onJoinGameResponse(packet:JoinGameResponse):Void { + Provider.get(GameData).game = packet.game; + Provider.get(IFrameSwitcher).change(GameFrame.ID); + } + public function onPacket(packet:Message):Void {} public function onPress(view:ButtonView):Void { diff --git a/src/client/haxe/ru/m/tankz/view/frames/game/GameView.hx b/src/client/haxe/ru/m/tankz/view/frames/game/GameView.hx old mode 100644 new mode 100755 index 2dc88cc..51f38de --- a/src/client/haxe/ru/m/tankz/view/frames/game/GameView.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/game/GameView.hx @@ -1,5 +1,6 @@ package ru.m.tankz.view.frames.game; +import ru.m.tankz.proto.Person; import ru.m.tankz.proto.Game; import haxework.gui.list.ListView.IListItemView; import haxework.gui.LabelView; @@ -25,7 +26,8 @@ class GameView extends HGroupView implements IListItemView { private function set_data(value:Game):Game { this.data = value; - nameLabel.text = "Game_" + this.data.id; + var persons:String = Lambda.map(this.data.persons, function(p:Person) return p.name).join(","); + nameLabel.text = "Game_" + this.data.id + "(" + persons + ")"; return this.data; } } \ No newline at end of file diff --git a/src/common/haxe/ru/m/tankz/PacketBuilder.hx b/src/common/haxe/ru/m/tankz/PacketBuilder.hx index b880771..b52c3a3 100755 --- a/src/common/haxe/ru/m/tankz/PacketBuilder.hx +++ b/src/common/haxe/ru/m/tankz/PacketBuilder.hx @@ -11,6 +11,8 @@ import ru.m.tankz.proto.GamesRequest; import ru.m.tankz.proto.GamesResponse; import ru.m.tankz.proto.CreateGameRequest; import ru.m.tankz.proto.CreateGameResponse; +import ru.m.tankz.proto.JoinGameRequest; +import ru.m.tankz.proto.JoinGameResponse; class PacketBuilder implements IPacketBuilder { @@ -29,6 +31,8 @@ class PacketBuilder implements IPacketBuilder { 0x0002 => GamesResponse, 0x0003 => CreateGameRequest, 0x0004 => CreateGameResponse, + 0x0005 => JoinGameRequest, + 0x0006 => JoinGameResponse, ] ]; @@ -47,6 +51,7 @@ class PacketBuilder implements IPacketBuilder { } } } + throw "Unsupported packet(" + Type.getClassName(Type.getClass(packet)).split(".").pop() + ")"; return null; } diff --git a/src/server/haxe/ru/m/tankz/server/Server.hx b/src/server/haxe/ru/m/tankz/server/Server.hx index 04460fb..f17f337 100755 --- a/src/server/haxe/ru/m/tankz/server/Server.hx +++ b/src/server/haxe/ru/m/tankz/server/Server.hx @@ -24,6 +24,7 @@ class Server extends ThreadServer { override function clientDisconnected(session:Session) { L.d(TAG, "Client disconnected"); + session.onDisconnected(); } override function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) { diff --git a/src/server/haxe/ru/m/tankz/server/session/Session.hx b/src/server/haxe/ru/m/tankz/server/session/Session.hx index 9e413f5..85a8dae 100755 --- a/src/server/haxe/ru/m/tankz/server/session/Session.hx +++ b/src/server/haxe/ru/m/tankz/server/session/Session.hx @@ -1,5 +1,8 @@ package ru.m.tankz.server.session; +import ru.m.tankz.proto.Person; +import ru.m.tankz.proto.JoinGameResponse; +import ru.m.tankz.proto.JoinGameRequest; import ru.m.tankz.proto.Game; import ru.m.tankz.proto.CreateGameResponse; import ru.m.tankz.proto.CreateGameRequest; @@ -19,13 +22,56 @@ import protohx.Message; import ru.m.core.connect.IConnection; import sys.net.Socket; + +class GameCenter { + + private var game_id:Int = 0; + private var games:Map; + private var persons:Map; + + public function new() { + games = new Map(); + persons = new Map(); + } + + public function getGames():Array { + return Lambda.array(games); + } + + public function createGame():Game { + var game:Game = new Game().setId(game_id++); + games.set(game.id, game); + return game; + } + + public function join(person:Person, gameId:Int):Game { + exit(person.id); + var game:Game = games.get(gameId); + game.addPersons(person); + persons.set(person.id, gameId); + return game; + } + + public function exit(personId):Void { + if (persons.exists(personId)) { + var game:Game = games.get(persons.get(personId)); + for (person in game.persons) if (person.id == personId) { + game.persons.remove(person); + break; + } + persons.remove(personId); + } + } +} + + class Session implements IConnectionHandler implements IPacketHandler { - private static var game_id:Int = 0; - private static var games:Array = new Array(); - + private static var games:GameCenter = new GameCenter(); public var account(default, null):Account; + public var person(default, null):Person; + public var gameId(default, null):Int = -1; private var connection(default, null):IConnection; private var socket:Socket; @@ -56,7 +102,9 @@ class Session implements IConnectionHandler implements IPacketHandler { } public function onDisconnected():Void { - + if (person != null) { + games.exit(person.id); + } } public function onError(error:Dynamic):Void { @@ -84,6 +132,7 @@ class Session implements IConnectionHandler implements IPacketHandler { var db = new Db(); var person = db.getPerson(account.id, packet.personId); if (person != null) { + this.person = person; connection.send(new PersonSelectResponse().setPerson(person)); } else { connection.send(new ErrorResponse().setCode(404).setMessage("Person not found")); @@ -91,15 +140,19 @@ class Session implements IConnectionHandler implements IPacketHandler { } public function onGamesRequest(packet:GamesRequest):Void { - connection.send(new GamesResponse().setGames(games)); + connection.send(new GamesResponse().setGames(games.getGames())); } public function onCreateGameRequest(packet:CreateGameRequest):Void { - var game = new Game().setId(game_id++); - games.push(game); + var game:Game = games.createGame(); connection.send(new CreateGameResponse().setGame(game)); } + public function onJoinGameRequest(packet:JoinGameRequest):Void { + var game:Game = games.join(person, packet.gameId); + connection.send(new JoinGameResponse().setGame(game)); + } + public function onPacket(packet:Message):Void { trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); }