better elect leader
@@ -986,33 +986,86 @@ class Tribe
986
986
}
987
987
988
988
if (!$hasLeader) {
989
$adultCandidates = array_filter($this->getKobolds(), function ($kobold) {
990
return $kobold->getAgeCategory() === 'adult';
989
$eligibleKobolds = array_filter($this->getKobolds(), function ($kobold) {
990
return $kobold->getAgeCategory() !== 'egg';
991
991
});
992
992
993
if (!empty($adultCandidates)) {
994
usort($adultCandidates, function ($a, $b) {
995
return $b->getAge() <=> $a->getAge();
996
});
997
998
$newLeader = $adultCandidates[0];
999
$newLeader->setLeader();
1000
1001
$leaderName = $newLeader->getName();
1002
$leaderStories = [
1003
"With no chieftain to guide the camp, the elders called a gathering. Voices rose and fell until a clear choice emerged, and {$leaderName} was named to lead. The tribe settled as the new chieftain accepted the duty.",
1004
"The tribe woke to a quiet worry with no leader among them. By sundown the council had met, and {$leaderName} was chosen to carry the tribe's future. The fires burned steady as the new chieftain took the mantle.",
1005
"Without a chieftain, the camp felt adrift. The oldest adults met in counsel and agreed that {$leaderName} should lead. The tribe marked the choice with a solemn vow and returned to their work with purpose.",
1006
"A leaderless tribe called for order, so the elders gathered the adults to decide. After careful words and long thought, {$leaderName} was raised as chieftain. A quiet cheer moved through the dens as the tribe accepted the new guide.",
1007
"As the month closed, the tribe turned to its oldest adults for direction. The council placed the burden of leadership on {$leaderName}. The camp steadied as the new chieftain promised to protect the tribe.",
1008
"The fires crackled while the elders spoke of who should lead. In the end, {$leaderName} stood before the tribe as the chosen chieftain. The tribe nodded as one, and a new season of leadership began.",
1009
"A tribe without a leader cannot last, so the adults met to decide. They chose {$leaderName} to take the chieftain's role. The camp settled into a calm rhythm as the new leader's voice carried.",
1010
"With no chieftain among them, the tribe held a council beneath the evening sky. The decision came in favor of {$leaderName}, who accepted the weight of leadership. The tribe returned to their huts with renewed confidence.",
1011
"The tribe convened an elders' circle to fill the empty seat of leadership. After debate and silence, {$leaderName} was named chieftain. The camp honored the choice and moved forward together.",
1012
"Leadership was needed, and the tribe did not delay. The oldest adults agreed that {$leaderName} should guide them, and the tribe affirmed the decision. The new chieftain's first night ended with a calm and steady watch."
993
if (count($eligibleKobolds) >= 3) {
994
$pickFromTier = function ($tier) {
995
return array_values(array_filter($this->getKobolds(), function ($kobold) use ($tier) {
996
return $kobold->getAgeCategory() === $tier;
997
}));
998
};
999
1000
$candidatePools = [
1001
$pickFromTier('adult'),
1002
$pickFromTier('old'),
1003
$pickFromTier('young')
1013
1004
];
1014
1005
1015
$foodStory .= "\n" . $leaderStories[array_rand($leaderStories)] . "\n";
1006
$candidates = [];
1007
foreach ($candidatePools as $pool) {
1008
if (!empty($pool)) {
1009
$candidates = $pool;
1010
break;
1011
}
1012
}
1013
1014
if (!empty($candidates)) {
1015
$scores = [];
1016
foreach ($candidates as $candidate) {
1017
$score = 0;
1018
foreach ($this->getKobolds() as $other) {
1019
if ($other->getId() === $candidate->getId()) {
1020
continue;
1021
}
1022
1023
$likes = 0;
1024
if (isset($other->dataStore) && $other->dataStore instanceof stdClass) {
1025
if (isset($other->dataStore->likes) && is_array($other->dataStore->likes)) {
1026
$likes = (int) ($other->dataStore->likes[$candidate->getId()] ?? 0);
1027
}
1028
}
1029
$score += $likes;
1030
}
1031
$scores[$candidate->getId()] = $score;
1032
}
1033
1034
$maxScore = max($scores);
1035
$topCandidates = array_values(array_filter($candidates, function ($candidate) use ($scores, $maxScore) {
1036
return $scores[$candidate->getId()] === $maxScore;
1037
}));
1038
1039
if (count($topCandidates) > 1) {
1040
usort($topCandidates, function ($a, $b) {
1041
return $b->getAge() <=> $a->getAge();
1042
});
1043
1044
$oldestAge = $topCandidates[0]->getAge();
1045
$topCandidates = array_values(array_filter($topCandidates, function ($candidate) use ($oldestAge) {
1046
return $candidate->getAge() === $oldestAge;
1047
}));
1048
}
1049
1050
$newLeader = $topCandidates[array_rand($topCandidates)];
1051
$newLeader->setLeader();
1052
1053
$leaderName = $newLeader->getName();
1054
$leaderStories = [
1055
"With no chieftain to guide the camp, the elders called a gathering. Voices rose and fell until a clear choice emerged, and {$leaderName} was named to lead. The tribe settled as the new chieftain accepted the duty.",
1056
"The tribe woke to a quiet worry with no leader among them. By sundown the council had met, and {$leaderName} was chosen to carry the tribe's future. The fires burned steady as the new chieftain took the mantle.",
1057
"Without a chieftain, the camp felt adrift. The elders weighed the tribe's voices and set their trust in {$leaderName}. The tribe marked the choice with a solemn vow and returned to their work with purpose.",
1058
"A leaderless tribe called for order, so the elders gathered to decide. After careful words and long thought, {$leaderName} was raised as chieftain. A quiet cheer moved through the dens as the tribe accepted the new guide.",
1059
"As the month closed, the tribe turned to its oldest voices for direction. The council placed the burden of leadership on {$leaderName}. The camp steadied as the new chieftain promised to protect the tribe.",
1060
"The fires crackled while the elders spoke of who should lead. In the end, {$leaderName} stood before the tribe as the chosen chieftain. The tribe nodded as one, and a new season of leadership began.",
1061
"A tribe without a leader cannot last, so the adults met to decide. They chose {$leaderName} to take the chieftain's role. The camp settled into a calm rhythm as the new leader's voice carried.",
1062
"With no chieftain among them, the tribe held a council beneath the evening sky. The decision came in favor of {$leaderName}, who accepted the weight of leadership. The tribe returned to their huts with renewed confidence.",
1063
"The tribe convened an elders' circle to fill the empty seat of leadership. After debate and silence, {$leaderName} was named chieftain. The camp honored the choice and moved forward together.",
1064
"Leadership was needed, and the tribe did not delay. The elders agreed that {$leaderName} should guide them, and the tribe affirmed the decision. The new chieftain's first night ended with a calm and steady watch."
1065
];
1066
1067
$foodStory .= "\n" . $leaderStories[array_rand($leaderStories)] . "\n";
1068
}
1016
1069
}
1017
1070
}
1018
1071
@@ -986,33 +986,86 @@ class Tribe986
}986
}987
987
988
if (!$hasLeader) {988
if (!$hasLeader) {989
$adultCandidates = array_filter($this->getKobolds(), function ($kobold) {989
$eligibleKobolds = array_filter($this->getKobolds(), function ($kobold) {990
return $kobold->getAgeCategory() === 'adult';990
return $kobold->getAgeCategory() !== 'egg';991
});991
});992
992
993
if (!empty($adultCandidates)) {993
if (count($eligibleKobolds) >= 3) {994
usort($adultCandidates, function ($a, $b) {994
$pickFromTier = function ($tier) {995
return $b->getAge() <=> $a->getAge();995
return array_values(array_filter($this->getKobolds(), function ($kobold) use ($tier) {996
});996
return $kobold->getAgeCategory() === $tier;997
997
}));998
$newLeader = $adultCandidates[0];998
};999
$newLeader->setLeader();999
1000
1000
$candidatePools = [1001
$leaderName = $newLeader->getName();1001
$pickFromTier('adult'),1002
$leaderStories = [1002
$pickFromTier('old'),1003
"With no chieftain to guide the camp, the elders called a gathering. Voices rose and fell until a clear choice emerged, and {$leaderName} was named to lead. The tribe settled as the new chieftain accepted the duty.",1003
$pickFromTier('young')1004
"The tribe woke to a quiet worry with no leader among them. By sundown the council had met, and {$leaderName} was chosen to carry the tribe's future. The fires burned steady as the new chieftain took the mantle.",1005
"Without a chieftain, the camp felt adrift. The oldest adults met in counsel and agreed that {$leaderName} should lead. The tribe marked the choice with a solemn vow and returned to their work with purpose.",1006
"A leaderless tribe called for order, so the elders gathered the adults to decide. After careful words and long thought, {$leaderName} was raised as chieftain. A quiet cheer moved through the dens as the tribe accepted the new guide.",1007
"As the month closed, the tribe turned to its oldest adults for direction. The council placed the burden of leadership on {$leaderName}. The camp steadied as the new chieftain promised to protect the tribe.",1008
"The fires crackled while the elders spoke of who should lead. In the end, {$leaderName} stood before the tribe as the chosen chieftain. The tribe nodded as one, and a new season of leadership began.",1009
"A tribe without a leader cannot last, so the adults met to decide. They chose {$leaderName} to take the chieftain's role. The camp settled into a calm rhythm as the new leader's voice carried.",1010
"With no chieftain among them, the tribe held a council beneath the evening sky. The decision came in favor of {$leaderName}, who accepted the weight of leadership. The tribe returned to their huts with renewed confidence.",1011
"The tribe convened an elders' circle to fill the empty seat of leadership. After debate and silence, {$leaderName} was named chieftain. The camp honored the choice and moved forward together.",1012
"Leadership was needed, and the tribe did not delay. The oldest adults agreed that {$leaderName} should guide them, and the tribe affirmed the decision. The new chieftain's first night ended with a calm and steady watch."1013
];1004
];1014
1005
1015
$foodStory .= "\n" . $leaderStories[array_rand($leaderStories)] . "\n";1006
$candidates = [];1007
foreach ($candidatePools as $pool) {1008
if (!empty($pool)) {1009
$candidates = $pool;1010
break;1011
}1012
}1013
1014
if (!empty($candidates)) {1015
$scores = [];1016
foreach ($candidates as $candidate) {1017
$score = 0;1018
foreach ($this->getKobolds() as $other) {1019
if ($other->getId() === $candidate->getId()) {1020
continue;1021
}1022
1023
$likes = 0;1024
if (isset($other->dataStore) && $other->dataStore instanceof stdClass) {1025
if (isset($other->dataStore->likes) && is_array($other->dataStore->likes)) {1026
$likes = (int) ($other->dataStore->likes[$candidate->getId()] ?? 0);1027
}1028
}1029
$score += $likes;1030
}1031
$scores[$candidate->getId()] = $score;1032
}1033
1034
$maxScore = max($scores);1035
$topCandidates = array_values(array_filter($candidates, function ($candidate) use ($scores, $maxScore) {1036
return $scores[$candidate->getId()] === $maxScore;1037
}));1038
1039
if (count($topCandidates) > 1) {1040
usort($topCandidates, function ($a, $b) {1041
return $b->getAge() <=> $a->getAge();1042
});1043
1044
$oldestAge = $topCandidates[0]->getAge();1045
$topCandidates = array_values(array_filter($topCandidates, function ($candidate) use ($oldestAge) {1046
return $candidate->getAge() === $oldestAge;1047
}));1048
}1049
1050
$newLeader = $topCandidates[array_rand($topCandidates)];1051
$newLeader->setLeader();1052
1053
$leaderName = $newLeader->getName();1054
$leaderStories = [1055
"With no chieftain to guide the camp, the elders called a gathering. Voices rose and fell until a clear choice emerged, and {$leaderName} was named to lead. The tribe settled as the new chieftain accepted the duty.",1056
"The tribe woke to a quiet worry with no leader among them. By sundown the council had met, and {$leaderName} was chosen to carry the tribe's future. The fires burned steady as the new chieftain took the mantle.",1057
"Without a chieftain, the camp felt adrift. The elders weighed the tribe's voices and set their trust in {$leaderName}. The tribe marked the choice with a solemn vow and returned to their work with purpose.",1058
"A leaderless tribe called for order, so the elders gathered to decide. After careful words and long thought, {$leaderName} was raised as chieftain. A quiet cheer moved through the dens as the tribe accepted the new guide.",1059
"As the month closed, the tribe turned to its oldest voices for direction. The council placed the burden of leadership on {$leaderName}. The camp steadied as the new chieftain promised to protect the tribe.",1060
"The fires crackled while the elders spoke of who should lead. In the end, {$leaderName} stood before the tribe as the chosen chieftain. The tribe nodded as one, and a new season of leadership began.",1061
"A tribe without a leader cannot last, so the adults met to decide. They chose {$leaderName} to take the chieftain's role. The camp settled into a calm rhythm as the new leader's voice carried.",1062
"With no chieftain among them, the tribe held a council beneath the evening sky. The decision came in favor of {$leaderName}, who accepted the weight of leadership. The tribe returned to their huts with renewed confidence.",1063
"The tribe convened an elders' circle to fill the empty seat of leadership. After debate and silence, {$leaderName} was named chieftain. The camp honored the choice and moved forward together.",1064
"Leadership was needed, and the tribe did not delay. The elders agreed that {$leaderName} should guide them, and the tribe affirmed the decision. The new chieftain's first night ended with a calm and steady watch."1065
];1066
1067
$foodStory .= "\n" . $leaderStories[array_rand($leaderStories)] . "\n";1068
}1016
}1069
}1017
}1070
}1018
1071