Jump to content

Countdown - Reverse this script


Delta01
 Share

Recommended Posts

Hi,

I looked in the help file for a countdown but couldn't find one. I did however find a timer, which counts up.

; *** Demo to show a timer window
#include <GUIConstants.au3>
#include <Date.au3>
opt("TrayIconDebug",1)
Global $Secs, $Mins, $Hour, $Time
;Create GUI
GUICreate("Timer",120, 50)
GUICtrlCreateLabel("00:00:00", 10,10)
GUISetState()
;Start timer
$timer = TimerInit()
AdlibEnable("Timer", 50)
;
While 1
 ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> before")
  $msg = GUIGetMsg()
 ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> after")
  Select
     Case $msg = $GUI_EVENT_CLOSE
        Exit
  EndSelect
Wend
;
Func Timer()
  _TicksToTime(Int(TimerDiff($timer)), $Hour, $Mins, $Secs )
  Local $sTime = $Time  ; save current time to be able to test and avoid flicker..
  $Time = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
  If $sTime <> $Time Then ControlSetText("Timer", "", "Static1", $Time)
EndFunc  ;==>Timer

What I want to do is make it so that if I input a time (2:00:00) it will then countdown until it equals 0 (2 hours)

I can do the input time and other things I want, I just need a script for a timer that counts backwards from a number that you input.

Hope you understand what I want

**EDIT: I can't use the sleep function, I still need to do other things in the GUI

Edited by Delta01
Link to comment
Share on other sites

Subtract your timerdeiff from the number of hours (min sec) in timer tics For 2 hours it's 7,200,000

; *** Demo to show a timer window

#include <GUIConstants.au3>

#include <Date.au3>

opt("TrayIconDebug",1)

Global $Secs, $Mins, $Hour, $Time

;Create GUI

GUICreate("Timer",120, 50)

GUICtrlCreateLabel("00:00:00", 10,10)

GUISetState()

;Start timer

$timer = TimerInit()

AdlibEnable("Timer", 50)

;

While 1

;FileWriteLine("debug.log",@min & ":" & @sec & " ==> before")

$msg = GUIGetMsg()

;FileWriteLine("debug.log",@min & ":" & @sec & " ==> after")

Select

Case $msg = $GUI_EVENT_CLOSE

Exit

EndSelect

Wend

;

Func Timer()

_TicksToTime(7200000-Int(TimerDiff($timer)), $Hour, $Mins, $Secs )

consolewrite($timer & @crlf)

consolewrite(TimerDiff($timer) & @crlf)

Local $sTime = $Time ; save current time to be able to test and avoid flicker..

$Time = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)

If $sTime <> $Time Then ControlSetText("Timer", "", "Static1", $Time)

EndFunc ;==>Timer

Link to comment
Share on other sites

You're welcome. If you are going to vary the coutdown number.. get the time (ie 2 hours 30 minutes) and mujltiply it out.. 2 hours = 2 * 60 minutes *60 secs * 1000 PLUS the 30 *60 *1000 etc to get the base from which to subtract ..

Make sense ?

Link to comment
Share on other sites

Yeah, I understand that.

I have another problem though, I need to make it so I can have various countdowns going at the same time. My problem is that once the second one starts, the first one stops.

my code is

#include <GUIConstants.au3>
#include <Date.au3>
opt("TrayIconDebug",1)
Global $Secs, $Mins, $Hour, $Time, $number1, $Number2, $Time2
;Create GUI
GUICreate("Taxi Script",300, 200)
$User1Time = GUICtrlCreateLabel("00:00:00", 80,30)
GuiCtrlCreateLabel("User", 10, 10)
GuiCtrlCreateLabel("Timeleft", 80, 10)
GuiCtrlCreateLabel("Gold", 150, 10)
GuiCtrlCreateLabel("Party number", 220, 10)
$Add1 = GuiCtrlCreateButton("Add", 10, 30, 30, 30, $BS_FLAT)
$User2Time = GuiCtrlCreateLabel("00:00:00", 80, 70)
$Add2 = GuiCtrlCreateButton("Add", 10, 70, 30, 30, $BS_FLAT)
GUISetState()


While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
Exit
Case $msg = $Add1
$User1 = InputBox("User's name", "Enter the name below", "BoT90210")
$Count1 = InPutBox("Time paid for", "How much did " & $User1 & " pay for, in hours?", "2")
$Number1 = $Count1 * 3600000
$Gold1 = InPutBox("Amount paid", "How much gold did they pay you?", "100,000")
$Party1 = InPutBox($User1 & "'s party number", $User1 & " is what number in the party? Remember that number 1 is the leader", "2")
$Msg1 = MsgBox(4, "Set up complete", "The set up for user: " & $User1 & " is complete." & @CRLF & @CRLF & "Do you wish to start the timer?")
If $Msg1 = 6 Then
    GuiCtrlDelete($Add1)
    $LabelUser1 = GuiCtrlCreateLabel($User1, 10, 30)
    $User1Gold = GuiCtrlCreateLabel($Gold1, 150, 30)
    $User1Party = GuiCtrlCreateLabel($Party1, 220, 30)
    $timer = TimerInit()
    AdlibEnable("Timer1", 50)
ElseIf $Msg1 = 7 then
    sleep(500)
    Endif
Case $msg = $Add2
$User2 = InputBox("User's name", "Enter the name below", "BoT90210")
$Count2 = InPutBox("Time paid for", "How much did " & $User2 & " pay for, in hours?", "2")
$Number2 = $Count2 * 3600000
$Gold2 = InPutBox("Amount paid", "How much gold did they pay you?", "100,000")
$Party2 = InPutBox($User2 & "'s party number", $User2 & " is what number in the party? Remember that number 1 is the leader", "2")
$Msg2 = MsgBox(4, "Set up complete", "The set up for user: " & $User2 & " is complete." & @CRLF & @CRLF & "Do you wish to start the timer?")
If $Msg2 = 6 Then
    GuiCtrlDelete($Add2)
    $LabelUser2 = GuiCtrlCreateLabel($User2, 10, 70)
    $User2Gold = GuiCtrlCreateLabel($Gold2, 150, 70)
    $User2Party = GuiCtrlCreateLabel($Party2, 220, 70)
    $timer2 = TimerInit()
    AdlibEnable("Timer2", 50)
    ElseIf $Msg2 = 7 then
    sleep(500)
    Endif
EndSelect
Wend
;
Func Timer1()
_TicksToTime($Number1-Int(TimerDiff($timer)), $Hour, $Mins, $Secs )
consolewrite($timer & @crlf)
consolewrite(TimerDiff($timer) & @crlf)
Local $sTime = $Time ; save current time to be able to test and avoid flicker..
$Time = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
If $sTime <> $Time Then ControlSetText("Taxi Script", "", "Static1", $Time)
EndFunc ;==>Timer

Func Timer2()
_TicksToTime($Number2-Int(TimerDiff($timer2)), $Hour, $Mins, $Secs )
consolewrite($timer2 & @crlf)
consolewrite(TimerDiff($timer2) & @crlf)
Local $sTime2 = $Time2 
$Time2 = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
If $sTime2 <> $Time2 Then ControlSetText("Taxi Script", "", "Static6", $Time2)
EndFunc

Is this possible to fix? Or so AutoIt only allow for one TimerInit to be running at once?

Link to comment
Share on other sites

You;'ll have to set the users up in arrays and use 1 timer function call then loop thru the arrays .. should be pretty fast

Hmmm, i may know how to do that. I'll have a go but I may not be successful, can you show me what you'd do?

Link to comment
Share on other sites

Yeah, I understand that.

I have another problem though, I need to make it so I can have various countdowns going at the same time. My problem is that once the second one starts, the first one stops.

....

Is this possible to fix? Or so AutoIt only allow for one TimerInit to be running at once?

Only one adlib function can be run ay any one time but I've rearranged your code to work

$bRunTimer1 and $bRunTimer2 are booleans used in the message loop to control the updating of the timers

#include <GUIConstants.au3>
#include <Date.au3>
Opt("TrayIconDebug", 1)
Global $Secs, $Mins, $Hour, $Time, $number1, $Number2, $Time2, $Timer, $Timer2, $bRunTimer1, $bRunTimer2 
;Create GUI
GUICreate("Taxi Script", 300, 200)
$User1Time = GUICtrlCreateLabel("00:00:00", 80, 30)
GUICtrlCreateLabel("User", 10, 10)
GUICtrlCreateLabel("Timeleft", 80, 10)
GUICtrlCreateLabel("Gold", 150, 10)
GUICtrlCreateLabel("Party number", 220, 10)
$Add1 = GUICtrlCreateButton("Add", 10, 30, 30, 30, $BS_FLAT)
$User2Time = GUICtrlCreateLabel("00:00:00", 80, 70)
$Add2 = GUICtrlCreateButton("Add", 10, 70, 30, 30, $BS_FLAT)
GUISetState()


While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $Add1
            SetupUser1()
        Case $msg = $Add2
            SetupUser2()
    EndSelect
    If $bRunTimer1 Then Timer1()
    If $bRunTimer2 Then Timer2()
WEnd


Func SetupUser1()
    $User1 = InputBox("User's name", "Enter the name below", "BoT90210")
    $Count1 = InputBox("Time paid for", "How much did " & $User1 & " pay for, in hours?", "2")
    $number1 = $Count1 * 3600000
    $Gold1 = InputBox("Amount paid", "How much gold did they pay you?", "100,000")
    $Party1 = InputBox($User1 & "'s party number", $User1 & " is what number in the party? Remember that number 1 is the leader", "2")
    $Msg1 = MsgBox(4, "Set up complete", "The set up for user: " & $User1 & " is complete." & @CRLF & @CRLF & "Do you wish to start the timer?")
    If $Msg1 = 6 Then
        GUICtrlDelete($Add1)
        $LabelUser1 = GUICtrlCreateLabel($User1, 10, 30)
        $User1Gold = GUICtrlCreateLabel($Gold1, 150, 30)
        $User1Party = GUICtrlCreateLabel($Party1, 220, 30)
        $timer = TimerInit()
        $bRunTimer1 = 1
    ElseIf $Msg1 = 7 Then
        Sleep(500)
    EndIf
EndFunc   ;==>SetupUser1

Func SetupUser2()
    $bRunTimer2 = 0
    $User2 = InputBox("User's name", "Enter the name below", "BoT90210")
    $Count2 = InputBox("Time paid for", "How much did " & $User2 & " pay for, in hours?", "2")
    $Number2 = $Count2 * 3600000
    $Gold2 = InputBox("Amount paid", "How much gold did they pay you?", "100,000")
    $Party2 = InputBox($User2 & "'s party number", $User2 & " is what number in the party? Remember that number 1 is the leader", "2")
    $Msg2 = MsgBox(4, "Set up complete", "The set up for user: " & $User2 & " is complete." & @CRLF & @CRLF & "Do you wish to start the timer?")
    If $Msg2 = 6 Then
        GUICtrlDelete($Add2)
        $LabelUser2 = GUICtrlCreateLabel($User2, 10, 70)
        $User2Gold = GUICtrlCreateLabel($Gold2, 150, 70)
        $User2Party = GUICtrlCreateLabel($Party2, 220, 70)
        $timer2 = TimerInit()
        $bRunTimer2 = 1
    ElseIf $Msg2 = 7 Then
        Sleep(500)
    EndIf
    
EndFunc   ;==>SetupUser2

Func Timer1()
    $iTimeToGo = $number1 - Int(TimerDiff($timer))
    _TicksToTime($iTimeToGo, $Hour, $Mins, $Secs)
    ConsoleWrite($timer & @CRLF)
    ConsoleWrite(TimerDiff($timer) & @CRLF)
    Local $sTime = $Time ; save current time to be able to test and avoid flicker..
    $Time = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
    If $sTime <> $Time Then ControlSetText("Taxi Script", "", "Static1", $Time)
    If $iTimeToGo <= 0 Then $bRunTimer1 = 0
EndFunc   ;==>Timer1

Func Timer2()
    $iTimeToGo = $number2 - Int(TimerDiff($timer2))
    _TicksToTime($iTimeToGo, $Hour, $Mins, $Secs)
    ConsoleWrite($timer2 & @CRLF)
    ConsoleWrite(TimerDiff($timer2) & @CRLF)
    Local $sTime2 = $Time2
    $Time2 = StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
    If $sTime2 <> $Time2 Then   ControlSetText("Taxi Script", "", "Static6", $Time2)
    If $iTimeToGo <= 0 Then $bRunTimer2 = 0
EndFunc   ;==>Timer2

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

You will be limited to whatever number of funtions/timers/people you code. If you do it in arrays you could as many as you like. by simply clicking ADD NEW and upping the arrays by 1

Your basic structure (for one user) was correct.. You could spend the time to think abot tghe logic what should be an array and what should be scaler. If it's max 7 and you don't mind the code repeated 7 times then do what is easiest.

Link to comment
Share on other sites

You will be limited to whatever number of funtions/timers/people you code. If you do it in arrays you could as many as you like. by simply clicking ADD NEW and upping the arrays by 1

Your basic structure (for one user) was correct.. You could spend the time to think abot tghe logic what should be an array and what should be scaler. If it's max 7 and you don't mind the code repeated 7 times then do what is easiest.

Ahhh. Yeah, I only need 7. I've already coded it like that.

Thanks all for your help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...