Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement gadgets in the backend #33

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions liberica/src/lib/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
*/
export type Stop = { name: string; id: string; lat: number; lon: number }

export type TeamKind = "MrX" | "Detective" | "Observer"
export type ClientResponse = { GameState: GameState } | { MrXGadget: MrXGadget } | { DetectiveGadget: DetectiveGadget } | { MrXPosition: MrXPosition } | { GameStart: null } | { DetectiveStart: null } | { GameEnd: null }

export type Team = { id: number; name: string; color: string; kind: TeamKind }
export type MrXGadget = { AlternativeFacts: { stop_id: string } } | { Midjourney: { image: number[] } } | "NotFound" | "Teleport" | "Shifter"

export type ClientMessage = { Position: { long: number; lat: number } } | { SetTeamPosition: { long: number; lat: number } } | { JoinTeam: { team_id: number } } | { EmbarkTrain: { train_id: string } } | "DisembarkTrain" | { Message: string }
export type MrXPosition = { Stop: string } | { Image: number[] } | "NotFound"

export type CreateTeamError = "InvalidName" | "NameAlreadyExists"
export type TeamState = { team: Team; long: number; lat: number; on_train: string | null }

export type Train = { id: number; long: number; lat: number; line_id: string; line_name: string; direction: string }
export type DetectiveGadget = { Stop: { stop_id: string } } | "OutOfOrder" | "Shackles"

export type TeamState = { team: Team; long: number; lat: number; on_train: string | null }
export type TeamKind = "MrX" | "Detective" | "Observer"

export type CreateTeam = { name: string; color: string; kind: TeamKind }

export type ClientResponse = { GameState: GameState }
export type Train = { id: number; long: number; lat: number; line_id: string; line_name: string; direction: string }

export type ClientMessage = { Position: { long: number; lat: number } } | { SetTeamPosition: { long: number; lat: number } } | { JoinTeam: { team_id: number } } | { EmbarkTrain: { train_id: string } } | "DisembarkTrain" | { MrXGadget: MrXGadget } | { DetectiveGadget: DetectiveGadget } | { Message: string }

export type Team = { id: number; name: string; color: string; kind: TeamKind }

export type GameState = { teams: TeamState[]; trains: Train[]; position_cooldown?: number | null; detective_gadget_cooldown?: number | null; mr_x_gadget_cooldown?: number | null; blocked_stop?: string | null }

export type GameState = { teams: TeamState[]; trains: Train[] }
export type CreateTeamError = "InvalidName" | "NameAlreadyExists"

5 changes: 4 additions & 1 deletion liberica/src/page/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { useTranslation } from "react-i18next";

export function Game() {
const [ws, setWS] = useState<WebSocketApi>();
const [gs, setGameState] = useState<GameState>({ teams: [], trains: [] });
const [gs, setGameState] = useState<GameState>({
teams: [],
trains: [],
});
const [embarkedTrain, setEmbarkedTrain] = useState<Train>();
const team = useLocation().state as Team | undefined; // this is how Home passes the team
const { t } = useTranslation();
Expand Down
63 changes: 63 additions & 0 deletions robusta/src/gadgets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::collections::HashSet;
use std::mem;

use serde::{Deserialize, Serialize};

#[derive(specta::Type, Clone, Serialize, Deserialize, Debug)]
pub enum MrXGadget {
AlternativeFacts { stop_id: String },
Midjourney { image: Vec<u8> },
NotFound,
Teleport,
Shifter,
}

#[derive(specta::Type, Clone, Serialize, Deserialize, Debug)]
pub enum DetectiveGadget {
Stop { stop_id: String },
OutOfOrder,
Shackles,
}
Comment on lines +7 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would speak against using just one enum? You could add a function is_usable_by(team: Team) -> bool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main advantage is that it's harder for the frontend to accidentally send a gadget from the wrong team. Using a single enum might be cleaner though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another advantage is that there are separate countdowns for Mr. X and Detective gadgets.


#[derive(Debug)]
pub struct GadgetState<T> {
can_be_used: bool,
cooldown: Option<f32>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cleaner to use chrono::DateTime<Utc>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cooldown is the number of seconds remaining, using a DateTime<Utc> requires more work to get the remaining time each iteration.

used: HashSet<mem::Discriminant<T>>,
}

impl<T> GadgetState<T> {
pub fn new() -> Self {
Self {
can_be_used: false,
cooldown: None,
used: HashSet::new(),
}
}

pub fn update_time(&mut self, delta: f32) {
if let Some(cooldown) = self.cooldown.as_mut() {
*cooldown -= delta;
if *cooldown < 0.0 {
self.cooldown = None;
}
}
}

pub fn remaining(&self) -> Option<f32> {
self.cooldown
}

pub fn try_use(&mut self, gadget: &T, cooldown: f32) -> bool {
if self.can_be_used && self.cooldown.is_none() && self.used.insert(mem::discriminant(gadget)) {
self.cooldown = Some(cooldown);
true
} else {
false
}
}

pub fn allow_use(&mut self) {
self.can_be_used = true;
}
}
4 changes: 2 additions & 2 deletions robusta/src/kvv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{DateTime, Utc};
use futures_util::future::join_all;
use lazy_static::lazy_static;
use serde::Serialize;
use serde::{Deserialize, Serialize};

use std::collections::HashMap;
use std::sync::OnceLock;
Expand All @@ -16,7 +16,7 @@ use crate::ws_message::Train;
const DEFAULT_WAIT_TIME: Duration = Duration::from_secs(30);

/// Information about a tram station.
#[derive(Debug, Serialize, specta::Type, PartialEq)]
#[derive(Debug, Serialize, Deserialize, specta::Type, Clone, PartialEq)]
pub struct Stop {
/// human readable stop name
pub name: String,
Expand Down
Loading
Loading