From e657fb16bd9f6106367c11ba4565400eb18d24b7 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 19 Mar 2018 17:39:34 +0300 Subject: [PATCH] [server] update --- .../haxe/ru/m/tankz/bundle/ConfigBundle.hx | 17 ++------- .../haxe/ru/m/tankz/frame/NetworkFrame.hx | 16 +++++++++ .../haxe/ru/m/tankz/frame/NetworkFrame.yaml | 6 +++- .../haxe/ru/m/tankz/frame/StartFrame.hx | 5 +-- .../haxe/ru/m/tankz/frame/StartFrame.yaml | 2 +- .../haxe/ru/m/tankz/network/NetworkManager.hx | 9 ++++- .../haxe/ru/m/tankz/bundle/IConfigBundle.hx | 10 ++++++ .../ru/m/tankz/control/NoneControlFactory.hx | 2 ++ src/common/haxe/ru/m/tankz/game/Spawner.hx | 3 +- src/server/haxe/ru/m/tankz/server/Server.hx | 12 ++++++- .../tankz/server/bundle/ServerConfigBundle.hx | 22 ++++++++++++ .../tankz/server/bundle/ServerLevelBundle.hx | 20 +++++++++++ .../ru/m/tankz/server/game/GameManager.hx | 2 +- .../haxe/ru/m/tankz/server/session/Session.hx | 7 ++-- tasks/debug.js | 36 +++++++++++++------ 15 files changed, 133 insertions(+), 36 deletions(-) create mode 100644 src/server/haxe/ru/m/tankz/server/bundle/ServerConfigBundle.hx create mode 100644 src/server/haxe/ru/m/tankz/server/bundle/ServerLevelBundle.hx diff --git a/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx b/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx index b387b10..8dcf381 100644 --- a/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx +++ b/src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx @@ -1,30 +1,17 @@ package ru.m.tankz.bundle; import openfl.Assets; +import ru.m.tankz.bundle.IConfigBundle; import ru.m.tankz.config.Config; import ru.m.tankz.Type; import yaml.Parser; import yaml.Yaml; -typedef ConfigSource = { - var game:GameConfig; - var map: MapConfig; - var bricks: Array; - var presets: Array; - var points: Array; - var tanks: Array; - var bonuses: Array; -} - class ConfigBundle implements IConfigBundle { - private static function convert(raw:Dynamic):ConfigSource { - return raw; - } - public function get(type:GameType):Config { - var source = convert(Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects())); + var source:ConfigSource = Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects()); return new Config(type, source.game, source.map, source.bricks, source.presets, source.points, source.tanks, source.bonuses); } } diff --git a/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx b/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx index 30ca5c6..a18e6ba 100644 --- a/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/NetworkFrame.hx @@ -1,5 +1,9 @@ package ru.m.tankz.frame; +import ru.m.tankz.game.ClassicGame; +import ru.m.tankz.game.GameSave; +import haxework.provider.Provider; +import ru.m.tankz.proto.core.GameState; import haxework.gui.ButtonView; import haxework.gui.frame.IFrameSwitcher; import haxework.gui.IGroupView; @@ -17,23 +21,29 @@ class NetworkFrame extends VGroupView { public static var ID(default, never):String = "network"; @:view var frameSwitcher(default, null):IFrameSwitcher; + @:view var loginFrame(default, null):IGroupView; @:view var stateLabel(default, null):LabelView; @:view var nameInput(default, null):InputView; @:view var loginButton(default, null):ButtonView; + @:view var gameListFrame(default, null):IGroupView; @:view var createGameButton(default, null):ButtonView; @:view var gameList(default, null):ListView; + @:view var gameFrame(default, null):IGroupView; @:view var leaveGameButton(default, null):ButtonView; + @:view var startGameButton(default, null):ButtonView; @:view var userList(default, null):ListView; @:provide var network:NetworkManager; + @:provide var mainFrameSwitcher:IFrameSwitcher; public function init():Void { loginButton.onPress = this; createGameButton.onPress = this; leaveGameButton.onPress = this; + startGameButton.onPress = this; gameList.dispatcher.addListener({ onListItemClick: function(item:IListItemView):Void { network.joinGame(item.data.id); @@ -69,6 +79,10 @@ class NetworkFrame extends VGroupView { if (game != null) { userList.data = game.players; frameSwitcher.change(gameFrame.id); + if (game.state == GameState.STARTED) { + Provider.set(GameSave, new GameSave({type: ClassicGame.TYPE, presetId: ClassicGame.PLAYER1})); + mainFrameSwitcher.change(GameFrame.ID); + } } else { frameSwitcher.change(gameListFrame.id); } @@ -88,6 +102,8 @@ class NetworkFrame extends VGroupView { network.createGame('classic'); case 'leaveGameButton': network.leaveGame(); + case 'startGameButton': + network.startGame(); } } } \ No newline at end of file diff --git a/src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml b/src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml index 3a19887..8512115 100644 --- a/src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml +++ b/src/client/haxe/ru/m/tankz/frame/NetworkFrame.yaml @@ -58,12 +58,16 @@ views: $type: haxework.gui.skin.ColorSkin color: "#000000" alpha: 0 - # game list + # game - id: gameFrame $type: haxework.gui.VGroupView pWidth: 100 pHeight: 100 views: + - id: startGameButton + $type: haxework.gui.ButtonView + $style: button + text: Start - id: leaveGameButton $type: haxework.gui.ButtonView $style: button diff --git a/src/client/haxe/ru/m/tankz/frame/StartFrame.hx b/src/client/haxe/ru/m/tankz/frame/StartFrame.hx index bef4b45..9ef3247 100644 --- a/src/client/haxe/ru/m/tankz/frame/StartFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/StartFrame.hx @@ -25,6 +25,7 @@ class StartFrame extends VGroupView { @:view var network(default, null):ButtonView; @:provide var frameSwitcher:IFrameSwitcher; + @:provide var storage:SaveStorage; public function init():Void { classic_1p.onPress = this; @@ -37,7 +38,7 @@ class StartFrame extends VGroupView { } public function onShow():Void { - var save:GameSave = Provider.get(SaveStorage).read(ClassicGame.TYPE); + var save:GameSave = storage.read(ClassicGame.TYPE); if (save != null) { classic_load.visible = true; classic_load.text = 'Load (Level ${save.state.level})'; @@ -71,7 +72,7 @@ class StartFrame extends VGroupView { } private function loadGame(type:GameType):Void { - var save:GameSave = Provider.get(SaveStorage).read(type); + var save:GameSave = storage.read(type); Provider.set(GameSave, save); frameSwitcher.change(GameFrame.ID); } diff --git a/src/client/haxe/ru/m/tankz/frame/StartFrame.yaml b/src/client/haxe/ru/m/tankz/frame/StartFrame.yaml index 6775307..a9bea97 100644 --- a/src/client/haxe/ru/m/tankz/frame/StartFrame.yaml +++ b/src/client/haxe/ru/m/tankz/frame/StartFrame.yaml @@ -57,7 +57,7 @@ views: # network - $type: haxework.gui.VGroupView contentSize: true - visible: false + visible: true views: - $type: haxework.gui.LabelView $style: label diff --git a/src/client/haxe/ru/m/tankz/network/NetworkManager.hx b/src/client/haxe/ru/m/tankz/network/NetworkManager.hx index 61d3cf1..b94b951 100644 --- a/src/client/haxe/ru/m/tankz/network/NetworkManager.hx +++ b/src/client/haxe/ru/m/tankz/network/NetworkManager.hx @@ -1,5 +1,6 @@ package ru.m.tankz.network; +import ru.m.tankz.proto.pack.StartGameRequest; import ru.m.tankz.proto.game.GameChange; import ru.m.tankz.proto.game.GameActionType; import ru.m.tankz.proto.pack.GameUpdateRequest; @@ -79,6 +80,10 @@ class NetworkManager { connection.send(new Request().setLeaveGame(new LeaveGameRequest())); } + public function startGame():Void { + connection.send(new Request().setStartGame(new StartGameRequest())); + } + public function action(action:TankAction):Void { var update:GameUpdateRequest = switch action { case TankAction.MOVE(direction): new GameUpdateRequest().setType(GameActionType.MOVE).setDirectionX(direction.x).setDirectionY(direction.y); @@ -119,7 +124,9 @@ class NetworkManager { gameSignal.emit(packet.joinGame.game); } else if (packet.hasLeaveGame()) { gameSignal.emit(null); - }else if (packet.hasUpdateGame()) { + } else if (packet.hasStartGame()) { + gameSignal.emit(packet.startGame.game); + } else if (packet.hasUpdateGame()) { gameUpdateSignal.emit(packet.updateGame.changes); } } diff --git a/src/common/haxe/ru/m/tankz/bundle/IConfigBundle.hx b/src/common/haxe/ru/m/tankz/bundle/IConfigBundle.hx index 06c2c31..291c5d3 100644 --- a/src/common/haxe/ru/m/tankz/bundle/IConfigBundle.hx +++ b/src/common/haxe/ru/m/tankz/bundle/IConfigBundle.hx @@ -4,6 +4,16 @@ import ru.m.tankz.config.Config; import ru.m.tankz.Type; +typedef ConfigSource = { + var game:GameConfig; + var map: MapConfig; + var bricks: Array; + var presets: Array; + var points: Array; + var tanks: Array; + var bonuses: Array; +} + interface IConfigBundle { public function get(type:GameType):Config; } diff --git a/src/common/haxe/ru/m/tankz/control/NoneControlFactory.hx b/src/common/haxe/ru/m/tankz/control/NoneControlFactory.hx index 620154e..15450ba 100644 --- a/src/common/haxe/ru/m/tankz/control/NoneControlFactory.hx +++ b/src/common/haxe/ru/m/tankz/control/NoneControlFactory.hx @@ -5,6 +5,8 @@ import ru.m.tankz.game.Player; class NoneControlFactory implements IControlFactory { + public function new() {} + public function build(player:Player):Control { return null; } diff --git a/src/common/haxe/ru/m/tankz/game/Spawner.hx b/src/common/haxe/ru/m/tankz/game/Spawner.hx index 404a42b..6248464 100644 --- a/src/common/haxe/ru/m/tankz/game/Spawner.hx +++ b/src/common/haxe/ru/m/tankz/game/Spawner.hx @@ -29,6 +29,7 @@ class Spawner { this.config = config; this.points = points; this.runner = null; + this.index = 0; queue = []; indexedPoints = new Map(); anyPoints = []; @@ -68,7 +69,7 @@ class Spawner { private function run():Void { if (timer == null) { - timer = new Timer(config.spawnInterval); + timer = new Timer(config.spawnInterval == null ? 1 : config.spawnInterval); timer.run = spawn; } } diff --git a/src/server/haxe/ru/m/tankz/server/Server.hx b/src/server/haxe/ru/m/tankz/server/Server.hx index 301d652..2c6151c 100755 --- a/src/server/haxe/ru/m/tankz/server/Server.hx +++ b/src/server/haxe/ru/m/tankz/server/Server.hx @@ -1,9 +1,16 @@ package ru.m.tankz.server; +import ru.m.tankz.control.NoneControlFactory; +import ru.m.tankz.control.IControlFactory; import haxe.io.Bytes; import haxework.log.TraceLogger; +import haxework.provider.Provider; import neko.net.ThreadServer; import ru.m.connect.IConnection.ConnectionEvent; +import ru.m.tankz.bundle.IConfigBundle; +import ru.m.tankz.bundle.ILevelBundle; +import ru.m.tankz.server.bundle.ServerConfigBundle; +import ru.m.tankz.server.bundle.ServerLevelBundle; import ru.m.tankz.server.session.Session; import sys.net.Socket; #if debug import haxework.log.SocketLogger; #end @@ -36,7 +43,7 @@ class Server extends ThreadServer { try { session.pushData(bytes); } catch (error:Dynamic) { - L.d(TAG, "_", error); + L.e(TAG, 'error', error); } } @@ -47,6 +54,9 @@ class Server extends ThreadServer { #end L.d(TAG, "Running"); L.i(TAG, "Build: " + CompilationOption.get("build")); + Provider.setFactory(IConfigBundle, ServerConfigBundle); + Provider.setFactory(ILevelBundle, ServerLevelBundle); + Provider.setFactory(IControlFactory, NoneControlFactory); var wserver = new Server(); wserver.run("localhost", 5001); } diff --git a/src/server/haxe/ru/m/tankz/server/bundle/ServerConfigBundle.hx b/src/server/haxe/ru/m/tankz/server/bundle/ServerConfigBundle.hx new file mode 100644 index 0000000..3852c32 --- /dev/null +++ b/src/server/haxe/ru/m/tankz/server/bundle/ServerConfigBundle.hx @@ -0,0 +1,22 @@ +package ru.m.tankz.server.bundle; + +import ru.m.tankz.bundle.IConfigBundle; +import ru.m.tankz.config.Config; +import ru.m.tankz.Type; +import sys.FileSystem; +import sys.io.File; +import yaml.Parser; +import yaml.Yaml; + + +class ServerConfigBundle implements IConfigBundle { + + public function new() {} + + public function get(type:GameType):Config { + var path:String = FileSystem.absolutePath('./target/html5/resources/${type}/config.yaml'); + var data:String = File.getContent(path); + var source:ConfigSource = Yaml.parse(data, Parser.options().useObjects()); + return new Config(type, source.game, source.map, source.bricks, source.presets, source.points, source.tanks, source.bonuses); + } +} diff --git a/src/server/haxe/ru/m/tankz/server/bundle/ServerLevelBundle.hx b/src/server/haxe/ru/m/tankz/server/bundle/ServerLevelBundle.hx new file mode 100644 index 0000000..575ebac --- /dev/null +++ b/src/server/haxe/ru/m/tankz/server/bundle/ServerLevelBundle.hx @@ -0,0 +1,20 @@ +package ru.m.tankz.server.bundle; + +import ru.m.tankz.bundle.ILevelBundle; +import ru.m.tankz.config.Config; +import ru.m.tankz.Type; +import ru.m.tankz.util.LevelUtil; +import sys.FileSystem; +import sys.io.File; + + +class ServerLevelBundle implements ILevelBundle { + + public function new() {} + + public function get(type:GameType, config:Config, level:Int):LevelConfig { + var path:String = FileSystem.absolutePath('./target/html5/resources/${type}/levels/level${LevelUtil.formatLevel(level)}.txt'); + var data:String = File.getContent(path); + return LevelUtil.loads(config, data); + } +} diff --git a/src/server/haxe/ru/m/tankz/server/game/GameManager.hx b/src/server/haxe/ru/m/tankz/server/game/GameManager.hx index dc9e29c..cb1d5ac 100644 --- a/src/server/haxe/ru/m/tankz/server/game/GameManager.hx +++ b/src/server/haxe/ru/m/tankz/server/game/GameManager.hx @@ -116,7 +116,7 @@ class GameManager { public function start() { game.setState(GameState.STARTED); runGame = new ClassicGame(); - runGame.start(new GameSave({type: ClassicGame.TYPE, presetId: ClassicGame.PLAYER1})); + runGame.start(new GameSave({type: ClassicGame.TYPE, presetId: ClassicGame.PLAYER1, level: 0})); timer = new NekoTimer(30); timer.run = update; broadcast(new Response().setStartGame(new StartGameResponse().setGame(game))); diff --git a/src/server/haxe/ru/m/tankz/server/session/Session.hx b/src/server/haxe/ru/m/tankz/server/session/Session.hx index c5dc945..1ae9176 100755 --- a/src/server/haxe/ru/m/tankz/server/session/Session.hx +++ b/src/server/haxe/ru/m/tankz/server/session/Session.hx @@ -1,6 +1,7 @@ package ru.m.tankz.server.session; import com.hurlant.crypto.extra.UUID; +import com.hurlant.crypto.prng.Random; import haxe.io.Bytes; import ru.m.connect.IConnection; import ru.m.connect.neko.NekoConnection; @@ -64,7 +65,7 @@ class Session { } private function onConnectionEvent(event:ConnectionEvent):Void { - L.d('Session', '${this}, ${event}'); + L.d('Session', '${event}'); } public function onRequest(request:Request):Void { @@ -80,12 +81,14 @@ class Session { send(new Response().setJoinGame(joinGame(request.joinGame))); } else if (request.hasLeaveGame()) { send(new Response().setLeaveGame(leaveGame(request.leaveGame))); + } else if (request.hasStartGame()) { + send(new Response().setStartGame(startGame(request.startGame))); } } private function login(request:LoginRequest):LoginResponse { user = new User() - .setUuid(request.uuid != null ? request.uuid : UUID.generateRandom().toString()) + .setUuid(request.uuid != null ? request.uuid : UUID.generateRandom(new Random()).toString()) .setName(request.name); sessions.set(user.uuid, this); GameManager.subscribers.set(user.uuid, true); diff --git a/tasks/debug.js b/tasks/debug.js index ddb80c4..1165581 100755 --- a/tasks/debug.js +++ b/tasks/debug.js @@ -3,18 +3,29 @@ const through = require('through2'); const colors = require('ansi-colors'); const log = require('fancy-log'); -const color = (level) => { - return { - '[DEBUG]': colors.white, - '[INFO]': colors.cyan, - '[ERROR]': colors.red, - '[WARNING]': colors.yellow, - }[level] || colors.reset; +const _colors = { + '[DEBUG]': colors.white, + '[INFO]': colors.cyan, + '[ERROR]': colors.red, + '[WARNING]': colors.yellow, }; -const _log = (line) => { - const result = line.split(' '); - console.log(colors.gray(result[0]) + ' ' + color(result[1])(result.slice(1).join(' '))); +const getColor = (line) => { + for (const [tag, color] of Object.entries(_colors)) { + if (line.indexOf(tag) > -1) { + return color; + } + } + return null;// colors.reset; +}; + +const _log = (line, color) => { + if (line[0] === '\t') { + console.log(color(line)) + } else { + const result = line.split(' '); + console.log(colors.gray(result.slice(0, 4).join(' ')) + ' ' + color(result.slice(4).join(' '))); + } }; module.exports = () => { @@ -25,11 +36,14 @@ module.exports = () => { callback(); return; } + let color = null; const server = net.createServer((socket) => { socket.on("data", (data) => { const lines = data.toString().split('\n'); for (let line of lines) if (line.length > 2) { - _log(line); + const newColor = getColor(line); + if (newColor != null) color = newColor; + _log(line, color); } }); socket.on("close", () => {