merge: observe selected diplomacy pairs
This commit is contained in:
@@ -115,6 +115,36 @@ function comparisonNationCooldownSelectors(mixed $value, string $label): array
|
|||||||
return array_values($result);
|
return array_values($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @return list<array{fromNationId: int, toNationId: int}> */
|
||||||
|
function comparisonDiplomacyPairSelectors(mixed $value, string $label): array
|
||||||
|
{
|
||||||
|
if ($value === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
if (!is_array($value)) {
|
||||||
|
throw new \InvalidArgumentException("{$label} must be an array");
|
||||||
|
}
|
||||||
|
$result = [];
|
||||||
|
foreach ($value as $entry) {
|
||||||
|
if (!is_array($entry)) {
|
||||||
|
throw new \InvalidArgumentException("{$label} entries must be objects");
|
||||||
|
}
|
||||||
|
$fromNationId = $entry['fromNationId'] ?? null;
|
||||||
|
$toNationId = $entry['toNationId'] ?? null;
|
||||||
|
if (!is_int($fromNationId) || $fromNationId < 1 || !is_int($toNationId) || $toNationId < 1) {
|
||||||
|
throw new \InvalidArgumentException(
|
||||||
|
"{$label} entries require positive fromNationId and toNationId",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$result["{$fromNationId}:{$toNationId}"] = [
|
||||||
|
'fromNationId' => $fromNationId,
|
||||||
|
'toNationId' => $toNationId,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
ksort($result, SORT_STRING);
|
||||||
|
return array_values($result);
|
||||||
|
}
|
||||||
|
|
||||||
/** @param array<string, mixed> $row */
|
/** @param array<string, mixed> $row */
|
||||||
function comparisonPickRow(array $row, array $mapping, array $jsonKeys = []): array
|
function comparisonPickRow(array $row, array $mapping, array $jsonKeys = []): array
|
||||||
{
|
{
|
||||||
@@ -168,6 +198,10 @@ function comparisonTurnStateSnapshot(array $request): array
|
|||||||
$observe['nationCooldowns'] ?? [],
|
$observe['nationCooldowns'] ?? [],
|
||||||
'nationCooldowns',
|
'nationCooldowns',
|
||||||
);
|
);
|
||||||
|
$diplomacyPairSelectors = comparisonDiplomacyPairSelectors(
|
||||||
|
$observe['diplomacyPairs'] ?? [],
|
||||||
|
'diplomacyPairs',
|
||||||
|
);
|
||||||
if (!is_int($logAfterId) || $logAfterId < 0 || !is_int($messageAfterId) || $messageAfterId < 0) {
|
if (!is_int($logAfterId) || $logAfterId < 0 || !is_int($messageAfterId) || $messageAfterId < 0) {
|
||||||
throw new \InvalidArgumentException('logAfterId and messageAfterId must be non-negative integers');
|
throw new \InvalidArgumentException('logAfterId and messageAfterId must be non-negative integers');
|
||||||
}
|
}
|
||||||
@@ -363,26 +397,38 @@ function comparisonTurnStateSnapshot(array $request): array
|
|||||||
comparisonRowsById('nation', 'nation', $nationIds),
|
comparisonRowsById('nation', 'nation', $nationIds),
|
||||||
);
|
);
|
||||||
|
|
||||||
$diplomacy = [];
|
$diplomacyPairs = [];
|
||||||
foreach ($nationIds as $fromNationId) {
|
foreach ($nationIds as $fromNationId) {
|
||||||
foreach ($nationIds as $toNationId) {
|
foreach ($nationIds as $toNationId) {
|
||||||
if ($fromNationId === $toNationId) {
|
if ($fromNationId === $toNationId) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$row = $db->queryFirstRow(
|
$diplomacyPairs["{$fromNationId}:{$toNationId}"] = [
|
||||||
'SELECT me, you, state, term, dead FROM diplomacy WHERE me = %i AND you = %i',
|
'fromNationId' => $fromNationId,
|
||||||
$fromNationId,
|
'toNationId' => $toNationId,
|
||||||
$toNationId,
|
];
|
||||||
);
|
}
|
||||||
if ($row !== null) {
|
}
|
||||||
$diplomacy[] = comparisonPickRow($row, [
|
foreach ($diplomacyPairSelectors as $pair) {
|
||||||
'fromNationId' => 'me',
|
$diplomacyPairs["{$pair['fromNationId']}:{$pair['toNationId']}"] = $pair;
|
||||||
'toNationId' => 'you',
|
}
|
||||||
'state' => 'state',
|
ksort($diplomacyPairs, SORT_STRING);
|
||||||
'term' => 'term',
|
|
||||||
'dead' => 'dead',
|
$diplomacy = [];
|
||||||
]);
|
foreach ($diplomacyPairs as $pair) {
|
||||||
}
|
$row = $db->queryFirstRow(
|
||||||
|
'SELECT me, you, state, term, dead FROM diplomacy WHERE me = %i AND you = %i',
|
||||||
|
$pair['fromNationId'],
|
||||||
|
$pair['toNationId'],
|
||||||
|
);
|
||||||
|
if ($row !== null) {
|
||||||
|
$diplomacy[] = comparisonPickRow($row, [
|
||||||
|
'fromNationId' => 'me',
|
||||||
|
'toNationId' => 'you',
|
||||||
|
'state' => 'state',
|
||||||
|
'term' => 'term',
|
||||||
|
'dead' => 'dead',
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user