Skip to content

Instantly share code, notes, and snippets.

@scottgwald
Last active August 29, 2015 14:00
Show Gist options
  • Save scottgwald/11026809 to your computer and use it in GitHub Desktop.
Save scottgwald/11026809 to your computer and use it in GitHub Desktop.
[wearscript] popdeals
<html style="width:100%; height:100%; overflow:hidden">
<head>
<style type="text/css">
body {
background: url("http://web.mit.edu/birkanu/www/popdeals-logo.png");
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.0/zepto.min.js"></script>
</head>
<body>
<div id="loading"></div>
<script>
var dealIdCounter = 0;
var showingCardTree = false;
var restaurants = [{
"name":"Au Bon Pain",
"icon":"http://mit.edu/birkanu/www/abp-logo.png",
"background":"http://mit.edu/birkanu/www/abp.jpg",
"address":"238 Main St., Cambridge, MA 02142",
"deal":"Free Chocolate Croissant with every Sandwich purchase!"
},
{
"name":"Chipotle Mexican Grill",
"icon":"http://mit.edu/birkanu/www/chipotle-logo.png",
"background": "http://mit.edu/birkanu/www/chipotle.jpg",
"address":"2 Cambridge Ctr, Cambridge, MA 02412",
"deal":"Buy one burrito, get one free!"
},
{
"name":"Starbucks",
"icon":"http://web.mit.edu/birkanu/www/starbucks-logo.png",
"background": "http://web.mit.edu/birkanu/www/starbucks.jpg",
"address":"6 Cambridge Ctr, Cambridge, MA 02142",
"deal":"Free samples of Starbucks Veranda Blend with your next order!"
},
{
"name":"Legal Sea Foods",
"icon":"http://web.mit.edu/birkanu/www/legal-logo.png",
"background": "http://web.mit.edu/birkanu/www/legal.jpeg",
"address":"5 Cambridge Ctr, Cambridge, MA 02142",
"deal":"25% Off Tuna Steak and Non-Alcoholic Drink!"
}];
var styles = {
backgroundImage : "url('http://web.mit.edu/birkanu/www/loading.gif')",
backgroundSize: "100px 100px",
backgroundColor: "black",
backgroundRepeat: "no-repeat",
backgroundPosition: "center"
};
function generateCouponCode(length) {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var couponCode = '';
for (var i = length; i > 0; --i) couponCode += chars[Math.round(Math.random() * (chars.length - 1))];
return couponCode;
}
function generateSubtree(index, restaurant) {
var subtree = new WS.Cards();
script = "<script type='text/html' id='" + "deal_subtree_" + index + "'> <article class='author'>" +
"<img src='" + restaurant.background + "' width='100%' height='100%'>" +
"<div class='overlay-full'/><header>" +
"<img src='" + restaurant.icon + "'/>" +
"<h1>" + restaurant.name + "</h1>" +
"<h2>" + restaurant.address + "</h2>" +
"</header><section><br>" +
"<center>" + generateCouponCode(6) + "</center>" +
"</section><footer><center>Use the above code to get your discount!</center></footer></article><\/script>";
$(script).appendTo('body');
subtree.addHTML('deal_subtree_' + index);
return subtree;
}
function displayDeals() {
// Find a way to destroy already existing card tree.
var dealsTree = new WS.Cards();
$.each(restaurants, function(index, restaurant) {
script = "<script type='text/html' id='" + "deal_" + dealIdCounter + "'> <article class='author'>" +
"<img src='" + restaurant.background + "' width='100%' height='100%'>" +
"<div class='overlay-full'/><header>" +
"<img src='" + restaurant.icon + "'/>" +
"<h1>" + restaurant.name + "</h1>" +
"<h2>" + restaurant.address + "</h2>" +
"</header><section>" +
"<center>" + restaurant.deal + "</center>" +
"</section><footer><center>Tap To Claim</p></center></article><\/script>";
$(script).appendTo('body');
dealsTree.addHTML('deal_' + dealIdCounter, generateSubtree(dealIdCounter,restaurant));
dealIdCounter += 1;
console.log(dealIdCounter);
});
WS.cardTree(dealsTree);
if (!showingCardTree) {
WS.displayCardTree();
showingCardTree = true;
}
}
function getDeals() {
var newLocation = {};
WS.sensorOn('gps', .10, function(data) {
newLocation.latitude = data.values[0]; // value from gps
newLocation.longitude = data.values[1]; // value from gps
console.log("Latitude: " + location.latitude + ", Longitude: " + location.longitude);
});
WS.sensorOff(-1);
// Send a request to the back-end for deals nearby and pass it into displayDeals
setTimeout(displayDeals, 1000);
}
function speech(data) {
if (data.indexOf("deal") !== -1 || data.indexOf("pop") !== -1) {
getDeals();
} else {
WS.say('Sorry you need to say pop deals');
WS.speechRecognize('Say "pop deals"', 'speech');
}
}
function speechRecognize() {
$('body').css(styles);
WS.speechRecognize('Say "pop deals"', 'speech');
}
function server() {
WS.say('Welcome to Pop Deals');
setTimeout(speechRecognize,2000);
// Swipe up with two fingers two restart the speech recognizer
WS.gestureCallback('onGestureTWO_SWIPE_UP', function () {
speechRecognize();
});
}
function main() {
if (WS.scriptVersion(1)) return;
WS.serverConnect('{{WSUrl}}', 'server');
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment