Safe-Mine v0.05b

This script will open upto 'NumTablesRequired' in each user area (or Pc) that it is run on. To use it, then do these steps in each user area:

a) Navigate to your selected section in the Party lobby.

b) Select your desired filters for the type of games you wish to mine in the Party Lobby.

c) Start running Safe-Mine.

NOTE: To change the number of tables you wish to open in each user area, simply edit the line which says NumTablesRequired and set this to the value you desire.

NOTE: If safemine locks up or stops working after you have closed one of your SafeMine instances, you may need to delete any ".lock" files, the "table_list" file, and "semaphore" file and restart all your SafeMine instances (this shouldn't happen very often, but can happen if you don't close SafeMine properly or if your PC crashes).

Credits

Written by JukOfYork, but largely based on Roland's original iWitness V1 code. Juk's site

Here's the original 2+2 thread.

Code available as SafeMine_0_05b.ahk

Alternately, copy the code below:

; Safe-Mine v0.05b
; ================
; This script will open upto 'NumTablesRequired' in each user area (or Pc) that it is run on.
; To use it, then do these steps in each user area:
; a) Navigate to your selected section in the Party lobby.
; b) Select your desired filters for the type of games you wish to mine in the Party Lobby.
; c) Start running Safe-Mine.
; NOTE: Before each group of runs, you may need to delete any old "table_list" and "semaphore" files, BUT
;       DO NOT delete them unless you are starting a new group of runs (ie: don't delete them for each copy of
;       Safe-Mine you run - only when you *FIRST* start running SNG mine in your different areas).

; v0.02: - Have changed the OpenTables timer to be called at just 30 seconds intervals.
;        - Have made the window minimizatoion get done in another thread.
;        - Have added a KillSpamWindows thread.

; v0.03: - Have fixed KillSpamWindows to use the LastFoundWindow.
;        - Have added a lock to each of the copies we are using, and an exit event trap to delete these locks.
;          When the last lock is deleted, we want to also delete the table_list and the Semaphore.

; v0.04: - Have made it call UpdateTable much more frequently, to try to reduce the
;          chance of opening a table in both user areas at the same time.
;        - We now deal with the table list properly if the exit event is triggered.
;          Before, it was possible to exit leaving the semaphore active and the table list
;          invalid...
;        - Have fixed the problem that was causing SNGs to not be closed.

; v0.05: - Have increased UpdateTable call frequency to 15s again (too much CPU used).
;        - Have tried to fix KillSpamWindows to work better (was locking up and failing to work before).
;          (possibly using LastFoundWindow. was causing this? / Also added criticals round the calls).
;        - Have made it so that MinimizeTables now also resets the postion of the lobby to fix the "tiny-lobby" bug.
;        - Have removed the ability top start the old version of FPHG as it may cause confusion with FPHG v2.
;        - Have made the script run at "AboveNormal" priority now.

; v0.05b:- Have added a code the spam-closer thread to close the "Due to an extended period of inactivity" window.

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

; This is the number of tables you wish to mine in each of your user areas (should be set to 4, as not logged in).
; NOTE: If you are logged in in your main user area, then you can make another copy of Safe-Mine with this set to
;       12 and then run this copy for your main user area, and the normal (4 tabling) copy in all the other areas.
NumTablesRequired = 12

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

#Persistent
SetTitleMatchMode, 2
WinGet, lobby_id, ID, PartyPoker.com

;Get this script's PID and set our own priority (thanks to _dave_ for this!).
pid := DllCall("GetCurrentProcessId")
Process, Priority, %pid%, AboveNormal

WeHaveSemaphore = 0 ; We don't have it yet.

; Lets create our lock file, for the OnExit trap to see.
FileDelete, %A_UserName%.%A_ComputerName%.lock
FileAppend, %A_UserName%.%A_ComputerName% is locked, %A_UserName%.%A_ComputerName%.lock

; Lets add a OnExit event trap.
OnExit, ExitSub

; Lets start the spam-busting thread (every 5 seconds).
GoSub, KillSpamWindows
SetTimer, KillSpamWindows, 5000

; Lets start the minimizing windows (every 5 seconds).
GoSub, MinimizeTables
SetTimer, MinimizeTables, 5000

; Lets start opening tables (30 seconds delay seems ok?).
GoSub, OpenTables
SetTimer, OpenTables, 15000

return

;====================================================================================================================
; ----------------------------------------- end of auto-execute section --------------------------------------------
;====================================================================================================================

OpenTables:

  ;_____make a list of all the party tables, excluding Lobby
  WinGet, id, List, Good Luck, , PartyPoker.com

  ; Lets see if we want to close any tables.
  Loop, %id%
  {
    StringTrimRight, Window, id%A_Index%, 0

    ; See if this window is open in another user area, if so lets close it.
    WinGetTitle, String, ahk_id%Window%
    StringSplit, StringArray, String, -
    StringTrimRight, StringArray1, StringArray1, 1
    if IsTableOpenInAnotherArea(StringArray1) = 1
    {
      WinClose, ahk_id%Window%
      WinWaitClose, ahk_id%Window%, , 3
      GoSub, UpdateTableNames ; Update the table names
    }

    ; Lets see if the SNG is over.
    ControlGetText, is_finished, RichEdit20W1, ahk_id%Window%
    IfInString, is_finished, This tournament has finished
    {
      WinClose, ahk_id%Window%
      WinWaitClose, ahk_id%Window%, , 3
      GoSub, UpdateTableNames ; Update the table names
   }

  }

  ; Update the table names, just incase Party closed any itself.
  GoSub, UpdateTableNames

  ; If we have >= 10 tables open allready, lets not bother.
  WinGet, NumTables, List, Good Luck, , PartyPoker.com
  If (NumTables >= NumTablesRequired)
    return

  ; Set the focus to the topmost row.
  ControlFocus, SysListView321, ahk_id%lobby_id% ;set focus to the ListView
  ControlGet, count, List, Count, SysListView321, ahk_id%lobby_id% ;____used below to break if we reach the end of the list before we find 10 tables
  ControlGet, focused, List, Count Focused, SysListView321, ahk_id%lobby_id% ;____we want to select the topmost row, 

  ; Send %focused% amount of UP keystrokes to get to the top of the list.
  Loop, %focused%
    ControlSend, SysListView321, {UP}, ahk_id%lobby_id%
  
  ; Open the tables.
  Loop
  {

    ; Save the index.
    ListIndex = %a_index%

    ControlGet, Row, List, Focused, SysListView321, ahk_id%lobby_id%
    Loop, Parse, Row, %A_Tab%   ; Fields (columns) in each row are delimited by tabs (A_Tab).
    {
      if A_Index = 1 ; 1 for all EXCEPT SNGS, which is 3.
      {
        IfWinNotExist %A_LoopField%
        {
          if IsTableOpenInAnotherArea(A_LoopField) = 0
          {
            ControlSend, SysListView321, {Enter}, ahk_id%lobby_id%
            If ErrorLevel = 0
            {
              NumTables++
              GoSub, UpdateTableNames ; Update the table names
            }
          }
        }
      }
    }

    ControlSend, SysListView321, {DOWN}, ahk_id%lobby_id% ;_____move to the next table

    If (NumTables >= NumTablesRequired OR ListIndex > count) ;____if we reach 10 tables or the end of the list, break ; 
      break
  }

  ; Update the table names, just incase any opened after a lag time.
  GoSub, UpdateTableNames

  return

; -------------------------------------------------------------------------------------------------------

UpdateTableNames:

  ; We don't want the other copies interfering while we do this...
  EnterCritcalSection()

  ; Read in everythig that was in the old table_list
  FileRead, OldList, table_list

  ; Lets move the olf table list (if it exists).
  FileMove, table_list, table_list_backup, 1

  ; Save all the lines which are not our's back to the file.
  Loop, Parse, OldList, `r`n  ; Rows are delimited by linefeeds (`n).
  {
    if StrLen(A_LoopField)>0
    {
      IfNotInString, A_LoopField, %A_UserName%.%A_ComputerName%
        FileAppend , %A_LoopField%`r`n, table_list
    }
  }

  WinGet, id, List, Good Luck, , PartyPoker.com
  Loop, %id%
  {
    StringTrimRight, Window, id%A_Index%, 0
    WinGetTitle, String, ahk_id%Window%
    StringSplit, StringArray, String, -
    StringTrimRight, StringArray1, StringArray1, 1
    FileAppend , %A_UserName%.%A_ComputerName%:%StringArray1%`r`n, table_list

  }

  ; Delete the old table list we backed up.
  FileDelete table_list_backup

  ; Other copies can run now if they need to.
  LeaveCriticalSection()

  return

; -------------------------------------------------------------------------------------------------------

IsTableOpenInAnotherArea(TableName)
{
  ; We don't want the other copies interfering while we do this...
  EnterCritcalSection()

  ; Read the current list of used tables.
  FileRead, CurrentTableList, table_list

  Loop, Parse, CurrentTableList, `r`n  ; Rows are delimited by linefeeds (`n).
  {
    if StrLen(A_LoopField)>0
    {
      IfNotInString, A_LoopField, %A_UserName%.%A_ComputerName%
      {
        IfInString, A_LoopField, %TableName%
        {

          ; Other copies can run now if they need to.
          LeaveCriticalSection()

          return 1 ; Found it.
        }
      }
    }
  }

  ; Other copies can run now if they need to.
  LeaveCriticalSection()

  return 0 ; Not found.
}

; -------------------------------------------------------------------------------------------------------

EnterCritcalSection()
{
  Loop
  {
    IfNotExist, semaphore
      break
    Sleep, 1000 ; Lets wait and try again then.
  }
  WeHaveSemaphore = 1 ; We have it now.
  FileAppend, semaphore in use, semaphore
  return
}

; -------------------------------------------------------------------------------------------------------

LeaveCriticalSection()
{
  ; Release the semaphore.
  FileDelete, semaphore
  WeHaveSemaphore = 0 ; We don't have it now.
  return
}

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

; Minimized any party tables it can see.
MinimizeTables:

  ; Restore party loby window.
  WinRestore, PartyPoker.com:

  ; Reset the Party lobby to top left and correct dimentions.
  WinMove, PartyPoker.com:, , 0, 0, 786, 566

  ;_____make a list of all the party tables, excluding Lobby
  WinGet, id, List, Good Luck, , PartyPoker.com

  ; Lets see if we want to minimize or close any tables.
  Loop, %id%
  {
    StringTrimRight, Window, id%A_Index%, 0

    ; Lets make sure its minimized.
    WinMinimize, ahk_id%Window%

  }

  return

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

; This is used to kill any spam windows that might be causing us problems.
KillSpamWindows:

 ; We want to close the spam windows safely...
 ; ### This will also help with debugging, as if thread locks all of SafeMine will lock ###
 Critical On

 ; Loop until all are closed.
 Loop
 {

   ; Lets assume no window has been closed yet.
   WindoHasBeenClosed = 0

   IfWinExist, PartyPoker.com, You already have
   {
     WinClose, PartyPoker.com, You already have
     WindoHasBeenClosed = 1 ; We closed one.
   }

   IfWinExist, PartyPoker.com, Due to an extended period of inactivity
   {
     WinClose, PartyPoker.com, Due to an extended period of inactivity
     WindoHasBeenClosed = 1 ; We closed one.
   }

   IfWinExist, PartyPoker.com, Your connection has been terminated due to an extended period of inactivity
   {
     WinClose, PartyPoker.com, Your connection has been terminated due to an extended period of inactivity
     WindoHasBeenClosed = 1 ; We closed one.
   }

   IfWinExist, PartyPoker.com, The table you wish to join is currently not available
   {
     WinClose, PartyPoker.com, The table you wish to join is currently not available
     WindoHasBeenClosed = 1 ; We closed one.
   }

   IfWinExist, PartyPoker.com, We're sorry, but we cannot take you to the table you requested
   {
     WinClose, PartyPoker.com, We're sorry, but we cannot take you to the table you requested
     WindoHasBeenClosed = 1 ; We closed one.
   }

   IfWinExist, Message from PartyPoker.com
   {
     WinClose, Message from PartyPoker.com
     WindoHasBeenClosed = 1 ; We closed one.
   }

   ; We can only exit when se see no more spam.
   if WindoHasBeenClosed = 0
     break

 }

 Critical Off

return

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

; This is used to clean up after we leave.
; 1. Return table_list to a state without our tables in it.
; 2. Remove the table list and the semaphore if we are the last to exit.
ExitSub:

  ; Not sure if other threads can still run in here, but to be safe...
  Critical On

  ; If we have the Semaphore, then we could be in the middle of a table_list write...
  if WeHaveSemaphore = 1
  {
    IfExist, table_list_backup
      FileMove, table_list_backup, table_list, 1
  }

  ; We want to set the Semaphore then.
  else
    EnterCritcalSection()

  ; Read in everythig that was in the old table_list
  FileRead, OldList, table_list

  ; Delete the old copy of table_list (if it exists).
  FileDelete, table_list

  ; Save all the lines which are not our's back to the file.
  Loop, Parse, OldList, `r`n  ; Rows are delimited by linefeeds (`n).
  {
    if StrLen(A_LoopField)>0
    {
      IfNotInString, A_LoopField, %A_UserName%.%A_ComputerName%
        FileAppend , %A_LoopField%`r`n, table_list
    }
  }

  ; Lets delete our own lock.
  FileDelete, %A_UserName%.%A_ComputerName%.lock

  ; If we can't see any other locks left, lets delete the table list.
  IfNotExist, *.lock
    FileDelete, table_list

  ; We have finished messing now.
  LeaveCriticalSection()

  ExitApp  ; The only way for an OnExit script to terminate itself is to use ExitApp in the OnExit subroutine.

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

CategoryAutoHotKey

SafeMine (last edited 2007-12-03 01:43:49 by MogobuTheFool)