scoreboard players set global wonGame 1
scoreboard players set #dozeoff wonGame 1
title @a[tag=inControl] subtitle ["",{"text":"Thanks for playing!","color":"#FFAA00"}]
title @a[tag=inControl] title ["",{"text":"You win!","color":"white","bold":true}]

#execute at @a[limit=1,sort=nearest] run playsound minecraft:ui.toast.challenge_complete master @a ~ ~100000 ~ 10000000
tellraw @a ["",{"text":"You've completed the Doze Off puzzle set!","color":"gold"}]
tellraw @a ["",{"text":"Click on `NEW GAME` to try a different puzzle set.","color":"white","italic":"true"}]

#record win
execute if score #dozeoff bestTime matches -1 run scoreboard players operation #dozeoff bestTime = #dozeoff gameTime
execute if score #dozeoff gameTime < #dozeoff bestTime run scoreboard players operation #dozeoff bestTime = #dozeoff gameTime
execute if score #dozeoff bestMoves matches -1 run scoreboard players operation #dozeoff bestMoves = #dozeoff gameMoves
execute if score #dozeoff gameMoves < #dozeoff bestMoves run scoreboard players operation #dozeoff bestMoves = #dozeoff gameMoves
scoreboard players operation global gameTime = #dozeoff bestTime
function bd:game/convert_raw_time/start
setblock 49 88 0 redstone_block

#write sign based on what rank we got
execute if score #dozeoff bestMoves matches ..4350 run data merge block 49 89 1 {GlowingText:1b,Text1:'{"text":"DOZE OFF","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/dozeoff"}}',Text2:'[{"text":" Rank: "},{"text":"S","color":"gold","bold":true}]',Text3:'[{"score":{"name":"#dozeoff","objective":"bestMoves"},"color":"black"},{"text":" moves","color":"black"}]',Text4:'[{"score":{"name":"global","objective":"timeHr"}},{"text":"h "},{"score":{"name":"global","objective":"timeMin2"}},{"score":{"name":"global","objective":"timeMin"}},{"text":"m "},{"score":{"name":"global","objective":"timeSec2"}},{"score":{"name":"global","objective":"timeSec"}},{"text":"s"}]'}
execute if score #dozeoff bestMoves matches 4351..8700 run data merge block 49 89 1 {GlowingText:1b,Text1:'{"text":"DOZE OFF","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/dozeoff"}}',Text2:'[{"text":" Rank: "},{"text":"A","color":"blue","bold":true}]',Text3:'[{"score":{"name":"#dozeoff","objective":"bestMoves"},"color":"black"},{"text":" moves","color":"black"}]',Text4:'[{"score":{"name":"global","objective":"timeHr"}},{"text":"h "},{"score":{"name":"global","objective":"timeMin2"}},{"score":{"name":"global","objective":"timeMin"}},{"text":"m "},{"score":{"name":"global","objective":"timeSec2"}},{"score":{"name":"global","objective":"timeSec"}},{"text":"s"}]'}
execute if score #dozeoff bestMoves matches 8701..17400 run data merge block 49 89 1 {GlowingText:1b,Text1:'{"text":"DOZE OFF","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/dozeoff"}}',Text2:'[{"text":" Rank: "},{"text":"B","color":"dark_green","bold":true}]',Text3:'[{"score":{"name":"#dozeoff","objective":"bestMoves"},"color":"black"},{"text":" moves","color":"black"}]',Text4:'[{"score":{"name":"global","objective":"timeHr"}},{"text":"h "},{"score":{"name":"global","objective":"timeMin2"}},{"score":{"name":"global","objective":"timeMin"}},{"text":"m "},{"score":{"name":"global","objective":"timeSec2"}},{"score":{"name":"global","objective":"timeSec"}},{"text":"s"}]'}
execute if score #dozeoff bestMoves matches 17401..34800 run data merge block 49 89 1 {GlowingText:1b,Text1:'{"text":"DOZE OFF","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/dozeoff"}}',Text2:'[{"text":" Rank: "},{"text":"C","color":"dark_purple","bold":true}]',Text3:'[{"score":{"name":"#dozeoff","objective":"bestMoves"},"color":"black"},{"text":" moves","color":"black"}]',Text4:'[{"score":{"name":"global","objective":"timeHr"}},{"text":"h "},{"score":{"name":"global","objective":"timeMin2"}},{"score":{"name":"global","objective":"timeMin"}},{"text":"m "},{"score":{"name":"global","objective":"timeSec2"}},{"score":{"name":"global","objective":"timeSec"}},{"text":"s"}]'}
execute if score #dozeoff bestMoves matches 34801.. run data merge block 49 89 1 {GlowingText:1b,Text1:'{"text":"DOZE OFF","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/dozeoff"}}',Text2:'[{"text":" Rank: "},{"text":"D","color":"dark_gray","bold":true}]',Text3:'[{"score":{"name":"#dozeoff","objective":"bestMoves"},"color":"black"},{"text":" moves","color":"black"}]',Text4:'[{"score":{"name":"global","objective":"timeHr"}},{"text":"h "},{"score":{"name":"global","objective":"timeMin2"}},{"score":{"name":"global","objective":"timeMin"}},{"text":"m "},{"score":{"name":"global","objective":"timeSec2"}},{"score":{"name":"global","objective":"timeSec"}},{"text":"s"}]'}

#summon trophy thing
kill @e[type=armor_stand,tag=trophy,tag=dozeoff]
summon armor_stand 49 89 0 {Tags:["nokill","trophy","dozeoff"],DisabledSlots:4144959,CustomNameVisible:0b,NoGravity:1b,Silent:1b,Invulnerable:1b,Invisible:1b,NoBasePlate:1b,ArmorItems:[{},{},{},{id:"minecraft:dark_oak_fence_gate",Count:1b}],CustomName:'{"text":"Trophy","color":"#FFAA00","bold":true}'}
execute as @e[type=armor_stand,tag=trophy,tag=dozeoff] at @s run tp @s ~ ~-.25 ~

execute as @a[tag=inControl] run function bd:game/check_for_advancements

summon firework_rocket 2 86 2 {LifeTime:15,FireworksItem:{id:firework_rocket,Count:1,tag:{Fireworks:{Flight:1,Explosions:[{Type:2,Flicker:1,Trail:1,Colors:[I;15435844,15790320],FadeColors:[I;14602026]}]}}}}