; CakeHHConverter
; date: 12:18 05 October 2007
; AHK version: 1.0.47.04
; author: Roland
; email: roland@overcards.com

#NoEnv
#SingleInstance force
#Include %a_scriptDir%
#Include functions_v3.ahk

SetWorkingDir %a_scriptDir%

OnExit exitSub

ini:="CakeHHConverter.ini"
version:="1.01"

ifNotExist HandHistories
  FileCreateDir HandHistories
  
Menu, Tray, NoStandard
Menu, Tray, Add, E&xit, exit

Menu, file, add, Convert &File`t`tCtrl+F, menu
Menu, file, add, Convert &Directory`t`tCtrl+D, menu
Menu, file, add
Menu, file, add, E&xit`t`tAlt+F4, menu
Menu, about, add, &Help`t`tF1, menu
Menu, about, add, &About, menu
Menu, about, add
Menu, about, add, &Donate, menu
Menu, menu, add, &File, :file
Menu, menu, add, &About, :about

gosub gui
;handleFile("C:\Documents and Settings\byb06xpu\Desktop\cakelimithands\CakeArchive\20070918 Lillehammer (Turbo, 6-max) 11672.txt")
return

;==========================

convertFile() {
global ini
Gui +ownDialogs
IniRead, dir, % ini, prefs, dir, % a_programFiles
FileSelectFile, file, 1, *%dir%, Select file to convert
ifEqual, file,, return
SplitPath, file,, dir
IniWrite, % dir, % ini, prefs, dir
handleFile(file)
}

;==========================

convertFolder() {
global ini
Gui +ownDialogs
IniRead, dirF, % ini, prefs, dirF, % a_programFiles
FileSelectFolder, folder, *%dirF%, 0, Select folder to convert
ifEqual, folder,, return
IniWrite, % folder, % ini, prefs, dirF
Loop %folder%\*.*
  handleFile(a_loopFileFullPath) 
}

;==========================

handleFile(file) {
global edit, gt
Gui, Submit, noHide
SplitPath, file, fileName
sb_setText("  Converting file " fileName "...")
FileRead, txt, % file
txt:=regExReplace(txt, "\R\R\R", chr(1))
StringSplit, hh, txt, % chr(1)
Loop % hh0 {
    ifEqual, hh%a_index%,, continue
    if this_hh:=convertHH(hh%a_index%, gt)
      listAdd(newTxt, this_hh, "`n`n`n`n")
  } FormatTime, now,, yyyyMMdd
FileDelete HandHistories\%fileName%
FileAppend, % newTxt, HandHistories\%fileName%
GuiControl,, edit, % ( edit!="" ? ( edit . "`n`n" ) : "" ) . "### " fileName " ###`n`n" newTxt
sb_setText("  Done.")
}

;==========================

convertHH(hh, gameType="Hold'em No Limit") {
hh:=regExReplace(hh, "\R", "`n")
regExReplace(hh, "posts (small|big) blind", "", c)
;ifLess, c, 3, return
regExMatch(hh, "U)^Hand #(\d+): (\w+)( \((\w+, )?((\d{1,2})-max)\))? \d+\R", m)
hh#:=m1
table:=m2
max:=m6="" ? 10 : m6
regExMatch(hh, ": posts small blind \$([\d.,]+)\R(.*): posts big blind \$([\d.,]+)", m)
sbA:=strRep(m1, ","), bbA:=strRep(m3, ",")
regExMatch(hh, "Seat (\d{1,2}): " m2 " \(", m)
bb:=m1
Loop, Parse, hh, `n
  { ifEqual, a_index, 1, continue
    if !regExMatch(a_loopfield, "U)Seat (\d{1,2}): (.*) \(", m)
      break
    listAdd(seats, m1)
    listAdd(seatInfo, strRep(a_loopfield, "(", "($"), "`n")
  } ;msgbox % seats "`n" bb "`n" ( pp:=( ( p:=(listGetPos(seats, bb)-2))>0 ? p : p-1 ) ) "`n" pp "`n"  listGet(seats, pp)
button:=listGet(seats, ( pp:=( ( p:=(listGetPos(seats, bb)-2))>0 ? p : p-1 ) ))
;msgbox % seats "`n" bb "`n" button
Loop, Parse, hh, `n
  { if regExMatch(a_loopfield, "U)(.*): posts (small|big) blind \$([\d.,]+)", m)
      listAdd(blindingInfo, a_loopfield, "`n"), mP:=m
  } x:=bbA, total_pot:=0, str:=0, extraSB:=-1
Loop, Parse, hh, `n
  { l:=a_loopfield
    ;msgbox % l "`n" x
    if regExMatch(l, "(.*): (bets|calls|raises|is all in)", m) {
        listAdd(players_%str%, m1)
        ;msgbox % m1 "`nl: " l "`n" str
      }
    if regExMatch(l, "(.*): bets \$([\d.,]+)", m)
      x:=strRep(m2, ",")
     else if regExMatch(l, "(.*): raises to \$([\d.,]+)", m)
        l:=strRep(l, "raises to", "raises $" (m2-x) " to"), x:=strRep(m2, ",")
     else if regExMatch(l, "\*\*\* [A-Z]+ \*\*\* \[", m) {
          Sort, players_%str%, D, U
          Loop, Parse, players_%str%, `,
            {  ifEqual, a_loopfield,, break
               this_p:=hexify(a_loopfield)
               ;msgbox % this_p "`n" a_loopfield
               ;msgbox % call_%this_p%_%str% "`n" a_loopfield "`n" str
            }
          x:=0, call:=0, str++
          ;l:=strRep(regExReplace(l, "[ \.]", "", "", -1, strLen(m)), ",", " ")
          l:=strRep(strRep(regExReplace(regExReplace(l, " +", " "), "[.,]"), "[ ", "["), " ]", "]")
          listAdd(board, strMid(l, "[", "]"), " ")
        }
     ;msgbox % call "`n" x
     if regExMatch(l, "(.*): is all in", m) {
          player_:=m1, player:=hexify(m1)
          regExMatch(hh, "Seat \d{1,2}: " m1 " \(([\d.]+) in chips\)", m)
          a:=round(m1-$pip_%player%, 2)
          ;msgbox % x "`n" a "`n" l "`n`n" hh "`n`n" $pip_%player%
          l:=player_ ": " ( x=0 ? "bets" : ( x>a ? "calls" : "raises $" round(a-x, 2) ) ) " to $" a " and is all-in"
          ;msgbox % l
        }
     if regExMatch(l, "(.*): (posts|bets|raises) ", m) {
          player:=hexify(m1)
          extraSB:=InStr(l, "posts small blind") ? extraSB+1 : extraSB
          regExMatch(l, "[\d.,]+", m, InStr(l, "$", 1, 0))
          ;msgbox % strRep(m, ",") "`n" x
          ;if InStr(l, "posts small blind")
          ;  total_pot-=(bbA-sbA)
          callA:=( InStr(l, "all-in") ? a : strRep(m, ",") )
          call:=( InStr(l, "posts small blind") ? bbA : callA )
          total_pot+=( callA-(call_%player%_%str% ? call_%player%_%str% : 0) )
          ;msgbox % l "`n" ( call-(call_%player%_%str% ? call_%player%_%str% : 0) ) "`n" x "`n" total_pot
          ;msgbox % l "`n" call "`n" str "`n" player "`n" x "`n" total_pot "`n" strRep(m, ",")
          call_%player%_%str%:=( extraSB>0 && InStr(l, "posts small blind") ) ? 0 : callA
          ;msgbox % $pip_%player% "`n" l
          $pip_%player%+=callA
          ;msgbox % $pip_%player% "`n" l
          ;msgbox % l "`n" call
        } else if regExMatch(l, "(.*): calls", m) {
          player:=hexify(m1)
          ;msgbox % l "`n" call_%player%_%str% "`n" call
          this_call:=call-(call_%player%_%str% ? call_%player%_%str% : 0)
          ;msgbox % l "`n" this_call "`n" call "`n`n" hh
          total_pot+=this_call
          ;if InStr(l, "all-in")
          ;  msgbox % l
          l:=( InStr(l, "all-in") ? l : m " $" this_call )
          ;msgbox % l "`n" call "`nthis_call: " this_call "`n" call_%player%_%str% "`n" str "`n" player "`n" total_pot
          if InStr(l, "all-in")
            this_call:=a
           call_%player%_%str%+=this_call
          $pip_%player%+=this_call
        }
     listAdd(body, l, "`n")
  } body:=regExReplace(body, "Dealt to (.*) \[ (.*)  \]", "Dealt to $1 [$2]")
StringTrimLeft, body, body, InStr(hh, mP)+StrLen(mP)
regExMatch(hh, "m`n)(.*) wins \$([\d.,]+)", m)
winner:=m1, final_pot:=strRep(m2, ",")
if p:=regExMatch(body, "m`n)^(.*): returns uncalled bet \$([\d.,]+)", m) {
    total_pot-=strRep(m2, ",")
    StringLeft, body, body, % p-1
    body.=winner " collected $" final_pot " from pot`n" winner ": doesn't show hand"
  } else {
    StringTrimLeft, sd, body, InStr(body, "*** SHOW DOWN ***")-1
    StringLeft, body, body, InStr(body, "*** SHOW DOWN ***")-2
    if InStr(sd, ": shows [") {
         ;msgbox % "x" body "x`n`nx" sd "x"
        Loop, Parse, sd, `n
           { l:=a_loopfield
              if InStr(l, ": shows [")
                listAdd(sd_, regExReplace(l, "(.*): shows \[ (.*)  \] \((.*)  \)", "$1: shows [$2] ($3)"), "`n")
           } listAdd(body, "*** SHOW DOWN***`n" strRep(regExReplace(strRep(sd_, ".]", "]"), " +", " "), " )", ")"), "`n")
      } listAdd(body, winner " collected $" final_pot " from pot", "`n")
  } FormatTime, dateTime,, yyyy/MM/dd - HH:mm:ss (ET)
newHH:="PokerStars Game #" hh# ":  " gameType " ($" sbA "/$" bbA ") - " dateTime 
            . "`nTable '" table "' " max "-max Seat #" button " is the button"
            . "`n" seatInfo
            . "`n" blindingInfo
            . "`n*** HOLE CARDS ***"
            . "`n" tws(body)
            . "`n*** SUMMARY ***"
            . "`nTotal pot $" total_pot " | Rake $" round(total_pot-final_pot,2)
            . (board!="" ? "`nBoard [" board "]" : "" )
return newHH
}

;==========================

#IfWinActive Cake HH Converter ahk_classAutoHotkeyGUI

^f:: convertFile()
^d:: convertFolder()
f1::gosub help

;==========================

menu:
label:=regExReplace(a_thisMenuItem, "[ &`t]")
ifEqual, label, ConvertFileCtrl+F
  convertFile()
else ifEqual, label, ConvertDirectoryCtrl+D
  convertFolder()
else ifEqual, label, ExitAlt+F4, gosub guiClose
else ifEqual, label, HelpF1, gosub help
else ifEqual, label, About, gosub about
else ifEqual, label, Donate, gosub donate
return

help:
run % web() "http://www.overcards.com/wiki/moin.cgi/CakeHHConverter"
return

about:
msgbox("CakeHHConverter v" version "`nAuthor: Roland`nE-mail: roland@overcards.com"
  , "iconI", "About", "Comic Sans MS")
return

donate:
donate()
return

;==========================

gui:
Gui +resize
Gui, Margin, 5, 5
Gui, Font,, Comic Sans MS
Gui, Menu, menu
Gui, Add, ddl, vgt, Hold'em No Limit|HOLD'EM LIMIT
GuiControl, ChooseString, gt, % iniRead(ini, "prefs", "lastGt")
Gui, Add, Edit, vedit r10 +hscroll
Gui, Add, Statusbar
IniRead, x, % ini, gui, x, center
IniRead, y, % ini, gui, y, center
IniRead, w, % ini, gui, w, 300
IniRead, h, % ini, gui, h, 200
Gui, Show, x%x% y%y% w%w% h%h%, Cake HH Converter
return

;==========================

GuiDropFiles:
Loop, Parse, a_guiEvent, `n
  handleFile(a_loopfield)
return

;==========================

GuiSize:
GuiControl, Move, edit, % "w" a_guiwidth-10 "h" a_guiheight-65
guiW:=a_guiwidth, guiH:=a_guiheight
return

;==========================

GuiClose:
Gui +lastfound
Gui, Submit, noHide
WinGetPos, x, y
IniWrite, % x, % ini, gui, x
IniWrite, % y, % ini, gui, y
IniWrite, % guiW, % ini, gui, w
IniWrite, % guiH, % ini, gui, h
IniWrite, % gt, % ini, prefs, lastGt
exitApp

;==========================

exit:
exitApp

;==========================

exitSub:
exitApp

CakeHHConverter (last edited 2007-10-05 11:21:34 by RolandAgain)