.From the work of Roland & SamIAm as posted on 2+2. Discuss the code and coding in the iWitness coding thread.

About:

This Party Poker script opens, and keeps open, tables with your defined characteristics. For instance, you want to observe $10/$20 and $15/$30 6max Limit Hold'Em tables with at least four players sitting. iWitness opens that type of table and watches to ensure they maintain enough players. If the table starts to empty, iWitness closes it and opens a new one.

If you are looking for a version to run with Empire, try iWitnessEmpire; the functionality will probably be integrated into a single version in the future.

Basic Instructions:

1. Copy the code into an editor of your choice and save it as iWitness.ahk. Install AutoHotkey if you haven’t yet. Double-click iWitness.ahk to run it.

2. Once iWitness is open, go to the “Groups” tab and create some groups. Simply choose a game, limit etc. When all the fields are filled in, double click on the row to add the group.

iwitness4fd

The script will open as many tables as it can find of the first group, then the second, the third etc. until it reaches the maximum number of tables (10 when logged in, 4 when logged out) or it runs out of groups. When checking tables, it will check if a table still has the minimum number of players you defined for it’s group; if it doesn’t it will be closed. The script will also close tables from groups with lower priority if there are available tables for an earlier group. If that sounds complicated, it is ;) .

You might find that this is overkill when witnessing low-stakes Limit Hold'Em because you’ll find so many of these tables. For games that are less popular, like NL or stud, this feature is more useful.

Note that you can edit a group by double clicking on it. Delete it by double right clicking on it.

3. Right click on the tray icon and select “Login”. If you don’t want the script to auto-login for you (because you don’t save your password), uncheck that option. You can now choose on which accounts you want to log in.

4. Go to the “Witness” tab and hit “Go”.

Warning: We didn’t know people would want to use this while playing. Therefore, I can’t recommend trying to run iWitness while you are playing. It might close a table you are playing at, or it might crash Party, kill your internet connection or whatever. Especially the “Close tables” option is dangerous - I don’t know why but it sometimes crashes the client. So be careful until we add support for play&witness;).

SNG Support:

NL Hold’em SNGs are tricky because once they have started there is no way to find out if they are 6max or full. Therefore, we have to open them while people are still registering (since that information is available then but not later). Now, we’d have a hard time finding 10 open SNGs right away; thus, for NL Hold’em SNGs and for NL Hold’em SNGs only, we do things differently: The script will keep trying to open SNGs until it has opened 10 (or 4 if not logged in). Then, it will continue to check every minute if one or more of them have finished and if yes, it will again try to open more SNGs until it has opened 10 (4).

IMPORTANT: This will only happen if your groups are entirely NL Hold’em SNGs. You can create other groups with different game types and the script will still try to open as many SNGs as it can. However, this won’t be many, since there are never more than a hand full open for registration at any given moment. So, if you want to witness SNGs effectively, there should be no other game types in your groups.

All other SNGs are treated similarly to ring games - we simply open as many SNG that are still at Level 1 as we can. (This is possible because we don’t have to check for 6max/full).

Networking Instructions:

iWitness can coordinate multiple windows accounts on multiple machines.

1. Choose your “networking folder”; meaning a folder all your windows accounts have access to.

2. Put the script in this folder and create shortcuts for every account. Now launch the script from each account. The groups are shared by all the clients so you’ll only have to create them once.


;___________________________________________________________________________________________________
;_____________________________________________iWitness v2.16___________________________________________
;___________________________________________________________________________________________________

;____ Date: 2006-04-11
;____ AutoHotkey Version: 1.0.43.06
;____ Platform: WinXP
;____ Authors: SamIAm & Roland
;____ E-mail: iWitness@overcards.com
;____ All rights reserved

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; auto-execute section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

string = -----------------------------------------------------------------------------------------------------------------------------`n
Log(string)

OnExit, ExitSub

DetectHiddenWindows On

period = 300000

version = iWitness v2.16

;Load preferences
IniRead, gui_x, iWitness.ini, Prefs, gui_x, Center
IniRead, gui_y, iWitness.ini, Prefs, gui_y
IniRead, gui_w, iWitness.ini, Prefs, gui_w, 500
IniRead, gui_h, iWitness.ini, Prefs, gui_h, 400
IniRead, open_password, iWitness.ini, Prefs, open_password, Yes
IniRead, login_on_first_client, iWitness.ini, Prefs, LogInOnFirstClient, Yes
IniRead, party_loc, iWitness.ini, Prefs, Party location, C:\Program Files\PartyGaming\PartyGaming.exe
IniRead, logging, iWitness.ini, Prefs, Logging, Yes
IniRead, auto_start, iWitness.ini, Prefs, auto_start, no
IniRead, tooltips, iWitness.ini, Prefs, tooltips, Yes
IniRead, sus_hotkeys, iWitness.ini, prefs, sus_hotkeys, no
IniRead, play_and_witness, iWitness.ini, Prefs, PlayAndWitness, no
IniRead, hand_history_folder, iWitness.ini, Prefs, HHFolder, blank
IniRead, SafeMode, iWitness.ini, Prefs, SafeMode, Yes
IniRead, EscapeTables, iWitness.ini, Prefs, EscapeTables, no


;create an array of usernames (if we have some)
;we have to find out if the current username was already recorded
;if not, it's added to the list
usernames = 0
Loop
 {
  IniRead, username%A_Index%, iWitness.ini, Accounts, username%A_Index%
  If username%A_Index% = Error
    Break
  usernames++
 }
Loop, %usernames%
 {
  If username%A_Index% = %A_UserName%
   {
    already_listed = Yes
    Break
   }
 }
If already_listed <> Yes
 {
  our_num := usernames + 1
  If our_num = 1
   {
    IniWrite, Yes, iWitness.ini, LoginPrefs, %A_UserName%
    max_tables = 12
   }
  username%our_num% = %A_UserName%
  usernames++
 }
IniDelete, iWitness.ini, Accounts  ;delte this section and write it back (in case this account wasn't recorded yet)
Loop, %usernames%
 {
  this_username := username%A_Index%
  IniWrite, %this_username%, iWitness.ini, Accounts, username%A_Index%
 }


FormatTime, Time

string =
(
%time%`n%a_workingdir%`n%a_ahkversion%`n%a_osversion%`n%version%`ncompiled: %a_iscompiled%
)
Log(string)

 ;Setup Tray
Menu, Tray, NoStandard
Menu, Tray, DeleteAll
Menu, Tray, Add, Show, Show
Menu, Tray, Add
Menu, Tray, Add, About, About
Menu, Tray, Add, Help, Help
Menu, Tray, Add
Menu, Tray, Add, Auto-Login, Auto-Login
If open_password = Yes
  Menu, Tray, Check, Auto-Login
Loop, %usernames%  ;add the usernames to the "login on.." menu and check if needed
 {
  this_username := username%A_Index%
  Menu, Login on..., Add, %this_username%, LoginPrefs
  IniRead, Check, iWitness.ini, LoginPrefs, %this_username%
  If Check = Yes
    Menu, Login on..., Check, %this_username%
 }
Menu, Tray, Add, Login on..., :Login on...
Menu, Tray, Add
Menu, Options, Add, Enable Logging, Logging
If logging = Yes
  Menu, Options, Check, Enable Logging
Menu, Options, Add, Safe mode, SafeMode
If SafeMode = Yes
  Menu, Options, Check, Safe mode
Menu, Options, Add, Tooltips, Tooltips
If tooltips = Yes
  Menu, Options, Check, Tooltips
Menu, Options, Add, Auto-Start, AutoStart
If auto_start = Yes
  Menu, Options, Check, Auto-Start
Menu, Options, Add, Suspend Hotkeys, SuspendHotkeys
If sus_hotkeys = Yes
  Menu, Options, Check, Suspend Hotkeys
Menu, Options, Add, Donate, Donate
Menu, Tray, Add, Options, :Options
Menu, Tray, Add
Menu, Play && Witness, Add, Play && Witness, PlayAndWitness
If play_and_witness = Yes
  Menu, Play && Witness, Check, Play && Witness
Menu, Play && Witness, Add, Escape tables, EscapeTables
If EscapeTables = Yes
  Menu, Play && Witness, Check, Escape tables
Menu, Play && Witness, Add, Reset%A_Tab%Ctrl+R, Reset
Menu, Tray, Add, Play && Witness, :Play && Witness
Menu, Tray, Add
Menu, Tray, Add, Close Tables, CloseAll
Menu, Tray, Add, Tile Tables, Tile
Menu, Tray, Add, Refresh Players%A_Tab%Ctrl+P, UpdatePlayers
Menu, Tray, Add, Refresh Tables%A_Tab%Ctrl+T, OpenTables
Menu, Tray, Add
Menu, Tray, Add, Exit, Exit
Menu, Tray, Default, Show
Menu, Tray, Tip, %version%

margin_x := 2*1.25*8

Gui, +Resize +LastFound
Gui, Color, FFFACD
Gui, Font, Arial c1C1C1C
Gui, Add, Tab, vMyTab Buttons, Witness|Groups

;Witness Tab
Gui, Tab, 1
Gui, Add, Text, vtxt_tables Section, Tables:
Gui, Add, ListView, Sort +LV0x1 BackgroundFFFFF0 C36648B vgui_tableList gShowSelectedTable, Priority|Table|Game|Limit|Players|Client
Gui, Add, Text, vtxt_clients ys, Clients:
Gui, Add, ListView,  Sort +LV0x1 BackgroundFFFFF0 CFF6347 vgui_clientList, Client|User Name|Machine|Tables
Gui, Add, Button,  vgui_gobutton2 w30 Section gGuiGo,Go
Gui, Add, Button,  vgui_stopbutton2 w30 ys gGuiStop, Stop

;Groups Tab
Gui, Tab, 2
Gui, Add, Text, vtext_game Section, Game:
Gui, Add, DDL,  Vwhich_game ggame_selection,
( Join
Limit Hold'em|No-Limit Hold'em|Pot-Limit Hold'em|Omaha|Omaha Hi-Lo|Pot-Limit Omaha|Pot-Limit Omaha Hi-Lo|7 Card Stud|7 Stud Hi-Lo|Beginners (Limit)|Beginners (NL)|Jackpot (Bad Beat)|Jackpot
(High Hand)|SNG (NL Hold'em)|SNG (PL Hold'em)|SNG (Limit Hold'em)|SNG (Omaha Hi-Lo)|SNG (PL Omaha Hi-Lo)|SNG (7 Card Stud)|SNG (7 Stud Hi-Lo)
)
Gui, Add, Text, ys Section, Limit:
Gui, Add, DDL, Vwhich_limit gprefs,
Gui, Add, Text, vmin_players_txt ys Section, Min Players:
Gui, Add, DropDownList,  vplayers_min w40 gprefs, 2|3|4|5|6|7|8|9|10
Gui, Add, Text, vtables_tops_txt, Max tables:
Gui, Add, DropDownList, vtables_tops w30 gprefs, ---|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20
Gui, Add, Text, vmin_pot_txt, Min. avg. pot:
Gui, Add, Edit, vmin_pot w30 r1 Number gprefs
Gui, Add, Radio, vgui_typebox Checked x%margin_x% Section gfull, Full
Gui, Add, Radio, vgui_typebox2 ys Section g6max, 6max
Gui, Add, Radio, vgui_speed ys Section gSpeed, speed
Gui, Add, ListView, vListViewEdit x%margin_x% r1 LV0x8000 NoSortHdr Grid BackgroundFFFFF0 C36648B gAdd_Rule, Priority|Game|6max/Full|Limit|Players|Tables|Pot
Gui, Add, ListView, vListViewPrefs x%margin_x% -Hdr -LV0x4 Sort Grid BackgroundFFFFF0 C36648B gEdit_Rule, Priority|Game|6max/Full|Limit|Players|Tables|Pot
LV_ModifyCol(1, "Integer")
 ;Add the icon if it exists.
IfExist, iWitness.ico
  Menu, Tray, Icon, iWitness.ico, 1
;Disable the stop button at startup.
GuiControl, Disable, gui_stopbutton
GuiControl, Disable, gui_stopbutton2

Gosub, ReadPrefs
Gosub, full
Gui, ListView, ListViewEdit
LV_Delete()
Mode = Add

max_tables := RecordClients("yes")
;Log the max tables
string = max_tables = %max_tables%
Log(string)
 ;Log whether the .ini exists.
IfExist, iWitness.ini
  string = .ini exists.
Else
  string = .ini does Not Exist.
Log(string)

If tooltips = Yes
  SetTimer, Tip, 1000

If this_client <> 1  ;better not let them mess with the prefs if the other client's already going
 {
  GuiControl, Disable, which_game
  GuiControl, Disable, which_limit
  GuiControl, Disable, players_min
  GuiControl, Disable, Priority
  GuiControl, Disable, 6max
  GuiControl, Disable, Full
  GuiControl, Disable, ListViewEdit
 }

If (gui_x = "center" OR gui_x < 0)
  Gui, Show, Center w%gui_w% h%gui_h%, iWitness (Client %this_client%)
Else
  Gui, Show, x%gui_x% y%gui_y% w%gui_w% h%gui_h%, iWitness (Client %this_client%)

If auto_start = Yes
  Gosub, GuiGo
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; subroutines ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;________________________________________ gui labels ________________________________________
GuiSize:
 If ErrorLevel = 1
  {
   Gui, Hide
   Return
  }
 Sleep, 50

 GuiControlGet, players_min, Pos
 GuiControlGet, which_limit, Pos

 GuiControl, Move, gui_gobutton, % "x" A_GuiWidth/2 - 50 - 1.25*8/2 "y" players_minY + players_minH + 20
 GuiControl, Move, gui_stopbutton, % "x" A_GuiWidth/2 + 1.25*8/2 "y" players_minY + players_minH + 20
 GuiControlGet, which_game, Pos
 GuiControl, Move, gui_typebox, % "y" which_gameY + which_gameH + 1.25*8 "x" which_gameX
 GuiControl, Move, gui_typebox2, % "y" which_gameY + which_gameH + 1.25*8
 GuiControl, Move, gui_Speed, % "y" which_gameY + which_gameH + 1.25*8

 GuiControl, Move, ListViewEdit, % "y" which_gameY + which_gameH*2 + 1.25*8*2 "w" A_GuiWidth - margin_x*2
 GuiControlGet, ListViewEdit, Pos
 GuiControl, Move, ListViewPrefs, % "y" ListViewEditY + ListViewEditH + 1.25*8  "w" A_GuiWidth - margin_x*2 "h" A_GuiHeight - (ListViewEditY + ListViewEditH + 1.25*8*2)

 GuiControlGet, min_players_txt, Pos
 GuiControl, Move, players_min, % "w" min_players_txtW
 GuiControl, Move, tables_tops_txt, % "x" min_players_txtW + min_players_txtX + 1.25*8 "y" min_players_txtY
 GuiControlGet, tables_tops_txt, Pos
 GuiControl, Move, tables_tops, % "x" min_players_txtW + min_players_txtX + 1.25*8 "y" min_players_txtY + min_players_txtH + .75*8 "w" tables_tops_txtW
 GuiControlGet, tables_tops_txt, Pos
 GuiControl, Move, min_pot_txt, % "x" tables_tops_txtX + tables_tops_txtW + 1.75*8 "y" min_players_txtY
 GuiControlGet, min_pot_txt, Pos
 GuiControl, Move, min_pot, % "x" tables_tops_txtX + tables_tops_txtW + 1.75*8 "y" min_players_txtY + min_players_txtH + .75*8 "w" min_pot_txtW ; "h" 18

 GuiControlGet, txt_tables, Pos
 GuiControl, Move, gui_tableList, % "y" txt_tablesY + txt_tablesH + .75*8 "h" A_GuiHeight - (txt_tablesY + txt_tablesH + 1.25*8*2 + .75*8) "w" (A_GuiWidth - txt_tablesX)/1.5
 GuiControl, Move, gui_clientList, % "x" (A_GuiWidth - txt_tablesX)/1.5 + 1.25*8*3 "w" (A_GuiWidth - txt_tablesX)/3 - 1.25*8*3 "h" ((A_GuiHeight - (txt_tablesY + txt_tablesH + .75*8*2))/3)*2
 GuiControl, Move, txt_clients, % "x" (A_GuiWidth - txt_tablesX)/1.5 + 1.25*8*3
 GuiControlGet, gui_clientList, Pos
 GuiControl, Move, gui_gobutton2, % "x" gui_clientListX "y" gui_clientListY + gui_clientListH + 1.25*8
 GuiControl, Move, gui_stopbutton2, % "x" gui_clientListX + 30 + 1.25*8 "y" gui_clientListY + gui_clientListH + 1.25*8

 Gui, ListView, gui_tableList
 LV_ModifyCol(1, ((A_GuiWidth - txt_tablesX)/1.5)/12)
 LV_ModifyCol(2, ((A_GuiWidth - txt_tablesX)/1.5)/4)
 LV_ModifyCol(3, ((A_GuiWidth - txt_tablesX)/1.5)/3.5)
 LV_ModifyCol(4, ((A_GuiWidth - txt_tablesX)/1.5)/6)
 LV_ModifyCol(5, ((A_GuiWidth - txt_tablesX)/1.5)/12)
 LV_ModifyCol(6, ((A_GuiWidth - txt_tablesX)/1.5)/12)

 Gui, ListView, gui_clientlist
 LV_ModifyCol(1, ((A_GuiWidth - txt_tablesX)/3 - 1.25*8*2)/6)
 LV_ModifyCol(2, ((A_GuiWidth - txt_tablesX)/3 - 1.25*8*2)/6*3)
 LV_ModifyCol(3, 0)
 LV_ModifyCol(4, ((A_GuiWidth - txt_tablesX)/3 - 1.25*8*2)/6)

 Gui, ListView, ListViewEdit
 LV_ModifyCol(1, (A_GuiWidth - margin_x*2)/20)
 LV_ModifyCol(2, (A_GuiWidth - margin_x*2)/3)
 LV_ModifyCol(3, (A_GuiWidth - margin_x*2)/7)
 LV_ModifyCol(4, (A_GuiWidth - margin_x*2)/6)
 LV_ModifyCol(5, (A_GuiWidth - margin_x*2)/10)
 LV_ModifyCol(6, (A_GuiWidth - margin_x*2)/12)
 LV_ModifyCol(7, (A_GuiWidth - margin_x*2)/10)

 Gui, ListView, ListViewPrefs
 LV_ModifyCol(1, (A_GuiWidth - margin_x*2)/20)
 LV_ModifyCol(2, (A_GuiWidth - margin_x*2)/3)
 LV_ModifyCol(3, (A_GuiWidth - margin_x*2)/7)
 LV_ModifyCol(4, (A_GuiWidth - margin_x*2)/6)
 LV_ModifyCol(5, (A_GuiWidth - margin_x*2)/10)
 LV_ModifyCol(6, (A_GuiWidth - margin_x*2)/12)
 LV_ModifyCol(7, (A_GuiWidth - margin_x*2)/10)

 WinSet, Redraw

 iWitness_w = %A_GuiWidth%
 iWitness_h = %A_GuiHeight%
Return

GuiClose:
 ExitApp
Return

gui2_buttonOK:
 Gui, 2: Destroy
 Gui, 1: -Disabled
Return

;________________________________________ tray menu labels ________________________________________

Exit:
 Gosub, ExitSub
Return

Show:
 Gui, Show
Return

;Displays the About Tooltip
About:
 TrayTip, %version%,Authors: SamIAm & Roland`nEmail: iWitness@overcards.com`n`nAll rights reserved., , 1
Return

;Displays the Help page.
Help:
 Run, http://overcards.com/wiki/moin.cgi/iWitness
Return

;En/disables logging
Logging:
 If logging = no
  {
   Menu, Options, Check, Enable Logging
   logging = Yes
  }
 Else
  {
   Menu, Options, UnCheck, Enable Logging
   logging = no
  }
Return

;this handles login/log out for every username
LoginPrefs:
 IniRead, yes_no, iWitness.ini, LoginPrefs, %A_ThisMenuItem%
 If yes_no = Yes
  {
   Menu, Login on..., UnCheck, %A_ThisMenuItem%
   IniWrite, no, iWitness.ini, LoginPrefs, %A_ThisMenuItem%
   max_tables = 4  ;switch to logged out mining, so max_tables has to be 4
  }
 Else
  {
   Menu, Login on..., Check, %A_ThisMenuItem%
   IniWrite, Yes, iWitness.ini, LoginPrefs, %A_ThisMenuItem%
   max_tables = 12
  }
Return

;Sets the auto-login gui according to the prefs.
Auto-Login:
 If open_password = Yes
  {
   Menu, Tray, UnCheck, Auto-Login
   open_password = no
  }
 Else
  {
   Menu, Tray, Check, Auto-Login
   open_password = Yes
  }
Return

SafeMode:
 If SafeMode = Yes
  {
   SafeMode = no
   Menu, Options, UnCheck, Safe mode
  }
 Else
  {
   SafeMode = Yes
   Menu, Options, Check, Safe mode
  }
Return

AutoStart:
 If auto_start = no
  {
   auto_start = Yes
   Menu, Options, Check, Auto-Start
  }
 Else
  {
   auto_start = no
   Menu, Options, UnCheck, Auto-Start
  }
Return

SuspendHotkeys:
 If sus_hotkeys = no
  {
   Menu, Options, Check, Suspend Hotkeys
   sus_hotkeys = Yes
  }
 Else
  {
   Menu, Options, UnCheck, Suspend Hotkeys
   sus_hotkeys = no
  }
Return

Tooltips:
 If tooltips = Yes
  {
   Menu, Options, UnCheck, Tooltips
   SetTimer, Tip, Off
   tooltips = no
  }
 Else
  {
   Menu, Options, Check, Tooltips
   SetTimer, Tip, On
   tooltips = Yes
  }
Return

Reset:
 Check("clear_tables_to_escape")
Return

PlayAndWitness:
 If play_and_witness = Yes
  {
   play_and_witness = no
   Menu, Play && Witness, UnCheck, Play && Witness
  }
 Else
  {
   Gui, +OwnDialogs
   If (hand_history_folder = "blank" OR FileExist(hand_history_folder) = "")
     FileSelectFolder, hand_history_folder, C:\Programs\PartyGaming\PartyPoker\HandHistory, 0, Select the folder <your screenname> in the HandHistory folder:
   If hand_history_folder =
     Return
   play_and_witness = Yes
   Menu, Play && Witness, Check, Play && Witness
  }
Return

EscapeTables:
 If EscapeTables = Yes
  {
   EscapeTables = no
   Menu, Play && Witness, UnCheck, Escape tables
  }
 Else
  {
   EscapeTables = Yes
   Menu, Play && Witness, Check, Escape tables
  }
Return

Donate:
 Run, http://s1.amazon.com/paypage/P2KU51EBLD97YW
Return


;Closes all tables.
;now we also delete the tables we closed from the list (duplicating the behavior of the Check() function)
CloseAll:
 CloseAll()
Return

;Displays all tables.
Tile:
 SetTitleMatchMode, 2
 WinGet, id, List, Good Luck ahk_class #32770, , PartyPoker.com
 o := - 30
 Loop, %id%
  {
   StringTrimRight, this_id, id%A_Index%, 0
   WinActivate, ahk_id%this_id%
  }
 Loop, %id%
  {
   o += 30
   StringTrimRight, this_id, id%A_Index%, 0
   WinMove, ahk_id%this_id%, , o, o
  }
Return

;________________________________________ gui control labels ________________________________________

;
6max:
 game_type = 6max
 IfNotInString, which_game, SNG
  {
   GuiControl, , players_min,  |2|3|4|5|6
   GuiControl, Choose, players_min, % players_min - 1
  }
 Else
   GuiControl, Choose, players_min, |1
 Gosub, prefs
Return

full:
 game_type = full
 IfNotInString, which_game, SNG
  {
   GuiControl, , players_min,  |2|3|4|5|6|7|8|9|10
   GuiControl, Choose, players_min, % players_min - 1
  }
 Else
   GuiControl, Choose, players_min, |1
 Gosub, prefs
Return

Speed:
 game_type = speed
 Gosub, prefs
Return



Add_Rule:
 If A_GuiEvent = DoubleClick
  {
   rules =
   Gui, ListView, ListViewEdit
   Loop, 7
    {
     LV_GetText(rule, 1, A_Index)
     If rule =
      {
       message = Missing Criteria, Your rule is Not complete.`nPlease fill in the missing criteria.
       Message(message)
       Return
      }
     rule%A_Index% = %rule%
    }

   Gui, ListView, ListViewPrefs
   LV_Add("", rule1, rule2, rule3, rule4, rule5, rule6, rule7)
   LV_ModifyCol(1, "Integer Sort")
   Gosub, RecordPrefs
   Gui, ListView, ListViewEdit
   LV_Delete(1)
   Mode = Add
  }
 Else if A_GuiEvent = R
  {
   Gui, ListView, ListViewEdit
   LV_Delete(1)
  }
Return

Edit_Rule:
 If A_GuiEvent = R
  {
   Gui, ListView, ListViewPrefs
   LV_Delete(A_EventInfo)
   Gosub, RecordPrefs
   LV_Delete()
   Gosub, ReadPrefs
   Return
  }
 Else if A_GuiEvent = DoubleClick
  {
   Mode = Edit
   Gui, ListView, ListViewEdit
   LV_Delete(1)
   Gui, ListView, ListViewPrefs
   Loop, 7
    {
     LV_GetText(rule, A_EventInfo, A_Index)
     rule%A_Index% = %rule%
    }
   Gui, ListView, ListViewEdit
   LV_Add("", rule1, rule2, rule3, rule4, rule5, rule6, rule7)
   GuiControl, Choose, which_game, %rule2%
   Gosub, game_selection
   GuiControl, Choose, which_limit, %rule4%
   GuiControl, Choose, players_min, % rule5 - 1
   GuiControl, Choose, tables_tops, % rule6 + 1
   StringTrimLeft, rule7, rule7, 1
   GuiControl, , min_pot, %rule7%
   Gosub, prefs

   Gui, ListView, ListViewPrefs
   LV_Delete(A_EventInfo)
  }
Return

prefs:
 n =
 Gui, Submit, NoHide
 IfInString, which_game, SNG
   GuiControl, Show, gui_speed
 Else
   GuiControl, Hide, gui_speed

 If (which_game = "7 Card Stud" OR which_game = "7 Stud Hi-Lo" OR which_game = "Beginners (Limit)" Or which_game = "Beginners (NL)")
  {
   GuiControl, Hide, gui_typebox
   GuiControl, Hide, gui_typebox2
   game_type = full
  }
 Else
  {
   GuiControl, Show, gui_typebox
   GuiControl, Show, gui_typebox2
   If (InStr(which_game, "SNG") <> 0 And InStr(which_game, "NL Hold'em") = 0)
     GuiControl, Disable, gui_typebox2
   Else
     GuiControl, Enable, gui_typebox2
  }

 If Mode = Add
  {
   Gui, ListView, ListViewPrefs
   n := LV_GetCount() + 1
  }
 Else
  {
   Gui, ListView, ListViewEdit
   LV_GetText(n, 1, 1)
  }
 Gui, ListView, ListViewEdit
 LV_Delete(1)
 If min_pot =
   min_pot = 0
 LV_Add("", n, which_game, game_type, which_limit, players_min, tables_tops, "$" min_pot)
Return

game_selection:
 Gui, Submit, NoHide
 If which_game = Limit Hold'em
   GuiControl, , which_limit, |$100/$200|$50/$100|$30/$60|$20/$40|$15/$30|$10/$20|$5/$10|$3/$6|$2/$4|$1/$2|$0.50/$1
 Else if which_game = No-Limit Hold'em
   GuiControl, , which_limit, |$25/50 NL|$10/20 NL|$5/10 NL|$3/6 NL|$2/4 NL|$1/2 NL|$0.50/1 NL|$0.25/0.50 NL|$0.10/0.25 NL
 Else if (which_game = "Pot-Limit Hold'em" OR which_game = "Pot-Limit Omaha" OR which_game = "Pot-Limit Omaha Hi-Lo")
   GuiControl, , which_limit, |$10/20 PL|$5/10 PL|$3/6 PL|$2/4 PL|$1/2 PL|$0.50/1 PL|$0.25/0.50 PL|$0.10/0.25 PL
 Else if (which_game = "Omaha"  OR which_game = "Omaha Hi-Lo" OR which_game = "7 Card Stud" OR which_game = "7 Stud Hi-Lo")
   GuiControl, , which_limit, |$30/$60|$20/$40|$15/$30|$10/$20|$5/$10|$3/$6|$2/$4|$1/$2|$0.50/$1
 Else if which_game = Beginners (Limit)
   GuiControl, , which_limit, |$0.05/$0.10|$0.10/$0.20|$0.15/$0.30|$0.25/$0.50|$0.50/$1|$1/$2
 Else if which_game = Beginners (NL)
   GuiControl, , which_limit, |$0.02/$0.04|$0.05/$0.10|$0.02/$0.04|$0.10/$0.20|$0.25/$0.50
 Else if (which_game = "Jackpot (High Hand)" OR which_game = "Jackpot (Bad Beat)")
   GuiControl, , which_limit, |$15/$30|$10/$20|$5/$10|$3/$6|$2/$4
 Else if (InStr(which_game, "SNG") <> 0 And InStr(which_game, "Hold'em") <> 0)
   GuiControl, , which_limit, |$530|$215|$109|$77|$55|$33|$22|$11|$6
 Else if (InStr(which_game, "SNG") <> 0)
   GuiControl, , which_limit, |$109|$77|$55|$33|$22|$11|$6
 If ((which_game = "7 Card Stud" OR which_game = "7 Stud Hi-Lo") And InStr(which_game, "SNG") = 0)
   GuiControl, , players_min, |2|3|4|5|6|7|8
 Else if (game_type = "full" And InStr(which_game, "SNG") = 0)
   GuiControl, , players_min,  |2|3|4|5|6|7|8|9|10
 Else if (game_type = "6max" And InStr(which_game, "SNG") = 0)
   GuiControl, , players_min,  |2|3|4|5|6
 Else
  {
   GuiControl, , players_min, |---
   GuiControl, Choose, players_min, |1
  }
 Gosub, prefs
Return

RecordPrefs:
 IniDelete, iWitness.ini, Rules
 Gui, ListView, ListViewPrefs
 criteria = game,6max?,Limit,players,tables_tops,min_pot
 i = 1
 Loop, Parse, criteria, `,
  {
   i++
   crit = %A_LoopField%
   total_rules := LV_GetCount()
   Loop, %total_rules%
    {
     LV_GetText(this_crit, A_Index, i)
     %crit%%A_Index% = %this_crit%
     IniWrite, %this_crit%, iWitness.ini, Rules, %crit%%A_Index%
    }
  }
 IniWrite, %total_rules%, iWitness.ini, Rules, total_rules
Return

;Read the rules from the .ini and display them in the listView.
ReadPrefs:
 IniRead, total_rules, iWitness.ini, Rules, total_rules
 Gui, ListView, ListViewPrefs
 Loop, %total_rules%
  {
   IniRead, game%A_Index%, iWitness.ini, Rules, game%A_Index%
   IniRead, 6max?%A_Index%, iWitness.ini, Rules, 6max?%A_Index%
   IniRead, Limit%A_Index%, iWitness.ini, Rules, Limit%A_Index%
   IniRead, players%A_Index%, iWitness.ini, Rules, players%A_Index%
   IniRead, tables_tops%A_Index%, iWitness.ini, Rules, tables_tops%A_Index%
   IniRead, min_pot%A_Index%, iWitness.ini, Rules, min_pot%A_Index%
  }
 Loop, %total_rules%
  {
   LV_Add("", A_Index, game%A_Index%, 6max?%A_Index%, Limit%A_Index%, players%A_Index%, tables_tops%A_Index%, min_pot%A_Index%)
  }
 LV_ModifyCol(1, "Integer Sort")
Return

;Restore the table highlighted in the TableList.
ShowSelectedTable:
 Gui, ListView, gui_tableList
 LV_GetText(selected_table, A_EventInfo, 2)
 WinRestore, %selected_table%
 WinMove, %selected_table%, , 0, 0
Return

;Go.
GuiGo:
 IniRead, total_rules, iWitness.ini, Rules, total_rules
 If (total_rules = "ERROR" OR total_rules = 0)
  {
   message = You didn't create any rules!`nPlease go to the "Groups" Tab And make some.
   Message(message)
   Return
  }

 TableMode := GetMode()
 If TableMode = Normal
   period = 300000
 Else
   period = 10000

 string = Safe mode: %SafeMode%
 Log(string)

 ;Disable preference buttons.
 GuiControl, Disable, gui_gobutton2
 GuiControl, Disable, which_game
 GuiControl, Disable, which_limit
 GuiControl, Disable, players_min
 GuiControl, Disable, Priority
 GuiControl, Disable, 6max
 GuiControl, Disable, Full
 GuiControl, Disable, ListViewEdit
 GuiControl, Disable, tables_tops
 GuiControl, Disable, min_pot
 ;Enable the stop button.
 GuiControl, Enable, gui_stopbutton2
 RecordClients("no")
 Sleep, 1000
 Gosub, OpenTables
 SetTimer, OpenTables, %period%
Return

GuiStop:
 SetTimer, OpenTables, Off
 GuiControl, Disable, gui_stopbutton2
 GuiControl, Enable, gui_gobutton2
 GuiControl, Enable, which_game
 GuiControl, Enable, which_limit
 GuiControl, Enable, players_min
 GuiControl, Enable, Priority
 GuiControl, Enable, 6max
 GuiControl, Enable, Full
 GuiControl, Enable, ListViewEdit
 GuiControl, Enable, tables_tops
 GuiControl, Enable, min_pot
Return

;________________________________________ exit subroutine ________________________________________

ExitSub:
 WinGetPos, x, y, , , iWitness
 IniWrite, %iWitness_w%, iWitness.ini, Prefs, gui_w
 IniWrite, %iWitness_h%, iWitness.ini, Prefs, gui_h
 IniWrite, %x%, iWitness.ini, Prefs, gui_x
 IniWrite, %y%, iWitness.ini, Prefs, gui_y
 IniWrite, %open_password%, iWitness.ini, Prefs, open_password
 IniWrite, %login_on_first_client%, iWitness.ini, Prefs, LogInOnFirstClient
 IniWrite, %logging%, iWitness.ini, Prefs, Logging
 IniWrite, %auto_start%, iWitness.ini, Prefs, auto_start
 IniWrite, %tooltips%, iWitness.ini, Prefs, tooltips
 IniWrite, %sus_hotkeys%, iWitness.ini, prefs, sus_hotkeys
 IniWrite, %play_and_witness%, iWitness.ini, Prefs, PlayAndWitness
 IniWrite, %hand_history_folder%, iWitness.ini, Prefs, HHFolder
 IniWrite, %SafeMode%, iWitness.ini, Prefs, SafeMode
 IniWrite, %EscapeTables%, iWitness.ini, Prefs, EscapeTables
 IniWrite, %ignore_mg%, iWitness.ini, Prefs, IgnoreMG
 FileDelete, %A_UserName%.%A_ComputerName%.ini

 IniDelete, iWitness.ini, Accounts  ;delte this section and write it back (in case this account wasn't recorded yet)
 Loop, %usernames%
  {
   this_username := username%A_Index%
   IniWrite, %this_username%, iWitness.ini, Accounts, username%A_Index%
  }
 string = `niWitness exits
 Log(string)
 ExitApp
Return

;________________________________________ timers subroutines ________________________________________

OpenTables:

 Gui, ListView, gui_tableList
 If (TableMode = "SNG" And LV_GetCount() = max_tables)
  {
   period = 60000
   string = Opened 10 (4) SNGs, resetting period to 1 min
   Log(string)
   SetTimer, OpenTables, %period%
  }

 string = TableMode: %TableMode%
 Log(string)

 IfWinExist, PartyPoker.com, Your connection has been terminated due to an extended period of inactivity
  {
   WinActivate
   Send, {Enter}        ;____ I think this is more reliable then clicking the button for some reason
   WinWaitClose, 5
   If ErrorLevel <> 0
    {
     string = Couldn't Close disconnect dialog
     Log(string)
     Return
    }
  }
 OpenTables()
 RecordTables()
 MinimizeAll()
 UpdatePlayers()
Return

UpdatePlayers:
 UpdatePlayers()
Return

Tip:
 IfWinNotActive, iWitness
  {
   ToolTip
   Return
  }
 MouseGetPos,,,window, ACtrl
 WinGetTitle, is_iW, ahk_id%window%
 IfNotInString, is_iW, iWitness
   Return
 Tip =

 If ACtrl = SysListView324
   Tip = double Click to Edit a row.`ndouble Right Click to Delete it

 If (ACtrl = "SysListView323" OR ACtrl = "SysHeader323")
   Tip = double Click to Add a new Group`ndouble Right Click to Delete this row

 If ACtrl = SysListView321
   Tip = You may double Click to Restore a table

 If ACtrl <> %LastCtrl%
  {
   ToolTip, %Tip%
   LastCtrl = %ACtrl%
  }
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;hotkeys;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

^P::
 If sus_hotkeys = no
   UpdatePlayers()
Return
^T::
 If sus_hotkeys = no
   Gosub, OpenTables
Return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;Send the lobby to %game_type%
NavigateToGameType(game_type)
 {
  ;Log beginning of journey.
  string = Trying to navigate to %game_type%.
  Log(string)
  global lobby_id
  static game_prev

  ErrorLevel = 0

  If (game_type = game_prev
    OR (game_type = "7 Card Stud" And game_prev = "7 Stud Hi-Lo")
    OR (game_type = "7 Stud Hi-Lo" And game_prev = "7 Card Stud")
    OR (game_type = "Omaha" And game_prev = "Omaha Hi-Lo")
    OR (game_type = "Omaha Hi-Lo" And game_prev = "Omaha")
    OR (game_type = "Pot-Limit Omaha" And game_prev = "Pot-Limit Omaha Hi-Lo")
    OR (game_type = "Pot-Limit Omaha Hi-Lo" And game_prev = "Pot-Limit Omaha")
    OR (game_type = "Beginners (Limit)" And game_prev = "Beginners (NL)")
    OR (game_type = "Beginners (NL)" And game_prev = "Beginners (Limit)")
    OR (game_type = "Jackpot (Bad Beat)" And game_prev = "Jackpot (High Hand)")
    OR (game_type = "Jackpot (High Hand)" And game_prev = "Jackpot (Bad Beat)"))
   {
    Success = Yes
    string = Already at game_type
    Log(string)
    Return Success
   }

  IfInString, game_type, SNG
    KeysToSend = S
  Else if (game_type = "Limit Hold'em")
    KeysToSend =
  Else
    StringLeft, KeysToSend, game_type, 1

  WinWait, ahk_id%lobby_id%, , 5
  WinActivate
  ControlFocus, SysTreeView321
  Success = no
  Loop, 5
   {
    ControlSend, SysTreeView321, Cash
    Loop, 5
     {
      ControlGet, is_cash, List, , SysListView321
      IfInString, is_cash, $100/$200
       {
        Continue = no
        Break
       }
      Sleep, 1000
     }
    If Continue <> no
      Continue

    Sleep, 2000
    ControlSend, SysTreeView321, %KeysToSend%
    Sleep, 1000
    IfInString, game_type, Pot-Limit Omaha
      ControlSend, SysTreeView321, %KeysToSend%
    Loop, 5
     {
      ControlGet, contains_data, List, , SysListView321
      IfInString, contains_data, $
       {
        Success = Yes
        Break = Yes
        Break
       }
      Sleep, 1000
     }
    If Break = Yes
      Break
   }
  If Success <> Yes
    TrayTip, Error, Unable to navigate to %game_type%, , 3
  is_cash =
  contains_data =
  game_prev = %game_type%
  ;Log that we successfully navigated.
  If Success = Yes
    string = Navigated to %game_type%
  Else
    string = Couldn't navigate to game_type
  Log(string)

  Return Success
 }


OpenTables()
 {
  global max_tables, this_client, lobby_id, TableMode, period, SafeMode, EscapeTables, ignore_mg
  ;Open Party if necessary.
  OpenParty()
  string = OpenTables started.
  Log(string)
  ;Read all the rules from the .ini.
  IniRead, total_rules, iWitness.ini, Rules, total_rules
  Loop, %total_rules%
   {
    IniRead, game%A_Index%, iWitness.ini, Rules, game%A_Index%
    IniRead, 6max?%A_Index%, iWitness.ini, Rules, 6max?%A_Index%
    IniRead, Limit%A_Index%, iWitness.ini, Rules, Limit%A_Index%
    IniRead, players%A_Index%, iWitness.ini, Rules, players%A_Index%
    IniRead, tables_tops%A_Index%, iWitness.ini, Rules, tables_tops%A_Index%
    IniRead, min_pot%A_Index%, iWitness.ini, Rules, min_pot%A_Index%
   }
  ;Log number of rules.
  string = -%total_rules% total rules.
  Log(string)

  tables_to_escape := Check()

  ;IniRead, num_tables, %A_UserName%.%A_ComputerName%.ini, Tables, NumTables, 0

  actual_tables_open := ActualTablesOpen()
  tables_to_open := max_tables - actual_tables_open
  string = tables_to_open = %tables_to_open%
  Log(string)

  string = actually_open: %actual_tables_open%
  Log(string)


  If tables_to_open = 0
    Return



  Gui, ListView, gui_tableList
  Loop, % LV_GetCount()
   {
    LV_GetText(table, A_Index, 2)
    match_list = %match_list%,%table%
   }
  StringTrimLeft, match_list, match_list, 1



  Loop, %total_rules%
   {
    i++
    table_list =

    tables_in_group = 0
    IniRead, tables_tops, iWitness.ini, Rules, tables_tops%i%
    If tables_tops is Not Number
      tables_tops = 1000
    Gui, ListView, gui_tableList
    Loop, % LV_GetCount()
     {
      LV_GetText(p, A_Index, 1)
      p = %p%
      If p = %i%
        tables_in_group++
     }
    tables_tops -= tables_in_group
    If tables_tops = 0
      Continue




    If (NavigateToGameType(game%i%) <> "yes")
      Continue


    If (6max?%i% = "6max" And InStr(game%i%, "SNG") = 0)
      6max%i% = /6
    Else if If (6max?%i% = "6max" And InStr(game%i%, "SNG") <> 0)
      6max%i% =
    Else if 6max?%i% = speed
      6max%i% = Speed
    Else
     {
      If (game%i% = "7 Card Stud" OR game%i% = "7 Stud Hi-Lo")
        6max%i% = /8
      Else if (InStr(game%i%, "SNG") <> 0)
        6max%i% =
      Else
        6max%i% = /10
     }
    If (6max?%i% = "6max" And InStr(game%i%, "SNG (NL Hold'em") <> 0)
      6max%i% = /6
    If (6max?%i% = "full" And InStr(game%i%, "SNG (NL Hold'em") <> 0)
      6max%i% = /10

    If (game%i% = "Limit Hold'em" OR game%i% = "No-Limit Hold'em" OR game%i% = "Pot-Limit Hold'em")
      g%i% =
    Else if (game%i% = "Pot-Limit Omaha" OR game%i% = "Pot-Limit Omaha Hi-Lo")
      StringReplace, g%i%, game%i%, Pot-Limit, PL
    Else if game%i% = Beginners (Limit)
      g%i% = Hold'em
    Else if game%i% = Beginners (NL)
      g%i% = NL Hold'em
    Else if game%i% = Jackpot (Bad Beat)
      g%i% = Bad Beat Jackpot
    Else if game%i% = Jackpot (High Hand)
      g%i% = High Hand Jackpot
    Else if (game%i% = "SNG (Limit Hold'em)")
      g%i% = Hold'em
    Else IfInString, game%i%, SNG
      StringMid, g%i%, game%i%, InStr(game%i%, "(") + 1, InStr(game%i%, ")") - InStr(game%i%, "(") - 1
    Else
     {
      g%i% := game%i%
     }

    StringTrimLeft, min_pot%i%, min_pot%i%, 1


    If (InStr(Limit%i%, "NL") <> 0 OR InStr(Limit%i%, "PL") <> 0)
     {
      StringTrimRight, l%i%, Limit%i%, 3
      StringReplace, l%i%, l%i%, /, /$
     }
    Else
      l%i% := Limit%i%

    WinWait, ahk_id%lobby_id%
    WinActivate
    ControlClick, SysListView321
    ControlGet, info, List, , SysListView321
    Loop, Parse, info, `n
     {
      If tables_tops = 0
        Break

;______ people having problems with Hi-Lo games, please replace this section
;##################################################################

      If (InStr(A_LoopField, l%i%) = 0 OR InStr(A_LoopField, 6max%i%) = 0 OR (InStr(A_LoopField, g%i%) = 0 
      AND g%i% <> "Omaha Hi-Lo" AND g%i% <> "7 Stud Hi-Lo") OR (g%i% = "Omaha" And InStr(A_LoopField, "Hi-Lo")) 
      OR (g%i% = "PL Omaha" And InStr(A_LoopField, "Hi-Lo")))
      Continue
      If ((g%i% = "Omaha Hi-Lo" OR g%i% = "7 Stud Hi-Lo") AND InStr(a_loopfield, "Hi-Lo") = 0)
                                continue
;##################################################################

      If (InStr(A_LoopField, "Beginners") And InStr(A_LoopField, "NL Hold'em") And g%i% = "Hold'em")
        Continue
      StringMid, num_players, A_LoopField, InStr(A_LoopField, "/", True, 0) - 1, 2, L
      num_players = %num_players%
      If (num_players < players%i% And InStr(game%i%, "SNG") = 0)
        Continue
      If (InStr(game%i%, "SNG") <> 0 And InStr(game%i%, "SNG (NL Hold'em") = 0 And InStr(A_LoopField, "Level 1") = 0)
        Continue
      If (InStr(game%i%, "SNG (NL Hold'em") <> 0 And InStr(A_LoopField, "Registering") = 0)
        Continue
      If (InStr(game%i%, "SNG (NL Hold'em") <> 0 And 6max?%i% = "full" And InStr(A_LoopField, "Speed") <> 0)
        Continue

      StringRight, pot, A_LoopField, StrLen(A_LoopField) - InStr(A_LoopField, "$", True, 0)

      If (pot < min_pot%i%)
        Continue

      StringLeft, table_name, A_LoopField, InStr(A_LoopField, A_Tab) - 1
      IfInString, table_name, $
        StringLeft, table_name, table_name, InStr(table_name, "$") - 1
      IfInString, table_name, (
        StringLeft, table_name, table_name, InStr(table_name, "(") - 1

      table_name = %table_name%

      IfWinExist, %table_name%
       {
        WinWait, ahk_id%lobby_id%
        Continue
       }
      If table_name in %match_list%
        Continue

      If EscapeTables = Yes
       {
        If table_name in %tables_to_escape%
          Continue
       }

      table_to_close =
      If tables_opened >= %tables_to_open%
       {
        Break = no
        table_to_close =
        Loop, %total_rules%
         {
          last_rule := total_rules - A_Index + 1

          If (last_rule <= i)
            Break

          Gui, ListView, gui_tableList
          Loop, % LV_GetCount()
           {
            LV_GetText(which_rule, A_Index, 1)
            If which_rule = %last_rule%
             {
              LV_GetText(is_our_client, A_Index, 6)
              If is_our_client = %this_client%
               {
                LV_GetText(table_to_close, A_Index, 2)
                If (CheckTable(table_to_close) <> "play")
                 {
                  LV_Delete(A_Index)
                  Break = Yes
                  Break
                 }
               }
             }
           }
          If Break = Yes
            Break
         }


        If table_to_close <>
         {
          WinClose, %table_to_close%
          WinWaitClose, %table_to_close%, , 3n
         }
        Else
         {
          string = OpenTables finished. (couldn't find a table to Close)
          Log(string)
          Return
         }
       }


      WinWait, ahk_id%lobby_id%
      ControlFocus, SysListView321

      Loop, 5
       {
        If SafeMode = Yes
          SendRaw, %table_name%
        Else
          ControlSendRaw, SysListView321, %table_name%

        string = Sent table_name (%table_name%) - (%A_Index%. try)
        Log(string)

        ControlGet, is_target, List, Selected, SysListView321

        string = Checking target (%is_target%)
        Log(string)

        IfNotInString, is_target, %table_name%
         {
          string = ########################Target didn't match table_name
          Log(string)

          If SafeMode = Yes
            SendMessage, 0x115, 1, 0, SysListView321

          ControlFocus, SysListView321
          ControlClick, SysListView321

          If A_Index > 4
           {
            Continue = Yes
            Break
           }
         }
        Else
          Break
       }

      If Continue = Yes
       {
        Continue
       }

      ControlFocus, SysListView321
      ControlSend, SysListView321, {Enter}

      WinWait, %table_name%, , 10

      If ErrorLevel = 0
       {
        string = table succesfully opened
        Log(string)

        tables_opened++
        tables_tops--
        Gui, ListView, gui_tableList
        ;changed it to put the actual number of players in the list - possibly better for play&witness
        LV_Add("", i, table_name, game%i%, Limit%i%, num_players, this_client)
        match_list = %match_list%,%table_name%
       }
      Else
       {

        string = Table could Not be opened
        Log(string)
        IfWinExist, PartyPoker.com, You already have    ;_______QUICK HACK
         {
          string = Error - Already opened 10 (4) tables
          Log(string)
          WinClose
          Return
         }
        IfWinExist, PartyPoker.com, The table you wish to Join is currently Not available
         {
          string = Table Not available
          Log(string)
          WinActivate
          Send, {Enter}
         }
        IfWinExist, PartyPoker.com, We're sorry, but we cannot take you to the table you requested
         {
          string = Table Not available
          Log(string)
          WinActivate
          Send {Enter}
         }
       }
      ;WinRestore, ahk_id%lobby_id%
      WinActivate, ahk_id%lobby_id%
      WinWaitActive, ahk_id%lobby_id%, , 2
      If SafeMode = Yes
        Sleep, 1000
     }
   }
  string = OpenTables finished.
  Log(string)
  Return
 }

;___________________________________________________________________________________________________

;Check all tables, both here and networked. If here, ensure that they meet criteria.
Check(clear_tables_to_escape = "")
 {
  global lobby_id
  static tables_to_escape

  If clear_tables_to_escape = clear_tables_to_escape
   {
    tables_to_escape =
    Return
   }


  ;Read all the rules from the .ini into memory.
  IniRead, total_rules, iWitness.ini, Rules, total_rules
  Loop, %total_rules%
   {
    IniRead, game%A_Index%, iWitness.ini, Rules, game%A_Index%
    IniRead, 6max?%A_Index%, iWitness.ini, Rules, 6max?%A_Index%
    IniRead, Limit%A_Index%, iWitness.ini, Rules, Limit%A_Index%
    IniRead, players%A_Index%, iWitness.ini, Rules, players%A_Index%
    IniRead, tables_tops%A_Index%, iWitness.ini, Rules, tables_tops%A_Index%
    IniRead, min_pot%A_Index%, iWitness.ini, Rules, min_pot%A_Index%
   }
  ;Findout how many tables we'll have to check.
  Loop, 10
   {
    ;Read from the title from the ini.
    IniRead, table_to_check%A_Index%, %A_UserName%.%A_ComputerName%.ini, Tables, table%A_Index%, Blank
    ;If it's blank, it's not a legit table.
    If table_to_check%A_Index% = Blank
      Break
    ;Otherwise, inc the number of tables.
    tables_to_check++
   }

  Loop, %total_rules%
   {
    If min_pot%A_Index% <> $0
     {
      check_pot = Yes
      Break
     }
   }


  ;Go through all the tables to be checked
  Loop, %tables_to_check%
   {
    ;Grab the table name
    StringReplace, table_name, table_to_check%A_Index%, (
    StringReplace, table_name, table_name, )
    StringTrimRight, table_name, table_name, 1
    table_name = %table_name%
    ;If the window exists
    IfWinExist, %table_name%
     {

      If check_pot = Yes
       {
        WinActivate, ahk_id%lobby_id%
        WinWaitActive, ahk_id%lobby_id%, , 3
        Sleep, 500
        IniRead, p, %A_UserName%.%A_ComputerName%.ini, Tables, table%A_Index%
        StringMid, p, p, InStr(p, "(") + 1, InStr(p, ")") - 1 -  InStr(p, "(")
        IniRead, game%p%, iWitness.ini, Rules, game%p%

        NavigateToGameType(game%p%)

        ControlGet, info, List, , SysListView321, ahk_id%lobby_id%
        IniRead, min_pot, iWitness.ini, Rules, min_pot%p%
        StringTrimLeft, min_pot, min_pot, 1
        StringTrimLeft, info, info, InStr(info, table_name) - 1
        StringLeft, info, info, InStr(info, "`n")
        StringTrimLeft, pot, info, InStr(info, "$", True, 0)
        info =
        StringReplace, pot, pot, `n, , All
       }



      WinGetTitle, check_for_SNG
      IfNotInString, check_for_SNG, Buy-in.
       {
        i = 3
        seats = 0
        ;Go through all the "Seat Open" buttons
        Loop, 10
         {
          i++
          ;If they're visible, this is an empty seat.
          ControlGet, is_visible, Visible, , AfxWnd42u%i%, %table_name%
          If is_visible = 0
            seats++
         }
       }
      Else
       {
        ControlGetText, is_finished, RICHEDIT1
        IfInString, is_finished, This tournament has finished
          seats = finished
        Else
         {
          ControlGetText, level, static5
          StringMid, seats, level, InStr(level, "Level:") + 6, 1
          If seats =
            seats = Registering
          Else
            seats = L%seats%
         }

       }
     }
    Else
     {
      tables_to_escape = %table_name%,%tables_to_escape%
      Continue
     }


    ;Read the name from the networking .ini.
    IniRead, p, %A_UserName%.%A_ComputerName%.ini, Tables, table%A_Index%
    StringMid, p, p, InStr(p, "(") + 1, InStr(p, ")") - 1 -  InStr(p, "(")
    ;Stud is special somehow. Who cares? Stud is a dumb game.
    If (game%p% = "7 Card Stud" OR game%p% = "7 Stud Hi-Lo")
      seats := seats - 2
    Else if (6max?%p% = "6max")
      seats := seats - 4
    ;If we have too few players
    If (((InStr(game%p%, "SNG") = 0 And seats < players%p%) OR (InStr(game%p%, "SNG") <> 0 And seats = "finished") OR (pot < min_pot)) And CheckTable(table_name) <> "play")
     {
      ;Close the table
      WinClose, %table_name%
      WinWaitClose, %table_name%, , 3
      If ErrorLevel <> 0
       {
        WinWaitClose, %table_name%, , 5
        ;____I SUSPECT THE PROBLEMS GOODGUY IS HAVING MIGHT BE DUE TO the table not closing; quick hack
        If ErrorLevel <> 0
          table_list = %table_list%,%table_name%(%p%)
       }
     }
    ;Otherwise add it to the table list.
    Else
      table_list = %table_list%,%table_name%(%p%)
   }
  StringTrimLeft, table_list, table_list, 1
  ;Remove the tables from the networking.ini
  IniDelete, %A_UserName%.%A_ComputerName%.ini, Tables
  ;Go through our table_list and write them all in the networking.ini.
  num_tables = 0
  Loop, Parse, table_list, `,
   {
    IniWrite, %A_LoopField%, %A_UserName%.%A_ComputerName%.ini, Tables, table%A_Index%
    num_tables++
   }
  ;Record the total number of tables.
  IniWrite, %num_tables%, %A_UserName%.%A_ComputerName%.ini, Tables, NumTables
  ;Empty both the TableList gui and the ClientList gui.
  Gui, ListView, gui_TableList
  LV_Delete()
  Gui, ListView, gui_clientList
  LV_Delete()
  ;Go through all the possible other clients' networking.ini in the folder
  Loop, *.ini
   {
    ;iWitness.ini doesn't count.
    If A_LoopFileName = iWitness.ini
      Continue
    ;Read the data on that client
    IniRead, which_client, %A_WorkingDir%\%A_LoopFileName%, Client, which_client
    IniRead, num_tables, %A_WorkingDir%\%A_LoopFileName%, Tables, NumTables, 0
    StringLeft, username, A_LoopFileName, InStr(A_LoopFileName, ".") - 1
    StringTrimLeft, computername, A_LoopFileName, InStr(A_LoopFileName, ".")
    StringTrimRight, computername, computername, 4
    ;And add them to the clientList.
    Gui, ListView, gui_clientList
    LV_Add("", which_client, username, computername, num_tables)
    client_matchlist =%client_matchlist%,%which_client%
    total_clients++
    ;Go through their tables
    Loop, %num_tables%
     {
      ;Grab the name
      IniRead, table_n, %A_LoopFileName%, Tables, table%A_Index%
      StringLeft, this_table, table_n, InStr(table_n, "(") - 1
      StringMid, rule, table_n, InStr(table_n, "(") + 1, InStr(table_n, ")") - InStr(table_n, "(") - 1
      ;And add it to the tableList.
      Gui, ListView, gui_tableList
      LV_Add("", rule, this_table, game%rule%, Limit%rule%, players%rule%, which_client)
     }
   }
  ;Go through this total list of tables
  Gui, ListView, gui_TableList
  Loop % LV_GetCount()
   {
    ;And add them together to form the math_list.
    LV_GetText(table, A_Index, 2)
    match_list = %match_list%,%table%
   }
  StringTrimLeft, match_list, match_list, 1
  Return tables_to_escape
 }

;_________________________________________________________________________________________________

;Minimizes all Party tables.
MinimizeAll()
 {
  global period, lobby_id

  IfWinExist, PartyPoker.com, Your connection has been terminated due to an extended period of inactivity
   {
    WinActivate
    Send, {Enter}       ;____ I think this is more reliable then clicking the button for some reason
    WinWaitClose, 5
    If ErrorLevel <> 0
     {
      string = Couldn't Close disconnect dialog
      Log(string)
      Return
     }
   }

  IfWinExist, PartyPoker.com, The table you wish to Join is currently Not available
   {
    string = Table Not available
    Log(string)
    WinActivate
    Send, {Enter}
   }
  IfWinExist, PartyPoker.com, We're sorry, but we cannot take you to the table you requested
   {
    string = Table Not available
    Log(string)
    WinActivate
    Send {Enter}
   }



  SetTitleMatchMode, 2
  If period = 10000
    WinGet, id, List, Good Luck ahk_class #32770, , PartyPoker.com:
  Else
   {
    WinGet, id, List, Good Luck ahk_class #32770
    WinMinimize, ahk_id%lobby_id%       ;_____ I had "PartyPoker.com" there - was this minimizing the popups maybe?
   }
  Loop, %id%
   {
    StringTrimRight, this_id, id%A_Index%, 0
    WinGetTitle, title, ahk_id%this_id%
    StringLeft, title, title, InStr(title, "-") - 1
    If (CheckTable(title) <> "play")
      WinMinimize, ahk_id%this_id%
   }
  Return
 }

;_________________________________________________________________________________________________
;close all the tables and delete them from the list
CloseAll()
 {
  global this_client

  SetTitleMatchMode, 2
  WinGet, id, List, Good Luck ahk_class #32770, , PartyPoker.com
  Loop, %id%
   {
    StringTrimRight, this_id, id%A_Index%, 0
    WinClose, ahk_id%this_id%
   }
  ;Record the total number of tables.
  numTables = 0
  IniWrite, %num_tables%, %A_UserName%.%A_ComputerName%.ini, Tables, NumTables
  ;Empty both the TableList gui and the ClientList gui.
  Gui, ListView, gui_TableList
  LV_Delete()
  Gui, ListView, gui_clientList
  LV_Delete()
  ;Go through all the possible other clients' networking.ini in the folder
  Loop, *.ini
   {
    ;iWitness.ini doesn't count.
    If A_LoopFileName = iWitness.ini
      Continue
    ;Read the data on that client
    IniRead, which_client, %A_WorkingDir%\%A_LoopFileName%, Client, which_client
    IniRead, num_tables, %A_WorkingDir%\%A_LoopFileName%, Tables, NumTables, 0
    StringLeft, username, A_LoopFileName, InStr(A_LoopFileName, ".") - 1
    StringTrimLeft, computername, A_LoopFileName, InStr(A_LoopFileName, ".")
    StringTrimRight, computername, computername, 4
    ;And add them to the clientList.
    Gui, ListView, gui_clientList
    LV_Add("", which_client, username, computername, num_tables)
    client_matchlist =%client_matchlist%,%which