scoreboard players set global wonGame 1
scoreboard players set #fingermaps 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 Fingermaps Map Jam puzzle set!","color":"yellow"}]
tellraw @a ["",{"text":"Click on `NEW GAME` to try a different puzzle set.","color":"white","italic":"true"}]

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

#write sign based on what rank we got
execute if score #fingermaps bestMoves matches ..4750 run data merge block 52 89 1 {GlowingText:1b,Text1:'{"text":"FINGERMAPS","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/fingermaps"}}',Text2:'[{"text":" Rank: "},{"text":"S","color":"gold","bold":true}]',Text3:'[{"score":{"name":"#fingermaps","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 #fingermaps bestMoves matches 4751..8000 run data merge block 52 89 1 {GlowingText:1b,Text1:'{"text":"FINGERMAPS","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/fingermaps"}}',Text2:'[{"text":" Rank: "},{"text":"A","color":"blue","bold":true}]',Text3:'[{"score":{"name":"#fingermaps","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 #fingermaps bestMoves matches 9001..16000 run data merge block 52 89 1 {GlowingText:1b,Text1:'{"text":"FINGERMAPS","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/fingermaps"}}',Text2:'[{"text":" Rank: "},{"text":"B","color":"dark_green","bold":true}]',Text3:'[{"score":{"name":"#fingermaps","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 #fingermaps bestMoves matches 16001..32000 run data merge block 52 89 1 {GlowingText:1b,Text1:'{"text":"FINGERMAPS","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/fingermaps"}}',Text2:'[{"text":" Rank: "},{"text":"C","color":"dark_purple","bold":true}]',Text3:'[{"score":{"name":"#fingermaps","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 #fingermaps bestMoves matches 32001.. run data merge block 52 89 1 {GlowingText:1b,Text1:'{"text":"FINGERMAPS","bold":true,"clickEvent":{"action":"run_command","value":"/function bd:game/rank_info/fingermaps"}}',Text2:'[{"text":" Rank: "},{"text":"D","color":"dark_gray","bold":true}]',Text3:'[{"score":{"name":"#fingermaps","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=fingermaps]
summon armor_stand 52 89 0 {Tags:["nokill","trophy","fingermaps"],DisabledSlots:4144959,CustomNameVisible:0b,NoGravity:1b,Silent:1b,Invulnerable:1b,Invisible:1b,NoBasePlate:1b,ArmorItems:[{},{},{},{id:"minecraft:gold_block",Count:1b,tag:{CustomModelData:1234567}}],CustomName:'{"text":"Trophy","color":"#FFAA00","bold":true}'}
execute as @e[type=armor_stand,tag=trophy,tag=fingermaps] at @s run tp @s ~ ~-.25 ~

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

summon firework_rocket 29 83 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]}]}}}}