content/hifi-content/liv/dev/Bingo/card/card.js
2022-02-14 02:04:11 +01:00

336 lines
No EOL
12 KiB
JavaScript

/* globals AccountServices */
(function(){
var TABLET_BUTTON_IMAGE = Script.resolvePath('../assets/icons/bingo-i.svg');
var TABLET_BUTTON_PRESSED = Script.resolvePath('../assets/icons/bingo-a.svg');
var WIN_SOUND = SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoWin.wav"));
var WINNER_AUDIO_VOLUME = 1;
var BINGO_STRING = "BINGO";
var headerSounds = [];
var currentHeaderSounds = [];
var winParticleEffects = [];
var injector;
var tablet = Tablet.getTablet('com.highfidelity.interface.tablet.system');
var appPage = Script.resolvePath('card.html');
var button = tablet.addButton({
text: 'BINGO',
icon: TABLET_BUTTON_IMAGE,
activeIcon: TABLET_BUTTON_PRESSED
});
var open = false;
var userName = AccountServices.username;
var userCardNumbers = [];
var spreadsheetURL = "https://script.google.com/macros/s/AKfycbzFuuJ30c_qUZmBB8PnjLtunaJx1VbhSRFjsy_6wocR2_p7wohJ/exec";
function onClicked() {
if (open) {
tablet.gotoHomeScreen();
} else {
tablet.gotoWebScreen(appPage);
}
}
function contains(array, number) {
for (var i = 0 ; i < array.length; i++) {
if (array[i] === number) {
return true;
}
}
return false;
}
function getRandomSound() {
var newSoundIndex = Math.floor(Math.random() * headerSounds.length);
var newSound = headerSounds[newSoundIndex];
if (!contains(currentHeaderSounds, newSound)) {
// print("adding sound ", newSound, " to headers");
currentHeaderSounds.push(newSound);
return newSound;
}
return getRandomSound();
}
function loadSounds() {
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoCat.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoDog.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoPlinks.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoPianoPlinks.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoBomb.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoSqueak.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoChatter.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoClang.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoGiggles.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoSplash.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoWaHaHa.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoFall.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoTwinkle.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoPopUp.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoWhistle.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoBubbles.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoHarp.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoKazoo.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoVibration.wav")));
headerSounds.push(SoundCache.getSound(Script.resolvePath("../assets/sounds/bingoSpiral.wav")));
for (var i = 0; i < BINGO_STRING.length; i++) {
getRandomSound();
}
}
function encodeURLParams(params) {
var paramPairs = [];
for (var key in params) {
paramPairs.push(key + "=" + params[key]);
}
return paramPairs.join("&");
}
function onScreenChanged(type, url) {
open = (url === appPage);
button.editProperties({isActive: open});
if (open) {
loadSounds();
print("Searching...");
var searchParamString = encodeURLParams({
type: "search",
username: userName
});
var searchRequest = new XMLHttpRequest();
searchRequest.open('GET', spreadsheetURL + "?" + searchParamString);
searchRequest.timeout = 10000;
searchRequest.ontimeout = function() {
print("bingo: request timed out");
};
searchRequest.onreadystatechange = function() { // called after load when readyState = 4
// print("STATE CHANGED");
if (searchRequest.readyState === 4) {
// print("request ready state is 4. response is ", searchRequest.response);
if (searchRequest.response === "New username") {
print("Needs to add user");
} else if (searchRequest.response) {
var userNumbersToSplit = searchRequest.response.substring(2, searchRequest.response.length - 2);
userCardNumbers = userNumbersToSplit.split(",");
tablet.emitScriptEvent(JSON.stringify({
type : 'displayNumbers',
numbers: userCardNumbers
}));
print("userCardNumbers is ", userCardNumbers);
print("user is in database");
}
}
};
searchRequest.send();
}
}
function playSound(sound, volume, localOnly){
if (sound.downloaded) {
if (injector) {
injector.stop();
}
injector = Audio.playSound(sound, {
position: MyAvatar.position,
volume: volume,
localOnly: localOnly
});
}
}
function callBingo() {
playSound(WIN_SOUND, WINNER_AUDIO_VOLUME, false);
winParticleEffects.push(Entities.addEntity({
accelerationSpread: {
x: 0.5,
y: 0,
z: 0.5
},
alphaFinish: 1,
alphaStart: 1,
collidesWith: "",
collisionMask: 0,
collisionless: true,
color: {
blue: 109,
green: 69,
red: 255
},
colorFinish: {
blue: 0,
green: 0,
red: 0
},
colorStart: {
blue: 0,
green: 0,
red: 255
},
dimensions: {
x: 6.6063995361328125,
y: 6.6063995361328125,
z: 6.6063995361328125
},
emitAcceleration: {
x: -0.10000000149011612,
y: -1,
z: -0.10000000149011612
},
emitDimensions: {
x: 1,
y: 1,
z: 1
},
emitOrientation: {
w: 0.7067862749099731,
x: -0.7074272036552429,
y: -1.5258869098033756e-05,
z: -1.5258869098033756e-05
},
emitRate: 19,
emitSpeed: 1.100000023841858,
emitterShouldTrail: true,
ignoreForCollisions: true,
isEmitting: true,
lifespan: 1.2000000476837158,
maxParticles: 3200,
name: "Bingo Particle",
parentID: MyAvatar.sessionUUID,
particleRadius: 0.47999998927116394,
polarFinish: 3.1415927410125732,
localPosition: {
x: 0.02892160415649414,
y: 0.4198780059814453,
z: -0.024219989776611328
},
radiusFinish: 0.4399999976158142,
radiusStart: 0.3799999952316284,
localRotation: {
w: 0.45094990730285645,
x: -0.16612499952316284,
y: 0.8005034923553467,
z: -0.35802245140075684
},
speedSpread: 0,
spinFinish: 0.7853981852531433,
spinSpread: 6.2831854820251465,
spinStart: null,
textures: "https://hifi-content.s3.amazonaws.com/alan/dev/Particles/sprite-bingo.jpg",
type: "ParticleEffect",
userData: "{\"grabbableKey\":{\"grabbable\":false}}",
lifetime: 10
}));
winParticleEffects.push(Entities.addEntity({
accelerationSpread: {
x: 0.10000000149011612,
y: 0,
z: 0.10000000149011612
},
alphaFinish: 1,
alphaStart: 1,
collidesWith: "",
collisionMask: 0,
collisionless: true,
colorFinish: {
blue: 0,
green: 0,
red: 0
},
colorSpread: {
blue: 255,
green: 255,
red: 255
},
colorStart: {
blue: 200,
green: 200,
red: 200
},
dimensions: {
x: 50.30699920654297,
y: 50.30699920654297,
z: 50.30699920654297
},
emitAcceleration: {
x: -0.10000000149011612,
y: -2.5,
z: -0.10000000149011612
},
emitDimensions: {
x: 1,
y: 1,
z: 1
},
emitOrientation: {
w: 0.7068167924880981,
x: -0.7073966860771179,
y: -1.5258869098033756e-05,
z: -1.5258869098033756e-05
},
emitRate: 61,
emitSpeed: 3.2699999809265137,
emitterShouldTrail: true,
ignoreForCollisions: true,
isEmitting: true,
maxParticles: 3200,
name: "Bingo Confetti Particle",
parentID: MyAvatar.sessionUUID,
particleRadius: 0.25,
polarFinish: 0.05235987901687622,
localPosition: {
x: -0.010210037231445312,
y: -0.3518967628479004,
z: 0.21695232391357422
},
radiusFinish: 0.23999999463558197,
radiusStart: 0.009999999776482582,
localRotation: {
w: 0.6433660984039307,
x: -0.29332417249679565,
y: 0.6434881687164307,
z: -0.29320210218429565
},
speedSpread: 0,
spinFinish: 6.2831854820251465,
spinSpread: 6.2831854820251465,
spinStart: null,
textures: "https://hifi-content.s3.amazonaws.com/alan/dev/Particles/sprite-confetti.jpg",
type: "ParticleEffect",
userData: "{\"grabbableKey\":{\"grabbable\":false}}",
lifetime: 10
}));
}
function onWebEventReceived(event) {
if (typeof event === 'string') {
event = JSON.parse(event);
if (event.type === 'playSoundFromBingoButton') {
playSound(currentHeaderSounds[event.index], 0.2, false);
}
if (event.type === 'calledBingo') {
callBingo();
}
}
}
function appEnding() {
button.clicked.disconnect(onClicked);
tablet.removeButton(button);
tablet.screenChanged.disconnect(onScreenChanged);
tablet.webEventReceived.disconnect(onWebEventReceived);
}
button.clicked.connect(onClicked);
tablet.screenChanged.connect(onScreenChanged);
tablet.webEventReceived.connect(onWebEventReceived);
Script.scriptEnding.connect(appEnding);
}());