## pctf:logic/game/main2.mcfunction
# this handles game logic and checks when to end the game

# game time
$scoreboard players add #tick_time game$(map_id) 1

$execute if score #tick_time game$(map_id) matches 1200 run function pctf:logic/game/update_time {map_id: "$(map_id)"}

# check for if player leaves or flag is missing, then put flag back

$execute unless entity @n[type=block_display,tag=aqua_flag,scores={map_id=$(map_id)}] unless entity @a[tag=has_aqua_flag,scores={on_map=$(map_id)}] unless entity @n[tag=red_flag_dropoff,tag=captured_aqua,scores={map_id=$(map_id)}] run function pctf:create_game_object/flag/aqua {map_id: "$(map_id)"}
$execute unless entity @n[type=block_display,tag=green_flag,scores={map_id=$(map_id)}] unless entity @a[tag=has_green_flag,scores={on_map=$(map_id)}] unless entity @n[tag=red_flag_dropoff,tag=captured_green,scores={map_id=$(map_id)}] run function pctf:create_game_object/flag/green {map_id: "$(map_id)"}
$execute unless entity @n[type=block_display,tag=yellow_flag,scores={map_id=$(map_id)}] unless entity @a[tag=has_yellow_flag,scores={on_map=$(map_id)}] unless entity @n[tag=blue_flag_dropoff,tag=captured_yellow,scores={map_id=$(map_id)}] run function pctf:create_game_object/flag/yellow {map_id: "$(map_id)"}
$execute unless entity @n[type=block_display,tag=light_purple_flag,scores={map_id=$(map_id)}] unless entity @a[tag=has_light_purple_flag,scores={on_map=$(map_id)}] unless entity @n[tag=blue_flag_dropoff,tag=captured_light_purple,scores={map_id=$(map_id)}] run function pctf:create_game_object/flag/light_purple {map_id: "$(map_id)"}

# time over

$execute if score #time game$(map_id) matches 0 at @s run function pctf:logic/game/end/draw {map_id: "$(map_id)"}

# seconds display
$execute if score #time game$(map_id) matches 1 run scoreboard players add #tick_second game$(map_id) 1
$execute if score #tick_second game$(map_id) matches 20 run scoreboard players remove #seconds game$(map_id) 1
$execute if score #tick_second game$(map_id) matches 20.. run scoreboard players set #tick_second game$(map_id) 0

$execute if score #tick_time game$(map_id) matches 1200.. run scoreboard players set #tick_time game$(map_id) 0

# game wins

$execute if score .elimination settings matches 1 if entity @e[tag=blue_flag_dropoff,tag=captured_light_purple,tag=captured_yellow,scores={map_id=$(map_id)}] unless entity @a[tag=!dead,team=red] run function pctf:logic/game/end/blue_wins {map_id: "$(map_id)"}
$execute if score .elimination settings matches 1 if entity @e[tag=red_flag_dropoff,tag=captured_green,tag=captured_aqua,scores={map_id=$(map_id)}] unless entity @a[tag=!dead,team=blue] run function pctf:logic/game/end/red_wins {map_id: "$(map_id)"}

$execute if score .elimination settings matches 0 if entity @e[tag=blue_flag_dropoff,tag=captured_light_purple,tag=captured_yellow,scores={map_id=$(map_id)}] run function pctf:logic/game/end/blue_wins {map_id: "$(map_id)"}
$execute if score .elimination settings matches 0 if entity @e[tag=red_flag_dropoff,tag=captured_green,tag=captured_aqua,scores={map_id=$(map_id)}] run function pctf:logic/game/end/red_wins {map_id: "$(map_id)"}


# count total players

$execute store result score #red_players game$(map_id) run execute if entity @a[team=red]
$execute store result score #blue_players game$(map_id) run execute if entity @a[team=blue]

execute if entity @s[tag=!in_game] run return 0

# check if there are enough players for a game, if not then end game

$execute if score #red_players game$(map_id) matches 0 unless entity @a[tag=ignore_not_enough_players] run function pctf:logic/game/end/not_enough_players {map_id: "$(map_id)"}
$execute if score #blue_players game$(map_id) matches 0 unless entity @a[tag=ignore_not_enough_players] run function pctf:logic/game/end/not_enough_players {map_id: "$(map_id)"}