content/hifi-content/ben/scripts/iamgreet_fsm.js
2022-02-13 21:50:01 +01:00

182 lines
No EOL
4.8 KiB
JavaScript

// Modified by Midnight 7/5/2017
// Further Modified by Flame Soulis 10/23/2017
(function () {
var APP_NAME = 'I AM GREET',
APP_ICON = Script.resolvePath('assets/heart.svg'),
APP_ICON_ACTIVE = Script.resolvePath('assets/heart-a.svg');
var tablet = Tablet.getTablet("com.highfidelity.interface.tablet.system"),
button = tablet.addButton({
icon: APP_ICON,
activeIcon: APP_ICON_ACTIVE,
text: APP_NAME
});
var BASE = 'https://metaverse.highfidelity.com/api/v1',
ENDPOINT = {
domain: BASE + '/domains/',
place: BASE + '/places/'
};
var domainID = '',
timer = null,
PLACE_NAME = 'Welcome',
NOTIFICATION_SOUND = SoundCache.getSound(Script.resolvePath('assets/bell.wav'));
var overlay = null;
function request(url, callback) {
var req = new XMLHttpRequest();
req.responseType = 'json';
req.open('GET', url, true);
req.send();
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
callback(req.response,true);
} else {
callback(null,false);
}
}
}
}
var lastUserCount = 0;
var lastOnlineCheck = false;
function updateHUD(data) {
var hudText = '';
hudText += PLACE_NAME + '\n';
if(data.online) {
hudText += '\n CONNECTED\n----------------------';
} else {
hudText += '\n OFFLINE\n----------------------';
}
if(data.online_users) {
hudText += '\n ' + data.online_users + ' Users';
hudText += '\n ' + data.online_anonymous_users + ' New\n';
}
if(data.online_users > lastUserCount) {
Audio.playSound(NOTIFICATION_SOUND, {
volume: 0.2,
localOnly: true,
position: MyAvatar.position
});
}
if(data.online_users != lastUserCount ||
data.online != lastOnlineCheck) {
//Only print IF the user count has changed
print(hudText);
}
lastUserCount = data.online_users ;
lastOnlineCheck = data.online;
//print(hudText);
Overlays.editOverlay(overlay, {text: hudText});
}
function clearOverlay() {
Overlays.deleteOverlay(overlay);
}
function update() {
if (!domainID) {
return;
}
function domainCallback(response,found) {
if (!found) {
print('Cannot find domain data', domainID);
return;
}
updateHUD(response.domain);
}
request(ENDPOINT.domain + domainID, domainCallback);
}
var singleRetry = true ;
function init() {
function placesCallback(response,found) {
if (!found) {
if(singleRetry) {
singleRetry = false;
init();
} else {
button.editProperties({isActive: false});
clean();
print('Cannot find place name.', PLACE_NAME);
return;
}
}
domainID = response.data.place.domain.id;
var WIDTH = 150;
var HEIGHT = 120;
var properties = {
color: {red: 255, green: 255, blue: 255},
alpha: 0.9,
backgroundColor: {red: 69, green: 142, blue: 220},
backgroundAlpha: 0.9,
font: {size: 20},
x: Window.innerWidth - WIDTH - 50,
y: 40,
width: WIDTH,
height: HEIGHT
};
overlay = Overlays.addOverlay('text', properties);
update();
timer = Script.setInterval(update, 2000);
}
request(ENDPOINT.place + PLACE_NAME, placesCallback);
}
this.clickDownOnEntity = function () {
location = 'hifi://' + PLACE_NAME
};
var _switch = true;
function buttonSwitch() {
if (_switch) {
init();
} else {
clean();
}
button.editProperties({isActive: _switch});
_switch = !_switch;
}
button.clicked.connect(buttonSwitch);
function clean() {
Script.clearInterval(timer);
if (overlay) clearOverlay();
singleRetry = true;
}
function cleanAll() {
clean();
tablet.removeButton(button);
}
Script.scriptEnding.connect(cleanAll);
})();