Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
hntr

HNTR

hntr is a realtime collaborative hunting list that helps you prepare for the oncoming winter.

Let's say Prometheus and Bob have a shopping list for their brand new cave. They both hunt in different jungles, and have a common set of supplies needed for the winter:

  • impala meat
  • flint
  • red vine fruit
  • snakeskin

Last year both had made a similar list before hunting, but when they joined up before winter found that both had found way too much impala meat and no flint to cook it with!

This year, they have shiny new smartphones and decide to make a synced todo list that can be checked at each hunter's convenience. The lists should update silently to not scare away any impala.

Usage

If you want to join the hunt, just open a few tabs, add items to the list. When you find an item, check it off and it should be checked and disabled for all hunters. When you leave the page, other hunters are notified.

<head>
<title> WINTER IS COMING </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://cdn.buglabs.net/swarm/swarm-v0.3.1.js"></script>
<script src="./hntr.js"></script>
</head>
<body>
<h1> What do we need? </h1>
<form id='items'>
<input type='text' placeholder='add an item' id='item' autofocus/>
<button type='button' name='Add' onclick='addItem(document.getElementById("item").value)'>Add</button>
<br>
</form>
hunters: <ul id='hunters'></ul>
</body>
var updateHunter, updateList, connectHandler, errorHandler;
addItem = function(itemName) {
SWARM.send({ item: itemName, found: false })
}
connectHandler = function() {
console.log('connected');
};
errorHandler = function(response) {
console.log('Error:');
console.log(response);
};
// alert to new or eaten hunters, and keep a list of current hunters
updateHunter = function(response) {
presence = JSON.parse(response).presence;
// Filter out swarm presence messages, which do not contain the 'type' field
if(presence.type) {
var who = presence.from.resource;
var alive = (presence.type === "available");
if (alive) {
alert('' + who + ' just joined the hunt!');
$('#hunters').append("<li id='" + who + "'>" + who + '</li>');
} else {
alert('' + who + ' was just eaten by the ravenous bugblatter beast of trall!');
$('#' + who).remove();
// if a hunter is eaten all their items are dropped
$('.' + who).prop('disabled', false);
$('.' + who).prop('checked', false);
}
}
};
// keep an list of what items need to be gathered or have already been gathered
// we use class to determine who requested the item
updateList = function(response) {
message = JSON.parse(response).message;
var who = message.from.resource;
var what = message.payload;
var dom_item = $('#' + what.item);
if (what.found && dom_item[0]) {
alert('' + who + ' found ' + what.item);
// Track who is carrying the item using class
dom_item.addClass(who);
// No other hunter needs these items now
dom_item.prop('checked', true);
dom_item.prop('disabled', true);
} else if (what.item && !(dom_item[0])) {
what.found = true;
alert('' + who + ' needs ' + what.item);
$('#items').append(' ' +
"<input type='checkbox' id='" + what.item + "' />"
+ "<label for='" + what.item + "'>" + what.item + '</label>'
+ '<br>');
$('#' + who.item).click(function() { SWARM.send({found: true, item: what.item})});
}
};
// Join the swarm
// All callbacks must be defined before calling SWARM.connect for it to work
// The apikey, swarm and resource was setup beforehand. Replace with your own!
SWARM.connect({ apikey: ''
, swarms: ''
, resource: ''
, onmessage: updateList
, onpresence: updateHunter
, onconnect: connectHandler
, onerror: errorHandler
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment