This commit is contained in:
Auric Vente 2024-07-20 06:55:15 -06:00
parent ba736560da
commit 0ad23a8d7d
2 changed files with 14 additions and 24 deletions

View File

@ -32,21 +32,15 @@ You watch who gets the most triumphs or the most hits.
The ant with the highest score is shown in the footer.
Clicking the footer scrolls to the bottom.
There is a filter to filter updates.
## Usage
Just open it, hatch some ants, and place it somewhere in your monitor.
## Algorithm
A random ant from ants that haven't had an update in at least 10 minutes is picked.
A random ant is picked based on weights (oldest update date weighs more).
Or any ant is picked if none meet that requirement.
Then a number between 1 and 10 is randomly picked.
Then a random number between 1 and 10 is picked.
For each number an action happens to produce an update.

View File

@ -147,25 +147,21 @@ class Ants:
if Ants.empty():
return None
if len(Ants.ants) == 1:
return Ants.ants[0]
ignore = []
current = Ants.get_current()
if current:
ignore.append(current)
now = Utils.now()
mins = 10 * 60
# Filter ants where updated is older than 10 minutes
ants = [a for a in Ants.ants if (now - a.updated) >= mins]
ages = [(now - ant.updated) for ant in Ants.ants]
if not len(ants):
ants = Ants.ants
# Normalize ages to create weights
total_age = sum(ages)
ants = [a for a in ants if a not in ignore]
return random.choice(ants)
if total_age == 0:
weights = [1] * len(Ants.ants) # If all ages are zero, use equal weights
else:
weights = [
int(age / total_age * 1000) for age in ages
] # Scale and cast to int
# Perform weighted random selection
return random.choices(Ants.ants, weights=weights, k=1)[0]
@staticmethod
def get_current() -> Ant | None: