Last active
January 23, 2019 22:16
-
-
Save donpdonp/97840e57247c1913723f22ed23e5a906 to your computer and use it in GitHub Desktop.
gluon todo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
setup() | |
return {name:"todo"} | |
}) | |
var key | |
var actives = [] | |
function setup() { | |
db.get('todo:key', function(value) { key = value; bot.say(bot.admin_channel, "todo key loaded") }) | |
} | |
function go(msg) { | |
var irc = false | |
if (msg.method == "irc.privmsg") { | |
var match = /^todo(\s+(\w+))?/.exec(msg.params.message) | |
if(match) { | |
irc = true | |
} | |
} | |
var clock = false | |
if (msg.method == "clocktower") { | |
var time = new Date(Date.parse(msg.params.time)) | |
if(time.getMinutes() % 5 == 0) { | |
clock = true | |
} | |
} | |
if (irc || clock) { | |
var me = lastLocation() | |
var todos = todolist() | |
var near_todos = todos_near(me, todos, 500) | |
if(irc) { | |
var date = new Date(me.location.date) | |
var ago = (new Date().getTime() - date.getTime())/1000/60 | |
bot.say(msg.params.channel, ago.toFixed(1)+" min ago location") | |
todos.forEach(function(todo){ | |
bot.say(msg.params.channel, todo.object.name + " "+todo.dist.toFixed(0)+"m away") | |
}) | |
} | |
near_todos.forEach(function(todo){ | |
var channel = irc ? msg.params.channel : bot.admin_channel | |
bot.say(channel, "donpdonp: your todo of "+todo.object.name + " is " + todo.dist.toFixed(0) +"m away") | |
}) | |
} | |
} | |
function todos_near(me, todos, m) { | |
return todos.filter(function(todo){ | |
var dist = pointDistance(me.location, | |
{latitude: todo.object.lat, | |
longitude: todo.object.lng}) | |
todo.dist = dist | |
return dist < m | |
}) | |
} | |
function lastLocation() { | |
var url = 'https://icecondor.com/donpdonp.json?key='+key | |
var json = http.get(url) | |
var data = JSON.parse(json) | |
return data | |
} | |
function todolist() { | |
var json = http.get('https://rml.donp.org/todo') | |
var data = JSON.parse(json) | |
return data | |
} | |
function numberToRadius(number) { | |
return number * Math.PI / 180; | |
} | |
// from http://www.movable-type.co.uk/scripts/latlong.html | |
function pointDistance(la, lb) { | |
var lat1 = la.latitude, lon1 = la.longitude | |
var lat2 = lb.latitude, lon2 = lb.longitude | |
var dLat = numberToRadius(lat2 - lat1), | |
dLon = numberToRadius(lon2 - lon1), | |
a = Math.pow(Math.sin(dLat / 2), 2) + Math.cos(numberToRadius(lat1)) | |
* Math.cos(numberToRadius(lat2)) * Math.pow(Math.sin(dLon / 2), 2), | |
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | |
return (6371 * c) * 1000; // returns meters | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment