#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
#    This function has been created by Skilendarz Group.
#             https://skilendarz.com
#
#      GIVE MAP MAKERS THE CREDIT THEY DESERVE!
#             COPYING IS PROHIBITED!
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

#Match UUID
scoreboard players operation UUID UUID = @s UUID
scoreboard players operation UUID UUID2 = @s UUID2
scoreboard players operation UUID UUID3 = @s UUID3
scoreboard players operation UUID UUID4 = @s UUID4
execute as @e[tag=database] if score @s UUID = UUID UUID if score @s UUID2 = UUID UUID2 if score @s UUID3 = UUID UUID3 if score @s UUID4 = UUID UUID4 run tag @s add matches

#Database error
execute unless entity @e[tag=database,tag=matches] run tag @s remove registered
tellraw @s[tag=!registered] ["",{"text":"<Debug> ","color":"red","bold":true},{"text":"Database error has been detected. Your leaderboard statistics will refresh after playing the next round.","color":"white"}]
execute if entity @s[tag=!registered] run tellraw @a[tag=debug] ["",{"text":"<Debug> ","color":"red","bold":true},{"text":"Database error detected. (","color":"white"},{"selector":"@s","color":"green"},{"text":")","color":"white"}]

#Operations
scoreboard players add @s wins 0
scoreboard players add @s kills 0
scoreboard players add @s pumpkins 0
scoreboard players operation @e[tag=database,tag=matches] wins = @s wins
scoreboard players operation @e[tag=database,tag=matches] kills = @s kills
scoreboard players operation @e[tag=database,tag=matches] pumpkins = @s pumpkins

#Store player data
setblock 314 103 -335 chest
loot insert 314 103 -335 loot f:wins
loot insert 314 103 -335 loot f:kills
loot insert 314 103 -335 loot f:pumpkins

data modify entity @e[tag=database,tag=matches,limit=1] data.Wins set from block 314 103 -335 Items[0].tag.display.Name
data modify entity @e[tag=database,tag=matches,limit=1] data.Kills set from block 314 103 -335 Items[1].tag.display.Name
data modify entity @e[tag=database,tag=matches,limit=1] data.Pumpkins set from block 314 103 -335 Items[2].tag.display.Name

#reset
tag @e[tag=database,tag=matches] remove matches