From 85826db097c12d7a1856bf5ea0200298dd9ddbf4 Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 29 Aug 2014 12:55:34 +0400 Subject: [PATCH] - --- proto/base.proto | 21 +++++++++++++-- .../ru/m/tankz/view/frames/GameReadyFrame.hx | 19 +++++++++++-- src/common/haxe/ru/m/tankz/PacketBuilder.hx | 8 ++++++ .../haxe/ru/m/tankz/server/session/Session.hx | 27 ++++++++++++++++++- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/proto/base.proto b/proto/base.proto index 50704e9..0ed8def 100755 --- a/proto/base.proto +++ b/proto/base.proto @@ -42,8 +42,9 @@ enum GameState { message Game { required int32 id = 1; - repeated Person persons = 2; - required GameState state = 3; + required Person creator = 2; + repeated Person persons = 3; + required GameState state = 4; } message GamesRequest { @@ -68,4 +69,20 @@ message JoinGameRequest { message JoinGameResponse { required Game game = 1; +} + +message StartGameRequest { + +} + +message StartGameResponse { + required Game game = 1; +} + +message ExitGameRequest { + +} + +message ExitGameResponse { + } \ No newline at end of file diff --git a/src/client/haxe/ru/m/tankz/view/frames/GameReadyFrame.hx b/src/client/haxe/ru/m/tankz/view/frames/GameReadyFrame.hx index 4fb0dc9..3e67004 100755 --- a/src/client/haxe/ru/m/tankz/view/frames/GameReadyFrame.hx +++ b/src/client/haxe/ru/m/tankz/view/frames/GameReadyFrame.hx @@ -1,5 +1,11 @@ package ru.m.tankz.view.frames; +import ru.m.tankz.proto.ExitGameResponse; +import haxework.frame.IFrameSwitcher; +import ru.m.tankz.proto.GamesResponse; +import ru.m.tankz.proto.ExitGameRequest; +import ru.m.tankz.proto.StartGameResponse; +import ru.m.tankz.proto.StartGameRequest; import ru.m.tankz.data.GameData; import protohx.Message; import haxework.gui.ButtonView; @@ -33,14 +39,23 @@ class GameReadyFrame extends VGroupView implements IPacketHandler { list.data = Provider.get(GameData).game.persons; } + public function onStartGameResponse(packet:StartGameResponse):Void { + Provider.get(GameData).game = packet.game; + Provider.get(IFrameSwitcher).change(GameFrame.ID); + } + + public function onExitGameResponse(packet:ExitGameResponse):Void { + Provider.get(IFrameSwitcher).change(GameListFrame.ID); + } + public function onPacket(packet:Message):Void {} public function onPress(view:ButtonView):Void { switch (view.id) { case "start": - + Provider.get(IConnection).send(new StartGameRequest()); case "exit": - + Provider.get(IConnection).send(new ExitGameRequest()); } } } diff --git a/src/common/haxe/ru/m/tankz/PacketBuilder.hx b/src/common/haxe/ru/m/tankz/PacketBuilder.hx index b52c3a3..097071f 100755 --- a/src/common/haxe/ru/m/tankz/PacketBuilder.hx +++ b/src/common/haxe/ru/m/tankz/PacketBuilder.hx @@ -13,6 +13,10 @@ import ru.m.tankz.proto.CreateGameRequest; import ru.m.tankz.proto.CreateGameResponse; import ru.m.tankz.proto.JoinGameRequest; import ru.m.tankz.proto.JoinGameResponse; +import ru.m.tankz.proto.StartGameRequest; +import ru.m.tankz.proto.StartGameResponse; +import ru.m.tankz.proto.ExitGameRequest; +import ru.m.tankz.proto.ExitGameResponse; class PacketBuilder implements IPacketBuilder { @@ -33,6 +37,10 @@ class PacketBuilder implements IPacketBuilder { 0x0004 => CreateGameResponse, 0x0005 => JoinGameRequest, 0x0006 => JoinGameResponse, + 0x0007 => StartGameRequest, + 0x0008 => StartGameResponse, + 0x0009 => ExitGameRequest, + 0x000a => ExitGameResponse ] ]; 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 e312efa..8b79c2c 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,9 @@ package ru.m.tankz.server.session; +import ru.m.tankz.proto.ExitGameResponse; +import ru.m.tankz.proto.ExitGameRequest; +import ru.m.tankz.proto.StartGameRequest; +import ru.m.tankz.proto.StartGameResponse; import ru.m.tankz.proto.GameState; import ru.m.tankz.proto.Person; import ru.m.tankz.proto.JoinGameResponse; @@ -28,10 +32,12 @@ class GameCenter { private var game_id:Int = 0; private var games:Map; + private var created:Map; private var persons:Map; public function new() { games = new Map(); + created = new Map(); persons = new Map(); } @@ -39,9 +45,17 @@ class GameCenter { return Lambda.array(games).filter(function(g) return g.state == GameState.READY); } + public function getCreatedGame(person_id:Int):Game { + return games.get(created.get(person_id)); + } + public function createGame(person:Person):Game { - var game:Game = new Game().setId(game_id++).setState(GameState.READY); + var game:Game = new Game() + .setId(game_id++) + .setState(GameState.READY) + .setCreator(person); games.set(game.id, game); + created.set(person.id, game.id); join(person, game.id); return game; } @@ -158,6 +172,17 @@ class Session implements IConnectionHandler implements IPacketHandler { connection.send(new JoinGameResponse().setGame(game)); } + public function onStartGameRequest(packet:StartGameRequest):Void { + var game:Game = games.getCreatedGame(person.id); + game.setState(GameState.STARTED); + connection.send(new StartGameResponse().setGame(game)); + } + + public function onExitGameRequest(packet:ExitGameRequest):Void { + games.exit(person.id); + connection.send(new ExitGameResponse()); + } + public function onPacket(packet:Message):Void { trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); }