In our geek club aka The Danish PowerShell user group, we always do a lottery in the end, for books or similar.

To draw the correct winner we have been using a script that I have developed.

I have for a long time, promised to release the script so that other user groups or other interested people can use it for fun!

It has amazing ascii graphics and colors!!! It will take you straight back to your C64 loading screen!

The script is a lottery script that takes a file from www.eventbrite.com containing all attendees.

The script need a LEAP Motion device for actually triggering the lottery, for this I have developed a few .NET DLL’s based on a sample from leapmotion.com

You can download the package with Script and files here:

[download id=”253″]

How to use:

1. Export at list of your users from eventbrite, use the sample file or edit the sample file to contain your users

2. Save the file as names.csv in same directory as this script (and the other files)

3. Connect your LEAP Motion

4. Start script using the shortcut (you might need to edit the shortcut for the location of your script

5. When it was “connected”. Press enter to start the lottery

6. To “turn the dail” use your hand and swipe over the LEAP Motion. The speed of your hand will have influence in how long the “wheel” turns.

Here is the content of the script:

# 
# //***************************************************************************
# // ***** Script Header *****
# //
# // Solution:  
# // File:      LEAP-Lottery-v7.ps1
# // Author:	Jakob Gottlieb Svendsen, Coretech A/S. https://blog.ctglobalservices.com
# // Purpose:   Lottery script made for Danish PowerShell User group
# //			www.PSUG.dk
# //            Designed to be used with a LEAP motion controller and a list over users from eventbrite.com
# //
# // Usage:     LEAP-Lottery-v7.ps1
# // 1. Export at list of your users from eventbrite, use the sample file or edit the sample file to contain your users
# // 2. Save the file as names.csv in same directory as this script (and the other files)
# // 3. Connect your LEAP Motion
# // 4. Start script using the shortcut (you might need to edit the shortcut for the location of your script
# // 5. When it was “connected”. Press enter to start the lottery
# // 6. To “turn the dail” use your hand and swipe over the LEAP Motion. The speed of your hand will have influence in how long the “wheel” turns.
# // 
# // CORETECH A/S History:
# // 0.0.1     JGS 29/02/20XX  Can't remember exact date of the first version!
# // 7.0.0     JGS 17/06/2015  Cleaned up script, tweaked wheel run time
# // Customer History:
# //
# // ***** End Header *****
# //***************************************************************************
# //----------------------------------------------------------------------------
#//
#//  Global constant and variable declarations
#/
#//----------------------------------------------------------------------------

$DebugPreference = "silentlycontinue"

#//----------------------------------------------------------------------------
#//  Procedures
#//----------------------------------------------------------------------------

Function RunLottery($runLength)
{

    [system.console]::ForegroundColor = "White"

   #$theEnd = 0..($script:names.Count-1) | Get-Random
    Write-Debug "Current length: $runLength"

    $theEnd = $runLength
    $currentName = 0
    $pause = 10
    for ($i = 0; $i -le $theEnd;$i++)
    {
        Clear-Host 

        Write-Debug "Current length: $runLength"
        Write-Host "THE PSUG LOTTERY: "

        $currentName++

        if($currentName -ge $script:names.count)  {$currentName = 0}

        for ($j = 5; $j -gt 0;$j--)
        {
         Write-host ($script:names[$currentName - $j]).{First Name} ($script:names[$currentName - $j]).{Surname}
        }

        Write-host "$($script:names[$currentName].{First Name}) $($script:names[$currentName].{Surname})" -ForegroundColor Yellow -BackgroundColor Black

        $currentj = $currentName
        for ($j = 1; $j -le 5;$j++)
        {
            if(($currentName + $j) -ge $script:names.count)
            {
                Write-host $script:names[$currentName + $j - $script:names.count].{First Name} $script:names[$currentName + $j - $script:names.count].{Surname}
            }
            else
            {
                Write-host $script:names[$currentName + $j].{First Name} $script:names[$currentName + $j].{Surname}h 
            }
        }

        if (($theEnd-30) -le $i) { $pause += 5 }

        Start-Sleep -Milliseconds $pause

    }

        Write-host ""
        Write-host ""
        $startTime = Get-Date
        while (((Get-date)-$startTime).TotalSeconds -le 10)
        {
            1..15 | foreach {[system.console]::ForegroundColor = $_; Write-host "THE WINNER IS $(($script:names[$currentName].{First Name}).ToUpper()) $(($script:names[$currentName].{Surname}).ToUpper()) `r" -nonewline; sleep -Milliseconds 50}

            $global:winner = $currentName;
        }

        Write-host "THE WINNER IS $(($script:names[$currentName].{First Name}).ToUpper()) $(($script:names[$currentName].{Surname}).ToUpper())" 
        $global:complete = $true
}

Function StartLottery($event)
{
    $global:ready = $true

    while (!$global:complete)
    {
         $event | receive-job
        start-sleep -Milliseconds 50
    }

    $script:names = $script:names | ? { $_.{Email Address} -ne $script:names[$global:winner].{Email Address} }

    Read-Host "Press Enter to start next lottery!"
    Write-host "Ready to Roll!"

    #Set-PSBreakpoint -Script .\LEAP-Lottery-v5.ps1 -Line 25
    $global:ready = $true
    $global:complete = $false
    #restart lottery
    StartLottery $event

}

#//----------------------------------------------------------------------------
#//  Main routines
#//----------------------------------------------------------------------------

cd $PSScriptRoot

$script:names = Import-Csv .\names.csv -Delimiter ","

[System.IO.Directory]::SetCurrentDirectory($pwd)
[System.Reflection.Assembly]::LoadFrom("LeapCSharp.NET4.0.dll")
[System.Reflection.Assembly]::LoadFrom("GestureListener.dll")
[System.IO.Directory]::getCurrentDirectory()

$ErrorActionPreference = "stop"

$onGesture = {

    $frame = $controller.Frame();

    foreach ($gesture in $frame.Gestures())
    {
    #$gesture
          $swipe = New-Object Leap.SwipeGesture -Argumentlist  $gesture
                    #progress
                    write-Debug "Speed: $($swipe.Speed/10)"
                    if ($global:ready)
                    {
                        $global:ready = $false
                        #$ready = $false
                        $length = 100..150 | %{ $_ + ($swipe.speed/10) } | Get-Random

                        #Start
                        RunLottery $length
                    }
    }
 }

$global:complete = $false;

#Create gesture listener
$listener = new-Object GestureListener.GestureListener -ArgumentList 1500

#Create LEAP Controller
$controller = New-Object LEAP.Controller -ArgumentList $listener
$controller.EnableGesture([Leap.Gesture+GestureType]::TYPESWIPE) 

#Display connecting status message until connected
$i = 1
while (!$controller.IsConnected)
{
    $dots = ("." * $i).PadRight(3)
    $msg = "Connecting$dots`r" 
    write-host $msg -NoNewline
    Start-Sleep -Milliseconds 200

    if ($i -eq 3) { $i = 1 } else { $i++ }
}
#Write empty line to make new line for next message
write-host ""

#Register OnGesture Event, to trigger action $onGesture when a gesture is detected by LEAP.
$event = Register-ObjectEvent -InputObject $listener -EventName OnGesture -Action $onGesture  -SourceIdentifier "LEAPEvent"

Read-Host "Press Enter to start"
Write-host "Ready to Roll!"

#Start First Lottery
StartLottery $event

#clean up
get-job | remove-job -Force
$controller.RemoveListener($listener);
$controller.Dispose();

#//----------------------------------------------------------------------------
#//  End Script
#//----------------------------------------------------------------------------

I know that not everyone owns a LEAP motion, this script could be easily edited to be used only using the keyboard and a random “wheel run time”

If you edit the script for this, please post it in the comments.

Otherwise if there is enough requests, I will do it myself! 😉

Go Lottery Go!!