#give temp tag
tag * remove tempinteract
tag @s remove temp
tag @s add tempinteract




#check compatibility
scoreboard players set @e[tag=dummy] interact_succeed 0



#check rare exception, if opening card is a wild card
#this needs to be checked first, as it will fail if its not the opening card
execute @e[scores={tableorder=0},tag=wild] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[scores={tableorder=1}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 0




#check compatible colors
scoreboard players set @e[tag=dummy] tablecompatible 0
execute @e[scores={tableorder=0},tag=red] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 1
execute @e[scores={tableorder=0},tag=blue] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 2
execute @e[scores={tableorder=0},tag=green] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 3
execute @e[scores={tableorder=0},tag=yellow] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 4

scoreboard players set @e[tag=dummy] playcompatible 0
execute @e[tag=tempinteract,tag=red] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 1
execute @e[tag=tempinteract,tag=blue] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 2
execute @e[tag=tempinteract,tag=green] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 3
execute @e[tag=tempinteract,tag=yellow] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 4

execute @e[tag=dummy,scores={tablecompatible=1,playcompatible=1}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=2,playcompatible=2}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=3,playcompatible=3}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=4,playcompatible=4}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1





#check compatible type
scoreboard players set @e[tag=dummy] tablecompatible 0
execute @e[scores={tableorder=0},tag=number0] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 1
execute @e[scores={tableorder=0},tag=number1] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 2
execute @e[scores={tableorder=0},tag=number2] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 3
execute @e[scores={tableorder=0},tag=number3] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 4
execute @e[scores={tableorder=0},tag=number4] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 5
execute @e[scores={tableorder=0},tag=number5] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 6
execute @e[scores={tableorder=0},tag=number6] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 7
execute @e[scores={tableorder=0},tag=number7] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 8
execute @e[scores={tableorder=0},tag=number8] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 9
execute @e[scores={tableorder=0},tag=number9] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 10
execute @e[scores={tableorder=0},tag=reverse] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 11
execute @e[scores={tableorder=0},tag=globe] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 12
execute @e[scores={tableorder=0},tag=plus2] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 13


scoreboard players set @e[tag=dummy] playcompatible 0
execute @e[tag=tempinteract,tag=number0] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 1
execute @e[tag=tempinteract,tag=number1] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 2
execute @e[tag=tempinteract,tag=number2] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 3
execute @e[tag=tempinteract,tag=number3] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 4
execute @e[tag=tempinteract,tag=number4] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 5
execute @e[tag=tempinteract,tag=number5] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 6
execute @e[tag=tempinteract,tag=number6] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 7
execute @e[tag=tempinteract,tag=number7] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 8
execute @e[tag=tempinteract,tag=number8] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 9
execute @e[tag=tempinteract,tag=number9] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 10
execute @e[tag=tempinteract,tag=reverse] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 11
execute @e[tag=tempinteract,tag=globe] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 12
execute @e[tag=tempinteract,tag=plus2] ~ ~ ~ scoreboard players set @e[tag=dummy] playcompatible 13


execute @e[tag=dummy,scores={tablecompatible=1,playcompatible=1}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=2,playcompatible=2}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=3,playcompatible=3}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=4,playcompatible=4}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=5,playcompatible=5}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=6,playcompatible=6}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=7,playcompatible=7}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=8,playcompatible=8}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=9,playcompatible=9}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=10,playcompatible=10}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=11,playcompatible=11}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=12,playcompatible=12}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1
execute @e[tag=dummy,scores={tablecompatible=13,playcompatible=13}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1




#wild card always succeeds
execute @e[tag=tempinteract,tag=wild] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1








#wild draw four card fits on everything, but only if no other card can be played, 0=the player has compatible cards, 1=the player has no compatible cards
scoreboard players set @e[tag=dummy] deckscan 1



#give activecards tag to the cards that associate with the player who's currently playing
tag * remove activecard
execute @e[tag=dummy,scores={turn=1}] ~ ~ ~ tag @e[type=cero:card,tag=player1] add activecard
execute @e[tag=dummy,scores={turn=2}] ~ ~ ~ tag @e[type=cero:card,tag=player2] add activecard
execute @e[tag=dummy,scores={turn=3}] ~ ~ ~ tag @e[type=cero:card,tag=player3] add activecard
execute @e[tag=dummy,scores={turn=4}] ~ ~ ~ tag @e[type=cero:card,tag=player4] add activecard



#check rare exception, if opening card is a wild card, and then check if the player has any cards that isnt a wild draw four
execute @e[scores={tableorder=0},tag=wild] ~ ~ ~ execute @e[tag=dummy] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=red] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[scores={tableorder=0},tag=wild] ~ ~ ~ execute @e[tag=dummy] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=blue] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[scores={tableorder=0},tag=wild] ~ ~ ~ execute @e[tag=dummy] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=green] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[scores={tableorder=0},tag=wild] ~ ~ ~ execute @e[tag=dummy] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=yellow] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[scores={tableorder=0},tag=wild] ~ ~ ~ execute @e[tag=dummy] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=wild] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0




#now check if table card has matching colors
execute @e[scores={tableorder=0},tag=red] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 1
execute @e[scores={tableorder=0},tag=blue] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 2
execute @e[scores={tableorder=0},tag=green] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 3
execute @e[scores={tableorder=0},tag=yellow] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 4


#check if player has that color
execute @e[tag=dummy,scores={tablecompatible=1}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=red] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=2}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=blue] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=3}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=green] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=4}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=yellow] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0



#now check if table card has a matching number or type
execute @e[scores={tableorder=0},tag=number0] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 1
execute @e[scores={tableorder=0},tag=number1] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 2
execute @e[scores={tableorder=0},tag=number2] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 3
execute @e[scores={tableorder=0},tag=number3] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 4
execute @e[scores={tableorder=0},tag=number4] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 5
execute @e[scores={tableorder=0},tag=number5] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 6
execute @e[scores={tableorder=0},tag=number6] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 7
execute @e[scores={tableorder=0},tag=number7] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 8
execute @e[scores={tableorder=0},tag=number8] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 9
execute @e[scores={tableorder=0},tag=number9] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 10
execute @e[scores={tableorder=0},tag=reverse] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 11
execute @e[scores={tableorder=0},tag=globe] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 12
execute @e[scores={tableorder=0},tag=plus2] ~ ~ ~ scoreboard players set @e[tag=dummy] tablecompatible 13


#check if player has that number or type
execute @e[tag=dummy,scores={tablecompatible=1}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number0] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=2}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number1] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=3}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number2] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=4}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number3] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=5}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number4] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=6}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number5] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=7}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number6] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=8}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number7] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=9}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number8] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=10}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=number9] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=11}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=reverse] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=12}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=globe] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0
execute @e[tag=dummy,scores={tablecompatible=13}] ~ ~ ~ execute @e[type=cero:card,tag=activecard,tag=plus2] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0


#finally check if player has a wild card, since that card can always be played
execute @e[tag=activecard,tag=wild] ~ ~ ~ scoreboard players set @e[tag=dummy] deckscan 0


#remove activecard tag
tag @e[tag=activecard] remove activecard


#check if its a wild draw four card, and then if its compatible
execute @e[tag=tempinteract,tag=plus4] ~ ~ ~ execute @e[tag=dummy,scores={deckscan=1}] ~ ~ ~ scoreboard players set @e[tag=dummy] interact_succeed 1






#check if card go to deck or choose color
tag @e remove choosecolor
tag @e[tag=tempinteract,tag=plus4] add choosecolor
tag @e[tag=tempinteract,tag=wild] add choosecolor



#tp buttons downwards
execute @e[tag=dummy,scores={interact_succeed=1}] ~ ~ ~ execute @e[type=cero:button,tag=button] ~ ~ ~ tp @s ~ 24 ~



#fail or succeed functions
execute @e[tag=dummy,scores={interact_succeed=0}] ~ ~ ~ execute @e[tag=tempinteract,tag=!fromdrawbutton] ~ ~ ~ function card_interact/failed
execute @e[tag=dummy,scores={interact_succeed=0}] ~ ~ ~ execute @e[tag=tempinteract,tag=fromdrawbutton] ~ ~ ~ function button_interact/play_failed
execute @e[tag=tempinteract,tag=!choosecolor] ~ ~ ~ execute @e[tag=dummy,scores={interact_succeed=1}] ~ ~ ~ function card_interact/succeed_go_to_table
execute @e[tag=tempinteract,tag=choosecolor] ~ ~ ~ execute @e[tag=dummy,scores={interact_succeed=1}] ~ ~ ~ function card_interact/succeed_choose_color


#remove drawing tag
execute @e[tag=tempinteract,tag=!choosecolor] ~ ~ ~ execute @e[tag=dummy,scores={interact_succeed=1}] ~ ~ ~ tag @a[tag=drawing] remove drawing
execute @e[tag=tempinteract,tag=choosecolor] ~ ~ ~ execute @e[tag=dummy,scores={interact_succeed=1}] ~ ~ ~ tag @a[tag=drawing] remove drawing





#remove tempinteract tag
tag * remove fromdrawbutton
tag * remove tempinteract




