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

70 lines
2.3 KiB
JavaScript

(function() {
var messageChannel = 'MESSAGE_CHANNEL_GATE_GAME';
var entityID;
var racers;
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 'PASS':
if (!racers[data.avatarSessionUUID]) {
racers[data.avatarSessionUUID] = {};
racers[data.avatarSessionUUID].avatarDisplayName = data.avatarDisplayName;
}
if (!racers[data.avatarSessionUUID].startTime) {
racers[data.avatarSessionUUID].startTime = Date.now();
racers[data.avatarSessionUUID].time = 'racing...';
} else {
racers[data.avatarSessionUUID].time = ((Date.now() - racers[data.avatarSessionUUID].startTime) / 1000).toFixed(3) + ' sec';
racers[data.avatarSessionUUID].startTime = null;
}
renderScoreboard();
break;
}
}
function renderScoreboard() {
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;
racers = {};
Messages.subscribe(messageChannel);
Messages.messageReceived.connect(null, onReceivedMessage);
print('Hello Scoreboard');
Entities.editEntity(entityID, {
text: 'Hello Scoreboard'
});
},
unload: function(id) {
Messages.unsubscribe(messageChannel);
Messages.messageReceived.disconnect(null, onReceivedMessage);
},
};
})