scoreboard players add @s total_games_played 0
execute unless score @s total_games_played matches 1.. run return run tellraw @s {"text":"You must play at least 1 game to display stats!","color":"red"}
execute if score @s item_cooldown matches 1.. run return 0

execute if entity @s[team=red] run tag @s add red_stat_temp
execute if entity @s[team=blue] run tag @s add blue_stat_temp
team join lobby

scoreboard players set #100 temp 100

# k = total_kills * 100
scoreboard players operation #k temp = @s total_kills
scoreboard players operation #k temp *= #100 temp

# d = total_deaths
scoreboard players operation #d temp = @s total_deaths

scoreboard players operation #kd temp = #k temp
scoreboard players operation #kd temp /= #d temp

# int = integer part of kd
scoreboard players operation #int temp = #kd temp
scoreboard players operation #int temp /= #100 temp

# frac = fractional part of kd
scoreboard players operation #frac temp = #kd temp
scoreboard players operation #frac temp %= #100 temp

# need zero?
execute if score #frac temp matches ..9 run scoreboard players set #leading_zero temp 1
execute if score #frac temp matches 10..99 run scoreboard players set #leading_zero temp 0

execute if score @s total_deaths matches 0 run scoreboard players operation #int temp = @s total_kills
execute if score @s total_deaths matches 0 run scoreboard players set #frac temp 0
execute if score @s total_deaths matches 0 run scoreboard players set #leading_zero temp 0

execute if score #leading_zero temp matches 1 run tellraw @s ["",{"selector":"@s","bold":true,"underlined":true,"color":"yellow"},{"text":"'s Stats","bold":true,"underlined":true,"color":"yellow"},{"text":"\n\n"},{"text":"Kills: ","color":"gold"},{"score":{"name":"@s","objective":"total_kills"},"bold":true,"color":"white"},{"text":"\n\n"},{"text":"Deaths: ","color":"gold"},{"score":{"name":"@s","objective":"total_deaths"},"bold":true,"color":"white"},{"text":"\n\n"},{"text":"K/D: ","color":"gold"},{"score":{"name":"#int","objective":"temp"},"bold":true,"color":"white"},{"text":".0","bold":true,"color":"white"},{"score":{"name":"#frac","objective":"temp"},"bold":true,"color":"white"},{"text":"\n\n"},{"text":"Wins: ","color":"gold"},{"score":{"name":"@s","objective":"total_wins"},"bold":true},{"text":"\n\n"},{"text":"Games Played: ","color":"gold"},{"score":{"name":"@s","objective":"total_games_played"},"bold":true}]
execute if score #leading_zero temp matches 0 run tellraw @s ["",{"selector":"@s","bold":true,"underlined":true,"color":"yellow"},{"text":"'s Stats","bold":true,"underlined":true,"color":"yellow"},{"text":"\n\n"},{"text":"Kills: ","color":"gold"},{"score":{"name":"@s","objective":"total_kills"},"bold":true,"color":"white"},{"text":"\n\n"},{"text":"Deaths: ","color":"gold"},{"score":{"name":"@s","objective":"total_deaths"},"bold":true,"color":"white"},{"text":"\n\n"},{"text":"K/D: ","color":"gold"},{"score":{"name":"#int","objective":"temp"},"bold":true,"color":"white"},{"text":".","bold":true,"color":"white"},{"score":{"name":"#frac","objective":"temp"},"bold":true,"color":"white"},{"text":"\n\n"},{"text":"Wins: ","color":"gold"},{"score":{"name":"@s","objective":"total_wins"},"bold":true},{"text":"\n\n"},{"text":"Games Played: ","color":"gold"},{"score":{"name":"@s","objective":"total_games_played"},"bold":true}]

playsound minecraft:entity.player.levelup player @s ~ ~ ~ 0.5 2

execute if entity @s[tag=red_stat_temp] run team join red
execute if entity @s[tag=blue_stat_temp] run team join blue

tag @s remove red_stat_temp
tag @s remove blue_stat_temp

particle dust{color:[1.000,0.902,0.251],scale:1} ~ ~-0.5 ~ 0.25 0.25 0.25 1 15 normal @s

scoreboard players set @s item_cooldown 50