aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-10-15 16:52:58 +0200
committermetamuffin <metamuffin@disroot.org>2025-10-15 16:52:58 +0200
commitc005910e375f0a56961967501c9bb855778ac09a (patch)
tree38b38e9f93b5fb300c815df361708977ddb58f05 /server
parenta47a61ec1ffb0d597783870a053cbe0760544301 (diff)
downloadhurrycurry-c005910e375f0a56961967501c9bb855778ac09a.tar
hurrycurry-c005910e375f0a56961967501c9bb855778ac09a.tar.bz2
hurrycurry-c005910e375f0a56961967501c9bb855778ac09a.tar.zst
Invert who is tagged
Diffstat (limited to 'server')
-rw-r--r--server/src/entity/tag_minigame.rs22
1 files changed, 14 insertions, 8 deletions
diff --git a/server/src/entity/tag_minigame.rs b/server/src/entity/tag_minigame.rs
index 02c3726f..ff4faccd 100644
--- a/server/src/entity/tag_minigame.rs
+++ b/server/src/entity/tag_minigame.rs
@@ -19,7 +19,7 @@ use super::{Entity, EntityContext};
use anyhow::Result;
use hurrycurry_client_lib::Item;
use hurrycurry_protocol::{Hand, ItemIndex, ItemLocation, Message, PacketC, PlayerID};
-use std::{collections::HashMap, fmt::Write};
+use std::{collections::HashMap, fmt::Write, random::random};
#[derive(Debug, Clone)]
pub struct TagMinigame {
@@ -42,15 +42,22 @@ impl Entity for TagMinigame {
fn tick(&mut self, c: EntityContext) -> Result<()> {
if !self.init_done {
self.init_done = true;
- // Hand out item every but one player (not random yet)
- for (id, player) in c.game.players.iter_mut().skip(1) {
+ // Hand out the item to some player(s)
+ let player_ids = c.game.players.keys().copied().collect::<Vec<_>>();
+ let num_tagged = 1 + (player_ids.len() / 5);
+ for _ in 0..num_tagged {
+ if player_ids.is_empty() {
+ break;
+ }
+ let id = player_ids[random::<usize>(..) % player_ids.len()];
+ let player = c.game.players.get_mut(&id).expect("just got the ids");
if let Some(slot) = player.items.get_mut(0) {
*slot = Some(Item {
active: None,
kind: self.tag_item,
});
c.packet_out.push_back(PacketC::SetItem {
- location: ItemLocation::Player(*id, Hand(0)),
+ location: ItemLocation::Player(id, Hand(0)),
item: Some(self.tag_item),
});
}
@@ -60,8 +67,7 @@ impl Entity for TagMinigame {
// Award points to players with the item
for (&id, player) in &c.game.players {
if let Some(slot) = player.items.get(0)
- && let Some(item) = slot
- && item.kind == self.tag_item
+ && slot.is_none()
{
*self.scores.entry(id).or_default() += c.dt
}
@@ -79,10 +85,10 @@ impl Entity for TagMinigame {
scores.sort_by_key(|(s, _)| -s);
let mut o = String::new();
- writeln!(o, "Tag Minigame — Try to not have your lettuce stolen!").unwrap();
+ writeln!(o, "Tag Minigame — Try to not have the lettuce!").unwrap();
for ((score, player), rank) in scores.into_iter().zip(1..) {
let name = c.game.players.get(&player).map_or("unknown", |p| &p.name);
- writeln!(o, "{rank:>2} {score:>4} {name}").unwrap();
+ writeln!(o, "{rank:>2}. {score:>5} {name}").unwrap();
}
o.pop(); // newline
c.packet_out.push_back(PacketC::ServerMessage {