[proto] add game and event proto

This commit is contained in:
2020-05-26 14:25:37 +03:00
parent f7f6423938
commit b7b1ac3871
13 changed files with 135 additions and 70 deletions

View File

@@ -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,

View File

@@ -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));
}
}
}

View File

@@ -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<GameEvent>;
public var events(default, null):Signal<GameEvent>;
@: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);
}
}

View File

@@ -5,7 +5,7 @@ import hw.view.IView;
import ru.m.puzzlez.core.GameEvent;
interface IRender extends IView<Dynamic> {
public var signal(default, null):Signal<GameEvent>;
public var actions(default, null):Signal<GameAction>;
public var scale(get, set):Float;
public var manager(default, null):RenderManager;

View File

@@ -23,7 +23,7 @@ import ru.m.puzzlez.storage.SettingsStorage;
class Render extends SpriteView implements IRender {
public var signal(default, null):Signal<GameEvent>;
public var actions(default, null):Signal<GameAction>;
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;

View File

@@ -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;

View File

@@ -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<GameEvent>;
public var events(default, null):Signal<GameEvent>;
private var partsById:Map<Int, Part>;
@@ -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();
}
}

View File

@@ -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;

View File

@@ -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<GameEvent>;
public var events(default, null):Signal<GameEvent>;
public function action(action:GameAction):Void;
public function start():Void;

View File

@@ -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;
}

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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;
}
}