[connection] send queue in NekoConnection
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tankz",
|
||||
"version": "0.13.2",
|
||||
"version": "0.13.3",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"dateformat": "^3.0.3",
|
||||
|
||||
@@ -7,16 +7,22 @@ class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
public var socket(default, null):Socket;
|
||||
|
||||
private var sendQueue:Array<O>;
|
||||
private var timer:Timer;
|
||||
|
||||
public function new(socket:Socket, i:Class<I>) {
|
||||
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<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
opened = false;
|
||||
}
|
||||
|
||||
override private function _send(packet:O):Void {
|
||||
override private function sendPacket(packet:O):Void {
|
||||
var data = PacketUtil.toBytes(packet);
|
||||
writeData(data, socket);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,4 @@ class Entity {
|
||||
public function toString():String {
|
||||
return '$type($id)';
|
||||
}
|
||||
|
||||
public function dispose() {}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package ru.m.tankz.core;
|
||||
|
||||
import haxework.signal.Signal;
|
||||
import ru.m.Timer;
|
||||
|
||||
class Modificator extends Signal2<Int, Bool> {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -27,6 +27,7 @@ class GameRecorder implements GameListener {
|
||||
ticker.stop();
|
||||
case _:
|
||||
}
|
||||
ticker.tick();
|
||||
record.events.push({time: ticker.time, event: event});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user