execute @e[tag=dummy,scores={startanimation=1..}] ~ ~ ~ function startanimation



#schedule card texture
scoreboard players add @e[scores={scheduletexture=0..}] scheduletexture 1
execute @e[scores={scheduletexture=8}] ~ ~ ~ function givecardtexture



#invisdelay
scoreboard players add @e[tag=choosecolor,scores={invisdelay=0..}] invisdelay 1
execute @e[tag=choosecolor,scores={invisdelay=5..}] ~ ~ ~ tp @s 38 -100 -38
execute @e[tag=choosecolor,scores={invisdelay=5..}] ~ ~ ~ event entity @s dummy_invisible
execute @e[tag=choosecolor,scores={invisdelay=5..}] ~ ~ ~ kill @s


#choosecolordelay
scoreboard players add @a[scores={choosecolordelay=0..}] choosecolordelay 1
execute @e[tag=wild,tag=selectingcolors] ~ ~ ~ execute @a[scores={choosecolordelay=8}] ~ ~ ~ function card_interact/choosecolor_wild
execute @e[tag=plus4,tag=selectingcolors] ~ ~ ~ execute @a[scores={choosecolordelay=8}] ~ ~ ~ function card_interact/choosecolor_plus4
scoreboard players reset @a[scores={choosecolordelay=10}] choosecolordelay


#wait for tp delay
scoreboard players add @e[scores={tpdelay=0..}] tpdelay 1
scoreboard players reset @e[scores={tpdelay=8}] tpdelay
tp @e[scores={tpdelay=7},tag=choose_red1] 40.0 27.7 -43.5
tp @e[scores={tpdelay=7},tag=choose_blue1] 39.0 27.7 -43.5
tp @e[scores={tpdelay=7},tag=choose_green1] 38.0 27.7 -43.5
tp @e[scores={tpdelay=7},tag=choose_yellow1] 37.0 27.7 -43.5
execute @e[scores={tpdelay=7},tag=choose_yellow1] ~ ~ ~ tp @e[tag=selectingcolors] 0 -1000 0
execute @e[scores={tpdelay=7},tag=choose_yellow1] ~ ~ ~ kill @e[tag=selectingcolors]



#count cards
function count_cards

#hastodraw delay
scoreboard players add @a[scores={hastodraw=0..50}] hastodrawdelay 1
execute @e[scores={deckcount=1..}] ~ ~ ~ execute @a[tag=turn,scores={player_id=1,hastodrawdelay=15}] ~ ~ ~ function draw/player1_drawcard
execute @e[scores={deckcount=1..}] ~ ~ ~ execute @a[tag=turn,scores={player_id=2,hastodrawdelay=15}] ~ ~ ~ function draw/player2_drawcard
execute @e[scores={deckcount=1..}] ~ ~ ~ execute @a[tag=turn,scores={player_id=3,hastodrawdelay=15}] ~ ~ ~ function draw/player3_drawcard
execute @e[scores={deckcount=1..}] ~ ~ ~ execute @a[tag=turn,scores={player_id=4,hastodrawdelay=15}] ~ ~ ~ function draw/player4_drawcard


execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=1,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @e[tag=dummy] reshuffle_pause 6
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=2,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @e[tag=dummy] reshuffle_pause 7
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=3,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @e[tag=dummy] reshuffle_pause 8
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=4,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @e[tag=dummy] reshuffle_pause 9

execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=1,hastodrawdelay=15}] ~ ~ ~ function reshuffle
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=2,hastodrawdelay=15}] ~ ~ ~ function reshuffle
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=3,hastodrawdelay=15}] ~ ~ ~ function reshuffle
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turn,scores={player_id=4,hastodrawdelay=15}] ~ ~ ~ function reshuffle

execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turnpaused,scores={player_id=1,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @s hastodrawdelay 100
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turnpaused,scores={player_id=2,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @s hastodrawdelay 100
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turnpaused,scores={player_id=3,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @s hastodrawdelay 100
execute @e[scores={deckcount=0}] ~ ~ ~ execute @a[tag=turnpaused,scores={player_id=4,hastodrawdelay=15}] ~ ~ ~ scoreboard players set @s hastodrawdelay 100


#afterdraw
execute @a[tag=turn,scores={hastodrawdelay=35}] ~ ~ ~ function nextturn/afterdraw





#stop buttons
scoreboard players add @a stopbutton 0
scoreboard players remove @a[scores={stopbutton=1..}] stopbutton 1

execute @a[scores={stopbutton=1..,player_id=1}] ~ ~ ~ detect 33 28 -45 minecraft:mangrove_button 10 function stop_button_confirm
execute @a[scores={stopbutton=1..,player_id=2}] ~ ~ ~ detect 43 28 -33 minecraft:mangrove_button 11 function stop_button_confirm
execute @a[scores={stopbutton=1..,player_id=3}] ~ ~ ~ detect 44 28 -44 minecraft:mangrove_button 13 function stop_button_confirm
execute @a[scores={stopbutton=1..,player_id=4}] ~ ~ ~ detect 32 28 -34 minecraft:mangrove_button 12 function stop_button_confirm

execute @a[scores={stopbutton=0,player_id=1}] ~ ~ ~ detect 33 28 -45 minecraft:mangrove_button 10 function stop_button_request
execute @a[scores={stopbutton=0,player_id=2}] ~ ~ ~ detect 43 28 -33 minecraft:mangrove_button 11 function stop_button_request
execute @a[scores={stopbutton=0,player_id=3}] ~ ~ ~ detect 44 28 -44 minecraft:mangrove_button 13 function stop_button_request
execute @a[scores={stopbutton=0,player_id=4}] ~ ~ ~ detect 32 28 -34 minecraft:mangrove_button 12 function stop_button_request

setblock 33 28 -45 minecraft:mangrove_button 2
setblock 43 28 -33 minecraft:mangrove_button 3
setblock 44 28 -44 minecraft:mangrove_button 5
setblock 32 28 -34 minecraft:mangrove_button 4




#hastodraw tag
tag * remove hastodraw
tag @a[scores={hastodraw=0..}] add hastodraw

#draw buttons
execute @e[tag=dummy,scores={hastodraw=0}] ~ ~ ~ execute @a[tag=!hastodraw,tag=!drawing,tag=turn,scores={player_id=1}] ~ ~ ~ detect 43 28 -45 minecraft:mangrove_button 10 function draw/player1_pressbutton
execute @e[tag=dummy,scores={hastodraw=0}] ~ ~ ~ execute @a[tag=!hastodraw,tag=!drawing,tag=turn,scores={player_id=2}] ~ ~ ~ detect 33 28 -33 minecraft:mangrove_button 11 function draw/player2_pressbutton
execute @e[tag=dummy,scores={hastodraw=0}] ~ ~ ~ execute @a[tag=!hastodraw,tag=!drawing,tag=turn,scores={player_id=3}] ~ ~ ~ detect 44 28 -34 minecraft:mangrove_button 13 function draw/player3_pressbutton
execute @e[tag=dummy,scores={hastodraw=0}] ~ ~ ~ execute @a[tag=!hastodraw,tag=!drawing,tag=turn,scores={player_id=4}] ~ ~ ~ detect 32 28 -44 minecraft:mangrove_button 12 function draw/player4_pressbutton

#fix draw button
setblock 43 28 -45 minecraft:mangrove_button 2
setblock 33 28 -33 minecraft:mangrove_button 3
setblock 44 28 -34 minecraft:mangrove_button 5
setblock 32 28 -44 minecraft:mangrove_button 4




#quit game when player leaves
tag @e[tag=dummy] add error_playercount
execute @e[tag=dummy,scores={playercount=2}] ~ ~ ~ execute @a[scores={player_id=1}] ~ ~ ~ execute @a[scores={player_id=2}] ~ ~ ~ tag @e[tag=dummy] remove error_playercount
execute @e[tag=dummy,scores={playercount=3}] ~ ~ ~ execute @a[scores={player_id=1}] ~ ~ ~ execute @a[scores={player_id=2}] ~ ~ ~ execute @a[scores={player_id=3}] ~ ~ ~ tag @e[tag=dummy] remove error_playercount
execute @e[tag=dummy,scores={playercount=4}] ~ ~ ~ execute @a[scores={player_id=1}] ~ ~ ~ execute @a[scores={player_id=2}] ~ ~ ~ execute @a[scores={player_id=3}] ~ ~ ~ execute @a[scores={player_id=4}] ~ ~ ~ tag @e[tag=dummy] remove error_playercount
execute @e[tag=dummy,tag=error_playercount,scores={ingame=1}] ~ ~ ~ function error_pause



#wingame delay
scoreboard players remove @e[tag=dummy,scores={stopdelay=1..}] stopdelay 1
execute @e[tag=dummy,scores={stopdelay=135}] ~ ~ ~ setblock 42 28 -58 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=130}] ~ ~ ~ setblock 9 29 -10 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=125}] ~ ~ ~ setblock 51 28 -26 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=120}] ~ ~ ~ setblock 15 29 -51 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=115}] ~ ~ ~ setblock 63 30 -50 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=110}] ~ ~ ~ setblock 32 29 -16 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=105}] ~ ~ ~ setblock 67 30 -36 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=100}] ~ ~ ~ setblock 9 35 -54 minecraft:redstone_block



execute @e[tag=dummy,scores={stopdelay=85}] ~ ~ ~ setblock 9 29 -10 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=80}] ~ ~ ~ setblock 9 35 -54 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=75}] ~ ~ ~ setblock 63 30 -50 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=70}] ~ ~ ~ setblock 67 30 -36 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=65}] ~ ~ ~ setblock 15 29 -51 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=60}] ~ ~ ~ setblock 42 28 -58 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=55}] ~ ~ ~ setblock 51 28 -26 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=50}] ~ ~ ~ setblock 32 29 -16 minecraft:redstone_block
execute @e[tag=dummy,scores={stopdelay=0}] ~ ~ ~ function stop



#offset random table
scoreboard players add @e[tag=dummy] x_offset 1
scoreboard players set @e[tag=dummy,scores={x_offset=8..}] x_offset 1

scoreboard players add @e[tag=dummy] z_offset 4
scoreboard players remove @e[tag=dummy,scores={z_offset=8..}] z_offset 7

scoreboard players add @e[tag=dummy] rot_offset 3
scoreboard players remove @e[tag=dummy,scores={rot_offset=8..}] rot_offset 7


#afkalarm
tag @e[tag=afkalarm] add afktest
execute @a[tag=turn] ^ ^ ^-1 execute @e[tag=afkalarm,r=0.01] ~ ~ ~ tag @s remove afktest
execute @e[tag=afkalarm,tag=!afktest] ~ ~ ~ scoreboard players add @s afkalarm 1
execute @e[tag=afkalarm,tag=afktest] ~ ~ ~ scoreboard players set @s afkalarm 0

execute @a[tag=turn] ^ ^ ^-1 tp @e[tag=afktest] ~ ~ ~ 
tag @e[tag=afktest] remove afktest

scoreboard players set @e[tag=afkalarm,scores={afkalarm=520..}] afkalarm 500
execute @e[tag=afkalarm,scores={afkalarm=400}] ~ ~ ~ playsound note.bell @a[tag=turn]
execute @e[tag=afkalarm,scores={afkalarm=400}] ~ ~ ~ title @a[tag=turn] times 0 100 0
execute @e[tag=afkalarm,scores={afkalarm=400}] ~ ~ ~ title @a[tag=turn] title §cIt's your turn!
execute @e[tag=afkalarm,scores={afkalarm=500}] ~ ~ ~ playsound note.bell @a[tag=turn]
execute @e[tag=afkalarm,scores={afkalarm=500}] ~ ~ ~ title @a[tag=turn] times 0 15 0
execute @e[tag=afkalarm,scores={afkalarm=500}] ~ ~ ~ title @a[tag=turn] title §cIt's your turn!


#afkalarm2
execute @a[tag=!turn,scores={afkalarm2=0..}] ~ ~ ~ titleraw @s actionbar {"rawtext": [{"text": " "}]}
scoreboard players reset @a[tag=!turn] afkalarm2
scoreboard players add @a[tag=turn] afkalarm2 1
execute @a[tag=turn,scores={afkalarm2=410}] ~ ~ ~ titleraw @s actionbar {"rawtext": [{"text": "§fIt's your turn!"}]}
execute @a[tag=turn,scores={afkalarm2=420}] ~ ~ ~ titleraw @s actionbar {"rawtext": [{"text": "§cIt's your turn!"}]}
scoreboard players set @a[tag=turn,scores={afkalarm2=420..}] afkalarm2 400







#reshuffle
scoreboard players add @e[tag=dummy,scores={reshuffle=1..}] reshuffle 1
tag @e[tag=dummy] add remainingtemp
execute @e[tag=dummy,scores={reshuffle=5}] ~ ~ ~ execute @e[scores={tableorder=1}] ~ ~ ~ tag @e[tag=dummy] remove remainingtemp
execute @e[tag=dummy,scores={reshuffle=5}] ~ ~ ~ execute @e[tag=dummy,tag=!remainingtemp] ~ ~ ~ execute @e[tag=table,scores={tableorder=1}] ~ ~ ~ function reshuffle2

execute @e[tag=dummy,scores={reshuffle=5}] ~ ~ ~ execute @e[tag=dummy,tag=remainingtemp] ~ ~ ~ scoreboard players set @s reshuffle 10
execute @e[tag=dummy,scores={reshuffle=10..}] ~ ~ ~ function reshuffle3


tag @e[tag=dummy] remove remainingtemp
scoreboard players set @e[tag=dummy,scores={reshuffle=5}] reshuffle 1




#blockinteract
tag @a[tag=spectator] add no_block
execute @e[tag=blockinteract] ~ ~-1.5 ~ execute @a[tag=spectator,r=1] ~ ~ ~ tag @s remove no_block
execute @a[tag=no_block] ~ ~ ~ function blockinteract_spawn
tag @a remove no_block

