tribeId(); $theirs = $b?->tribeId(); if ($mine === null || $theirs === null || $mine === $theirs) { return Diplomacy::relation($mine, $theirs, [], $now); } $rows = TribeRelation::where(fn ($q) => $q->where('tribe_id', $mine)->where('other_tribe_id', $theirs)) ->orWhere(fn ($q) => $q->where('tribe_id', $theirs)->where('other_tribe_id', $mine)) ->get()->map->row(); return Diplomacy::relation($mine, $theirs, $rows, $now); } /** * Every other tribe this one has a relation with: tribe id => relation. * * @return array */ public static function relationsOf(?int $tribeId, CarbonImmutable $now): array { if ($tribeId === null) { return []; } $rows = TribeRelation::where('tribe_id', $tribeId)->orWhere('other_tribe_id', $tribeId)->get()->map->row(); $out = []; foreach ($rows as $row) { $other = $row['tribe_id'] === $tribeId ? $row['other_tribe_id'] : $row['tribe_id']; $out[$other] ??= Diplomacy::relation($tribeId, $other, $rows, $now); } return array_filter($out, fn (string $r) => $r !== Diplomacy::NONE); } /** Drop pacts whose notice has run out. */ public static function prune(int $tribeId, CarbonImmutable $now): void { TribeRelation::where(fn ($q) => $q->where('tribe_id', $tribeId)->orWhere('other_tribe_id', $tribeId)) ->whereNotNull('ends_at')->where('ends_at', '<=', Engine::stamp($now))->delete(); } }