Pokernight.hex

mIRC Scripting Guest 30 Views Size: 9.32 KB Posted on: Jun 18, 26 @ 2:48 AM
  1. ; Poker Night
  2. ;   data model (lists/maps)  +  view{} table  +  age.* encrypted transport
  3.  
  4. on LOAD {
  5.   set %chan #game
  6.   set %sb 5
  7.   set %bb 10
  8.   set %rankmap $map(A,14,K,13,Q,12,J,11,T,10,9,9,8,8,7,7,6,6,5,5,4,4,3,3,2,2)
  9.   sidebar add poker "♠ Poker Night" poker_open
  10. }
  11.  
  12. ; join/open
  13. alias poker {
  14.   age.join %chan
  15.   age.send %chan join $age.me $nick 1000
  16.   toast Joined the table — waiting for players
  17. }
  18.  
  19. on SIGNAL:age_msg {
  20.   ; $from = sender fp ; $1 = move type ; $2- = payload
  21.   if ($1 == join)     { poker_onjoin }
  22.   elseif ($1 == start) { poker_onstart }
  23.   elseif ($1 == act)   { poker_onact }
  24.   elseif ($1 == board) { poker_onboard }
  25.   elseif ($1 == show)  { poker_onshow }
  26. }
  27.  
  28. alias poker_onjoin {
  29.   ; $2 = fp, $3 = name, $4 = stack
  30.   if ($has(%seat,$2) == false) {
  31.     set %seat $list()        ; ensure containers exist (idempotent-ish)
  32.   }
  33.   ; track roster as a map fp->name and an ordered list
  34.   setat %name $2 $3
  35.   setat %stack $2 $4
  36.   push %seats $2
  37.   poker_render
  38. }
  39.  
  40. ; start a hand
  41. alias poker_deal {
  42.   set %handid $calc(%handid + 1)
  43.   set %board $list()
  44.   set %street preflop
  45.   set %pot 0
  46.   set %tocall %bb
  47.   ; secret deck (dealer only). seed kept private; revealed at showdown to verify.
  48.   set %seed $age.rand(32)
  49.   poker_builddeck
  50.   ; deal 2 hole cards per seat, sealed to each player's +AGE key
  51.   set %i 0
  52.   foreach %fp %seats {
  53.     set %c1 $get(%deck,$calc(%i * 2))
  54.     set %c2 $get(%deck,$calc(%i * 2 + 1))
  55.     age.seal %fp %c1 %c2
  56.     set %i $calc(%i + 1)
  57.   }
  58.   ; community sits after all hole cards
  59.   set %base $calc($len(%seats) * 2)
  60.   ; post blinds (seats 0,1) and open betting
  61.   poker_postblinds
  62.   age.send %chan start %handid
  63.   poker_render
  64. }
  65.  
  66. alias poker_builddeck {
  67.   set %deck $list()
  68.   set %keyed $list()
  69.   foreach %r $list(2,3,4,5,6,7,8,9,T,J,Q,K,A) {
  70.     foreach %s $list(c,d,h,s) {
  71.       ; keyed shuffle: sort cards by a PRF of (seed, card)
  72.       push %keyed $age.sha(%seed%r%s)~%r%s
  73.     }
  74.   }
  75.   set %keyed $sort(%keyed)
  76.   foreach %k %keyed {
  77.     set %card $get($split(%k,~),1)
  78.     push %deck %card
  79.   }
  80. }
  81.  
  82. alias poker_postblinds {
  83.   set %sbfp $get(%seats,0)
  84.   set %bbfp $get(%seats,1)
  85.   setat %bet %sbfp %sb
  86.   setat %bet %bbfp %bb
  87.   setat %stack %sbfp $calc($get(%stack,%sbfp) - %sb)
  88.   setat %stack %bbfp $calc($get(%stack,%bbfp) - %bb)
  89.   set %pot $calc(%sb + %bb)
  90.   set %turn 2                 ; UTG (simplified; heads-up handled by wrap)
  91.   if ($len(%seats) < 3) { set %turn 0 }
  92. }
  93.  
  94. ; our sealed hole cards arrive here
  95. on SIGNAL:age_deal {
  96.   ; $data = "<c1> <c2>"
  97.   setat %myhole me $data
  98.   poker_render
  99. }
  100.  
  101. ; betting
  102. on SIGNAL:poker_fold  { age.send %chan act fold }
  103. on SIGNAL:poker_call  { age.send %chan act call }
  104. on SIGNAL:poker_raise { age.send %chan act raise %betamount }
  105. on SIGNAL:poker_min   { set %betamount %bb }
  106. on SIGNAL:poker_q     { set %betamount $calc(%pot / 4) }
  107. on SIGNAL:poker_h     { set %betamount $calc(%pot / 2) }
  108. on SIGNAL:poker_t     { set %betamount $calc(%pot * 3 / 4) }
  109. on SIGNAL:poker_allin { set %betamount $get(%stack,$age.me) }
  110.  
  111. alias poker_onact {
  112.   ; $from acted: $2 = fold|check|call|raise ; $3 = amount
  113.   set %fp $from
  114.   if ($2 == fold) { setat %folded %fp 1 }
  115.   elseif ($2 == call) {
  116.     set %need $calc(%tocall - $get(%bet,%fp))
  117.     setat %bet %fp %tocall
  118.     setat %stack %fp $calc($get(%stack,%fp) - %need)
  119.     set %pot $calc(%pot + %need)
  120.   }
  121.   elseif ($2 == raise) {
  122.     set %need $calc($3 - $get(%bet,%fp))
  123.     setat %bet %fp $3
  124.     setat %stack %fp $calc($get(%stack,%fp) - %need)
  125.     set %pot $calc(%pot + %need)
  126.     set %tocall $3
  127.   }
  128.   poker_advance
  129.   poker_render
  130. }
  131.  
  132. alias poker_advance {
  133.   ; next live seat; if betting closed, deal next street (dealer authoritative)
  134.   set %turn $calc((%turn + 1) % $len(%seats))
  135.   ; round closes when every live player has matched %tocall (simplified: no "has acted" bit)
  136.   set %open 0
  137.   foreach %fp %seats {
  138.     if ($get(%folded,%fp) != 1) { if ($get(%bet,%fp) != %tocall) { set %open 1 } }
  139.   }
  140.   if (%open == 0) { poker_nextstreet }
  141. }
  142.  
  143. alias poker_nextstreet {
  144.   ; dealer reveals the next board card(s) from its deck
  145.   if (%street == preflop) { set %street flop | poker_reveal 3 }
  146.   elseif (%street == flop) { set %street turn | poker_reveal 1 }
  147.   elseif (%street == turn) { set %street river | poker_reveal 1 }
  148.   elseif (%street == river) { poker_showdown }
  149.   set %tocall 0
  150. }
  151.  
  152. alias poker_reveal {
  153.   ; $1 = count. dealer pulls from %deck at %base and broadcasts.
  154.   set %n 0
  155.   while (%n < $1) {
  156.     set %card $get(%deck,%base)
  157.     push %board %card
  158.     age.send %chan board %card
  159.     set %base $calc(%base + 1)
  160.     set %n $calc(%n + 1)
  161.   }
  162. }
  163.  
  164. on SIGNAL:poker_onboard { push %board $2 | poker_render }
  165.  
  166. ; hand evaluation (returns a sortable score)
  167. alias handscore {
  168.   set %ranks $list()
  169.   set %hist $map()
  170.   set %suits $map()
  171.   foreach %card $1- {
  172.     set %rv $get(%rankmap,$left(%card,1))
  173.     push %ranks %rv
  174.     setat %hist %rv $calc($get(%hist,%rv) + 1)
  175.     setat %suits $right(%card,1) $calc($get(%suits,$right(%card,1)) + 1)
  176.   }
  177.   set %ranks $sort(%ranks)
  178.   set %top 0
  179.   set %pairs 0
  180.   foreach %k $keys(%hist) {
  181.     set %c $get(%hist,%k)
  182.     if (%c > %top) { set %top %c }
  183.     if (%c == 2) { set %pairs $calc(%pairs + 1) }
  184.   }
  185.   set %flush 0
  186.   foreach %k $keys(%suits) { if ($get(%suits,%k) > 4) { set %flush 1 } }
  187.   set %straight $hasstraight($join($sort($keys(%hist))))
  188.   set %cat 0
  189.   if (%top == 4) { set %cat 7 }
  190.   elseif (%top == 3) { if (%pairs > 0) { set %cat 6 } else { set %cat 3 } }
  191.   elseif (%flush == 1) { set %cat 5 }
  192.   elseif (%straight == 1) { set %cat 4 }
  193.   elseif (%pairs > 1) { set %cat 2 }
  194.   elseif (%pairs == 1) { set %cat 1 }
  195.   return $calc(%cat * 100 + $get(%ranks,$calc($len(%ranks) - 1)))
  196. }
  197.  
  198. ; returns 1 if the sorted distinct rank-values ($1-) contain a 5-long run.
  199. alias hasstraight {
  200.   set %prev 0
  201.   set %run 1
  202.   set %best 1
  203.   foreach %v $1- {
  204.     if (%prev > 0) {
  205.       if (%v == $calc(%prev + 1)) { set %run $calc(%run + 1) } else { set %run 1 }
  206.       if (%run > %best) { set %best %run }
  207.     }
  208.     set %prev %v
  209.   }
  210.   if (%best > 4) { return 1 }
  211.   return 0
  212. }
  213.  
  214. alias poker_showdown {
  215.   set %best 0
  216.   set %winner
  217.   foreach %fp %seats {
  218.     if ($get(%folded,%fp) != 1) {
  219.       ; needs each player's revealed hole (collected via SIGNAL:poker_onshow); board is public
  220.       set %sc $handscore($get(%hole,%fp) $join(%board))
  221.       if (%sc > %best) { set %best %sc | set %winner %fp }
  222.     }
  223.   }
  224.   setat %stack %winner $calc($get(%stack,%winner) + %pot)
  225.   signal poker_win $get(%name,%winner) %pot
  226.   poker_render
  227. }
  228.  
  229. on SIGNAL:poker_onshow { setat %hole $from $2- }
  230. on SIGNAL:poker_win {
  231.   if ($1 == $nick) { echo %chan *** you won %2 chips! } else { echo %chan *** $1 wins %2 }
  232. }
  233.  
  234. ; the table view
  235. alias poker_render {
  236.   set %turnfp $get(%seats,%turn)
  237.   set %dealerfp $get(%seats,0)
  238.   ; precompute per-seat display into maps
  239.   foreach %fp %seats {
  240.     set %bc #3a3350
  241.     if (%fp == %turnfp) { set %bc #ffd166 }
  242.     setat %seatborder %fp %bc
  243.     set %dbg #00000000
  244.     set %dl
  245.     if (%fp == %dealerfp) { set %dl D | set %dbg #f0c020 }
  246.     setat %seatdealer %fp %dl
  247.     setat %seatdealerbg %fp %dbg
  248.     set %st
  249.     if ($get(%bet,%fp) > 0) { set %st BET $get(%bet,%fp) }
  250.     if ($get(%folded,%fp) == 1) { set %st FOLD }
  251.     setat %seatstatus %fp %st
  252.   }
  253.   set %myc $split($get(%myhole,me))
  254.   set %h1 $get(%myc,0)
  255.   set %h2 $get(%myc,1)
  256.   view {
  257.     surface bg #241b3a pad 8 {
  258.       column gap 12 pad 4 align center fill {
  259.         ; seats around the oval — index 0 (us) anchored at the bottom
  260.         ring radius 150 {
  261.           foreach %fp %seats {
  262.             column align center gap 2 {
  263.               stack {
  264.                 surface circle width 54 height 54 bg #6c5ce7 border $get(%seatborder,%fp) {
  265.                   column align center { text $left($get(%name,%fp),1) bold color #ffffff textsize 20 }
  266.                 }
  267.                 surface circle width 20 height 20 bg $get(%seatdealerbg,%fp) align bottom {
  268.                   text $get(%seatdealer,%fp) bold color #1a1a1a textsize 11
  269.                 }
  270.               }
  271.               text $get(%name,%fp) bold color #ffffff textsize 11
  272.               text $get(%stack,%fp) color #c8c8d8 textsize 11
  273.               surface bg #000000 pad 2 { text $get(%seatstatus,%fp) bold color #ffd166 textsize 10 }
  274.             }
  275.           }
  276.         }
  277.         ; the felt centre: pot + community board
  278.         surface circle bg #2b2440 pad 14 {
  279.           column align center gap 4 {
  280.             text POT bold color #b9a8e0 textsize 12
  281.             text %pot bold color #ffffff textsize 24
  282.             row gap 4 {
  283.               card $get(%board,0) width 38 | card $get(%board,1) width 38 | card $get(%board,2) width 38 | card $get(%board,3) width 38 | card $get(%board,4) width 38
  284.             }
  285.           }
  286.         }
  287.         ; our hole cards
  288.         row gap 6 { card %h1 width 50 | card %h2 width 50 }
  289.         ; actions
  290.         row gap 6 fill {
  291.           button "FOLD" poker_fold weight 1
  292.           button "CALL" poker_call weight 1
  293.           button "RAISE" poker_raise weight 1
  294.         }
  295.         row gap 4 fill {
  296.           button "MIN" poker_min weight 1
  297.           button "1/4" poker_q weight 1
  298.           button "1/2" poker_h weight 1
  299.           button "3/4" poker_t weight 1
  300.           button "ALL IN" poker_allin weight 1.2
  301.         }
  302.       }
  303.     }
  304.   }
  305. }

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.