///| The Gamerig Library desktop module's data (components/gamerig-library): what gamerig's library /// daemon reports at `GET /api/state` (gamerig docker/desktop/server.py), and how the library /// sorts it into categories and searches it. The same types serve the shell, which polls the daemon /// and parses its JSON here (`parse_state`), and the module, which gets them back through the /// `game-library` capability. ///| A sidebar filter. `Running` is not a category of the catalog but a view of what runs now. #derive(Eq, Debug) pub(all) enum Category { All Games Launchers Tools Running } ///| pub let categories : Array[Category] = [All, Games, Launchers, Tools, Running] ///| pub fn Category::key(self : Category) -> String { match self { All => "all" Games => "games" Launchers => "launchers" Tools => "tools" Running => "running" } } ///| pub fn Category::label(self : Category) -> String { match self { All => "All" Games => "Games" Launchers => "Launchers" Tools => "Tools" Running => "Running" } } ///| The heading over the grid ("All apps", "Games" ...). pub fn Category::title(self : Category) -> String { match self { All => "All apps" Running => "Running now" c => c.label() } } ///| pub fn Category::parse(key : String) -> Category { match key.trim().to_owned().to_lower() { "games" | "game" => Games "launchers" | "launcher" => Launchers "tools" | "tool" => Tools "running" => Running _ => All } } ///| The singular label of a game's own category, for its badge. pub fn category_badge(key : String) -> String { match Category::parse(key) { Games => "Game" Launchers => "Launcher" Tools => "Tool" _ => "App" } } ///| One game or app of the library. #derive(Eq, Debug) pub(all) struct Game { id : String name : String category : String description : String icon : String install_hint : String featured : Bool installed : Bool running : Bool } ///| A way to start `game` other than plain Launch (Steam's Big Picture). #derive(Eq, Debug) pub(all) struct Mode { game : String id : String label : String } ///| The rig, as the library daemon describes it. #derive(Eq, Debug) pub(all) struct Rig { connected : Bool error : String gpu : String resolution : String display : String audio : String audio_ok : Bool hostname : String } ///| Before the daemon first answered. pub fn Rig::unknown() -> Rig { { connected: false, error: "", gpu: "Detecting GPU…", resolution: "—", display: "—", audio: "Detecting audio…", audio_ok: false, hostname: "", } } ///| What a card says about its game. #derive(Eq, Debug) pub(all) enum Status { Running Ready NotInstalled } ///| pub fn Game::status(self : Game) -> Status { if self.running { Running } else if self.installed { Ready } else { NotInstalled } } ///| pub fn Status::label(self : Status) -> String { match self { Running => "Running" Ready => "Ready" NotInstalled => "Not installed" } } ///| Everything `GET /api/state` says. #derive(Eq, Debug) pub(all) struct State { rig : Rig games : Array[Game] modes : Array[Mode] } // --------------------------------------------------------------------------- // JSON (gamerig desktop/ui/library parse_state, in Chandra's types) ///| fn str_field(obj : Map[String, Json], key : String, fallback : String) -> String { match obj.get(key) { Some(String(s)) => s Some(Number(n, ..)) => n.to_int().to_string() _ => fallback } } ///| fn bool_field(obj : Map[String, Json], key : String) -> Bool { match obj.get(key) { Some(True) => true Some(String("true")) => true Some(Number(n, ..)) => n != 0.0 _ => false } } ///| One game from its JSON, and its extra launch modes. Without an id there is nothing to act on. fn parse_game(j : Json, modes : Array[Mode]) -> Game? { guard j is Object(m) else { return None } let id = str_field(m, "id", "") if id == "" { return None } if m.get("modes") is Some(Array(items)) { for item in items { if item is Object(md) { let mid = str_field(md, "id", "") if mid != "" && mid != "default" { modes.push({ game: id, id: mid, label: str_field(md, "label", mid) }) } } } } Some({ id, name: str_field(m, "name", id), category: str_field(m, "category", "tools").to_lower(), description: str_field(m, "description", ""), icon: str_field(m, "icon", ""), install_hint: str_field(m, "install_hint", ""), featured: bool_field(m, "featured"), installed: bool_field(m, "installed"), running: bool_field(m, "running"), }) } ///| fn parse_rig(v : Json?) -> Rig { let base = Rig::unknown() guard v is Some(Object(m)) else { return { ..base, connected: true } } { connected: true, error: "", gpu: str_field(m, "gpu", base.gpu), resolution: str_field(m, "resolution", base.resolution), display: str_field(m, "display", base.display), audio: str_field(m, "audio", base.audio), audio_ok: bool_field(m, "audio_ok"), hostname: str_field(m, "hostname", ""), } } ///| Parse the body of `GET /api/state`. pub fn parse_state(body : String) -> Result[State, String] { let j = @json.parse(body) catch { e => return Err("Invalid answer from the library daemon: \{e}") } guard j is Object(m) else { return Err("The library daemon answered with something other than an object") } let games : Array[Game] = [] let modes : Array[Mode] = [] if m.get("apps") is Some(Array(items)) { for item in items { if parse_game(item, modes) is Some(g) { games.push(g) } } } Ok({ rig: parse_rig(m.get("system")), games, modes }) } ///| The daemon's `{"error": "..."}`, or the body itself when it is not one. pub fn error_message(body : String) -> String { let j = @json.parse(body) catch { _ => return body.trim().to_owned() } match j { Object(m) => match m.get("error") { Some(String(s)) => s _ => body } _ => body } } // --------------------------------------------------------------------------- // Filtering ///| fn in_category(g : Game, cat : Category) -> Bool { match cat { All => true Running => g.running c => g.category == c.key() } } ///| fn matches_query(g : Game, query : String) -> Bool { let q = query.trim().to_owned().to_lower() if q == "" { return true } let hay = "\{g.name} \{g.id} \{g.category} \{g.description}".to_lower() for word in q.split(" ") { let w = word.to_owned() if w != "" && hay.find(w) is None { return false } } true } ///| The cards to show: in the category, matching every word of the search, featured games first /// and otherwise in catalog order. pub fn filter_games(games : Array[Game], category : Category, query : String) -> Array[Game] { let picked = games.filter(g => in_category(g, category) && matches_query(g, query)) let featured = picked.filter(g => g.featured) let rest = picked.filter(g => !g.featured) [..featured, ..rest] } ///| pub fn count_in(games : Array[Game], category : Category) -> Int { games.filter(g => in_category(g, category)).length() } ///| `game`'s extra launch modes. pub fn modes_of(modes : Array[Mode], game : String) -> Array[Mode] { modes.filter(m => m.game == game) } ///| A non-negative decimal number (an HTTP status), 0 when `s` isn't one. pub fn parse_int(s : String) -> Int { let t = s.trim() if t.length() == 0 || t.length() > 9 { return 0 } let mut v = 0 for ch in t { if ch < '0' || ch > '9' { return 0 } v = v * 10 + (ch.to_int() - '0'.to_int()) } v }