#include-once

;; SMAC XML APi
Func lookupSteamId($input)
	Local $oXML = ObjCreate("Microsoft.XMLHTTP")
	;$oXML.Open("GET", "http://labor.spiffytek.com/smacheck/api/steamid/" & $input & "/", 0)
	$oXML.Open("GET", "http://api.spiffytek.com/pub/smacheck/steamid/" & $input, 0)
	$oXML.Send
	Global $g_eventerror
	If $g_eventerror <> 0 Then
		MsgBox(16, "ERROR", "API not available.")
		Return "ERROR_HANDELED"
	EndIf

	Local $sFile = _TempFile(@TempDir, '~', '.xml')
	FileWrite($sFile, $oXML.responseText)

	If _XMLFileOpen($sFile) Then
		Local $pStatus = _XMLGetValue('/isbanned/status')
	EndIf

	If @Compiled <> 0 Then
		Local $curVersion = FileGetVersion(@ScriptName, "FileVersion")
		Local $pMinVersion = _XMLGetValue("/isbanned/smaCheck/minVersion")
		Local $pActVersion = _XMLGetValue("/isbanned/smaCheck/actVersion")
		Global $gVersionChecked
		If IsArray($pMinVersion) Then
			If $curVersion < $pActVersion[1] Then
				If $curVersion <  $pMinVersion[1] Then
					MsgBox(16, "Required Update available", "Required Update available." & @CRLF & @CRLF & "Please download new Version from smacbans.com.")
					FileDelete($sFile)
					Return "ERROR_HANDELED"
				ElseIf $gVersionChecked = 0 Then
					MsgBox(48, "Update available", "New Update available." & @CRLF & @CRLF & "Visit smacbans.com for more Info and Download.")
					$gVersionChecked = 1
				EndIf
			EndIf
		EndIf
	EndIf


	If IsArray($pStatus) Then
		Local $bReason = _XMLGetValue("/isbanned/reason")
		Local $bNick = _XMLGetValue("/isbanned/nickname")
		Local $bTime = _XMLGetValue("/isbanned/banTime")
		Local $bVac = _XMLGetValue("/isbanned/vacStatus")
		Local $bMod = _XMLGetValue("/isbanned/mod")
		Local $bCc = _XMLGetValue("/isbanned/country")

		If $bVac[1] = "1" Then
			Local $pVac = "Bans on record"
		ElseIf $bVac[1] = "-1" Then
			Local $pVac = "Could not get Status from Steam"
		Else
			Local $pVac = "No"
		EndIf

		If $bCc[1] = "" Then
			$bCc[1] = "nocountry"
		EndIf

		If $pStatus[1] = "Y" Then
			Local $oStatus = "SteamID is Banned"
			Local $return[8]
			$return[0] = $oStatus
			$return[1] = $bNick[1]
			$return[2] = $bReason[1]
			$return[3] = $bTime[1]
			$return[4] = $pStatus[1]
			$return[5] = $pVac
			$return[6] = $bMod[1]
			$return[7] = $bCc[1]
		Else
			Local $oStatus = "SteamID is not banned"
			Local $return[8]
			$return[0] = $oStatus
			$return[1] = $bNick[1]
			$return[2] = "-"
			$return[3] = "-"
			$return[4] = $pStatus[1]
			$return[5] = $pVac
			$return[6] = "-"
			$return[7] = "nocountry"
		EndIf

	Else
		FileDelete($sFile)
		MsgBox(16, "ERROR", "API not available.")
		Return "ERROR_HANDELED"
	EndIf

	FileDelete($sFile)

	Return $return

EndFunc



;; SteamID Convert
Dim Const $COMM_ID_START = 76561197960265728

Func steamId64ToSteamId($sComID)
    $sComID = Number($sComID)

    $a = ($sComID - $COMM_ID_START) / 2
    $b = Floor($a)
    $c = Round( Mod($a,Floor($a)) )

    Return "STEAM_0:" & String( $c ) & ":" & String( $b )
EndFunc


Func steamIdToCommunityId($sSteamID)
    Local $sResult, $expl, $serverid, $accountid

    $expl = StringSplit($sSteamID,":")

    if (UBound($expl) == 4) Then
        $serverid = Number($expl[2])
        $accountid = Number($expl[3])

        $sResult = ($accountid * 2) + $COMM_ID_START + $serverid
    EndIf

    Return $sResult
EndFunc


;; Misc
Func resetInput($iID)
	GUICtrlSetData($iID, "")
EndFunc


Func MyErrFunc()
	Global $oError
	;Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"      & @CRLF  & @CRLF & _
	;		"err.description is: "    & @TAB & $oError.description    & @CRLF & _
	;		"err.windescription:"     & @TAB & $oError.windescription & @CRLF & _
	;		"err.number is: "         & @TAB & hex($oError.number,8)  & @CRLF & _
	;		"err.lastdllerror is: "   & @TAB & $oError.lastdllerror   & @CRLF & _
	;		"err.scriptline is: "     & @TAB & $oError.scriptline     & @CRLF & _
	;		"err.source is: "         & @TAB & $oError.source         & @CRLF & _
	;		"err.helpfile is: "       & @TAB & $oError.helpfile       & @CRLF & _
	;		"err.helpcontext is: "    & @TAB & $oError.helpcontext _
	;)

	Local $err = $oError.number
	If $err = 0 Then
		$err = -1
	EndIf
	$g_eventerror = $err  ; to check for after this function returns
Endfunc