Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jochen Bauer
spn-website
Commits
4316ca23
Commit
4316ca23
authored
May 04, 2018
by
Hubert Denkmair
Browse files
simplify protocol handling
parent
2e968461
Changes
5
Hide whitespace changes
Inline
Side-by-side
ide/static/ide/ide.js
View file @
4316ca23
...
...
@@ -39,7 +39,7 @@ function setupPreview()
{
game
=
new
Game
(
assets
,
strategy
,
document
.
getElementById
(
'
preview
'
));
game
.
SetViewerKey
(
viewer_key
);
game
.
protocol
.
AddEvent
Handler
(
'
Log
'
,
addLogLine
);
game
.
AddLog
Handler
(
addLogLine
);
game
.
Run
();
game
.
vis
.
FollowDbId
(
snake_id
);
}
...
...
visualization/static/visualization/Game.js
View file @
4316ca23
...
...
@@ -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
visualization/static/visualization/GameVisualization.js
View file @
4316ca23
...
...
@@ -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
;
...
...
visualization/static/visualization/JsonProtocol.js
deleted
100644 → 0
View file @
2e968461
"
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
;
}
};
}
visualization/templates/visualization/js.html
View file @
4316ca23
{% 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" %}
'
,
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment