This commit is contained in:
2014-08-29 12:55:34 +04:00
parent 17c3db46ba
commit 85826db097
4 changed files with 70 additions and 5 deletions

View File

@@ -42,8 +42,9 @@ enum GameState {
message Game { message Game {
required int32 id = 1; required int32 id = 1;
repeated Person persons = 2; required Person creator = 2;
required GameState state = 3; repeated Person persons = 3;
required GameState state = 4;
} }
message GamesRequest { message GamesRequest {
@@ -69,3 +70,19 @@ message JoinGameRequest {
message JoinGameResponse { message JoinGameResponse {
required Game game = 1; required Game game = 1;
} }
message StartGameRequest {
}
message StartGameResponse {
required Game game = 1;
}
message ExitGameRequest {
}
message ExitGameResponse {
}

View File

@@ -1,5 +1,11 @@
package ru.m.tankz.view.frames; 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 ru.m.tankz.data.GameData;
import protohx.Message; import protohx.Message;
import haxework.gui.ButtonView; import haxework.gui.ButtonView;
@@ -33,14 +39,23 @@ class GameReadyFrame extends VGroupView implements IPacketHandler {
list.data = Provider.get(GameData).game.persons; 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 onPacket(packet:Message):Void {}
public function onPress(view:ButtonView):Void { public function onPress(view:ButtonView):Void {
switch (view.id) { switch (view.id) {
case "start": case "start":
Provider.get(IConnection).send(new StartGameRequest());
case "exit": case "exit":
Provider.get(IConnection).send(new ExitGameRequest());
} }
} }
} }

View File

@@ -13,6 +13,10 @@ import ru.m.tankz.proto.CreateGameRequest;
import ru.m.tankz.proto.CreateGameResponse; import ru.m.tankz.proto.CreateGameResponse;
import ru.m.tankz.proto.JoinGameRequest; import ru.m.tankz.proto.JoinGameRequest;
import ru.m.tankz.proto.JoinGameResponse; 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 { class PacketBuilder implements IPacketBuilder {
@@ -33,6 +37,10 @@ class PacketBuilder implements IPacketBuilder {
0x0004 => CreateGameResponse, 0x0004 => CreateGameResponse,
0x0005 => JoinGameRequest, 0x0005 => JoinGameRequest,
0x0006 => JoinGameResponse, 0x0006 => JoinGameResponse,
0x0007 => StartGameRequest,
0x0008 => StartGameResponse,
0x0009 => ExitGameRequest,
0x000a => ExitGameResponse
] ]
]; ];

View File

@@ -1,5 +1,9 @@
package ru.m.tankz.server.session; 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.GameState;
import ru.m.tankz.proto.Person; import ru.m.tankz.proto.Person;
import ru.m.tankz.proto.JoinGameResponse; import ru.m.tankz.proto.JoinGameResponse;
@@ -28,10 +32,12 @@ class GameCenter {
private var game_id:Int = 0; private var game_id:Int = 0;
private var games:Map<Int, Game>; private var games:Map<Int, Game>;
private var created:Map<Int, Int>;
private var persons:Map<Int, Int>; private var persons:Map<Int, Int>;
public function new() { public function new() {
games = new Map<Int, Game>(); games = new Map<Int, Game>();
created = new Map<Int, Int>();
persons = new Map<Int, Int>(); persons = new Map<Int, Int>();
} }
@@ -39,9 +45,17 @@ class GameCenter {
return Lambda.array(games).filter(function(g) return g.state == GameState.READY); 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 { 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); games.set(game.id, game);
created.set(person.id, game.id);
join(person, game.id); join(person, game.id);
return game; return game;
} }
@@ -158,6 +172,17 @@ class Session implements IConnectionHandler implements IPacketHandler {
connection.send(new JoinGameResponse().setGame(game)); 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 { public function onPacket(packet:Message):Void {
trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop()); trace("Unknown packet: " + Type.getClassName(Type.getClass(packet)).split(".").pop());
} }