[proto] update
This commit is contained in:
@@ -1,20 +1,24 @@
|
|||||||
package ru.m.puzzlez.net;
|
package ru.m.puzzlez.net;
|
||||||
|
|
||||||
import haxe.Unserializer;
|
import haxe.Unserializer;
|
||||||
import com.hurlant.crypto.extra.UUID;
|
|
||||||
import com.hurlant.crypto.prng.Random;
|
|
||||||
import hw.connect.ConnectionFactory;
|
import hw.connect.ConnectionFactory;
|
||||||
import hw.connect.IConnection;
|
import hw.connect.IConnection;
|
||||||
import hw.signal.Signal;
|
import hw.signal.Signal;
|
||||||
import hw.storage.SharedObjectStorage;
|
import hw.storage.SharedObjectStorage;
|
||||||
|
import promhx.Promise;
|
||||||
|
import ru.m.data.IDataSource;
|
||||||
import ru.m.puzzlez.core.GameEvent;
|
import ru.m.puzzlez.core.GameEvent;
|
||||||
|
import ru.m.puzzlez.core.GameState;
|
||||||
|
import ru.m.puzzlez.core.Id;
|
||||||
import ru.m.puzzlez.proto.core.GameProto;
|
import ru.m.puzzlez.proto.core.GameProto;
|
||||||
import ru.m.puzzlez.proto.core.UserProto;
|
import ru.m.puzzlez.proto.core.UserProto;
|
||||||
|
import ru.m.puzzlez.proto.pack.GameCreateRequest;
|
||||||
|
import ru.m.puzzlez.proto.pack.GameJoinRequest;
|
||||||
import ru.m.puzzlez.proto.pack.LoginRequest;
|
import ru.m.puzzlez.proto.pack.LoginRequest;
|
||||||
import ru.m.puzzlez.proto.pack.Request;
|
import ru.m.puzzlez.proto.pack.Request;
|
||||||
import ru.m.puzzlez.proto.pack.Response;
|
import ru.m.puzzlez.proto.pack.Response;
|
||||||
|
|
||||||
@:provide class Network {
|
@:provide class Network implements IDataIndex<ImageId> {
|
||||||
public var user(default, null):UserProto;
|
public var user(default, null):UserProto;
|
||||||
public var userSignal(default, null):Signal<UserProto> = new Signal();
|
public var userSignal(default, null):Signal<UserProto> = new Signal();
|
||||||
|
|
||||||
@@ -33,11 +37,11 @@ import ru.m.puzzlez.proto.pack.Response;
|
|||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
gameList = [];
|
gameList = [];
|
||||||
storage = new SharedObjectStorage("netwok");
|
storage = new SharedObjectStorage("network");
|
||||||
if (storage.exists(USER_KEY)) {
|
if (storage.exists(USER_KEY)) {
|
||||||
user = storage.read(USER_KEY);
|
user = storage.read(USER_KEY);
|
||||||
} else {
|
} else {
|
||||||
user = new UserProto().setName('Anonimus #${UUID.generateRandom(new Random()).toString().split("-").shift()}');
|
user = new UserProto().setName('Anonimus #${IdUtil.generate()}');
|
||||||
storage.write(USER_KEY, user);
|
storage.write(USER_KEY, user);
|
||||||
}
|
}
|
||||||
connection = ConnectionFactory.buildClientConnection("127.0.0.1", 5000, Response);
|
connection = ConnectionFactory.buildClientConnection("127.0.0.1", 5000, Response);
|
||||||
@@ -50,6 +54,14 @@ import ru.m.puzzlez.proto.pack.Response;
|
|||||||
connection.send(new Request().setLogin(new LoginRequest().setUser(user)));
|
connection.send(new Request().setLogin(new LoginRequest().setUser(user)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function startGame(state:GameState):Void {
|
||||||
|
connection.send(new Request().setGameCreate(new GameCreateRequest().setImageId(state.preset.imageId)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function joinGame(state:GameState):Void {
|
||||||
|
connection.send(new Request().setGameJoin(new GameJoinRequest().setGameId(state.id)));
|
||||||
|
}
|
||||||
|
|
||||||
private function onConnectionChange(event:ConnectionEvent):Void {
|
private function onConnectionChange(event:ConnectionEvent):Void {
|
||||||
L.i("network", '${event}');
|
L.i("network", '${event}');
|
||||||
switch event {
|
switch event {
|
||||||
@@ -82,4 +94,14 @@ import ru.m.puzzlez.proto.pack.Response;
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getIndexPage(page:Page):Promise<DataPage<ImageId>> {
|
||||||
|
// ToDo: return gameList
|
||||||
|
var data = {
|
||||||
|
page: page,
|
||||||
|
total: 1,
|
||||||
|
data: [new ImageId('asset', 'resources/image/raccoon.jpg')],
|
||||||
|
}
|
||||||
|
return Promise.promise(data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/app/haxe/ru/m/puzzlez/net/NetworkGame.hx
Normal file
41
src/app/haxe/ru/m/puzzlez/net/NetworkGame.hx
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package ru.m.puzzlez.net;
|
||||||
|
|
||||||
|
import hw.signal.Signal;
|
||||||
|
import ru.m.puzzlez.core.GameState;
|
||||||
|
import ru.m.puzzlez.core.GameEvent;
|
||||||
|
import ru.m.puzzlez.core.IGame;
|
||||||
|
|
||||||
|
class NetworkGame implements IGame {
|
||||||
|
public var state(default, null):GameState;
|
||||||
|
public var signal(default, null):Signal<GameEvent>;
|
||||||
|
|
||||||
|
@:provide private var network:Network;
|
||||||
|
|
||||||
|
public function new(state:GameState) {
|
||||||
|
this.state = state;
|
||||||
|
signal = new Signal();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onEvent(event:GameEvent):Void {
|
||||||
|
signal.emit(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function start():Void {
|
||||||
|
network.gameEventSignal.connect(onEvent);
|
||||||
|
switch state.status {
|
||||||
|
case READY:
|
||||||
|
network.startGame(state);
|
||||||
|
case _:
|
||||||
|
network.joinGame(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stop():Void {
|
||||||
|
network.gameEventSignal.disconnect(onEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dispose():Void {
|
||||||
|
stop();
|
||||||
|
signal.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import ru.m.puzzlez.core.Game;
|
|||||||
import ru.m.puzzlez.core.GameEvent;
|
import ru.m.puzzlez.core.GameEvent;
|
||||||
import ru.m.puzzlez.core.GameState;
|
import ru.m.puzzlez.core.GameState;
|
||||||
import ru.m.puzzlez.core.IGame;
|
import ru.m.puzzlez.core.IGame;
|
||||||
|
import ru.m.puzzlez.net.NetworkGame;
|
||||||
import ru.m.puzzlez.render.IRender;
|
import ru.m.puzzlez.render.IRender;
|
||||||
import ru.m.puzzlez.storage.GameStorage;
|
import ru.m.puzzlez.storage.GameStorage;
|
||||||
import ru.m.puzzlez.storage.SettingsStorage;
|
import ru.m.puzzlez.storage.SettingsStorage;
|
||||||
@@ -32,7 +33,11 @@ import ru.m.puzzlez.view.popup.PreviewPopup;
|
|||||||
|
|
||||||
override public function onShow(state:GameState):Void {
|
override public function onShow(state:GameState):Void {
|
||||||
onHide();
|
onHide();
|
||||||
|
if (state.online) {
|
||||||
|
game = new NetworkGame(state);
|
||||||
|
} else {
|
||||||
game = new Game(state);
|
game = new Game(state);
|
||||||
|
}
|
||||||
game.signal.connect(render.onGameEvent);
|
game.signal.connect(render.onGameEvent);
|
||||||
game.signal.connect(onGameEvent);
|
game.signal.connect(onGameEvent);
|
||||||
render.signal.connect(game.signal.emit);
|
render.signal.connect(game.signal.emit);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.puzzlez.view;
|
package ru.m.puzzlez.view;
|
||||||
|
|
||||||
|
import ru.m.puzzlez.net.Network;
|
||||||
import hw.geom.IntPoint;
|
import hw.geom.IntPoint;
|
||||||
import hw.view.data.DataView;
|
import hw.view.data.DataView;
|
||||||
import hw.view.form.ToggleButtonView;
|
import hw.view.form.ToggleButtonView;
|
||||||
@@ -18,6 +19,7 @@ import ru.m.puzzlez.view.common.PresetView;
|
|||||||
|
|
||||||
@:provide var imageStorage:ImageStorage;
|
@:provide var imageStorage:ImageStorage;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
|
@:provide var network:Network;
|
||||||
|
|
||||||
private var imageId:ImageId;
|
private var imageId:ImageId;
|
||||||
|
|
||||||
@@ -53,7 +55,8 @@ import ru.m.puzzlez.view.common.PresetView;
|
|||||||
selectSize(sizesView.data[sizesView.data.length - 1]);
|
selectSize(sizesView.data[sizesView.data.length - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function start():Void {
|
private function start(online:Bool = false):Void {
|
||||||
|
imageView.state.online = online;
|
||||||
switcher.change(GameFrame.ID, imageView.state);
|
switcher.change(GameFrame.ID, imageView.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,11 @@ views:
|
|||||||
geometry.margin.left: 15
|
geometry.margin.left: 15
|
||||||
text: Start
|
text: Start
|
||||||
+onPress: ~start()
|
+onPress: ~start()
|
||||||
|
- $type: hw.view.form.ButtonView
|
||||||
|
style: button.active
|
||||||
|
geometry.margin.left: 15
|
||||||
|
text: Network
|
||||||
|
+onPress: ~start(true)
|
||||||
- id: image
|
- id: image
|
||||||
$type: ru.m.puzzlez.view.common.PresetView
|
$type: ru.m.puzzlez.view.common.PresetView
|
||||||
geometry.stretch: true
|
geometry.stretch: true
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.puzzlez.view;
|
package ru.m.puzzlez.view;
|
||||||
|
|
||||||
|
import ru.m.puzzlez.net.Network;
|
||||||
import hw.view.data.DataView;
|
import hw.view.data.DataView;
|
||||||
import hw.view.form.ButtonView;
|
import hw.view.form.ButtonView;
|
||||||
import hw.view.frame.FrameSwitcher;
|
import hw.view.frame.FrameSwitcher;
|
||||||
@@ -26,6 +27,7 @@ import ru.m.update.Updater;
|
|||||||
@:provide var storage:ImageStorage;
|
@:provide var storage:ImageStorage;
|
||||||
@:provide var switcher:FrameSwitcher;
|
@:provide var switcher:FrameSwitcher;
|
||||||
@:provide var gameStorage:GameStorage;
|
@:provide var gameStorage:GameStorage;
|
||||||
|
@:provide var network:Network;
|
||||||
@:provide static var appUpdater:Updater;
|
@:provide static var appUpdater:Updater;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
@@ -75,7 +77,11 @@ import ru.m.update.Updater;
|
|||||||
switcher.change(ImageListFrame.ID, source);
|
switcher.change(ImageListFrame.ID, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function games(status:GameStatus):Void {
|
private function openGames(status:GameStatus):Void {
|
||||||
switcher.change(ImageListFrame.ID, gameStorage.statusSource(status));
|
switcher.change(ImageListFrame.ID, gameStorage.statusSource(status));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function openNetwork():Void {
|
||||||
|
switcher.change(ImageListFrame.ID, {title: 'Network', source: network});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,11 +35,15 @@ views:
|
|||||||
- id: load
|
- id: load
|
||||||
$type: hw.view.form.ButtonView
|
$type: hw.view.form.ButtonView
|
||||||
text: Load
|
text: Load
|
||||||
+onPress: ~games('started')
|
+onPress: ~openGames('started')
|
||||||
- id: complete
|
- id: complete
|
||||||
$type: hw.view.form.ButtonView
|
$type: hw.view.form.ButtonView
|
||||||
text: Complete
|
text: Complete
|
||||||
+onPress: ~games('complete')
|
+onPress: ~openGames('complete')
|
||||||
|
- id: network
|
||||||
|
$type: hw.view.form.ButtonView
|
||||||
|
text: Network
|
||||||
|
+onPress: ~openNetwork()
|
||||||
- $type: hw.view.SpriteView
|
- $type: hw.view.SpriteView
|
||||||
geometry.width: 100%
|
geometry.width: 100%
|
||||||
- $type: hw.view.form.LabelView
|
- $type: hw.view.form.LabelView
|
||||||
|
|||||||
10
src/common/haxe/ru/m/IdUtil.hx
Normal file
10
src/common/haxe/ru/m/IdUtil.hx
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package ru.m;
|
||||||
|
|
||||||
|
import com.hurlant.crypto.prng.Random;
|
||||||
|
import com.hurlant.crypto.extra.UUID;
|
||||||
|
|
||||||
|
class IdUtil {
|
||||||
|
public static function generate():String {
|
||||||
|
return UUID.generateRandom(new Random()).toString().split("-").shift();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,9 @@ enum abstract GameStatus(String) from String to String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
typedef GameState = {
|
typedef GameState = {
|
||||||
|
var id:String;
|
||||||
var status:GameStatus;
|
var status:GameStatus;
|
||||||
var preset:GamePreset;
|
var preset:GamePreset;
|
||||||
var parts:Array<Part>;
|
var parts:Array<Part>;
|
||||||
|
@:optional var online:Bool;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ class GameUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
|
id: IdUtil.generate(),
|
||||||
status: READY,
|
status: READY,
|
||||||
preset: preset,
|
preset: preset,
|
||||||
parts: parts,
|
parts: parts,
|
||||||
|
|||||||
@@ -10,7 +10,5 @@ interface IGame {
|
|||||||
|
|
||||||
public function stop():Void;
|
public function stop():Void;
|
||||||
|
|
||||||
public function shuffle():Void;
|
|
||||||
|
|
||||||
public function dispose():Void;
|
public function dispose():Void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,29 @@ message UserProto {
|
|||||||
string name = 2;
|
string name = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message PuzzlezPartProto {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
message PuzzlezGridProto {
|
||||||
|
int32 width = 1;
|
||||||
|
int32 height = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GamePresetProto {
|
||||||
|
string imageId = 1;
|
||||||
|
PuzzlezGridProto grid = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GameStateProto {
|
||||||
|
string id = 1;
|
||||||
|
GamePresetProto preset = 2;
|
||||||
|
repeated PuzzlezPartProto parts = 3;
|
||||||
|
}
|
||||||
|
|
||||||
message GameProto {
|
message GameProto {
|
||||||
int32 id = 1;
|
GameStateProto state = 2;
|
||||||
string imageId = 2;
|
repeated UserProto users = 3;
|
||||||
string status = 3;
|
|
||||||
repeated UserProto users = 4;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message GameEventProto {
|
message GameEventProto {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ message GameCreateRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
message GameJoinRequest {
|
message GameJoinRequest {
|
||||||
int32 gameId = 1;
|
string gameId = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
message GameLeaveRequest {}
|
message GameLeaveRequest {}
|
||||||
@@ -57,7 +57,7 @@ message Request {
|
|||||||
LogoutRequest logout = 2;
|
LogoutRequest logout = 2;
|
||||||
|
|
||||||
GameCreateRequest gameCreate = 10;
|
GameCreateRequest gameCreate = 10;
|
||||||
GameLeaveRequest gameJoin = 11;
|
GameJoinRequest gameJoin = 11;
|
||||||
GameLeaveRequest gameLeave = 12;
|
GameLeaveRequest gameLeave = 12;
|
||||||
|
|
||||||
GameListRequest gameList = 20;
|
GameListRequest gameList = 20;
|
||||||
|
|||||||
@@ -27,18 +27,17 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
@:provide static var gameManager:IGameManager;
|
@:provide static var gameManager:IGameManager;
|
||||||
|
|
||||||
public var user(default, null):UserProto;
|
public var user(default, null):UserProto;
|
||||||
public var gameId(default, null):Int;
|
public var gameId(default, null):String;
|
||||||
|
|
||||||
private var subscribed:Bool;
|
private var subscribed:Bool;
|
||||||
private var tag(get, never):String;
|
private var tag(get, never):String;
|
||||||
|
|
||||||
private function get_tag():String {
|
private function get_tag():String {
|
||||||
return '[${id}|${user == null ? '-' : user.name}|${gameId == -1 ? '-' : Std.string(gameId)}]';
|
return '[${id}|${user == null ? '-' : user.name}|${gameId == null ? '-' : gameId}]';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function new(socket:Socket) {
|
public function new(socket:Socket) {
|
||||||
super(socket, Request);
|
super(socket, Request);
|
||||||
gameId = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sendError(code:Int, message:String):Void {
|
private function sendError(code:Int, message:String):Void {
|
||||||
@@ -60,7 +59,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function logout(leave:Bool = true):Void {
|
private function logout(leave:Bool = true):Void {
|
||||||
gameId = -1;
|
gameId = null;
|
||||||
gameManager.disconnect(this);
|
gameManager.disconnect(this);
|
||||||
if (user != null && leave) {
|
if (user != null && leave) {
|
||||||
gameManager.leave(user);
|
gameManager.leave(user);
|
||||||
@@ -68,13 +67,10 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function join(gameId:Int, restore:Bool):Void {
|
private function join(gameId:String):Void {
|
||||||
this.gameId = gameId;
|
this.gameId = gameId;
|
||||||
gameManager.join(gameId, user);
|
gameManager.join(gameId, user);
|
||||||
var game = gameManager.gamesById[gameId];
|
var game = gameManager.gamesById[gameId];
|
||||||
if (restore) {
|
|
||||||
// ToDo: restore
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override private function onRequest(request:Request):Void {
|
override private function onRequest(request:Request):Void {
|
||||||
@@ -86,41 +82,35 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
// login
|
// login
|
||||||
if (request.hasLogin()) {
|
if (request.hasLogin()) {
|
||||||
user = new UserProto()
|
user = new UserProto()
|
||||||
.setUuid(request.login.uuid != null ? request.login.uuid : UUID.generateRandom(new Random()).toString())
|
.setUuid(request.login.user.uuid != null ? request.login.user.uuid : UUID.generateRandom(new Random()).toString())
|
||||||
.setName(request.login.name);
|
.setName(request.login.user.name);
|
||||||
gameManager.connect(this);
|
gameManager.connect(this);
|
||||||
send(new Response().setLogin(new LoginResponse().setUser(user)));
|
send(new Response().setLogin(new LoginResponse().setUser(user)));
|
||||||
if (gameManager.gamesByUser.exists(user.uuid)) {
|
if (gameManager.gamesByUser.exists(user.uuid)) {
|
||||||
join(gameManager.gamesByUser[user.uuid].id, false);
|
join(gameManager.gamesByUser[user.uuid].id);
|
||||||
}
|
}
|
||||||
// logout
|
// logout
|
||||||
} else if (request.hasLogout()) {
|
} else if (request.hasLogout()) {
|
||||||
logout();
|
logout();
|
||||||
send(new Response().setLogout(new LogoutResponse()));
|
send(new Response().setLogout(new LogoutResponse()));
|
||||||
// room
|
// game
|
||||||
} else if (request.hasRoom()) {
|
} else if (request.hasGameCreate()) {
|
||||||
if (request.room.hasCreate()) {
|
var game = gameManager.create(user, request.gameCreate.imageId);
|
||||||
var game = gameManager.create(user);
|
|
||||||
gameId = game.id;
|
gameId = game.id;
|
||||||
send(new Response().setRoom(new RoomResponse().setRoom(game.room)));
|
send(new Response().setGame(new GameResponse().setGame(game.proto)));
|
||||||
} else if (request.room.hasJoin()) {
|
} else if (request.hasGameJoin()) {
|
||||||
join(request.room.join.gameId, request.room.join.restore);
|
join(request.gameJoin.gameId);
|
||||||
} else if (request.room.hasLeave()) {
|
} else if (request.hasGameLeave()) {
|
||||||
gameManager.leave(user);
|
gameManager.leave(user);
|
||||||
} else if (request.room.hasSlot()) {
|
// game list
|
||||||
gameManager.slot(user, request.room.slot.slot);
|
} else if (request.hasGameList()) {
|
||||||
} else if (request.room.hasStart()) {
|
subscribed = request.gameList.subscribe;
|
||||||
gameManager.start(gameId);
|
|
||||||
}
|
|
||||||
// room list
|
|
||||||
} else if (request.hasRoomList()) {
|
|
||||||
subscribed = request.roomList.subscribe;
|
|
||||||
if (subscribed) {
|
if (subscribed) {
|
||||||
send(new Response().setRoomList(listGame()));
|
send(new Response().setGameList(listGame()));
|
||||||
}
|
}
|
||||||
} else if (request.hasGameEvent()) {
|
} else if (request.hasGameEvent()) {
|
||||||
if (gameManager.gamesById.exists(gameId)) {
|
if (gameManager.gamesById.exists(gameId)) {
|
||||||
var event:GameEvent = Unserializer.run(request.gameEvent.event.event);
|
var events:Array<GameEvent> = cast request.gameEvent.events.map(item -> Unserializer.run(item.event));
|
||||||
// ToDo: emit event
|
// ToDo: emit event
|
||||||
///gameManager.gamesById[gameId].gameEventSignal.emit(event);
|
///gameManager.gamesById[gameId].gameEventSignal.emit(event);
|
||||||
}
|
}
|
||||||
@@ -148,7 +138,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
switch change {
|
switch change {
|
||||||
case LEAVE(user):
|
case LEAVE(user):
|
||||||
if (user.uuid == this.user.uuid) {
|
if (user.uuid == this.user.uuid) {
|
||||||
gameId = -1;
|
gameId = null;
|
||||||
send(new Response().setGame(new GameResponse()));
|
send(new Response().setGame(new GameResponse()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -163,7 +153,7 @@ class GameSession extends ProtoSession<Response, Request> implements GameManager
|
|||||||
|
|
||||||
public function onDelete(game:ServerGame):Void {
|
public function onDelete(game:ServerGame):Void {
|
||||||
if (gameId == game.id) {
|
if (gameId == game.id) {
|
||||||
gameId = -1;
|
gameId = null;
|
||||||
send(new Response().setGame(new GameResponse()));
|
send(new Response().setGame(new GameResponse()));
|
||||||
}
|
}
|
||||||
if (subscribed) {
|
if (subscribed) {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package ru.m.puzzlez.game;
|
package ru.m.puzzlez.game;
|
||||||
|
|
||||||
|
import ru.m.puzzlez.proto.core.GamePresetProto;
|
||||||
|
import ru.m.puzzlez.proto.core.GameStateProto;
|
||||||
|
import ru.m.puzzlez.core.GameState;
|
||||||
import ru.m.puzzlez.core.Id;
|
import ru.m.puzzlez.core.Id;
|
||||||
import ru.m.puzzlez.core.GameEvent;
|
import ru.m.puzzlez.core.GameEvent;
|
||||||
import ru.m.puzzlez.game.IGameManager;
|
import ru.m.puzzlez.game.IGameManager;
|
||||||
@@ -34,24 +37,21 @@ class _GameListener implements GameListener {
|
|||||||
|
|
||||||
@:dispatcher(GameManagerListener) class GameManager implements IGameManager {
|
@:dispatcher(GameManagerListener) class GameManager implements IGameManager {
|
||||||
public var games(default, null):Array<ServerGame>;
|
public var games(default, null):Array<ServerGame>;
|
||||||
public var gamesById(default, null):Map<Int, ServerGame>;
|
public var gamesById(default, null):Map<String, ServerGame>;
|
||||||
public var gamesByUser(default, null):Map<String, ServerGame>;
|
public var gamesByUser(default, null):Map<String, ServerGame>;
|
||||||
|
|
||||||
private var counter:Int;
|
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
counter = 0;
|
|
||||||
games = [];
|
games = [];
|
||||||
gamesById = new Map();
|
gamesById = new Map();
|
||||||
gamesByUser = new Map();
|
gamesByUser = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create(user:UserProto, imageId:ImageId):ServerGame {
|
public function create(user:UserProto, imageId:ImageId):ServerGame {
|
||||||
if (gamesByCreator.exists(user.uuid)) {
|
var gameProto = new GameProto().setState(
|
||||||
delete(gamesByCreator[user.uuid].id);
|
new GameStateProto()
|
||||||
}
|
.setId(IdUtil.generate())
|
||||||
var gameProto = new GameProto().setId(++counter).setImageId(imageId);
|
.setPreset(new GamePresetProto().setImageId(imageId))
|
||||||
gameProto.users.push(user);
|
);
|
||||||
var game = new ServerGame(gameProto);
|
var game = new ServerGame(gameProto);
|
||||||
games.push(game);
|
games.push(game);
|
||||||
gamesById[game.id] = game;
|
gamesById[game.id] = game;
|
||||||
@@ -60,7 +60,7 @@ class _GameListener implements GameListener {
|
|||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function join(gameId:Int, user:UserProto):Void {
|
public function join(gameId:String, user:UserProto):Void {
|
||||||
if (gamesById.exists(gameId)) {
|
if (gamesById.exists(gameId)) {
|
||||||
var game = gamesById[gameId];
|
var game = gamesById[gameId];
|
||||||
game.join(user);
|
game.join(user);
|
||||||
@@ -69,7 +69,7 @@ class _GameListener implements GameListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete(gameId:Int):Void {
|
public function delete(gameId:String):Void {
|
||||||
if (gamesById.exists(gameId)) {
|
if (gamesById.exists(gameId)) {
|
||||||
var game = gamesById[gameId];
|
var game = gamesById[gameId];
|
||||||
games.remove(game);
|
games.remove(game);
|
||||||
@@ -87,7 +87,7 @@ class _GameListener implements GameListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(gameId:Int):Void {
|
public function start(gameId:String):Void {
|
||||||
if (gamesById.exists(gameId)) {
|
if (gamesById.exists(gameId)) {
|
||||||
var game:ServerGame = gamesById[gameId];
|
var game:ServerGame = gamesById[gameId];
|
||||||
changeSignal.emit(game, START);
|
changeSignal.emit(game, START);
|
||||||
|
|||||||
@@ -20,8 +20,7 @@ interface GameManagerListener {
|
|||||||
|
|
||||||
@:provide(GameManager) interface IGameManager {
|
@:provide(GameManager) interface IGameManager {
|
||||||
public var games(default, null):Array<ServerGame>;
|
public var games(default, null):Array<ServerGame>;
|
||||||
public var gamesById(default, null):Map<Int, ServerGame>;
|
public var gamesById(default, null):Map<String, ServerGame>;
|
||||||
public var gamesByCreator(default, null):Map<String, ServerGame>;
|
|
||||||
public var gamesByUser(default, null):Map<String, ServerGame>;
|
public var gamesByUser(default, null):Map<String, ServerGame>;
|
||||||
|
|
||||||
private var createSignal(default, null):Signal<ServerGame>;
|
private var createSignal(default, null):Signal<ServerGame>;
|
||||||
@@ -35,8 +34,8 @@ interface GameManagerListener {
|
|||||||
public function disconnect(listener:GameManagerListener):Void;
|
public function disconnect(listener:GameManagerListener):Void;
|
||||||
|
|
||||||
public function create(user:UserProto, imageId:ImageId):ServerGame;
|
public function create(user:UserProto, imageId:ImageId):ServerGame;
|
||||||
public function delete(gameId:Int):Void;
|
public function delete(gameId:String):Void;
|
||||||
public function join(gameId:Int, user:UserProto):Void;
|
public function join(gameId:String, user:UserProto):Void;
|
||||||
public function leave(user:UserProto):Void;
|
public function leave(user:UserProto):Void;
|
||||||
public function start(gameId:Int):Void;
|
public function start(gameId:String):Void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.puzzlez.game;
|
package ru.m.puzzlez.game;
|
||||||
|
|
||||||
|
import ru.m.puzzlez.core.GameUtil;
|
||||||
import haxe.Timer;
|
import haxe.Timer;
|
||||||
import ru.m.puzzlez.core.Game;
|
import ru.m.puzzlez.core.Game;
|
||||||
import ru.m.puzzlez.proto.core.GameProto;
|
import ru.m.puzzlez.proto.core.GameProto;
|
||||||
@@ -7,17 +8,19 @@ import ru.m.puzzlez.proto.core.UserProto;
|
|||||||
|
|
||||||
@:dispatcher(GameListener) class ServerGame extends Game {
|
@:dispatcher(GameListener) class ServerGame extends Game {
|
||||||
|
|
||||||
public var id(get, null):Int;
|
public var id(get, null):String;
|
||||||
public var proto(default, default):GameProto;
|
public var proto(default, default):GameProto;
|
||||||
|
|
||||||
private var timer:Timer;
|
private var timer:Timer;
|
||||||
|
|
||||||
public function new(proto:GameProto) {
|
public function new(proto:GameProto) {
|
||||||
|
// ToDo:
|
||||||
|
super(GameUtil.buildState(GameUtil.buildPreset(proto.state.preset.imageId)));
|
||||||
this.proto = proto;
|
this.proto = proto;
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function get_id():Int {
|
private inline function get_id():String {
|
||||||
return proto.id;
|
return proto.state.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function contains(user:UserProto):Bool {
|
public function contains(user:UserProto):Bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user