Live location tracking and playback for the game "manhunt"
1use reqwest::StatusCode;
2
3use manhunt_logic::prelude::*;
4
5const fn server_host() -> &'static str {
6 if let Some(host) = option_env!("SIGNAL_SERVER_HOST") {
7 host
8 } else {
9 "localhost"
10 }
11}
12
13const fn server_port() -> u16 {
14 if let Some(port) = option_env!("SIGNAL_SERVER_PORT") {
15 const_str::parse!(port, u16)
16 } else {
17 3536
18 }
19}
20
21const fn server_secure() -> bool {
22 if let Some(secure) = option_env!("SIGNAL_SERVER_SECURE") {
23 const_str::eq_ignore_ascii_case!(secure, "true") || const_str::equal!(secure, "1")
24 } else {
25 false
26 }
27}
28
29const fn server_ws_proto() -> &'static str {
30 if server_secure() { "wss" } else { "ws" }
31}
32
33const fn server_http_proto() -> &'static str {
34 if server_secure() { "https" } else { "http" }
35}
36
37const SERVER_HOST: &str = server_host();
38const SERVER_PORT: u16 = server_port();
39const SERVER_WS_PROTO: &str = server_ws_proto();
40const SERVER_HTTP_PROTO: &str = server_http_proto();
41
42const SERVER_SOCKET: &str = const_str::concat!(SERVER_HOST, ":", SERVER_PORT);
43
44const SERVER_WEBSOCKET_URL: &str = const_str::concat!(SERVER_WS_PROTO, "://", SERVER_SOCKET);
45const SERVER_HTTP_URL: &str = const_str::concat!(SERVER_HTTP_PROTO, "://", SERVER_SOCKET);
46
47pub fn room_url(code: &str, host: bool) -> String {
48 let query_param = if host { "?create" } else { "" };
49 format!("{SERVER_WEBSOCKET_URL}/{code}{query_param}")
50}
51
52pub async fn room_exists(code: &str) -> Result<bool> {
53 let url = format!("{SERVER_HTTP_URL}/room_exists/{code}");
54 reqwest::get(url)
55 .await
56 .map(|resp| resp.status() == StatusCode::OK)
57 .context("Failed to make request")
58}
59
60pub async fn mark_room_started(code: &str) -> Result {
61 let url = format!("{SERVER_HTTP_URL}/mark_started/{code}");
62 let client = reqwest::Client::builder().build()?;
63 client
64 .post(url)
65 .send()
66 .await
67 .context("Could not send request")?
68 .error_for_status()
69 .context("Server returned an error")?;
70 Ok(())
71}
72
73const SERVER_GEN_CODE_URL: &str = const_str::concat!(SERVER_HTTP_URL, "/gen_code");
74
75pub async fn request_room_code() -> Result<String> {
76 reqwest::get(SERVER_GEN_CODE_URL)
77 .await
78 .context("Failed to contact signaling server")?
79 .error_for_status()
80 .context("Server returned an error")?
81 .text()
82 .await
83 .context("Failed to decode response")
84}