Skip to content

Commit 85e9e21

Browse files
committed
relay fallback
1 parent fdb5ace commit 85e9e21

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

src-tauri/src/relays.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub struct Relay {
1515
pub id: String,
1616
pub name: String,
1717
pub host: String,
18+
#[serde(default)]
19+
pub fallback: bool,
1820
}
1921

2022
#[derive(Debug, Clone, Serialize, Deserialize, specta::Type)]
@@ -90,46 +92,55 @@ fn get_default_relays() -> Vec<Relay> {
9092
id: "direct".to_string(),
9193
name: "Direct".to_string(),
9294
host: "direct.cm-ss13.com".to_string(),
95+
fallback: true,
9396
},
9497
Relay {
9598
id: "nyc".to_string(),
9699
name: "NYC".to_string(),
97100
host: "nyc.cm-ss13.com".to_string(),
101+
fallback: false,
98102
},
99103
Relay {
100104
id: "uk".to_string(),
101105
name: "UK".to_string(),
102106
host: "uk.cm-ss13.com".to_string(),
107+
fallback: false,
103108
},
104109
Relay {
105110
id: "eu-e".to_string(),
106111
name: "EU East".to_string(),
107112
host: "eu-e.cm-ss13.com".to_string(),
113+
fallback: false,
108114
},
109115
Relay {
110116
id: "eu-w".to_string(),
111117
name: "EU West".to_string(),
112118
host: "eu-w.cm-ss13.com".to_string(),
119+
fallback: false,
113120
},
114121
Relay {
115122
id: "aus".to_string(),
116123
name: "Australia".to_string(),
117124
host: "aus.cm-ss13.com".to_string(),
125+
fallback: false,
118126
},
119127
Relay {
120128
id: "us-e".to_string(),
121129
name: "US East".to_string(),
122130
host: "us-e.cm-ss13.com".to_string(),
131+
fallback: false,
123132
},
124133
Relay {
125134
id: "us-w".to_string(),
126135
name: "US West".to_string(),
127136
host: "us-w.cm-ss13.com".to_string(),
137+
fallback: false,
128138
},
129139
Relay {
130140
id: "asia-se".to_string(),
131141
name: "SE Asia".to_string(),
132142
host: "asia-se.cm-ss13.com".to_string(),
143+
fallback: false,
133144
},
134145
]
135146
}
@@ -235,6 +246,21 @@ pub async fn init_relays(state: &Arc<RelayState>, handle: &AppHandle) {
235246
.collect();
236247

237248
futures_util::future::join_all(ping_futures).await;
249+
250+
let selected = state.get_selected().await;
251+
if selected.is_empty() {
252+
let relays = state.get_relays().await;
253+
if let Some(fallback) = relays.iter().find(|r| r.relay.fallback) {
254+
tracing::warn!(
255+
"All relay pings failed, falling back to: {} ({})",
256+
fallback.relay.id,
257+
fallback.relay.host
258+
);
259+
let id = fallback.relay.id.clone();
260+
state.set_selected(id.clone()).await;
261+
let _ = handle.emit("relay-selected", &id);
262+
}
263+
}
238264
}
239265

240266
#[tauri::command]

src/bindings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ export type LauncherFeatures = { relay_selector: boolean; singleplayer: boolean;
543543
export type LauncherStrings = { auth_provider_name: string; login_prompt: string; discord_game_name: string }
544544
export type LauncherUrls = { server_api: string; hub_api: string | null; auth_base: string | null; steam_auth: string | null; byond_hash_api: string | null; register_url: string | null; help_url: string }
545545
export type OidcConfig = { client_id: string; auth_url: string; token_url: string; userinfo_url: string }
546-
export type RelayWithPing = ({ id: string; name: string; host: string }) & { ping: number | null; checking: boolean }
546+
export type RelayWithPing = ({ id: string; name: string; host: string; fallback?: boolean }) & { ping: number | null; checking: boolean }
547547
export type ReleaseInfo = { tag_name: string; name: string; published_at: string; download_url: string | null; size: number }
548548
export type RenderingPipeline = "dxvk" | "wined3d"
549549
export type Server = { id: string | null; name: string; url: string; status: string; hub_status?: string; players?: number; data?: ServerData | null; is_18_plus?: boolean; version?: string | null; engine?: EngineRequirements | null; tags?: string[]; auth_methods?: string[]; engine_type?: string | null; description?: string | null; links?: ServerLink[]; verified_domain?: string | null; region?: string | null; language?: string | null }

src/components/RelayDropdown.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const RelayDropdown = ({
5555
return a.ping - b.ping;
5656
})
5757
.map((relay) => {
58-
const isDisabled = relay.ping === null && !relay.checking;
58+
const isDisabled = relay.ping === null && !relay.checking && !relay.fallback;
5959
const isSelected = selectedRelay === relay.id;
6060

6161
return (

src/stores/serverStore.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ interface ServerStore {
3333
initRelays: () => Promise<UnlistenFn>;
3434
}
3535

36-
const hasValidPing = (relays: RelayWithPing[]): boolean => {
37-
return relays.some((r) => r.ping !== null && !r.checking);
36+
const relaysResolved = (relays: RelayWithPing[]): boolean => {
37+
const hasValidPing = relays.some((r) => r.ping !== null && !r.checking);
38+
const allDone = relays.length > 0 && relays.every((r) => !r.checking);
39+
return hasValidPing || allDone;
3840
};
3941

4042
export const useServerStore = create<ServerStore>()((set) => ({
@@ -100,7 +102,7 @@ export const useServerStore = create<ServerStore>()((set) => ({
100102
initRelays: async () => {
101103
try {
102104
const relays = unwrap(await commands.getRelays());
103-
const ready = hasValidPing(relays);
105+
const ready = relaysResolved(relays);
104106
set({ relays, relaysReady: ready });
105107

106108
const selectedRelay = unwrap(await commands.getSelectedRelay());
@@ -113,7 +115,7 @@ export const useServerStore = create<ServerStore>()((set) => ({
113115
"relays-updated",
114116
(event) => {
115117
const relays = event.payload;
116-
const isReady = hasValidPing(relays);
118+
const isReady = relaysResolved(relays);
117119
set({ relays, relaysReady: isReady });
118120
}
119121
);

0 commit comments

Comments
 (0)