﻿# GORA UI+ — Improved Parliament Graph: on-action hooks
#
# All recalcs are gated by `is_player = yes` so work is limited to the
# human player's country. This is what keeps the mod safe for multiplayer:
# each client runs the same calc on its own scope, producing deterministic
# variables that survive a save/load identically across clients.

# Initial seed and slow heartbeat. `on_monthly_pulse_country` is a registered
# vanilla code on-action with Country root. The date guard skips 1 January's
# world-construction pass, where party / interest-group ownership links are not
# ready yet. The first normal monthly pulse initializes the cache and every
# later monthly pulse refreshes it.
on_monthly_pulse_country = {
	on_actions = { ipg_on_player_monthly }
}

# Snap update right after an election concludes — seat counts can swing hard.
on_election_campaign_end = {
	on_actions = { ipg_on_player_recalc }
}

# ─── Bridges (scope guard + dispatch) ─────────────────────────────────────

ipg_on_player_monthly = {
	effect = {
		if = {
			limit = {
				is_player = yes
				game_date >= 1836.1.2
				NOT = { has_variable = ipg_last_refresh_day }
			}
			if = {
				limit = {
					NOT = { has_variable = ipg_initialized }
				}
				set_variable = { name = ipg_initialized value = 1 }
			}
			ipg_recalculate_parliament = yes
		}
	}
}

ipg_on_player_recalc = {
	effect = {
		if = {
			limit = { 
				AND = {
					is_player = yes 
					has_variable = ipg_initialized
					NOT = { has_variable = ipg_last_refresh_day }
				}
			}
			ipg_recalculate_parliament = yes
		}
	}
}

# Do not subscribe to on_law_activated. During world initialization Victoria
# 3 1.13.9 dispatches that hook with a null law root. The monthly country pulse
# and election-end refresh keep all three views current without touching the
# invalid startup scope.
