## The kill feed and the count of who is standing, on the wire, for the HUD (A9). ## Snapshots only carry players near the one a client watches, so the client can't ## count the living or see eliminations across the island by itself; the server tells ## everyone with a FEED message on the control channel (reliable) whenever someone is ## placed, and when a match begins. ## ## server -> FEED ## u8 type, u8 standing, u8 players in the match, u8 entry count, then entries: ## s32 victim handle, s32 eliminator handle (0 = none), u8 cause (CAUSES), ## u8 item code plus one (0 = none), u8 place, u8 flags (1 = tied, 2 = downed, ## not out: duos, M7), ## str victim name, str eliminator name, ## where str is u8 byte length then that many UTF-8 bytes (at most MAX_NAME_BYTES). ## ## The decoder returns an empty Dictionary for anything malformed and never raises. class_name NetFeed extends RefCounted const MSG_FEED: int = 19 ## How an entry came about, by the Match's cause names. const CAUSES: Array[String] = ["none", "weapon", "fall", "drown", "tide", "left"] const HEADER_BYTES: int = 4 const ENTRY_FIXED_BYTES: int = 12 const MAX_ENTRIES: int = 16 const MAX_NAME_BYTES: int = 32 const FLAG_TIED: int = 1 const FLAG_DOWNED: int = 2 ## Encodes the count and `entries`, newest last. Each entry has "victim", "by", ## "cause" (a name in CAUSES), "item" (-1 none), "place", "tied", "downed", "victim_name" ## and "by_name". Only the newest MAX_ENTRIES are kept. static func encode(standing: int, total: int, entries: Array[Dictionary]) -> PackedByteArray: var out: StreamPeerBuffer = StreamPeerBuffer.new() out.put_u8(MSG_FEED) out.put_u8(clampi(standing, 0, 255)) out.put_u8(clampi(total, 0, 255)) var first: int = maxi(0, entries.size() - MAX_ENTRIES) out.put_u8(entries.size() - first) for index: int in range(first, entries.size()): var entry: Dictionary = entries[index] out.put_32(int(entry.get("victim", 0))) out.put_32(int(entry.get("by", 0))) out.put_u8(maxi(0, CAUSES.find(String(entry.get("cause", "none"))))) out.put_u8(clampi(int(entry.get("item", -1)) + 1, 0, 255)) out.put_u8(clampi(int(entry.get("place", 0)), 0, 255)) out.put_u8((FLAG_TIED if bool(entry.get("tied", false)) else 0) | (FLAG_DOWNED if bool(entry.get("downed", false)) else 0)) _put_name(out, String(entry.get("victim_name", ""))) _put_name(out, String(entry.get("by_name", ""))) return out.data_array ## {"standing", "total", "entries": Array[Dictionary]} with the fields encode() takes, ## or an empty Dictionary. static func decode(bytes: PackedByteArray) -> Dictionary: if bytes.size() < HEADER_BYTES or bytes[0] != MSG_FEED: return {} var count: int = bytes[3] if count > MAX_ENTRIES: return {} var entries: Array[Dictionary] = [] var at: int = HEADER_BYTES for _index: int in count: if at + ENTRY_FIXED_BYTES > bytes.size(): return {} var cause: int = bytes[at + 8] if cause >= CAUSES.size(): return {} var entry: Dictionary = { "victim": bytes.decode_s32(at), "by": bytes.decode_s32(at + 4), "cause": CAUSES[cause], "item": int(bytes[at + 9]) - 1, "place": int(bytes[at + 10]), "tied": bytes[at + 11] & FLAG_TIED != 0, "downed": bytes[at + 11] & FLAG_DOWNED != 0, } at += ENTRY_FIXED_BYTES var names: Array[String] = [] for _name: int in 2: if at >= bytes.size(): return {} var length: int = bytes[at] if length > MAX_NAME_BYTES or at + 1 + length > bytes.size(): return {} names.append(bytes.slice(at + 1, at + 1 + length).get_string_from_utf8()) at += 1 + length entry["victim_name"] = names[0] entry["by_name"] = names[1] entries.append(entry) if at != bytes.size(): return {} return {"standing": int(bytes[1]), "total": int(bytes[2]), "entries": entries} static func _put_name(out: StreamPeerBuffer, text: String) -> void: var raw: PackedByteArray = text.to_utf8_buffer() # Cut on a character boundary, never inside one. while raw.size() > MAX_NAME_BYTES: text = text.substr(0, text.length() - 1) raw = text.to_utf8_buffer() out.put_u8(raw.size()) out.put_data(raw)