57 lines
1.6 KiB
JavaScript
57 lines
1.6 KiB
JavaScript
(function() {
|
|
var messageChannel = 'MESSAGE_CHANNEL_GATE_GAME';
|
|
var entityID;
|
|
|
|
function sendMessage(message) {
|
|
message.entityID = entityID;
|
|
Messages.sendMessage(
|
|
messageChannel,
|
|
JSON.stringify(message)
|
|
);
|
|
}
|
|
|
|
function onReceivedMessage(channel, message, senderID) {
|
|
try {
|
|
var data = JSON.parse(message);
|
|
} catch (e) {
|
|
print('Error parsing message: ' + message);
|
|
return;
|
|
}
|
|
print('data: ' + JSON.stringify(data));
|
|
|
|
switch (data.type) {
|
|
case 'RENDER':
|
|
renderScoreboard(data.racers);
|
|
break;
|
|
}
|
|
}
|
|
|
|
function renderScoreboard(racers) {
|
|
var text = '';
|
|
for (var sessionUUID in racers) {
|
|
var racer = racers[sessionUUID];
|
|
text += racer.avatarDisplayName + ' ' + racer.time + '\n';
|
|
}
|
|
Entities.editEntity(entityID, {
|
|
text: text
|
|
});
|
|
}
|
|
|
|
|
|
return {
|
|
preload: function(id) {
|
|
entityID = id;
|
|
Messages.subscribe(messageChannel);
|
|
Messages.messageReceived.connect(null, onReceivedMessage);
|
|
print('hi scoreboard');
|
|
},
|
|
unload: function(id) {
|
|
sendMessage({
|
|
type: 'REMOVE_RACER',
|
|
avatarSessionUUID: MyAvatar.sessionUUID
|
|
});
|
|
Messages.unsubscribe(messageChannel);
|
|
Messages.messageReceived.disconnect(null, onReceivedMessage);
|
|
},
|
|
};
|
|
})
|