Commit 4316ca23 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

simplify protocol handling

parent 2e968461
......@@ -39,7 +39,7 @@ function setupPreview()
{
game = new Game(assets, strategy, document.getElementById('preview'));
game.SetViewerKey(viewer_key);
game.protocol.AddEventHandler('Log', addLogLine);
game.AddLogHandler(addLogLine);
game.Run();
game.vis.FollowDbId(snake_id);
}
......
......@@ -7,21 +7,14 @@ function Game(assets, snakeMoveStrategy, container)
this.speed = 2;
this.viewer_key = 0;
this.vis = new GameVisualization(assets, snakeMoveStrategy, container);
this.protocol = new JsonProtocol();
this.protocol.AddEventHandler('GameInfo', this.vis.HandleGameInfoMessage, this.vis);
this.protocol.AddEventHandler('PlayerInfo', this.vis.HandlePlayerInfoMessage, this.vis);
this.protocol.AddEventHandler('Tick', this.vis.HandleTickMessage, this.vis);
this.protocol.AddEventHandler('WorldUpdate', this.vis.HandleWorldUpdateMessage, this.vis);
this.protocol.AddEventHandler('BotSpawn', this.vis.HandleBotSpawnMessage, this.vis);
this.protocol.AddEventHandler('BotKilled', this.vis.HandleBotKilledMessage, this.vis);
this.protocol.AddEventHandler('FoodSpawn', this.vis.HandleFoodSpawnMessage, this.vis);
this.protocol.AddEventHandler('FoodConsumed', this.vis.HandleFoodConsumedMessage, this.vis);
this.protocol.AddEventHandler('FoodDecayed', this.vis.HandleFoodDecayedMessage, this.vis);
this.protocol.AddEventHandler('BotMoved', this.vis.HandleBotMovedMessage, this.vis);
this.protocol.AddEventHandler('BotsMovedDone', this.vis.HandleBotMovedMessagesDone, this.vis);
this.logHandlers = [];
}
Game.prototype.AddLogHandler = function(callback, thisArg)
{
this.logHandlers.push([callback, thisArg]);
};
Game.prototype.Run = function()
{
this.ConnectWebsocket();
......@@ -41,7 +34,7 @@ Game.prototype.ConnectWebsocket = function()
}
});
this.ws.addEventListener('message', function(event) {
self.protocol.HandleMessage(event);
self.HandleMessage(event);
});
};
......@@ -59,4 +52,66 @@ Game.prototype.SetViewerKey = function(key)
let s = JSON.stringify(msg);
this.ws.send(s);
}
};
Game.prototype.HandleMessage = function(event)
{
let data = JSON.parse(event.data);
switch (data.t)
{
case "GameInfo":
return this.vis.HandleGameInfoMessage(data.world_size_x, data.world_size_y, data.food_decay_per_frame);
case "WorldUpdate":
return this.vis.HandleWorldUpdateMessage(data);
case "Tick":
return this.vis.HandleTickMessage(data.frame_id);
case "BotSpawn":
return this.vis.HandleBotSpawnMessage(data.bot);
case "BotKill":
return this.vis.HandleBotKilledMessage(data.killer_id, data.victim_id);
case "BotMove":
for (let i=0; i<data.items.length; i++)
{
let b = data.items[i];
this.vis.HandleBotMovedMessage(b.bot_id, b.segment_data, b.length, b.segment_radius);
}
return this.vis.HandleTickMessage(null); // FIXME this is a workaround because we somehow do not receive TickMessage
case "FoodSpawn":
for (let item of data.items)
{
this.vis.HandleFoodSpawnMessage(item.id, item.pos_x, item.pos_y, item.value);
}
return;
case "FoodConsume":
for (let item of data.items)
{
this.vis.HandleFoodConsumedMessage(item.food_id, item.bot_id);
}
return;
case "FoodDecay":
for (let item of data.items)
{
this.vis.HandleFoodDecayedMessage(item);
}
return;
case "Log":
for (let item of this.logHandlers)
{
item[0].call(item[1], data.frame, data,msg);
}
return;
default:
return;
}
};
\ No newline at end of file
......@@ -118,10 +118,8 @@ GameVisualization.prototype.HandleTickMessage = function(frame_id)
this.foodMap.CleanUp();
}
};
GameVisualization.prototype.HandlePlayerInfoMessage = function(player_id)
{
this.UpdateStagePosition();
};
GameVisualization.prototype.HandleWorldUpdateMessage = function(data)
......@@ -209,12 +207,6 @@ GameVisualization.prototype.HandleBotMoved2Message = function(bot_id, heading, s
}
};
GameVisualization.prototype.HandleBotMovedMessagesDone = function(data)
{
this.HandleTickMessage();
this.UpdateStagePosition();
};
GameVisualization.prototype.FollowDbId = function(db_id)
{
this.follow_db_id = db_id;
......
"use strict";
function JsonProtocol()
{
this.GameInfoMessageHandlers = [];
this.TickMessageHandlers = [];
this.PlayerInfoMessageHandlers = [];
this.WorldUpdateMessageHandlers = [];
this.BotSpawnMessageHandlers = [];
this.BotMovedMessageHandlers = [];
this.BotsMovedDoneMessageHandlers = [];
this.BotKilledMessageHandlers = [];
this.FoodSpawnMessageHandlers = [];
this.FoodConsumedMessageHandlers = [];
this.FoodDecayedMessageHandlers = [];
this.LogMessageHandlers = [];
this.AddEventHandler = function(messageName, func, thisArg)
{
let varName = messageName + 'MessageHandlers'
let arr = this[varName];
if (!arr)
{
console.error("cannot register handler for event '"+messageName+"': property '" + varName + "' does not exist.")
return;
}
arr.push([func, thisArg]);
};
this.CallHandlers = function(handlers, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
{
for (let i in handlers)
{
let func = handlers[i][0];
let thisArg = handlers[i][1];
func.call(thisArg, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
return true;
};
this.HandleMessage = function(event)
{
let data = JSON.parse(event.data);
//console.log(data);
switch (data.t)
{
case "GameInfo":
{
this.CallHandlers(this.GameInfoMessageHandlers, data.world_size_x, data.world_size_y, data.food_decay_per_frame);
return;
}
case "WorldUpdate":
{
this.CallHandlers(this.WorldUpdateMessageHandlers, data);
return;
}
case "Tick":
{
this.CallHandlers(this.TickMessageHandlers, data.frame_id);
return;
}
case "BotSpawn":
{
this.CallHandlers(this.BotSpawnMessageHandlers, data.bot);
return;
}
case "BotKill":
{
this.CallHandlers(this.BotKilledMessageHandlers, data.killer_id, data.victim_id);
return;
}
case "BotMove":
{
for (let i=0; i<data.items.length; i++)
{
let b = data.items[i];
this.CallHandlers(this.BotMovedMessageHandlers, b.bot_id, b.segment_data, b.length, b.segment_radius);
}
this.CallHandlers(this.BotsMovedDoneMessageHandlers);
return;
}
case "Log":
{
this.CallHandlers(this.LogMessageHandlers, data.frame, data.msg);
return;
}
case "FoodSpawn":
{
for (let i=0; i<data.items.length; i++)
{
let f = data.items[i];
this.CallHandlers(this.FoodSpawnMessageHandlers, f.id, f.pos_x, f.pos_y, f.value);
}
return;
}
case "FoodConsume":
{
for (let i in data.items)
{
let item = data.items[i];
this.CallHandlers(this.FoodConsumedMessageHandlers, item.food_id, item.bot_id);
}
return;
}
case "FoodDecay":
{
for (let i in data.items)
{
this.CallHandlers(this.FoodDecayedMessageHandlers, data.items[i]);
}
return;
}
/*case 0xF0:
{
let player_id = data[2];
this.CallHandlers(this.PlayerInfoMessageHandlers, player_id);
return;
}*/
default:
return;
}
};
}
{% load static %}
<script src="{% static "visualization/lib/pixi.min.js" %}"></script>
<script src="{% static "visualization/lib/websocket/reconnecting-websocket.min.js" %}"></script>
<script src="{% static "visualization/JsonProtocol.js" %}"></script>
<script src="{% static "visualization/ParticleGeoMap.js" %}"></script>
<script src="{% static "visualization/Snake.js" %}"></script>
<script src="{% static "visualization/FoodSprite.js" %}"></script>
<script src="{% static "visualization/SimpleDirectionSnakeMoveStrategy.js" %}"></script>
<script src="{% static "visualization/ImpulseSnakeMoveStrategy.js" %}"></script>
<script src="{% static "visualization/TwoPredecessorSnakeMoveStrategy.js" %}"></script>
<script src="{% static "visualization/GameVisualization.js" %}"></script>
<script src="{% static "visualization/Game.js" %}"></script>
<script src="{% static "visualization/DynamicSegmentCountSnakeMoveStrategy.js" %}"></script>
<script>
let assets = {
'body.png': '{% static "visualization/assets/body.png" %}',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment