content/hifi-content/wadewatts/scoreboard_client.js
2022-02-14 02:04:11 +01:00

53 lines
1.4 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) {
Messages.unsubscribe(messageChannel);
Messages.messageReceived.disconnect(null, onReceivedMessage);
},
};
})