2018 work :)

This commit is contained in:
Maël Nison 2018-10-29 21:09:22 -07:00
parent 6ad707ddba
commit 48e55aafab
4 changed files with 197 additions and 40 deletions

View file

@ -2,6 +2,7 @@ var SecretSanta = function () {
this.names = []; this.names = [];
this.enforced = Object.create( null );
this.blacklists = Object.create( null ); this.blacklists = Object.create( null );
}; };
@ -15,6 +16,14 @@ SecretSanta.prototype.add = function ( name ) {
var subapi = { }; var subapi = { };
subapi.enforce = function ( other ) {
this.enforced[ name ] = other;
return subapi;
}.bind( this );
subapi.blacklist = function ( other ) { subapi.blacklist = function ( other ) {
if ( ! Object.prototype.hasOwnProperty.call( this.blacklists, name ) ) if ( ! Object.prototype.hasOwnProperty.call( this.blacklists, name ) )
@ -38,6 +47,25 @@ SecretSanta.prototype.generate = function () {
this.names.forEach( function ( name ) { this.names.forEach( function ( name ) {
if ( Object.prototype.hasOwnProperty.call( this.enforced, name ) ) {
var enforced = this.enforced[ name ];
if ( this.names.indexOf( enforced ) === -1 )
throw new Error( name + ' is paired with ' + enforced + ', which hasn\'t been declared as a possible pairing' );
Object.keys( pairings ).forEach( function ( name ) {
if ( pairings[ name ] === enforced ) {
throw new Error( 'Per your rules, multiple persons are paired with ' + enforced );
}
} );
pairings[ name ] = this.enforced[ name ];
} else {
var candidates = _.difference( this.names, [ name ] ); var candidates = _.difference( this.names, [ name ] );
if ( Object.prototype.hasOwnProperty.call( this.blacklists, name ) ) if ( Object.prototype.hasOwnProperty.call( this.blacklists, name ) )
@ -45,6 +73,8 @@ SecretSanta.prototype.generate = function () {
candidatePairings[ name ] = candidates; candidatePairings[ name ] = candidates;
}
}, this ); }, this );
var findNextGifter = function () { var findNextGifter = function () {

BIN
assets/snow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

View file

@ -152,16 +152,13 @@
<div class="main"> <div class="main">
<div class="part"> <div class="part" style="overflow-y: auto">
<div class="instructions"> <div class="instructions">
<h1><img src="assets/mistletoe.png" style="vertical-align: middle" /> Secret Santa Generator</h1> <h1><img src="assets/mistletoe.png" style="vertical-align: middle" /> Secret Santa Generator</h1>
<p>No signup, no email, no bullshit. Just a straightforward <a href="https://github.com/arcanis/secretsanta">open-source</a> tool to help you generate your secret santa pairings. Static page only.</p> <p>No signup, no email, no bullshit. Just a straightforward <a href="https://github.com/arcanis/secretsanta">open-source</a> tool to help you generate your secret santa pairings. One static page, and that's it.</p>
<p>The DSL that is used to set the pairing rules is dead simple: in the most common use case (no special rule, pair each guest to another), you just have to enter the name of your guests, one line at a time. Once done, press "generate" and voilà, you will get a set of links that you will just have to give to each one of them (by mail, chat, whatever float your boat). Once they access the link, their pairing will be revealed (to them and only them).</p> <p>In the most common case (no exclusion rules, pair each guest with another at random), enter the name of your guests one line at a time. Once done, press "generate" and you're all set: send the generated links to your guests (by mail, chat, whatever floats your boat) and their pairing will be revealed to them (and only them) once they open the link.</p>
<p>Should you have more complex needs (for example if you want to prevent a couple from being paired together, or want to prevent someone to be paired with someone else they don't know), just append <code>!&lt;name&gt;</code> after a guest name, and he will never be paired with <code>&lt;name&gt;</code> (check on the right for an example).</p>
<h2>Where does this tool come from?</h2> <h2>Where does this tool come from?</h2>
<p>I just wanted to make a Secret Santa over Facebook without giving up my guests email addresses to do so (so nothing that requires a backend). I also wanted to ignore who was paired with me, so I had to find a way to obfuscate it. And since I'm a developer, well, I just thought "Let's AES it, for fun and profits!". Classic.</p> <p>I wanted to make a Secret Santa over Facebook without having to reveal to anyone my guests email addresses (so nothing that would require a backend). I also wanted not to know who was paired with me, so I had to find a way to somehow obfuscate the information. And being a developer, well, my first thought was "Let's AES it, for fun and profits!". Classic.</p>
<!-- <p>If you want to tip me, feel free to send me anything on the following Bitcoin address! I'll probably use it on some 3DS game (Fire Emblem, maybe?) :) Anyway, merry christmas!</p>
<p><img src="assets/bitcoin.ico" style="vertical-align: middle" /> <a href="bitcoin:1HLgRVCGcXabWhMfdLazFVUWiH2W1kCDQK"><code>1HLgRVCGcXabWhMfdLazFVUWiH2W1kCDQK</code></a></p>-->
</div> </div>
</div> </div>
@ -181,6 +178,13 @@
Maël !Aurélie Maël !Aurélie
Aurélie !Maël Aurélie !Maël
# You can also exclude someone from being paired with multiple people
# Careful: too many exclusion rules can make your secret santa less interesting!
Rudolph !Maël !Santa
# You can also cheat a bit and force someone to be paired with another
Aurélie =Santa
... ...
</script> </script>
@ -188,13 +192,23 @@
<script> <script>
function isAmazonEnabled() {
const checkBox = document.getElementById('amazon');
return checkBox ? checkBox.checked : false;
}
function persist() { function persist() {
if ( ! window.localStorage ) if ( ! window.localStorage )
return ; return ;
var amazon = isAmazonEnabled();
var content = document.getElementById( 'input' ).value; var content = document.getElementById( 'input' ).value;
window.localStorage.setItem( 'amazon', amazon );
window.localStorage.setItem( 'input', content ); window.localStorage.setItem( 'input', content );
} }
@ -204,7 +218,17 @@
if ( ! window.localStorage ) if ( ! window.localStorage )
return ; return ;
var content = window.localStorage.getItem( 'input' ) || ''; var amazon = window.localStorage.getItem( 'amazon' );
var content = window.localStorage.getItem( 'input' );
if (typeof amazon === 'undefined')
amazon = true;
if (typeof content === 'undefined')
content = '';
if ( document.getElementById( 'amazon' ) )
document.getElementById( 'amazon' ).checked = amazon;
document.getElementById( 'input' ).value = content; document.getElementById( 'input' ).value = content;
@ -270,6 +294,9 @@
var pairingPath = window.location.pathname.replace( /[^/]+$/, '' ) + 'pairing.html'; var pairingPath = window.location.pathname.replace( /[^/]+$/, '' ) + 'pairing.html';
var pairingQueryString = '?name=' + encodeURIComponent( name ) + '&key=' + encodeURIComponent( key ) + '&pairing=' + encodeURIComponent( encryptedPairing ); var pairingQueryString = '?name=' + encodeURIComponent( name ) + '&key=' + encodeURIComponent( key ) + '&pairing=' + encodeURIComponent( encryptedPairing );
if ( isAmazonEnabled() )
pairingQueryString += '&extra=1';
tdName.innerText = name; tdName.innerText = name;
link.addEventListener( 'click', protect ); link.addEventListener( 'click', protect );
@ -281,6 +308,26 @@
} }
function updateAmazon() {
var result = document.getElementById( 'result' );
var links = result.getElementsByTagName( 'a' );
for ( var t = 0; t < links.length; ++t ) {
var link = links[t];
if ( isAmazonEnabled() )
link.href += '&extra=1';
else
link.href = link.href.replace( /&extra=[01]/, '' );
link.innerText = link.href;
}
}
function generate( e ) { function generate( e ) {
e.preventDefault(); e.preventDefault();
@ -302,11 +349,13 @@
// Strip empty lines // Strip empty lines
content = content.replace( /\n+/g, '\n' ); content = content.replace( /\n+/g, '\n' );
// Remove trailing newline // Remove leading/trailing newlines
content = content.replace( /\n$/, '' ); content = content.replace( /^\n|\n$/g, '' );
var lines = content.split( /\n/g ); var lines = content.split( /\n/g );
console.log(lines);
if ( lines.length === 0 || lines.length === 1 && lines[ 0 ].length === 0 ) if ( lines.length === 0 || lines.length === 1 && lines[ 0 ].length === 0 )
return reset(); return reset();
@ -314,18 +363,31 @@
for ( var t = 0, T = lines.length; t < T; ++ t ) { for ( var t = 0, T = lines.length; t < T; ++ t ) {
var match = lines[ t ].match( /^([^ ]+)(?: ((?:![^ ]+)(?: ![^ ]+)*))?$/ ); var match = lines[ t ].match( /^([^ ]+)((?: [!=][^ ]+)*)$/ );
if ( ! match ) if ( ! match )
return error( 'Syntax error: "' + lines[ t ] + '" isn\'t a valid line' ); return error( 'Syntax error: "' + lines[ t ] + '" isn\'t valid' );
var name = match[ 1 ]; var name = match[ 1 ];
var exclusions = match[ 2 ] ? match[ 2 ].replace( /!/g, '' ).split( / /g ) : []; var rules = match[ 2 ] ? match[ 2 ].match(/[^ ]+/) : null;
var person = santa.add( name ); var person = santa.add( name );
for ( var u = 0, U = exclusions.length; u < U; ++ u ) { if (rules) {
person.blacklist( exclusions[ u ] );
for ( var u = 0, U = rules.length; u < U; ++ u ) {
var fnName = {
'=': 'enforce',
'!': 'blacklist'
}[ rules[ u ].charAt( 0 ) ];
person[ fnName ]( rules[ u ].slice( 1 ) );
}
} }
} }
@ -355,6 +417,12 @@
document.getElementById( 'input' ).placeholder = document.getElementById( 'input-placeholder' ).innerHTML.trim().replace( /^[ \t]+/gm, '' ); document.getElementById( 'input' ).placeholder = document.getElementById( 'input-placeholder' ).innerHTML.trim().replace( /^[ \t]+/gm, '' );
document.getElementById( 'input' ).addEventListener( 'change', persist ); document.getElementById( 'input' ).addEventListener( 'change', persist );
if ( document.getElementById( 'amazon' ) ) {
document.getElementById( 'amazon' ).addEventListener( 'change', updateAmazon );
document.getElementById( 'amazon' ).addEventListener( 'change', persist );
}
document.getElementById( 'form' ).addEventListener( 'submit', generate ); document.getElementById( 'form' ).addEventListener( 'submit', generate );
restore(); restore();

View file

@ -25,19 +25,71 @@
} }
body { body {
background-image: url(assets/santa.png); background: url(./assets/snow.png), url(./assets/santa.png), url(./assets/snow.png), radial-gradient(#FB3B3B, #EF3D3D);
background-position: bottom right; background-repeat: repeat, no-repeat, repeat, no-repeat;
background-repeat: no-repeat;
animation-name: snow;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
font-family: 'Comic Sans MS'; font-family: 'Comic Sans MS';
} }
@keyframes snow {
from { background-position: 130px 40px, bottom right, 0 0, 0 0; }
to { background-position: 130px 640px, bottom right, 0 300px, 0 0; }
}
.spirit-of-christmas {
display: block;
position: absolute;
bottom: 0;
left: 0;
padding: 20px;
font-size: 10px;
text-decoration: none;
color: #FFFFFF;
}
.main { .main {
display: flex; display: flex;
} }
.content { .background {
position: absolute;
left: 0;
bottom: 0;
}
.wrapper {
margin: auto; margin: auto;
padding: 20px;
background: repeating-linear-gradient(
45deg,
#5CC48A,
#5CC48A 30px,
#FFFFFF 30px,
#FFFFFF 60px,
#EF3D3D 60px,
#EF3D3D 90px,
#FFFFFF 90px,
#FFFFFF 120px
);
box-shadow: 3px 3px 10px rgba(0, 0, 0, .5);
}
.content {
padding: 40px;
background: #ffffff;
text-align: center; text-align: center;
} }
@ -54,10 +106,9 @@
display: block; display: block;
box-sizing: content-box; box-sizing: content-box;
margin-top: 70px; margin-top: 40px;
border: 10px solid #0CB50C; border: none;
border-radius: 10px;
background: rgba(255, 255, 255, .7); background: rgba(255, 255, 255, .7);
} }
@ -68,7 +119,7 @@
</style> </style>
<script>1 <script>
var queryString = _.chain( location.search.slice( 1 ).split( /&/g ) ) var queryString = _.chain( location.search.slice( 1 ).split( /&/g ) )
.map( function ( item ) { if ( item ) return item.split( /=/ ).map( function ( str ) { return decodeURIComponent( str ); } ); } ) .map( function ( item ) { if ( item ) return item.split( /=/ ).map( function ( str ) { return decodeURIComponent( str ); } ); } )
@ -85,21 +136,29 @@
<div class="main"> <div class="main">
<div class="wrapper">
<div class="content"> <div class="content">
<h3 class="title">Hi <span id="name"></span>! You've been paired with</h3> <div class="title">Hi <span id="name"></span>! You've been paired with</div>
<script>document.getElementById('name').innerText = name</script> <script>document.getElementById('name').innerText = name</script>
<h1 class="pairing"><span id="pairing"></span></h1> <div class="pairing"><span id="pairing"></span></div>
<script>document.getElementById('pairing').innerText = pairing</script> <script>document.getElementById('pairing').innerText = pairing</script>
<h3 class="title">Good luck!</h3> <div class="title">Good luck!</div>
<script>
if (queryString.extra) {
document.write('<iframe class="affiliate" src="http://rcm-eu.amazon-adsystem.com/e/cm?t=secrsant02e-21&o=8&p=48&l=ur1&category=books&banner=10DQAXJ7D1D2VTXMR682&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" frameborder="0"></iframe>');
}
</script>
<!--
<iframe class="affiliate" src="http://rcm-eu.amazon-adsystem.com/e/cm?t=secrsant02e-21&o=8&p=48&l=ur1&category=jeuxetjouets&banner=0HS03ACZ89HPK7F4F5G2&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" frameborder="0"></iframe>
<iframe class="affiliate" src="http://rcm-eu.amazon-adsystem.com/e/cm?t=secrsant02e-21&o=8&p=48&l=ur1&category=books&banner=10DQAXJ7D1D2VTXMR682&f=ifr" width="728" height="90" scrolling="no" border="0" marginwidth="0" frameborder="0"></iframe>
-->
</div> </div>
</div>
<a href="http://arcanis.github.io/secretsanta/" class="spirit-of-christmas">
Want to start your own Secret Santa with your friends? Click here to get started!
</a>
</div> </div>