From 4c8ae66624abbd2fecec511bdd6abadcd0b5013e Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 3 Jun 2019 11:23:20 +0300 Subject: [PATCH] [connection] send queue in NekoConnection --- package.json | 2 +- .../haxe/ru/m/connect/neko/NekoConnection.hx | 31 ++++++++++- .../ru/m/connect/neko/NekoWSConnection.hx | 2 +- src/common/haxe/ru/m/tankz/core/Eagle.hx | 9 +-- src/common/haxe/ru/m/tankz/core/Entity.hx | 2 - .../haxe/ru/m/tankz/core/Modificator.hx | 38 ------------- src/common/haxe/ru/m/tankz/core/Tank.hx | 16 ++---- src/common/haxe/ru/m/tankz/engine/Engine.hx | 3 - src/common/haxe/ru/m/tankz/engine/Ticker.hx | 2 - .../haxe/ru/m/tankz/game/EntityBuilder.hx | 3 - src/common/haxe/ru/m/tankz/game/GameRunner.hx | 55 ++++++++++++------- src/common/haxe/ru/m/tankz/game/Spawner.hx | 1 - .../ru/m/tankz/game/record/GameRecorder.hx | 1 + 13 files changed, 75 insertions(+), 90 deletions(-) delete mode 100644 src/common/haxe/ru/m/tankz/core/Modificator.hx diff --git a/package.json b/package.json index 3335b48..9b57343 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tankz", - "version": "0.13.2", + "version": "0.13.3", "private": true, "devDependencies": { "dateformat": "^3.0.3", diff --git a/src/common/haxe/ru/m/connect/neko/NekoConnection.hx b/src/common/haxe/ru/m/connect/neko/NekoConnection.hx index 53626a3..24dd0e4 100755 --- a/src/common/haxe/ru/m/connect/neko/NekoConnection.hx +++ b/src/common/haxe/ru/m/connect/neko/NekoConnection.hx @@ -7,16 +7,22 @@ class NekoConnection extends BaseConnection { public var socket(default, null):Socket; + private var sendQueue:Array; + private var timer:Timer; + public function new(socket:Socket, i:Class) { super(i); this.socket = socket; socket.setFastSend(true); socket.output.bigEndian = false; socket.input.bigEndian = false; - sendHandler.connect(_send); + sendHandler.connect(pushPacket); + sendQueue = []; + timer = new Timer(1); + timer.run = sendRun; } - private function _send(packet:O):Void { + private function sendPacket(packet:O):Void { try { var bytes = PacketUtil.toBytes(packet); socket.output.writeUInt16(bytes.length); @@ -26,4 +32,25 @@ class NekoConnection extends BaseConnection { L.e('Proto', 'Error send packet: ${packet}', error); } } + + private function sendRun():Void { + if (sendQueue.length > 0) { + for (packet in sendQueue) { + sendPacket(packet); + } + sendQueue = []; + } + } + + private function pushPacket(packet:O):Void { + sendQueue.push(packet); + } + + override public function disconnect():Void { + if (timer != null) { + timer.stop(); + timer = null; + } + super.disconnect(); + } } diff --git a/src/common/haxe/ru/m/connect/neko/NekoWSConnection.hx b/src/common/haxe/ru/m/connect/neko/NekoWSConnection.hx index 84c4aa3..5938bbc 100644 --- a/src/common/haxe/ru/m/connect/neko/NekoWSConnection.hx +++ b/src/common/haxe/ru/m/connect/neko/NekoWSConnection.hx @@ -16,7 +16,7 @@ class NekoWSConnection extends NekoConnection { opened = false; } - override private function _send(packet:O):Void { + override private function sendPacket(packet:O):Void { var data = PacketUtil.toBytes(packet); writeData(data, socket); } diff --git a/src/common/haxe/ru/m/tankz/core/Eagle.hx b/src/common/haxe/ru/m/tankz/core/Eagle.hx index 93bd51a..9b8f6ef 100644 --- a/src/common/haxe/ru/m/tankz/core/Eagle.hx +++ b/src/common/haxe/ru/m/tankz/core/Eagle.hx @@ -11,7 +11,7 @@ class Eagle extends Entity { public var config(default, null):EagleConfig; public var color(default, default):Color; public var death(default, default):Bool; - public var protect(default, null):Modificator; + public var protect(default, default):Bool; public var score(get, null):Int; @@ -20,15 +20,10 @@ class Eagle extends Entity { this.team = team; this.config = config; this.death = false; - this.protect = new Modificator(id); + this.protect = false; } private inline function get_score():Int { return config != null ? config.score : 0; } - - override public function dispose() { - super.dispose(); - protect.dispose(); - } } diff --git a/src/common/haxe/ru/m/tankz/core/Entity.hx b/src/common/haxe/ru/m/tankz/core/Entity.hx index 570a7ab..172591d 100755 --- a/src/common/haxe/ru/m/tankz/core/Entity.hx +++ b/src/common/haxe/ru/m/tankz/core/Entity.hx @@ -18,6 +18,4 @@ class Entity { public function toString():String { return '$type($id)'; } - - public function dispose() {} } diff --git a/src/common/haxe/ru/m/tankz/core/Modificator.hx b/src/common/haxe/ru/m/tankz/core/Modificator.hx deleted file mode 100644 index bf9a871..0000000 --- a/src/common/haxe/ru/m/tankz/core/Modificator.hx +++ /dev/null @@ -1,38 +0,0 @@ -package ru.m.tankz.core; - -import haxework.signal.Signal; -import ru.m.Timer; - -class Modificator extends Signal2 { - - public var id(default, default):Int; - public var active(default, default):Bool; - private var timer:Timer; - - public function new(id) { - super(); - this.id = id; - active = false; - } - - override public function connect(receiver:Int->Bool->Void):Void { - super.connect(receiver); - receiver(id, active); - } - - public function on(seconds:Float):Void { - off(); - active = true; - timer = Timer.delay(off, Std.int(seconds * 1000)); - emit(id, true); - } - - public function off():Void { - if (timer != null) { - timer.stop(); - timer = null; - emit(id, false); - } - active = false; - } -} diff --git a/src/common/haxe/ru/m/tankz/core/Tank.hx b/src/common/haxe/ru/m/tankz/core/Tank.hx index efe564f..8279043 100755 --- a/src/common/haxe/ru/m/tankz/core/Tank.hx +++ b/src/common/haxe/ru/m/tankz/core/Tank.hx @@ -12,13 +12,13 @@ class Tank extends MobileEntity { public var color(default, default):Color; public var hits(default, default):Int; public var bonus(default, default):Bool; - public var protect(default, null):Modificator; - public var freezing(default, null):Modificator; + public var protect(default, default):Bool; + public var freezing(default, default):Bool; public function new(id:Int, rect:Rectangle, playerId:PlayerId, config:TankConfig) { super(id, rect, config.speed, Direction.RIGHT); - this.protect = new Modificator(id); - this.freezing = new Modificator(id); + this.protect = false; + this.freezing = false; this.playerId = playerId; this.config = config; this.layer = 1; @@ -35,14 +35,8 @@ class Tank extends MobileEntity { } override public function move(direction:Direction):Void { - if (!freezing.active) { + if (!freezing) { super.move(direction); } } - - override public function dispose() { - super.dispose(); - protect.dispose(); - freezing.dispose(); - } } diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 951d166..ddf73a6 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -136,9 +136,6 @@ import ru.m.tankz.map.LevelMap; } public function dispose():Void { - for (entity in allEntities) { - entity.dispose(); - } allEntities = new Map(); entities = new Map(); //map = null; diff --git a/src/common/haxe/ru/m/tankz/engine/Ticker.hx b/src/common/haxe/ru/m/tankz/engine/Ticker.hx index 1de03fb..6adcb6e 100644 --- a/src/common/haxe/ru/m/tankz/engine/Ticker.hx +++ b/src/common/haxe/ru/m/tankz/engine/Ticker.hx @@ -58,7 +58,6 @@ class Ticker implements ITicker { var action = it.next(); var i = 0; while (action != null && time >= action.time) { - L.d("Ticker", 'run action: $action(${action.time}) on $time'); action.action(); i++; action = it.next(); @@ -69,7 +68,6 @@ class Ticker implements ITicker { } public function emit(action:Void->Void, delay:Int):Void { - L.d("Ticker", 'emit: $action on ${time+delay}'); actions.push({action:action, time:time+delay}); actions.sort(function(a, b) return a.time - b.time); } diff --git a/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx b/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx index 7093a55..63c9c67 100644 --- a/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx +++ b/src/common/haxe/ru/m/tankz/game/EntityBuilder.hx @@ -52,9 +52,6 @@ class EntityBuilder { if (!bonusOff) { tank.bonus = Math.random() < playerConfig.bonus; } - if (playerConfig.protect > 0) { - tank.protect.on(playerConfig.protect); - } return tank; } diff --git a/src/common/haxe/ru/m/tankz/game/GameRunner.hx b/src/common/haxe/ru/m/tankz/game/GameRunner.hx index 088c60b..747b012 100644 --- a/src/common/haxe/ru/m/tankz/game/GameRunner.hx +++ b/src/common/haxe/ru/m/tankz/game/GameRunner.hx @@ -27,10 +27,12 @@ class GameRunner extends Game implements EngineListener { } override function changePause(value:Bool):Void { - if (value) { - engine.ticker.stop(); - } else { - engine.ticker.start(); + if (engine != null) { + if (value) { + engine.ticker.stop(); + } else { + engine.ticker.start(); + } } } @@ -62,7 +64,6 @@ class GameRunner extends Game implements EngineListener { var eagle = builder.buildEagle(point, team.id); engine.spawn(eagle); gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle)); - eagle.protect.connect(onEagleProtectChange); } } gameEventSignal.emit(EventUtil.buildBricksSpawn(engine.map)); @@ -75,20 +76,36 @@ class GameRunner extends Game implements EngineListener { var tank = builder.buildTank(task.point, task.playerId, task.tankType, player.state.color); engine.spawn(tank); gameEventSignal.emit(EventUtil.buildTankSpawn(tank)); - tank.protect.connect(onTankProtectChange); - tank.freezing.connect(onTankFreezingChange); + if (player.config.protect > 0) { + protectTank(tank, player.config.protect); + } } - private function onEagleProtectChange(id:Int, state:Bool):Void { - gameEventSignal.emit(CHANGE(EAGLE_PROTECT(id, state))); + private function protectEagle(eagle:Eagle, duration:Float):Void { + eagle.protect = true; + gameEventSignal.emit(CHANGE(EAGLE_PROTECT(eagle.id, eagle.protect))); + engine.ticker.emit(function() { + eagle.protect = false; + gameEventSignal.emit(CHANGE(EAGLE_PROTECT(eagle.id, eagle.protect))); + }, Std.int(duration * 1000)); } - private function onTankProtectChange(id:Int, state:Bool):Void { - gameEventSignal.emit(CHANGE(TANK_PROTECT(id, state))); + private function protectTank(tank:Tank, duration:Float):Void { + tank.protect = true; + gameEventSignal.emit(CHANGE(TANK_PROTECT(tank.id, tank.protect))); + engine.ticker.emit(function() { + tank.protect = false; + gameEventSignal.emit(CHANGE(TANK_PROTECT(tank.id, tank.protect))); + }, Std.int(duration * 1000)); } - private function onTankFreezingChange(id:Int, state:Bool):Void { - gameEventSignal.emit(CHANGE(TANK_FREEZE(id, state))); + private function freezeTank(tank:Tank, duration:Float):Void { + tank.freezing = true; + gameEventSignal.emit(CHANGE(TANK_FREEZE(tank.id, tank.freezing))); + engine.ticker.emit(function() { + tank.freezing = false; + gameEventSignal.emit(CHANGE(TANK_FREEZE(tank.id, tank.freezing))); + }, Std.int(duration * 1000)); } private function checkComplete():Void { @@ -175,7 +192,7 @@ class GameRunner extends Game implements EngineListener { if (bullet.tankId == tank.id || (!config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) { // Nothing } else { - if (!tank.protect.active) { + if (!tank.protect) { if (tank.hits > 0) { tank.hits--; if (tank.bonus) { @@ -199,7 +216,7 @@ class GameRunner extends Game implements EngineListener { gameEventSignal.emit(DESTROY(BULLET(bullet.id))); } case [BULLET(bullet), EAGLE(eagle)]: - if (!eagle.protect.active) { + if (!eagle.protect) { gameEventSignal.emit(DESTROY(EAGLE(eagle.id, buildShot(bullet, eagle.score)))); } gameEventSignal.emit(DESTROY(BULLET(bullet.id))); @@ -247,10 +264,10 @@ class GameRunner extends Game implements EngineListener { gameEventSignal.emit(DESTROY(TANK(t.id, {tankId: tank.id}))); } case "helmet": - tank.protect.on(bonus.config.duration); + protectTank(tank, bonus.config.duration); case "clock": for (t in engine.iterTanks(alienTank(tank.playerId.team))) { - t.freezing.on(bonus.config.duration); + freezeTank(t, bonus.config.duration); t.stop(); gameEventSignal.emit(STOP(TANK(t.id))); } @@ -259,7 +276,7 @@ class GameRunner extends Game implements EngineListener { var team:Team = teams[tank.playerId.team]; if (team.eagleId > 0) { var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle); - eagle.protect.on(bonus.config.duration); + protectEagle(eagle, bonus.config.duration); } case "gun": upgradeTank(tank, 5); @@ -321,7 +338,7 @@ class GameRunner extends Game implements EngineListener { case ACTION(tankId, SHOT): var tank:Tank = cast engine.entities.get(tankId); var player = getPlayer(tank.playerId); - if (!tank.freezing.active && player.bullets < tank.config.bullets) { + if (!tank.freezing && player.bullets < tank.config.bullets) { var rect = tank.rect; var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y)); var bullet = builder.buildBullet(point, rect.direction, player.id, tank.config.type); diff --git a/src/common/haxe/ru/m/tankz/game/Spawner.hx b/src/common/haxe/ru/m/tankz/game/Spawner.hx index 6f02be8..b8092e9 100644 --- a/src/common/haxe/ru/m/tankz/game/Spawner.hx +++ b/src/common/haxe/ru/m/tankz/game/Spawner.hx @@ -3,7 +3,6 @@ package ru.m.tankz.game; import ru.m.tankz.engine.ITicker; import ru.m.tankz.config.Config; import ru.m.tankz.Type; -import ru.m.Timer; typedef SpawnTask = { var point:SpawnPoint; diff --git a/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx b/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx index 6b85075..ba3b7ce 100644 --- a/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx +++ b/src/common/haxe/ru/m/tankz/game/record/GameRecorder.hx @@ -27,6 +27,7 @@ class GameRecorder implements GameListener { ticker.stop(); case _: } + ticker.tick(); record.events.push({time: ticker.time, event: event}); } }