All pastes #49567 Raw Edit

Something

public text v1 · immutable
#49567 ·published 2006-04-17 04:11 UTC
rendered paste body
;ROOMMATES IN A HAT
;SPEC:  Every roommate gets a respective weight, based on which two are chosen (not the same two)
;       to clean the house.
;       Using a "pseudo-array" of strings bsaed on $ROOMMATE[1-6], we'll prompt the
;       caller for a weight [1-9], and make a corresponding array of integers (called $MATEWEIGT[1-6]).
;       Finally, bsaed on roommates weight, roommates are given a relative probability of drawing the
;       cleaning straw.  Two separate winners are put into $ClEANER[1] and $CLEANER[2]


[riah]
;who are the roommates?
exten => s,1,set(__ROOMMATE[1]="Brad")
exten => s,n,set(__ROOMMATE[2]="Brian")
exten => s,n,set(__ROOMMATE[3]="James")
exten => s,n,set(__ROOMMATE[4]="Jeff")
exten => s,n,set(__ROOMMATE[5]="Marc")
exten => s,n,set(__ROOMMATE[6]="Aubrey")

;for each roommate prompt for a weight
exten => s,n,set(__COUNT=1)                             ;set roommate counter
exten => s,n,while($[${COUNT}<7])                       ;while we havent assigned a weight to all the roommates...
exten => s,n,background(beep)                           ;prompt with a tone...
exten => s,n,waitexten(5)                               ;and givem five seconds to enter an extension 0-9
exten => s,n(repeat),set(__COUNT=$[${COUNT}+1])      ;then increment the counter
exten => s,n,endwhile

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;take the weight and build the array;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        exten => _X,1,set(__MATEWEIGHT[${COUNT}]= ${EXTEN})     ;give each roommate a relative weight using the counter
        exten => _X,n,goto(s,repeat)                            ;then repeat
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;after weights are assigned, pick roommates until we have two winners
exten => s,n,set(__WINNERS=0)                           ;at first we have no winners
exten => s,n,set(__LASTCOUNT=1)                         ;and we'll use a new counter
exten => s,n(draw),while(${WINNERS} < 2)                        ;while we dont have two winners...
        exten => s,n,while(${LASTCOUNT} < 6)                    ;and while we havent checked all the roommates
        exten => s,n,goto(${ROOMMATE[${LASTCOUNT}]})            ;go to an extension based on roommate name below
        exten => s,n,set(__LASTCOUNT= $[${LASTCOUNT} + 1 ])     ;should i just reset the old counter?  who cares.
        exten => s,n,endwhile                                   ;
exten => s,n,set(__LASTCOUNT = 1)                               ;reset counter if we dont have two winners and try again
exten => s,n,endwhile


        ;;;;;;;;Go to exten ${ROOMMATEN[1-6], and use their weight to "draw" for a "win";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        exten => _${ROOMMATE[${LASTCOUNT}]},1,random(${MATEWEIGHT[${LASTCOUNT}]},${ROOMMATE[${LASTCOUNT}]},3)           ;jump to three if we have a winner
        exten => _${ROOMMATE[${LASTCOUNT}]},2,goto(s,draw)                                                              ;or else draw again.  LABEL BROKEN?
        exten => _${ROOMMATE[${LASTCOUNT}]},3,gotoif("${ROOMMATE[${LASTCOUNT} != $[${ROOMMATE[$[${LASTCOUNT} - 1]]}?4|6);did we just pick him?
        exten => _${ROOMMATE[${LASTCOUNT}]},4,set(__WINNERS = $[${WINNERS} + 1])                                        ;if not we have a winner
        exten => _${ROOMMATE[${LASTCOUNT}]},5,set(__CLEANER[${WINNERS}]="${ROOMMATE[${LASTCOUNT}]}")                    ;and we set the CLEANER variable
        exten => _${ROOMMATE[${LASTCOUNT}]},6,goto(s,draw)                                                              ;or else we draw again
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;spit out the winners at the end
exten => s,n,noop("${CLEANER[1]} and ${CLEANER[2]} are the winners."