[connection] send queue in NekoConnection
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tankz",
|
"name": "tankz",
|
||||||
"version": "0.13.2",
|
"version": "0.13.3",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"dateformat": "^3.0.3",
|
"dateformat": "^3.0.3",
|
||||||
|
|||||||
@@ -7,16 +7,22 @@ class NekoConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
|||||||
|
|
||||||
public var socket(default, null):Socket;
|
public var socket(default, null):Socket;
|
||||||
|
|
||||||
|
private var sendQueue:Array<O>;
|
||||||
|
private var timer:Timer;
|
||||||
|
|
||||||
public function new(socket:Socket, i:Class<I>) {
|
public function new(socket:Socket, i:Class<I>) {
|
||||||
super(i);
|
super(i);
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
socket.setFastSend(true);
|
socket.setFastSend(true);
|
||||||
socket.output.bigEndian = false;
|
socket.output.bigEndian = false;
|
||||||
socket.input.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 {
|
try {
|
||||||
var bytes = PacketUtil.toBytes(packet);
|
var bytes = PacketUtil.toBytes(packet);
|
||||||
socket.output.writeUInt16(bytes.length);
|
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);
|
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;
|
opened = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
override private function _send(packet:O):Void {
|
override private function sendPacket(packet:O):Void {
|
||||||
var data = PacketUtil.toBytes(packet);
|
var data = PacketUtil.toBytes(packet);
|
||||||
writeData(data, socket);
|
writeData(data, socket);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class Eagle extends Entity {
|
|||||||
public var config(default, null):EagleConfig;
|
public var config(default, null):EagleConfig;
|
||||||
public var color(default, default):Color;
|
public var color(default, default):Color;
|
||||||
public var death(default, default):Bool;
|
public var death(default, default):Bool;
|
||||||
public var protect(default, null):Modificator;
|
public var protect(default, default):Bool;
|
||||||
|
|
||||||
public var score(get, null):Int;
|
public var score(get, null):Int;
|
||||||
|
|
||||||
@@ -20,15 +20,10 @@ class Eagle extends Entity {
|
|||||||
this.team = team;
|
this.team = team;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.death = false;
|
this.death = false;
|
||||||
this.protect = new Modificator(id);
|
this.protect = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline function get_score():Int {
|
private inline function get_score():Int {
|
||||||
return config != null ? config.score : 0;
|
return config != null ? config.score : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
override public function dispose() {
|
|
||||||
super.dispose();
|
|
||||||
protect.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,4 @@ class Entity {
|
|||||||
public function toString():String {
|
public function toString():String {
|
||||||
return '$type($id)';
|
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 color(default, default):Color;
|
||||||
public var hits(default, default):Int;
|
public var hits(default, default):Int;
|
||||||
public var bonus(default, default):Bool;
|
public var bonus(default, default):Bool;
|
||||||
public var protect(default, null):Modificator;
|
public var protect(default, default):Bool;
|
||||||
public var freezing(default, null):Modificator;
|
public var freezing(default, default):Bool;
|
||||||
|
|
||||||
public function new(id:Int, rect:Rectangle, playerId:PlayerId, config:TankConfig) {
|
public function new(id:Int, rect:Rectangle, playerId:PlayerId, config:TankConfig) {
|
||||||
super(id, rect, config.speed, Direction.RIGHT);
|
super(id, rect, config.speed, Direction.RIGHT);
|
||||||
this.protect = new Modificator(id);
|
this.protect = false;
|
||||||
this.freezing = new Modificator(id);
|
this.freezing = false;
|
||||||
this.playerId = playerId;
|
this.playerId = playerId;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.layer = 1;
|
this.layer = 1;
|
||||||
@@ -35,14 +35,8 @@ class Tank extends MobileEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override public function move(direction:Direction):Void {
|
override public function move(direction:Direction):Void {
|
||||||
if (!freezing.active) {
|
if (!freezing) {
|
||||||
super.move(direction);
|
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 {
|
public function dispose():Void {
|
||||||
for (entity in allEntities) {
|
|
||||||
entity.dispose();
|
|
||||||
}
|
|
||||||
allEntities = new Map();
|
allEntities = new Map();
|
||||||
entities = new Map();
|
entities = new Map();
|
||||||
//map = null;
|
//map = null;
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ class Ticker implements ITicker {
|
|||||||
var action = it.next();
|
var action = it.next();
|
||||||
var i = 0;
|
var i = 0;
|
||||||
while (action != null && time >= action.time) {
|
while (action != null && time >= action.time) {
|
||||||
L.d("Ticker", 'run action: $action(${action.time}) on $time');
|
|
||||||
action.action();
|
action.action();
|
||||||
i++;
|
i++;
|
||||||
action = it.next();
|
action = it.next();
|
||||||
@@ -69,7 +68,6 @@ class Ticker implements ITicker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function emit(action:Void->Void, delay:Int):Void {
|
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.push({action:action, time:time+delay});
|
||||||
actions.sort(function(a, b) return a.time - b.time);
|
actions.sort(function(a, b) return a.time - b.time);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,9 +52,6 @@ class EntityBuilder {
|
|||||||
if (!bonusOff) {
|
if (!bonusOff) {
|
||||||
tank.bonus = Math.random() < playerConfig.bonus;
|
tank.bonus = Math.random() < playerConfig.bonus;
|
||||||
}
|
}
|
||||||
if (playerConfig.protect > 0) {
|
|
||||||
tank.protect.on(playerConfig.protect);
|
|
||||||
}
|
|
||||||
return tank;
|
return tank;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,12 +27,14 @@ class GameRunner extends Game implements EngineListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override function changePause(value:Bool):Void {
|
override function changePause(value:Bool):Void {
|
||||||
|
if (engine != null) {
|
||||||
if (value) {
|
if (value) {
|
||||||
engine.ticker.stop();
|
engine.ticker.stop();
|
||||||
} else {
|
} else {
|
||||||
engine.ticker.start();
|
engine.ticker.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function update():Void {
|
private function update():Void {
|
||||||
engine.update();
|
engine.update();
|
||||||
@@ -62,7 +64,6 @@ class GameRunner extends Game implements EngineListener {
|
|||||||
var eagle = builder.buildEagle(point, team.id);
|
var eagle = builder.buildEagle(point, team.id);
|
||||||
engine.spawn(eagle);
|
engine.spawn(eagle);
|
||||||
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
|
gameEventSignal.emit(EventUtil.buildEagleSpawn(eagle));
|
||||||
eagle.protect.connect(onEagleProtectChange);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(EventUtil.buildBricksSpawn(engine.map));
|
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);
|
var tank = builder.buildTank(task.point, task.playerId, task.tankType, player.state.color);
|
||||||
engine.spawn(tank);
|
engine.spawn(tank);
|
||||||
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
gameEventSignal.emit(EventUtil.buildTankSpawn(tank));
|
||||||
tank.protect.connect(onTankProtectChange);
|
if (player.config.protect > 0) {
|
||||||
tank.freezing.connect(onTankFreezingChange);
|
protectTank(tank, player.config.protect);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function onEagleProtectChange(id:Int, state:Bool):Void {
|
private function protectEagle(eagle:Eagle, duration:Float):Void {
|
||||||
gameEventSignal.emit(CHANGE(EAGLE_PROTECT(id, state)));
|
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 {
|
private function protectTank(tank:Tank, duration:Float):Void {
|
||||||
gameEventSignal.emit(CHANGE(TANK_PROTECT(id, state)));
|
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 {
|
private function freezeTank(tank:Tank, duration:Float):Void {
|
||||||
gameEventSignal.emit(CHANGE(TANK_FREEZE(id, state)));
|
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 {
|
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)) {
|
if (bullet.tankId == tank.id || (!config.game.friendlyFire && tank.playerId.team == bullet.playerId.team)) {
|
||||||
// Nothing
|
// Nothing
|
||||||
} else {
|
} else {
|
||||||
if (!tank.protect.active) {
|
if (!tank.protect) {
|
||||||
if (tank.hits > 0) {
|
if (tank.hits > 0) {
|
||||||
tank.hits--;
|
tank.hits--;
|
||||||
if (tank.bonus) {
|
if (tank.bonus) {
|
||||||
@@ -199,7 +216,7 @@ class GameRunner extends Game implements EngineListener {
|
|||||||
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
||||||
}
|
}
|
||||||
case [BULLET(bullet), EAGLE(eagle)]:
|
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(EAGLE(eagle.id, buildShot(bullet, eagle.score))));
|
||||||
}
|
}
|
||||||
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
|
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})));
|
gameEventSignal.emit(DESTROY(TANK(t.id, {tankId: tank.id})));
|
||||||
}
|
}
|
||||||
case "helmet":
|
case "helmet":
|
||||||
tank.protect.on(bonus.config.duration);
|
protectTank(tank, bonus.config.duration);
|
||||||
case "clock":
|
case "clock":
|
||||||
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
|
||||||
t.freezing.on(bonus.config.duration);
|
freezeTank(t, bonus.config.duration);
|
||||||
t.stop();
|
t.stop();
|
||||||
gameEventSignal.emit(STOP(TANK(t.id)));
|
gameEventSignal.emit(STOP(TANK(t.id)));
|
||||||
}
|
}
|
||||||
@@ -259,7 +276,7 @@ class GameRunner extends Game implements EngineListener {
|
|||||||
var team:Team = teams[tank.playerId.team];
|
var team:Team = teams[tank.playerId.team];
|
||||||
if (team.eagleId > 0) {
|
if (team.eagleId > 0) {
|
||||||
var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle);
|
var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle);
|
||||||
eagle.protect.on(bonus.config.duration);
|
protectEagle(eagle, bonus.config.duration);
|
||||||
}
|
}
|
||||||
case "gun":
|
case "gun":
|
||||||
upgradeTank(tank, 5);
|
upgradeTank(tank, 5);
|
||||||
@@ -321,7 +338,7 @@ class GameRunner extends Game implements EngineListener {
|
|||||||
case ACTION(tankId, SHOT):
|
case ACTION(tankId, SHOT):
|
||||||
var tank:Tank = cast engine.entities.get(tankId);
|
var tank:Tank = cast engine.entities.get(tankId);
|
||||||
var player = getPlayer(tank.playerId);
|
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 rect = tank.rect;
|
||||||
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
|
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);
|
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.engine.ITicker;
|
||||||
import ru.m.tankz.config.Config;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.Type;
|
import ru.m.tankz.Type;
|
||||||
import ru.m.Timer;
|
|
||||||
|
|
||||||
typedef SpawnTask = {
|
typedef SpawnTask = {
|
||||||
var point:SpawnPoint;
|
var point:SpawnPoint;
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ class GameRecorder implements GameListener {
|
|||||||
ticker.stop();
|
ticker.stop();
|
||||||
case _:
|
case _:
|
||||||
}
|
}
|
||||||
|
ticker.tick();
|
||||||
record.events.push({time: ticker.time, event: event});
|
record.events.push({time: ticker.time, event: event});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user