Last active
August 29, 2015 14:08
-
-
Save spolu/15d6c06b3075bdbe02a8 to your computer and use it in GitHub Desktop.
Christmas Draw
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
var async = require('async'); | |
var send = function(html, email, cb_) { | |
var sg = new (require('sendgrid').SendGrid)('teleportd', | |
'<PASSWORD>'); | |
sg.send({ | |
to: email, | |
from: '[email protected]', | |
fromname: 'Christmas Bot', | |
subject: 'Noyel 2014 - Cadeaux de cousins - FINAL!', | |
html: html | |
}, function(success, message) { | |
if(!success) { | |
return cb_(new Error(message)); | |
} | |
return cb_(); | |
}); | |
}; | |
var message = function(to, match) { | |
var message = ''; | |
message += '<p>Imotep ' + to + '!</p>'; | |
message += '<p style="color: red"><i>Ce message annule tout les messages précédents!</i></p>'; | |
message += '<p>Tu dois faire un cadeau à : <strong style="color: blue">' + match + '</strong></p>'; | |
message += '<p><i>Rappel des règles<i>: Budget 10€. Un cadeau frais et stylé comme toi!</p>'; | |
message += '<p>A plus humain!</p>'; | |
return message; | |
}; | |
var EMAILS = { | |
'Gabrielle': '<EMAIL>', | |
'Ambroise': '<EMAIL>', | |
'Bertille': '<EMAIL>', | |
'Louise': '<EMAIL>', | |
'Laure': '<EMAIL>', | |
'Sophie': '<EMAIL>', | |
'Constance': '<EMAIL>', | |
'Stanislas': '<EMAIL>', | |
'Xavier': '<EMAIL>', | |
'Sixtine': '<EMAIL>', | |
'Domitille': '<EMAIL>', | |
'Bérangère': '<EMAIL>', | |
'Briac': '<EMAIL>', | |
'Ben': '<EMAIL>', | |
'Marie': '<EMAIL>', | |
'Philippine': '<EMAIL>' | |
}; | |
var FAMILIES = [ | |
['Constance', 'Stanislas', 'Xavier', 'Sixtine', 'Domitille', 'Bérangère'], | |
['Gabrielle', 'Ambroise', 'Bertille', 'Louise'], | |
['Briac', 'Ben', 'Marie', 'Philippine'], | |
['Laure', 'Sophie'], | |
]; | |
var draw = function() { | |
var DRAW = []; | |
var DONE_SND = {}; | |
var DONE_RCV = {}; | |
var check = function(from, to) { | |
if(DONE_RCV[to] || DONE_SND[from]) return false; | |
var safe = true; | |
FAMILIES.forEach(function(f, i) { | |
var snd = f.reduce(function(acc, c) { | |
return acc + ((!DONE_SND[c] && c !== from) ? 1 : 0); | |
}, 0); | |
var rcv = FAMILIES.reduce(function(acc, f, j) { | |
if(j === i) return acc; | |
return acc + f.reduce(function(acc, c) { | |
return acc + ((!DONE_RCV[c] && c !== to) ? 1 : 0); | |
}, 0); | |
}, 0); | |
if(snd > rcv) safe = false; | |
}); | |
return safe; | |
}; | |
FAMILIES.forEach(function(f, i) { | |
f.forEach(function(c, j) { | |
var family = i; | |
var cousin = j; | |
while(family === i || !check(c, FAMILIES[family][cousin])) { | |
family = Math.floor(Math.random() * FAMILIES.length); | |
cousin = Math.floor(Math.random() * FAMILIES[family].length); | |
} | |
DRAW.push([c, FAMILIES[family][cousin]]); | |
DONE_SND[c] = true; | |
DONE_RCV[FAMILIES[family][cousin]] = true; | |
}); | |
}); | |
return DRAW; | |
}; | |
async.eachSeries(draw(), function(d, cb_) { | |
send(message(d[0], d[1]), EMAILS[d[0]], cb_); | |
}, function(err) { | |
if(err) { | |
console.log('ERROR: ' + err); | |
} | |
console.log('OK!'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment