extends Node

# Server-authoritative kill feed. Unlike ChatManager, a death is only ever
# decided on the server (see ships/ship_movement.gd's _die()), so there's no
# client -> server leg here -- just one broadcast RPC per kill.

signal kill_reported(victim_name: String, victim_race: int, killer_name: String, killer_race: int, is_environment_kill: bool)


# source_peer_id == 0 means no attacking ship (hazard/wall collision damage,
# see ship_movement.gd's take_damage()/_apply_collision_bounce()).
func report_kill(victim_peer_id: int, killer_peer_id: int) -> void:
	if not multiplayer.is_server():
		return

	var victim_info := PlayerRegistry.get_info(victim_peer_id)
	var victim_name: String = victim_info.get("name", "Player")
	var victim_race: int = victim_info.get("race", 0)

	var is_environment_kill := killer_peer_id == 0 or killer_peer_id == victim_peer_id
	var killer_name := ""
	var killer_race := 0
	if not is_environment_kill:
		var killer_info := PlayerRegistry.get_info(killer_peer_id)
		killer_name = killer_info.get("name", "Player")
		killer_race = killer_info.get("race", 0)

	_receive_kill.rpc(victim_name, victim_race, killer_name, killer_race, is_environment_kill)


@rpc("authority", "call_local", "reliable")
func _receive_kill(victim_name: String, victim_race: int, killer_name: String, killer_race: int, is_environment_kill: bool) -> void:
	kill_reported.emit(victim_name, victim_race, killer_name, killer_race, is_environment_kill)
