[server] update
This commit is contained in:
@@ -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<BrickConfig>;
|
||||
var presets: Array<GamePreset>;
|
||||
var points: Array<SpawnPoint>;
|
||||
var tanks: Array<TankConfig>;
|
||||
var bonuses: Array<BonusConfig>;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Game>;
|
||||
|
||||
@:view var gameFrame(default, null):IGroupView;
|
||||
@:view var leaveGameButton(default, null):ButtonView;
|
||||
@:view var startGameButton(default, null):ButtonView;
|
||||
@:view var userList(default, null):ListView<User>;
|
||||
|
||||
@: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<Game>):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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ views:
|
||||
# network
|
||||
- $type: haxework.gui.VGroupView
|
||||
contentSize: true
|
||||
visible: false
|
||||
visible: true
|
||||
views:
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
|
||||
@@ -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,6 +124,8 @@ class NetworkManager {
|
||||
gameSignal.emit(packet.joinGame.game);
|
||||
} else if (packet.hasLeaveGame()) {
|
||||
gameSignal.emit(null);
|
||||
} else if (packet.hasStartGame()) {
|
||||
gameSignal.emit(packet.startGame.game);
|
||||
} else if (packet.hasUpdateGame()) {
|
||||
gameUpdateSignal.emit(packet.updateGame.changes);
|
||||
}
|
||||
|
||||
@@ -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<BrickConfig>;
|
||||
var presets: Array<GamePreset>;
|
||||
var points: Array<SpawnPoint>;
|
||||
var tanks: Array<TankConfig>;
|
||||
var bonuses: Array<BonusConfig>;
|
||||
}
|
||||
|
||||
interface IConfigBundle {
|
||||
public function get(type:GameType):Config;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Session, Bytes> {
|
||||
try {
|
||||
session.pushData(bytes);
|
||||
} catch (error:Dynamic) {
|
||||
L.d(TAG, "_", error);
|
||||
L.e(TAG, 'error', error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +54,9 @@ class Server extends ThreadServer<Session, Bytes> {
|
||||
#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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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)));
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -3,18 +3,29 @@ const through = require('through2');
|
||||
const colors = require('ansi-colors');
|
||||
const log = require('fancy-log');
|
||||
|
||||
const color = (level) => {
|
||||
return {
|
||||
const _colors = {
|
||||
'[DEBUG]': colors.white,
|
||||
'[INFO]': colors.cyan,
|
||||
'[ERROR]': colors.red,
|
||||
'[WARNING]': colors.yellow,
|
||||
}[level] || colors.reset;
|
||||
};
|
||||
|
||||
const _log = (line) => {
|
||||
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[0]) + ' ' + color(result[1])(result.slice(1).join(' ')));
|
||||
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", () => {
|
||||
|
||||
Reference in New Issue
Block a user