-
This commit is contained in:
@@ -55,3 +55,11 @@ message CreateGameRequest {
|
||||
message CreateGameResponse {
|
||||
required Game game = 1;
|
||||
}
|
||||
|
||||
message JoinGameRequest {
|
||||
required int32 game_id = 1;
|
||||
}
|
||||
|
||||
message JoinGameResponse {
|
||||
required Game game = 1;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
9
src/client/haxe/ru/m/tankz/view/frames/GameListFrame.hx
Normal file → Executable file
9
src/client/haxe/ru/m/tankz/view/frames/GameListFrame.hx
Normal file → Executable file
@@ -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<Game>):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 {
|
||||
|
||||
4
src/client/haxe/ru/m/tankz/view/frames/game/GameView.hx
Normal file → Executable file
4
src/client/haxe/ru/m/tankz/view/frames/game/GameView.hx
Normal file → Executable file
@@ -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<Game> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ class Server extends ThreadServer<Session, Bytes> {
|
||||
|
||||
override function clientDisconnected(session:Session) {
|
||||
L.d(TAG, "Client disconnected");
|
||||
session.onDisconnected();
|
||||
}
|
||||
|
||||
override function readClientMessage(session:Session, buf:Bytes, pos:Int, len:Int) {
|
||||
|
||||
@@ -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<Int, Game>;
|
||||
private var persons:Map<Int, Int>;
|
||||
|
||||
public function new() {
|
||||
games = new Map<Int, Game>();
|
||||
persons = new Map<Int, Int>();
|
||||
}
|
||||
|
||||
public function getGames():Array<Game> {
|
||||
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<Game> = new Array<Game>();
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user