No announcement yet.

[script][silly] Secret Santa

  • Filter
  • Time
  • Show
Clear All
new posts

    [script][silly] Secret Santa

    Perhaps the silliest script I've ever written.

    This year a group of us friends decided that instead of all having to come up with gift ideas for all the rest of us, we'd do a Secret Santa. That's where you put slips of paper with your names in a hat, and then draw names, and everyone gets just one gift, for the one person on their slip of paper. (Only you can't draw your own name, and there are usually other exclusions -- like if there are couples in the group, you don't let someone get assigned their own spouse/partner.)

    The only trouble was, we decided this by email, and the next time we'll all be in a room together so we can draw names from a hat is way past when some of us want to do our holiday shopping. We could do it manually and email out the results, but the "secret" part disappears then.

    Enter HomeSeer. (Well, the only HomeSeer capability I'm using is hs.SendEmail, only because it's easier than using Postie.) This quick-and-dirty script does the job and emails the results directly to the players, so no one is in on the secret. As an extra bonus, the script can do something you can't do with paper and a hat -- prevent the situation where Jane is buying something for Dick, and Dick is buying something for Jane.

    Put the script in your scripts folder, and link an event to it.

    Because this is a quick-and-dirty script, all configuration is done by editing the actual script. First, there's three lines around line 12 that control where the emails will come from, where any error emails (telling you of problems in the script) go to, and whether to run in test mode -- where all the emails go to you, instead of to the actual players, so you can be sure all is well before you do it "in production".

    Then there's the list of players. For each player, you should have a name and an email address. Optionally you can include a "wish list" (some hints that the person passes to his/her Secret Santa for what kind of things to get). You can also have zero, one, or two "exclusions" -- names of other people on the list who this person should not be assigned (if they drew that name from the hat, they'd throw it back in and get another one). These exclusions are unidirectional; if Dick excludes Jane, Jane can still be assigned Dick, unless Jane also excludes Dick.

    Once you have this information, change the number of players on line 39 (if you have more than 50 you'll also need to change the Public statement on line 18), then the players themselves below. Yes, this is a very cheesy way to store them all -- it's not as nice as a true class or data structure, but hey, this is a quick and dirty thing, and it works. The example players should make fairly clear how to format these items. Remember, wishlists and exclusions are optional.

    Then run the event. If it looks good, change the test mode to False and run it again. Then put it away until next year.

    Algorithm Notes:
    On the surface this might seem like a very simple thing to do, but when you add in exclusions, it can actually be a little tricky. Even without exclusions, it's possible that by the time the hat gets to the last person, the only name left is his. Add in exclusions, and it's possible for someone before the last person to find that none of the names in the hat is acceptable. How likely this is depends on the proportion between how many exclusions there are, and how many players. If there's almost as many exclusions as players, it can get pretty tricky (and this script can fail too often). With enough exclusions there can even be no valid solutions!

    If it's even possible to make an algorithm that intelligently (not just by brute force) finds a complete solution, it's way more trouble than it's worth. Even detecting when the situation is, or has become, impossible, is not worth it. Instead, I simply optimized a little bit for the normal situation (not too many exclusions), and then made the script only retry so many times before giving up. If the script fails, it'll tell you so in an email. In this case, just try again. If it keeps failing, maybe you have too many exclusions for the number of players you have.

    But for a typical group, just install it, and it'll work, no problem.

    I don't think this really needs a thread created in the script library discussion forum... [img]/infopop/emoticons/icon_biggrin.gif[/img]

    Revision History:
    v1.0 2002-11-15: first uploaded to the Scripts Library

    Nucleus Home Automation | System Specs
    Attached Files
    News, support, and updates for Rover, Network Monitor, TimeIcons, and more