[server] update
This commit is contained in:
@@ -60,6 +60,9 @@ class PuzzlezTheme extends Theme {
|
||||
register(new Style("icon.orange", [
|
||||
"skin.color" => 0xcc5500,
|
||||
]));
|
||||
register(new Style("icon.green", [
|
||||
"skin.color" => 0x00ff00,
|
||||
]));
|
||||
register(new Style("icon.control", [
|
||||
"geometry.hAlign" => HAlign.RIGHT,
|
||||
"geometry.vAlign" => VAlign.TOP,
|
||||
|
||||
@@ -10,26 +10,24 @@ import ru.m.puzzlez.core.Id;
|
||||
import ru.m.puzzlez.proto.core.User;
|
||||
import ru.m.puzzlez.proto.event.GameAction;
|
||||
import ru.m.puzzlez.proto.event.GameEvent;
|
||||
import ru.m.puzzlez.proto.game.GameItem;
|
||||
import ru.m.puzzlez.proto.game.GamePreset;
|
||||
import ru.m.puzzlez.proto.game.GameState;
|
||||
import ru.m.puzzlez.proto.pack.AuthRequest;
|
||||
import ru.m.puzzlez.proto.pack.GameActionRequest;
|
||||
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.GameLeaveRequest;
|
||||
import ru.m.puzzlez.proto.pack.GameListRequest;
|
||||
import ru.m.puzzlez.proto.pack.GameListResponse;
|
||||
import ru.m.puzzlez.proto.pack.NotificationResponse;
|
||||
import ru.m.puzzlez.proto.pack.Request;
|
||||
import ru.m.puzzlez.proto.pack.Response;
|
||||
|
||||
@:provide class Network implements IDataIndex<ImageId> {
|
||||
public var user(default, null):User;
|
||||
public var userSignal(default, null):Signal<User> = new Signal();
|
||||
|
||||
public var gameList(default, null):Array<GameItem>;
|
||||
public var gameListSignal(default, null):Signal<Array<GameItem>> = new Signal();
|
||||
|
||||
public var game(default, null):GameItem;
|
||||
public var gameSignal(default, null):Signal<GameItem> = new Signal();
|
||||
|
||||
public var gameEventSignal(default, null):Signal<GameEvent> = new Signal();
|
||||
public var userSignal:Signal<User> = new Signal();
|
||||
public var notificationSignal:Signal<NotificationResponse> = new Signal();
|
||||
public var listSignal:Signal<GameListResponse> = new Signal();
|
||||
public var joinSignal:Signal<GameState> = new Signal();
|
||||
public var gameEventSignal:Signal<GameEvent> = new Signal();
|
||||
|
||||
private var connection:IConnection<Request, Response>;
|
||||
private var storage:SharedObjectStorage;
|
||||
@@ -37,41 +35,49 @@ import ru.m.puzzlez.proto.pack.Response;
|
||||
private static var USER_KEY = "user";
|
||||
|
||||
public function new() {
|
||||
gameList = [];
|
||||
storage = new SharedObjectStorage("network/2");
|
||||
if (storage.exists(USER_KEY)) {
|
||||
user = storage.read(USER_KEY);
|
||||
} else {
|
||||
user = new User().setName('Anonimus #${IdUtil.generate()}');
|
||||
storage.write(USER_KEY, user);
|
||||
}
|
||||
connection = ConnectionFactory.buildClientConnection("127.0.0.1", 5000, Response);
|
||||
connection.handler.connect(onConnectionChange);
|
||||
connection.receiveHandler.connect(onReceivePacket);
|
||||
connection.connect().catchError(_ -> {});
|
||||
}
|
||||
|
||||
public function login():Void {
|
||||
connection.send(new Request().setLogin(new LoginRequest().setUser(user)));
|
||||
private function restoreUser():User {
|
||||
if (storage.exists(USER_KEY)) {
|
||||
return storage.read(USER_KEY);
|
||||
} else {
|
||||
return new User().setName('Anonimus #${IdUtil.generate()}');
|
||||
}
|
||||
}
|
||||
|
||||
public function startGame(state:GameState):Void {
|
||||
connection.send(new Request().setGameCreate(new GameCreateRequest().setImageId(state.preset.imageId)));
|
||||
public function auth():Promise<User> {
|
||||
connection.send(new Request().setAuth(new AuthRequest().setUser(restoreUser())));
|
||||
return userSignal.next();
|
||||
}
|
||||
|
||||
public function joinGame(state:GameState):Void {
|
||||
connection.send(new Request().setGameJoin(new GameJoinRequest().setGameId(state.id)));
|
||||
public function createGame(preset:GamePreset):Promise<GameState> {
|
||||
connection.send(new Request().setJoin(new GameJoinRequest().setPreset(preset)));
|
||||
return joinSignal.next();
|
||||
}
|
||||
|
||||
public function joinGame(gameId:String):Promise<GameState> {
|
||||
connection.send(new Request().setJoin(new GameJoinRequest().setGameId(gameId)));
|
||||
return joinSignal.next();
|
||||
}
|
||||
|
||||
public function leaveGame():Void {
|
||||
connection.send(new Request().setLeave(new GameLeaveRequest()));
|
||||
}
|
||||
|
||||
public function sendGameAction(action:GameAction):Void {
|
||||
connection.send(new Request().setGameAction(new GameActionRequest().setActions([action])));
|
||||
connection.send(new Request().setAction(new GameActionRequest().setActions([action])));
|
||||
}
|
||||
|
||||
private function onConnectionChange(event:ConnectionEvent):Void {
|
||||
L.i("network", '${event}');
|
||||
switch event {
|
||||
case CONNECTED:
|
||||
login();
|
||||
auth().then(user -> storage.write(USER_KEY, user));
|
||||
case DISCONNECTED:
|
||||
//
|
||||
case ERROR(error):
|
||||
@@ -80,34 +86,32 @@ import ru.m.puzzlez.proto.pack.Response;
|
||||
}
|
||||
|
||||
private function onReceivePacket(packet:Response):Void {
|
||||
if (packet.hasLogin()) {
|
||||
user = packet.login.user;
|
||||
storage.write(USER_KEY, user);
|
||||
userSignal.emit(user);
|
||||
} else if (packet.hasLogout()) {
|
||||
user = null;
|
||||
userSignal.emit(user);
|
||||
} else if (packet.hasGameList()) {
|
||||
gameList = packet.gameList.games;
|
||||
gameListSignal.emit(gameList);
|
||||
} else if (packet.hasGame()) {
|
||||
game = packet.game.game;
|
||||
gameSignal.emit(game);
|
||||
} else if (packet.hasGameEvent()) {
|
||||
for (event in packet.gameEvent.events) {
|
||||
// ToDo: convert event?
|
||||
//gameEventSignal.emit(Unserializer.run(event.event));
|
||||
if (packet.hasAuth()) {
|
||||
userSignal.emit(packet.auth.user);
|
||||
} else if (packet.hasNotification()) {
|
||||
notificationSignal.emit(packet.notification);
|
||||
} else if (packet.hasList()) {
|
||||
listSignal.emit(packet.list);
|
||||
} else if (packet.hasJoin()) {
|
||||
joinSignal.emit(packet.join.game);
|
||||
} else if (packet.hasEvent()) {
|
||||
for (event in packet.event.events) {
|
||||
gameEventSignal.emit(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
connection.send(new Request().setList(new GameListRequest().setCount(page.count).setPage(page.index)));
|
||||
return listSignal.next().then((list:GameListResponse) -> ({
|
||||
page: {
|
||||
index: list.page,
|
||||
count: list.count,
|
||||
filter: null,
|
||||
order: null,
|
||||
},
|
||||
total: list.total,
|
||||
data: list.games.map(item -> ImageId.fromString(item.preset.imageId)),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package ru.m.puzzlez.net;
|
||||
|
||||
import ru.m.puzzlez.proto.event.GameStart;
|
||||
import hw.signal.Signal;
|
||||
import ru.m.puzzlez.core.IGame;
|
||||
import ru.m.puzzlez.proto.event.GameAction;
|
||||
import ru.m.puzzlez.proto.event.GameEvent;
|
||||
import ru.m.puzzlez.proto.game.GameState;
|
||||
import ru.m.puzzlez.proto.game.GameStatus;
|
||||
|
||||
class NetworkGame implements IGame {
|
||||
public var state(default, null):GameState;
|
||||
@@ -23,17 +23,13 @@ class NetworkGame implements IGame {
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
events.emit(new GameEvent().setStart(new GameStart().setState(state)));
|
||||
network.gameEventSignal.connect(onEvent);
|
||||
switch state.status {
|
||||
case GameStatus.READY:
|
||||
network.startGame(state);
|
||||
case _:
|
||||
network.joinGame(state);
|
||||
}
|
||||
}
|
||||
|
||||
public function stop():Void {
|
||||
network.gameEventSignal.disconnect(onEvent);
|
||||
network.leaveGame();
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
|
||||
@@ -35,7 +35,8 @@ class Render extends SpriteView implements IRender {
|
||||
private var playerId(get, never):PlayerId;
|
||||
|
||||
private function get_playerId():PlayerId {
|
||||
return network.user.hasUuid() ? network.user.uuid : "local";
|
||||
// ToDo: network user
|
||||
return "local";
|
||||
}
|
||||
|
||||
private function get_scale():Float {
|
||||
|
||||
@@ -56,8 +56,11 @@ import ru.m.puzzlez.view.common.PresetView;
|
||||
}
|
||||
|
||||
private function start(online:Bool = false):Void {
|
||||
imageView.state.online = online;
|
||||
switcher.change(GameFrame.ID, imageView.state);
|
||||
if (online) {
|
||||
network.createGame(imageView.state.preset).then(state -> switcher.change(GameFrame.ID, state));
|
||||
} else {
|
||||
switcher.change(GameFrame.ID, imageView.state);
|
||||
}
|
||||
}
|
||||
|
||||
private function back():Void {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package ru.m.puzzlez.view;
|
||||
|
||||
import ru.m.puzzlez.proto.game.GameStatus;
|
||||
import ru.m.puzzlez.net.Network;
|
||||
import hw.view.data.DataView;
|
||||
import hw.view.form.ButtonView;
|
||||
import hw.view.frame.FrameSwitcher;
|
||||
@@ -9,6 +7,9 @@ import hw.view.frame.FrameView;
|
||||
import ru.m.data.IDataSource;
|
||||
import ru.m.pixabay.PixabayApi;
|
||||
import ru.m.puzzlez.core.ImageListSource;
|
||||
import ru.m.puzzlez.net.Network;
|
||||
import ru.m.puzzlez.proto.game.GameStatus;
|
||||
import ru.m.puzzlez.proto.pack.NotificationResponse;
|
||||
import ru.m.puzzlez.source.AssetSource;
|
||||
import ru.m.puzzlez.source.FileSource;
|
||||
import ru.m.puzzlez.source.PixabaySource;
|
||||
@@ -22,6 +23,7 @@ import ru.m.update.Updater;
|
||||
@:view var sources:DataView<ImageListSource, ButtonView>;
|
||||
@:view("load") var loadButton:ButtonView;
|
||||
@:view("complete") var completeButton:ButtonView;
|
||||
@:view("network") var networkButton:ButtonView;
|
||||
@:view("update") var updateButton:ButtonView;
|
||||
|
||||
@:provide var storage:ImageStorage;
|
||||
@@ -55,6 +57,13 @@ import ru.m.update.Updater;
|
||||
completeButton.text = 'Complete (${total})';
|
||||
completeButton.disabled = total == 0;
|
||||
});
|
||||
network.notificationSignal.next().then(onNotification);
|
||||
}
|
||||
|
||||
private function onNotification(notification:NotificationResponse):Void {
|
||||
var total = notification.games;
|
||||
networkButton.text = 'Network (${total})';
|
||||
networkButton.disabled = total == 0;
|
||||
}
|
||||
|
||||
override public function onShow(data:Dynamic):Void {
|
||||
@@ -65,6 +74,11 @@ import ru.m.update.Updater;
|
||||
updateButton.text = 'Update ${info.version}';
|
||||
}
|
||||
}).catchError(error -> L.w('Update', 'failed: ${error}'));
|
||||
network.notificationSignal.connect(onNotification);
|
||||
}
|
||||
|
||||
override public function onHide():Void {
|
||||
network.notificationSignal.disconnect(onNotification);
|
||||
}
|
||||
|
||||
private function sourceViewFactory(index:Int, source:ImageListSource):ButtonView {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.puzzlez.view.common;
|
||||
|
||||
import ru.m.puzzlez.net.Network;
|
||||
import hw.view.data.DataView;
|
||||
import hw.view.form.ToggleButtonView;
|
||||
import hw.view.frame.FrameSwitcher;
|
||||
@@ -24,6 +25,7 @@ import ru.m.puzzlez.view.common.PuzzleImageView;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
@:provide var gameStorage:GameStorage;
|
||||
@:provide var imageStorage:ImageStorage;
|
||||
@:provide var network:Network;
|
||||
|
||||
public var data(default, set):DataPage<ImageId>;
|
||||
|
||||
@@ -74,6 +76,8 @@ import ru.m.puzzlez.view.common.PuzzleImageView;
|
||||
gameStorage.delete(imageId).then(_ -> refresh());
|
||||
}
|
||||
});
|
||||
case JOIN:
|
||||
// ToDo:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.puzzlez.view.common;
|
||||
|
||||
import ru.m.puzzlez.net.Network;
|
||||
import hw.view.data.DataView;
|
||||
import hw.view.form.ButtonView;
|
||||
import hw.view.form.LabelView;
|
||||
@@ -14,6 +15,7 @@ import ru.m.puzzlez.storage.ImageStorage;
|
||||
enum Action {
|
||||
CLEAN;
|
||||
REMOVE;
|
||||
JOIN;
|
||||
}
|
||||
|
||||
@:template class PuzzleImageView extends GroupView {
|
||||
@@ -42,14 +44,17 @@ enum Action {
|
||||
@:view("label") var labelView:LabelView;
|
||||
@:view("clean") var cleanButton:ButtonView;
|
||||
@:view("remove") var removeButton:ButtonView;
|
||||
@:view("join") var joinButton:ButtonView;
|
||||
@:provide static var imageStorage:ImageStorage;
|
||||
@:provide static var gameStorage:GameStorage;
|
||||
@:provide static var network:Network;
|
||||
private var loading:LoadingWrapper;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
cleanButton.visible = false;
|
||||
removeButton.visible = false;
|
||||
joinButton.visible = false;
|
||||
loading = new LoadingWrapper(this);
|
||||
}
|
||||
|
||||
@@ -73,6 +78,8 @@ enum Action {
|
||||
result.removeButton.visible = true;
|
||||
}
|
||||
});
|
||||
// ToDo:
|
||||
result.joinButton.visible = false;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,3 +18,8 @@ views:
|
||||
propagation: false
|
||||
style: icon.control.small.close.orange
|
||||
+onPress: ~emit(Action.CLEAN)
|
||||
- id: join
|
||||
$type: hw.view.form.ButtonView
|
||||
propagation: false
|
||||
style: icon.control.small.close.green
|
||||
+onPress: ~emit(Action.JOIN)
|
||||
|
||||
Reference in New Issue
Block a user