diff --git a/protohx.json b/protohx.json index b2f6db9..f1d36aa 100755 --- a/protohx.json +++ b/protohx.json @@ -2,6 +2,8 @@ "protoPath": "src/common/proto", "protoFiles": [ "src/common/proto/core.proto", + "src/common/proto/game.proto", + "src/common/proto/event.proto", "src/common/proto/pack.proto" ], "cleanOut": true, diff --git a/src/app/haxe/ru/m/puzzlez/net/Network.hx b/src/app/haxe/ru/m/puzzlez/net/Network.hx index 04c372e..08a3344 100644 --- a/src/app/haxe/ru/m/puzzlez/net/Network.hx +++ b/src/app/haxe/ru/m/puzzlez/net/Network.hx @@ -10,7 +10,7 @@ import ru.m.data.IDataSource; 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.game.GameProto; import ru.m.puzzlez.proto.core.UserProto; import ru.m.puzzlez.proto.pack.GameCreateRequest; import ru.m.puzzlez.proto.pack.GameJoinRequest; @@ -62,6 +62,11 @@ import ru.m.puzzlez.proto.pack.Response; connection.send(new Request().setGameJoin(new GameJoinRequest().setGameId(state.id))); } + public function sendGameAction(action:GameAction):Void { + // ToDo: send action + //connection.send(new Request().setGameEvent()); + } + private function onConnectionChange(event:ConnectionEvent):Void { L.i("network", '${event}'); switch event { @@ -90,7 +95,8 @@ import ru.m.puzzlez.proto.pack.Response; gameSignal.emit(game); } else if (packet.hasGameEvent()) { for (event in packet.gameEvent.events) { - gameEventSignal.emit(Unserializer.run(event.event)); + // ToDo: convert event? + //gameEventSignal.emit(Unserializer.run(event.event)); } } } diff --git a/src/app/haxe/ru/m/puzzlez/net/NetworkGame.hx b/src/app/haxe/ru/m/puzzlez/net/NetworkGame.hx index 75762a4..72850cc 100644 --- a/src/app/haxe/ru/m/puzzlez/net/NetworkGame.hx +++ b/src/app/haxe/ru/m/puzzlez/net/NetworkGame.hx @@ -7,17 +7,17 @@ import ru.m.puzzlez.core.IGame; class NetworkGame implements IGame { public var state(default, null):GameState; - public var signal(default, null):Signal; + public var events(default, null):Signal; @:provide private var network:Network; public function new(state:GameState) { this.state = state; - signal = new Signal(); + events = new Signal(); } - private function onEvent(event:GameEvent):Void { - signal.emit(event); + public function action(action:GameAction):Void { + network.sendGameAction(action); } public function start():Void { @@ -36,6 +36,10 @@ class NetworkGame implements IGame { public function dispose():Void { stop(); - signal.dispose(); + events.dispose(); + } + + private function onEvent(event:GameEvent):Void { + events.emit(event); } } diff --git a/src/app/haxe/ru/m/puzzlez/render/IRender.hx b/src/app/haxe/ru/m/puzzlez/render/IRender.hx index 3c0e65a..cff1f90 100644 --- a/src/app/haxe/ru/m/puzzlez/render/IRender.hx +++ b/src/app/haxe/ru/m/puzzlez/render/IRender.hx @@ -5,7 +5,7 @@ import hw.view.IView; import ru.m.puzzlez.core.GameEvent; interface IRender extends IView { - public var signal(default, null):Signal; + public var actions(default, null):Signal; public var scale(get, set):Float; public var manager(default, null):RenderManager; diff --git a/src/app/haxe/ru/m/puzzlez/render/Render.hx b/src/app/haxe/ru/m/puzzlez/render/Render.hx index 41a48a2..9b7aaf7 100644 --- a/src/app/haxe/ru/m/puzzlez/render/Render.hx +++ b/src/app/haxe/ru/m/puzzlez/render/Render.hx @@ -23,7 +23,7 @@ import ru.m.puzzlez.storage.SettingsStorage; class Render extends SpriteView implements IRender { - public var signal(default, null):Signal; + public var actions(default, null):Signal; public var scale(get, set):Float; public var manager(default, null):RenderManager; @@ -69,7 +69,7 @@ class Render extends SpriteView implements IRender { content.addChild(container); manager = new RenderManager(content, container); progress = new ProgressView(); - signal = new Signal(); + actions = new Signal(); tableView = new Sprite(); tableView.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); imageView = new CompleteView(); @@ -80,7 +80,7 @@ class Render extends SpriteView implements IRender { public function onGameEvent(event:GameEvent):Void { switch event { - case START(state) | RESUME(state): + case START(state, resume): onStart(state); case CHANGE(PART_UPDATE(id, TABLE(point))): var part:PartView = parts[id]; @@ -203,21 +203,21 @@ class Render extends SpriteView implements IRender { activePoint = RenderUtil.convertPoint(tableView.globalToLocal(point)); tableView.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); tableView.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); - signal.emit(ACTION(PART_TAKE(playerId, activePart.id))); + actions.emit(PART_TAKE(playerId, activePart.id)); } } private function onMouseMove(event:MouseEvent):Void { var newPoint:Point = RenderUtil.convertPoint(tableView.globalToLocal(new FlashPoint(event.stageX, event.stageY))); var partPosition = activePart.position.add(newPoint).subtract(activePoint); - signal.emit(ACTION(PART_MOVE(playerId, activePart.id, partPosition.clone()))); + actions.emit(PART_MOVE(playerId, activePart.id, partPosition.clone())); activePoint = newPoint; } private function onMouseUp(event:MouseEvent):Void { var newPoint:Point = RenderUtil.convertPoint(tableView.globalToLocal(new FlashPoint(event.stageX, event.stageY))); var partPosition = activePart.position.add(newPoint).subtract(activePoint); - signal.emit(ACTION(PART_PUT(playerId, activePart.id, partPosition.clone()))); + actions.emit(PART_PUT(playerId, activePart.id, partPosition.clone())); tableView.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); tableView.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); activePart = null; diff --git a/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx b/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx index a37fffa..a242024 100644 --- a/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx +++ b/src/app/haxe/ru/m/puzzlez/view/GameFrame.hx @@ -38,9 +38,9 @@ import ru.m.puzzlez.view.popup.PreviewPopup; } else { game = new Game(state); } - game.signal.connect(render.onGameEvent); - game.signal.connect(onGameEvent); - render.signal.connect(game.signal.emit); + game.events.connect(render.onGameEvent); + game.events.connect(onGameEvent); + render.actions.connect(game.action); game.start(); } @@ -49,7 +49,7 @@ import ru.m.puzzlez.view.popup.PreviewPopup; save(); } if (game != null) { - render.signal.disconnect(game.signal.emit); + render.actions.disconnect(game.action); game.stop(); game.dispose(); game = null; diff --git a/src/common/haxe/ru/m/puzzlez/core/Game.hx b/src/common/haxe/ru/m/puzzlez/core/Game.hx index edfa170..4d9486d 100644 --- a/src/common/haxe/ru/m/puzzlez/core/Game.hx +++ b/src/common/haxe/ru/m/puzzlez/core/Game.hx @@ -8,7 +8,7 @@ import ru.m.puzzlez.core.PartLocation; class Game implements IGame { public var state(default, null):GameState; - public var signal(default, null):Signal; + public var events(default, null):Signal; private var partsById:Map; @@ -18,8 +18,12 @@ class Game implements IGame { for (part in state.parts) { partsById[part.id] = part; } - signal = new Signal(); - signal.connect(onGameEvent); + events = new Signal(); + events.connect(onGameEvent); + } + + public function action(action:GameAction):Void { + events.emit(ACTION(action)); } public function start():Void { @@ -27,13 +31,13 @@ class Game implements IGame { case READY: shuffle(); state.status = STARTED; - signal.emit(START(state)); + events.emit(START(state, false)); case _: - signal.emit(RESUME(state)); + events.emit(START(state, true)); } } - public function shuffle():Void { + private function shuffle():Void { for (part in state.parts) { switch part.location { case TABLE(_): @@ -41,7 +45,6 @@ class Game implements IGame { var x = bound + Math.random() * (state.preset.tableRect.width - part.rect.width - bound * 2); var y = bound + Math.random() * (state.preset.tableRect.height - part.rect.height - bound * 2); part.location = TABLE(new Point(x, y)); - //signal.emit(CHANGE(PART_UPDATE(part.id, part.location))); case _: } } @@ -69,7 +72,7 @@ class Game implements IGame { switch part.location { case TABLE(point): part.location = HAND(playerId, point); - signal.emit(CHANGE(PART_UPDATE(partId, part.location))); + events.emit(CHANGE(PART_UPDATE(partId, part.location))); case _: } case ACTION(PART_MOVE(playerId, partId, point)): @@ -77,7 +80,7 @@ class Game implements IGame { switch part.location { case HAND(currentPlayerId, _) if (currentPlayerId == playerId): part.location = HAND(playerId, point); - signal.emit(CHANGE(PART_UPDATE(partId, part.location))); + events.emit(CHANGE(PART_UPDATE(partId, part.location))); case _: } case ACTION(PART_PUT(playerId, partId, point)): @@ -87,14 +90,14 @@ class Game implements IGame { var d = distance(target, point); if (d < 70) { part.location = IMAGE; - signal.emit(CHANGE(PART_UPDATE(partId, part.location))); + events.emit(CHANGE(PART_UPDATE(partId, part.location))); if (checkIsComplete()) { state.status = COMPLETE; - signal.emit(COMPLETE); + events.emit(COMPLETE); } } else { part.location = TABLE(point); - signal.emit(CHANGE(PART_UPDATE(partId, part.location))); + events.emit(CHANGE(PART_UPDATE(partId, part.location))); } case _: } @@ -105,6 +108,6 @@ class Game implements IGame { } public function dispose():Void { - signal.dispose(); + events.dispose(); } } diff --git a/src/common/haxe/ru/m/puzzlez/core/GameEvent.hx b/src/common/haxe/ru/m/puzzlez/core/GameEvent.hx index b9f3509..6dce154 100644 --- a/src/common/haxe/ru/m/puzzlez/core/GameEvent.hx +++ b/src/common/haxe/ru/m/puzzlez/core/GameEvent.hx @@ -14,8 +14,7 @@ enum GameChange { } enum GameEvent { - START(state:GameState); - RESUME(state:GameState); + START(state:GameState, resume:Bool); ACTION(action:GameAction); CHANGE(change:GameChange); COMPLETE; diff --git a/src/common/haxe/ru/m/puzzlez/core/IGame.hx b/src/common/haxe/ru/m/puzzlez/core/IGame.hx index 4b7d823..ef0b942 100644 --- a/src/common/haxe/ru/m/puzzlez/core/IGame.hx +++ b/src/common/haxe/ru/m/puzzlez/core/IGame.hx @@ -1,10 +1,13 @@ package ru.m.puzzlez.core; +import ru.m.puzzlez.core.GameEvent.GameAction; import hw.signal.Signal; interface IGame { public var state(default, null):GameState; - public var signal(default, null):Signal; + public var events(default, null):Signal; + + public function action(action:GameAction):Void; public function start():Void; diff --git a/src/common/proto/core.proto b/src/common/proto/core.proto index 0eeff50..b945657 100644 --- a/src/common/proto/core.proto +++ b/src/common/proto/core.proto @@ -2,37 +2,12 @@ syntax = "proto3"; package ru.m.puzzlez.proto.core; +message PointProto { + float x = 1; + float y = 2; +} + message UserProto { string uuid = 1; 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 { - GameStateProto state = 2; - repeated UserProto users = 3; -} - -message GameEventProto { - int32 time = 1; - string event = 2; -} diff --git a/src/common/proto/event.proto b/src/common/proto/event.proto new file mode 100644 index 0000000..5a29f86 --- /dev/null +++ b/src/common/proto/event.proto @@ -0,0 +1,41 @@ +syntax = "proto3"; + +import "core.proto"; +import "game.proto"; + +package ru.m.puzzlez.proto.event; + +message GameStartProto { + ru.m.puzzlez.proto.game.GameStateProto state = 1; + bool resume = 2; +} + +message GameCompleteProto { + +} + +message GameActionProto { + string playerId = 1; + int32 partId = 2; + enum Action { + TAKE = 0; + MOVE = 1; + PUT = 2; + } + Action action = 3; + ru.m.puzzlez.proto.core.PointProto point = 4; +} + +message GameChangeProto { + int32 partId = 1; +} + +message GameEventProto { + int32 time = 1; + oneof event { + GameStartProto start = 10; + GameCompleteProto complete = 11; + GameActionProto action = 12; + GameChangeProto change = 13; + } +} diff --git a/src/common/proto/game.proto b/src/common/proto/game.proto new file mode 100644 index 0000000..851a2e9 --- /dev/null +++ b/src/common/proto/game.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; + +import "core.proto"; + +package ru.m.puzzlez.proto.game; + +message PartProto { + +} + +message GridProto { + int32 width = 1; + int32 height = 2; +} + +message GamePresetProto { + string imageId = 1; + GridProto grid = 2; +} + +message GameStateProto { + string id = 1; + GamePresetProto preset = 2; + repeated PartProto parts = 3; +} + +message GameProto { + GameStateProto state = 2; + repeated ru.m.puzzlez.proto.core.UserProto users = 3; +} diff --git a/src/common/proto/pack.proto b/src/common/proto/pack.proto index 9a7b756..4f5b493 100644 --- a/src/common/proto/pack.proto +++ b/src/common/proto/pack.proto @@ -1,6 +1,8 @@ syntax = "proto3"; import "core.proto"; +import "game.proto"; +import "event.proto"; package ru.m.puzzlez.proto.pack; @@ -32,7 +34,7 @@ message GameJoinRequest { message GameLeaveRequest {} message GameResponse { - ru.m.puzzlez.proto.core.GameProto game = 1; + ru.m.puzzlez.proto.game.GameProto game = 1; } message GameListRequest { @@ -40,15 +42,15 @@ message GameListRequest { } message GameListResponse { - repeated ru.m.puzzlez.proto.core.GameProto games = 1; + repeated ru.m.puzzlez.proto.game.GameProto games = 1; } -message GameEventRequest { - repeated ru.m.puzzlez.proto.core.GameEventProto events = 1; +message GameActionRequest { + repeated ru.m.puzzlez.proto.event.GameActionProto actions = 1; } message GameEventResponse { - repeated ru.m.puzzlez.proto.core.GameEventProto events = 1; + repeated ru.m.puzzlez.proto.event.GameEventProto events = 1; } message Request { @@ -62,7 +64,7 @@ message Request { GameListRequest gameList = 20; - GameEventRequest gameEvent = 100; + GameActionRequest gameAction = 100; } }