Skip to content

Instantly share code, notes, and snippets.

@shtrih
Created February 17, 2021 09:07
Show Gist options
  • Save shtrih/e80b293ae9c9ea387cd0979c411a8653 to your computer and use it in GitHub Desktop.
Save shtrih/e80b293ae9c9ea387cd0979c411a8653 to your computer and use it in GitHub Desktop.
#cs
Reads ChronoUp.txt content and send it's data to specified URL.
#ce
#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#pragma compile(Console, false)
#pragma compile(x64, true)
#pragma compile(Icon, "clock.ico")
#pragma compile(Out, "build/TimeSenderHPG3.exe")
#pragma compile(FileVersion, 1.1.1, 1.1.1)
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <InetConstants.au3>
#include <StringConstants.au3>
#include <Inet.au3>
#include <Timers.au3>
Const $g_sVERSION = "1.1.1"
Opt("GUIOnEventMode", 1)
Global $g_hGUI, $g_idExit
Global Const $g_iSendIntervalMinutes = 5
Global $g_iCallInterval = 1000 * 60 * $g_iSendIntervalMinutes
Global $g_sConfigURL
Global Const $eg_sConfigFilePath = @ScriptFullPath & '.ini'
configRead($eg_sConfigFilePath)
_Main()
Func _Main()
Local $idYes
If $g_sConfigURL = "" Then
MsgBox($MB_OK + $MB_ICONERROR, "Ошибка", "Невозможно прочитать значение параметра url!")
Exit
EndIf
$g_hGUI = GUICreate("Time sender for HPG3", 230, 80)
GUICtrlCreateLabel("Автоматическая отправка таймера игры " & @CRLF _
& "осуществляется каждые " & $g_iSendIntervalMinutes & " минут." & @CRLF & @CRLF _
& "Отправить таймер сейчас?", 10, 10)
GUICtrlSetStyle(-1, $SS_LEFT)
$idYes = GUICtrlCreateButton("Да", 165, 45, 50, 20)
GUICtrlSetOnEvent($idYes, "OnYes")
GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")
GUISetState()
; ↓ This function does not work on some machines.
;_Timer_SetTimer($g_hGUI, $g_iCallInterval, "OnTimerElapsed")
AdlibRegister("OnAdlibElapsed", $g_iCallInterval)
While 1
Sleep(1000)
WEnd
EndFunc
Func OnYes()
$sData = sendCurrentTime()
$sMsg = "Не отправлено"
If $sData <> "" Then
$sMsg = "Отправлено ("& $sData &")"
EndIf
MsgBox($MB_SYSTEMMODAL, "", $sMsg)
EndFunc
Func OnExit()
;_Timer_KillAllTimers($g_hGUI)
AdlibUnregister("OnAdlibElapsed")
sendCurrentTime()
Exit
EndFunc
Func OnTimerElapsed($hWnd, $iMsg, $iIDTimer, $iTime)
#forceref $hWnd, $iMsg, $iIDTimer, $iTime
sendCurrentTime()
EndFunc
Func OnAdlibElapsed()
sendCurrentTime()
EndFunc
Func sendCurrentTime()
$sData = readFile(".\ChronoUp.txt")
If Not $sData Then
ConsoleWrite("No Data" & @CRLF)
Return ""
EndIf
$sUrl = StringFormat($g_sConfigURL, $sData)
$iStatusCode = fetchURL($sUrl)
ConsoleWrite($iStatusCode & " " & $sUrl & @CRLF)
If $iStatusCode <> 200 Then
Return ""
EndIf
Return $sData
EndFunc
Func fetchURL($sUrl)
Local $oMyError = ObjEvent("AutoIt.Error", "errorHandler")
Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
$oHTTP.Open("GET", $sUrl, False)
$oHTTP.SetRequestHeader("User-Agent", "TimeSenderHPG3 " & $g_sVERSION)
$oHTTP.Send()
;$sData = $oHTTP.ResponseText()
If $oHTTP.Status <> 200 Then
MsgBox($MB_OK, "Ошибка", "Невозможно отправить запрос. " & @CRLF & "Код: " & $oHTTP.Status)
EndIf
Return $oHTTP.Status
#cs
HttpSetUserAgent("TimeSenderHPG3")
; ↓ This function does not work on some machines or with some URLs.
$dData = InetRead($sUrl, $INET_FORCERELOAD)
; https://www.autoitscript.com/forum/topic/136249-inetget-fails/?do=findComment&comment=955615
If @error And @error <> 33 Then
MsgBox($MB_OK + $MB_ICONERROR, "Ошибка", "Невозможно отправить запрос. " & @CRLF & "Код: " & @error & ":" & @extended)
EndIf
Return BinaryToString($dData, $SB_UTF8)
#ce
EndFunc
Func readFile($sFilePath)
Local $hFileOpen = FileOpen($sFilePath, $FO_READ)
If $hFileOpen = -1 Then
MsgBox($MB_OK + $MB_ICONERROR, "Ошибка", "Невозможно прочитать файл " & $sFilePath)
Return False
EndIf
Local $sFileRead = FileRead($hFileOpen)
FileClose($hFileOpen)
Return StringStripWS($sFileRead, $STR_STRIPALL)
EndFunc
Func configRead($sFilePath)
$g_sConfigURL = IniRead($sFilePath, 'General', 'url', '')
EndFunc
Func errorHandler($oError)
MsgBox($MB_OK, "We intercepted a COM Error !", _
"Number: 0x" & Hex($oError.number, 8) & @CRLF & _
"Description: " & $oError.windescription & _
"At line: " & $oError.scriptline)
EndFunc
[General]
url = https://hpg.su/timer?token=&time=%s&name=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment