;~ #include <WindowsConstants.au3>

GUICreate("Jeopardy", @DesktopWidth, @DesktopHeight, 0, 0, 0x80000000)
;Berechnungen&Definitionen:
$ContinueGame = False
$sQDB = "QDB.ini"
$sAutoSave = "autosave.ini"
$sBGMusic = "10minbg.mp3"
If FileExists($sAutoSave) Then $ContinueGame = True
$xborder = 10; border
$xwidth = @DesktopWidth - 2 * $xborder
$yborder = 10; border
$ywidth = @DesktopHeight - 2 * $xborder

;GUI-Aufbau:

;Developer/Host-Tool:
HotKeySet("+0","_AdminPanel")
;--

Dim $buttons[5][5]
Dim $captions[5]
Dim $names[3]
Dim $credits[3] = [0, 0, 0]
Dim $username[3] = ["User1", "User2", "User3"]
Dim $buttonsleft = 25

For $x = 0 To 4
	$captions[$x] = GUICtrlCreateLabel("Kategorie" & $x, $xborder + $x * ($xwidth / 5), $yborder, (($xwidth - 20) / 5), 50, 0x01)
	GUICtrlSetFont(-1, 16)
	For $y = 0 To 4
;~ 		$buttons[$x][$y] = GUICtrlCreateButton($y+1 & "00",$xborder+$x*($xwidth/5),$yborder+50+$y*120,(($xwidth-20)/5),100)
		$buttons[$x][$y] = GUICtrlCreateButton($y + 1 & "00", $xborder + $x * ($xwidth / 5), $yborder + 50 + $y * ($ywidth / 8), (($xwidth - 20) / 5), 100)
		GUICtrlSetFont(-1, 16)
	Next
Next

For $x = 0 To 2
	$names[$x] = GUICtrlCreateLabel("Username: 0", $xborder + $x * ($xwidth / 3), $yborder + 100 + 5 * ($ywidth / 8), (($xwidth - 20) / 3), 100, 0x01)
	GUICtrlSetFont(-1, 20)
Next
GUICtrlSetColor($names[0], 0xff0000)
GUICtrlSetColor($names[1], 0x00ff00)
GUICtrlSetColor($names[2], 0x0000ff)

;Notify-feld:
$oNotify = GUICtrlCreateLabel("Willkommen!", 0, $yborder + 100 + 6 * ($ywidth / 8), @DesktopWidth, 100, 0x01)
GUICtrlSetBkColor(-1, 0xaaaaaa)
GUICtrlSetFont(-1, 26)

$player1 = GUICtrlCreateDummy()
$player2 = GUICtrlCreateDummy()
$player3 = GUICtrlCreateDummy()
$host = GUICtrlCreateDummy()
Dim $accel[4][2] = [["1", $player1],["2", $player2],["3", $player3],["{SPACE}", $host]]
GUISetAccelerators($accel)
_Highlight(0, 0)
_Highlight(1, 0)
_Highlight(2, 0)
;GUI-Aufbau Ende

GUISetState()

_LoadCaptions($captions)

If $ContinueGame Then
	_LoadSaveFile()
Else
	SoundPlay("3mintheme.mp3",0)
	_ThisIsJeopardy()
	_AskForNames()
EndIf
_updateCredits()
SoundPlay("10minbg.mp3",0)
While 1
	$msg = GUIGetMsg()
	Switch $msg
		Case -3
			Exit
		Case Else
			For $x = 0 To 4
				For $y = 0 To 4
					If $msg = $buttons[$x][$y] Then _ClickButton($x, $y)
				Next
			Next
	EndSwitch
WEnd

Func _Notify($sText = "", $iType = 0)
	;iType: 0 = normal, 1 = Info, 2 = Error
	GUICtrlSetData($oNotify, $sText)
	Switch $iType
		Case 1
			GUICtrlSetColor($oNotify, 0x0000ff)
		Case 2
			GUICtrlSetColor($oNotify, 0xff0000)
		Case Else
			GUICtrlSetColor($oNotify, 0x000000)
	EndSwitch
EndFunc   ;==>_Notify
Func _LoadCaptions(ByRef $captions)
	$sections = IniReadSectionNames($sQDB)
	For $i = 1 To $sections[0]
		GUICtrlSetData($captions[$i - 1], $sections[$i])
	Next
EndFunc   ;==>_LoadCaptions
Func _ClickButton($x, $y)
	Local $text = IniRead($sQDB, GUICtrlRead($captions[$x]), $y, "")
	_Notify($text, 1)
	While 1
		Switch GUIGetMsg()
			Case $player1
				$ret = _Play(0, $y)
				If $ret Then ExitLoop
			Case $player2
				$ret = _Play(1, $y)
				If $ret Then ExitLoop
			Case $player3
				$ret = _Play(2, $y)
				If $ret Then ExitLoop
			Case $host
				ExitLoop
		EndSwitch
	WEnd
	GUICtrlSetState($buttons[$x][$y], 128)
	$buttonsleft -= 1
	IniWrite($sAutoSave, "buttons", $x & $y, "1")
	If $buttonsleft = 0 Then _FinishGame()
EndFunc   ;==>_ClickButton
Func _Highlight($user, $set = 1)
	Switch $set
		Case 0
			GUICtrlSetBkColor($names[$user], 0xffffff)
		Case Else
			GUICtrlSetBkColor($names[$user], 0xaaaaaa)
	EndSwitch
EndFunc   ;==>_Highlight

Func _Play($who, $y)
	_Highlight($who, 1)
	$res = MsgBox(4, "Jeopardy", "Gewonnen?")
	Switch $res
		Case 6;YES
			$credits[$who] += GUICtrlRead($buttons[0][$y])
			_Notify($username[$who] & " hat " & GUICtrlRead($buttons[0][$y]) & " Punkte gewonnen!")
		Case 7;NO
			$credits[$who] -= GUICtrlRead($buttons[0][$y])
			_Notify($username[$who] & " hat " & GUICtrlRead($buttons[0][$y]) & " Punkte verloren!", 2)
	EndSwitch
	_updateCredits()
	_Highlight($who, 0)
	If $res = 6 Then Return True
	Return False
EndFunc   ;==>_Play

Func _updateCredits()
	For $i = 0 To 2
		GUICtrlSetData($names[$i], $username[$i] & ": " & $credits[$i])
		IniWrite($sAutoSave, "credits", $i, $credits[$i])
	Next
EndFunc   ;==>_updateCredits
Func _LoadSaveFile()
	For $i = 0 To 2
		$credits[$i] = IniRead($sAutoSave, "credits", $i, 0)
		$username[$i] = IniRead($sAutoSave, "usernames", $i, "User" & $i+1)
	Next
	For $x = 0 To 4
		For $y = 0 To 4
			If IniRead($sAutoSave, "buttons", $x & $y, 0) = 1 Then
				GUICtrlSetState($buttons[$x][$y], 128)
				$buttonsleft -= 1
			EndIf
		Next
	Next
	If $buttonsleft < 1 Then _Reset()
EndFunc   ;==>_LoadSaveFile
Func _FinishGame()
	If $credits[0] > $credits[1] And $credits[0] > $credits[2] Then $w=0
	If $credits[1] > $credits[0] And $credits[1] > $credits[2] Then $w=1
	If $credits[2] > $credits[1] And $credits[2] > $credits[0] Then $w=2
	If $credits[0] = $credits[1] Or $credits[2] = $credits[1]  Or $credits[0] = $credits[2] Then
		If $credits[0] = $credits[1] And $credits[1] = $credits[2] Then
			_Notify("Alle gewinnen! (Jeweils mit " & $credits[0] & " Punkten)",1)
		ElseIf $credits[0] = $credits[1] Then
			_Notify($username[0] & " und " & $username[1] & " teilen sich den Sieg mit jeweils " & $credits[0] & " Punkten.",1)
		ElseIf $credits[0] = $credits[2] Then
			_Notify($username[0] & " und " & $username[2] & " teilen sich den Sieg mit jeweils " & $credits[0] & " Punkten.",1)
		Else
			_Notify($username[2] & " und " & $username[1] & " teilen sich den Sieg mit jeweils " & $credits[1] & " Punkten.",1)
		EndIf
	Else
		_Notify($username[$w] & " gewinnt mit " & $credits[$w] & " Punkten!",1)
	EndIf
EndFunc
Func _Reset()
	FileDelete($sAutoSave)
	Exit ShellExecute(@ScriptFullPath)
EndFunc
Func _AskForNames()
	For $i = 0 To 2
		$answer = InputBox("Jeopardy","User " & $i+1 & ", wie möchten Sie heißen?","User" & $i+1)
		$username[$i] = $answer
		IniWrite($sAutoSave,"usernames",$i,$answer)
	Next
EndFunc
Func _AdminPanel()
	$cmd=InputBox("Jeopardy","Befehl?")
	Select
		Case $cmd = "r"
			_Reset()
		Case $cmd = "x"
			FileDelete($sAutoSave)
			Exit
		Case $cmd = "e"
			$buttonsleft = 0
			_FinishGame()
		Case StringInStr($cmd,"uc") = 1
			$credits[StringTrimLeft(StringLeft($cmd,3),2)] = StringTrimLeft($cmd,3)
			IniWrite($sAutoSave,"credits",StringTrimLeft(StringLeft($cmd,3),2),StringTrimLeft($cmd,3))
		Case StringInStr($cmd,"un") = 1
			$username[StringTrimLeft(StringLeft($cmd,3),2)] = StringTrimLeft($cmd,3)
			IniWrite($sAutoSave,"usernames",StringTrimLeft(StringLeft($cmd,3),2),StringTrimLeft($cmd,3))
		Case StringInStr($cmd,"a") = 1
			GUICtrlSetState($buttons[StringTrimLeft(StringLeft($cmd,2),1)][StringRight($cmd,1)],64)
	EndSelect
	_updateCredits()
EndFunc
Func _ThisIsJeopardy()
	$gui3 = GUICreate("This is BG",@DesktopWidth,@DesktopHeight,0,0,0x80000000,0x00000080)
	GUISetBkColor(0x000000)
	GUISetState()
	$gui2 = GUICreate("This is Jeopardy!",@DesktopWidth,@DesktopHeight,0,0,0x80000000,0x00000080)
	GUISetBkColor(0x000000)
	GUICtrlCreatePic("jeopardy_logo.jpg",@DesktopWidth/2-221,@DesktopHeight/2-63,443,127)
	WinSetTrans("This is Jeopardy!","",0)
	GUISetState(@SW_SHOW,$gui2)
	For $i=0 To 255 Step 2
		Sleep(50)
		WinSetTrans("This is Jeopardy!","",$i)
	Next
	GUIDelete($gui3)
	Sleep(3000)
	For $i=255 To 0 Step -5
		Sleep(80)
		WinSetTrans("This is Jeopardy!","",$i)
	Next
	GUIDelete($gui2)
EndFunc