﻿Type=StaticCode
Version=7.01
ModulesStructureVersion=1
B4A=true
@EndOfDesignText@
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.
	Type    TReplay(RandomList As List)
	
    Public  IsInitialized    As Boolean = False

	
	Private mRandomList      As List
    Private mLastRandom      As Int
		
	Private mDoingReplay     As Boolean
	
	
	Private mReplay          As TReplay
End Sub

#Region Initialize
Public  Sub Initialize

	    	IsInitialized  = True


			mReplay.Initialize
			mReplay.RandomList.Initialize
	
    		mRandomList.Initialize
	
			Clear
End Sub
#End Region

Public  Sub DoReplay                 
	   		mDoingReplay = True     
End Sub

Public  Sub DoingReplay  As Boolean  
	   		Return mDoingReplay     
End Sub

Public  Sub Clear
    		mLastRandom = 0
			mRandomList.Clear
	
			If mDoingReplay Then LoadReplay
End Sub

Public  Sub SaveReplay
    		If mDoingReplay Then Return
	
    		Dim raf As RandomAccessFile

'   Log(File.DirRootExternal &Main.gDirectoryApp &"Replay.raf")

    		raf.Initialize(File.DirRootExternal, Starter.gDirectoryApp &"Replay.raf", False)

			mReplay.RandomList.Clear
    		mReplay.RandomList.AddAll(mRandomList)
	
    		raf.WriteObject(mReplay, True, 0)
    		raf.Close
End Sub

Public  Sub LoadReplay

			Dim ret As String
 			Dim fd As FileDialog
	
 			fd.FastScroll 	 = True
'			fd.KeyboardPopUp = False
			fd.ChosenName    = "Replay.raf"
			fd.FilePath  	 = File.DirRootExternal &Starter.gDirectoryApp ' also sets ChosenName to an emtpy string
			fd.FileFilter 	 = ".raf"
	
			ret = fd.Show("Pick replay file", "Replay", "Cancel", "", Null)

			'-----------------------------------------------------------------------
			'  See if the user wants to restore a database
			'-----------------------------------------------------------------------	
			If  ret = -1 Then 
	   			If  fd.ChosenName.EqualsIgnoreCase("replay.raf") Then
	      			File.Copy(fd.FilePath, fd.ChosenName, File.DirRootExternal, Starter.gDirectoryApp &"Replay.raf")
	   			End If
			End If
 

    		Dim raf  As RandomAccessFile

    		Try
        		Log(File.DirRootExternal &Starter.gDirectoryApp &"Replay.raf")

        		If  cGenFuncs.DoesFileExist(Starter.gDirectoryApp, "Replay.raf") Then
           			raf.Initialize(File.DirRootExternal, Starter.gDirectoryApp &"Replay.raf", True)

           			mReplay     = raf.ReadObject(0)
		   			mLastRandom = 0
		   			mRandomList.Clear
					mRandomList.AddAll(mReplay.RandomList)

           			raf.Close

#if Debug					

					Log("Replay File")
					
					For i = 0 To mRandomList.Size-1
						Log("ReplayEntry: " &i &"  Value:" &mRandomList.Get(i))
					Next
					
					Log("Replay File - Done")					
#end if
					
        		End If
    		Catch
        		CallSubDelayed3("Main", "Error_MsgBox", LastException.Message, "Error - LoadReplay")
    		End Try
End Sub


Public  Sub GetRnd(GetMin As Int, GetMax As Int) As Int

    		If  mDoingReplay Then
				If  mLastRandom >= mRandomList.Size Then					
					Log("ERROR - Reply-GetRnd(Entry:" &mLastRandom &"  Min:" &GetMin &"  Max:" &GetMax)

					Return Rnd(GetMin, GetMax)					
				End If
				
	   			Dim ReplayNumber As Int = mRandomList.Get(mLastRandom)

		        Log("Reply-GetRnd(Entry:" &mLastRandom &"  Min:" &GetMin &"  Max:" &GetMax &"  Return:" &ReplayNumber &")")
	   
	   			mLastRandom = mLastRandom + 1
	   
	   			If  ReplayNumber < GetMin Or ReplayNumber > GetMax Then
	      			CallSubDelayed3("Main", "Error_MsgBox", "Requested: " &GetMin &" to " &GetMax &" found:" &ReplayNumber, "Replay GetRnd Error")
	   			End If

			#if Debug
				Log("GetRnd - Min:" &GetMin &"  Max:" &GetMax &" Replay:" &ReplayNumber)
			#end if
			
	   
	   			Return ReplayNumber
			End If
	
			Dim RandomNumber As Int = GetMin
			
			If  GetMin = GetMax Then 
				RandomNumber = GetMin
			Else			
    			RandomNumber = Rnd(GetMin, GetMax)
			End If

			#if DebugX
				Log("GetRnd - Min:" &GetMin &"  Max:" &GetMax &" Replay:" &RandomNumber)
			#end if
			
			mRandomList.Add(RandomNumber)
	
			SaveReplay
	
'	Log("GetRnd(Entry:" &mRandomList.Size &"  Min:" &GetMin &"  Max:" &GetMax &"  Return:" &RandomNumber &")")
	
'#If Debug
'    If mRandomList.Size Mod 500 = 0 Then
'	   Log("")
'	End If
'#End If 
	
			Return RandomNumber
End Sub

Public  Sub NextNumber
			mLastRandom = mLastRandom + 1
End Sub
