Skip to content

Instantly share code, notes, and snippets.

@kunofellasleep
Last active June 26, 2020 10:02
Show Gist options
  • Save kunofellasleep/69290c897e3ac3eaa3e2e2612fbe3267 to your computer and use it in GitHub Desktop.
Save kunofellasleep/69290c897e3ac3eaa3e2e2612fbe3267 to your computer and use it in GitHub Desktop.
How we shared the small world in Spark AR Hackathon India
// Modules
const Patches = require('Patches');
const Diagnostics = require('Diagnostics');
const Networking = require("Networking");
const R = require('Reactive');
const isDebugLog = false;
const url = "https://team-chrome.firebaseio.com/data.json";
var idNames = new Array("A","B","C","D","E","F","G","H","I","J","K");
var data = null;
var playCounts = new Array(11);
var isAlreadyPlayed = new Array(11);
var isInitSoundCounter = false;
/*=========================
タップ監視用の初期設定
=========================*/
function SetupTapObservers(_id) {
var tapPulseName = "TapPulse" + idNames[_id];
var playPulseName = "PlayPulse" + idNames[_id];
Patches.getPulseValue(tapPulseName).subscribe(function () {
data[_id] = data[_id] + 1;
isAlreadyPlayed[_id] = true;
SendPulse(playPulseName);
firebase.set(data);
if(isDebugLog) {
Diagnostics.log(tapPulseName);
}
});
}
/*=========================
カウントアップを確認
=========================*/
function checkPlayCounts() {
for(var id = 0; id < idNames.length; id += 1) {
if(playCounts[id] != data[id]){
playCounts[id] = data[id];
if(isAlreadyPlayed[id]) {
isAlreadyPlayed[id] = false;
} else {
var playPulseName = "PlayPulse" + idNames[id];
SendPulse(playPulseName);
}
}
}
}
/*=========================
カウントアップを確認後、パッチエディタに通知
=========================*/
function SendPulse(_playPulseName) {
Patches.setPulseValue(_playPulseName, R.once());
if(isDebugLog) {
Diagnostics.log(_playPulseName);
}
}
/*=========================
Firebase
=========================*/
const Firebase = (function () {
function Firebase(url) {
this._url = url;
this._isObserving = false;
this._onUpdate = null;
}
Firebase.prototype.set = function(data) {
let options = {
method: 'PUT',
body: JSON.stringify(data)
}
Networking.fetch(this._url, options).then(function(result) {
if (!((result.status >= 200) && (result.status < 300))) {
Diagnostics.log("Failed to post data!");
}
});
}
Firebase.prototype.subscribe = function(onUpdate) {
this._onUpdate = onUpdate;
}
Firebase.prototype.start = function() {
this._isObserving = true;
fetch.call(this);
}
Firebase.prototype.stop = function() {
this._isObserving = false;
}
Firebase.prototype.toString = function() {
return 'Firebase("' + this._url + '")';
}
function fetch() {
const self = this;
Networking.fetch(self._url).then(function(result) {
if (self._isObserving) {
fetch.call(self);
}
if ((result.status >= 200) && (result.status < 300)) {
return result.json();
}
throw new Error("HTTP status code " + result.status);
}).then(function(json) {
if (self._onUpdate != null) {
self._onUpdate(json);
}
}).catch(function(error) {
Diagnostics.log("There was an issue with fetch operation: " + error);
});
}
return Firebase;
})();
//
var firebase = new Firebase(url);
firebase.start();
firebase.subscribe(function(e) {
data = e;
if(!isInitSoundCounter) {
for(var id = 0; id < idNames.length; id += 1) {
// 一度目のカウントを無視する
playCounts[id] = data[id];
// タップの監視を開始
SetupTapObservers(id);
isAlreadyPlayed[id] = false;
}
isInitSoundCounter = true;
}
checkPlayCounts();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment