diff --git a/src/components/views/LceOnlineView.tsx b/src/components/views/LceOnlineView.tsx index b7e7bb5..dfbae69 100644 --- a/src/components/views/LceOnlineView.tsx +++ b/src/components/views/LceOnlineView.tsx @@ -23,8 +23,6 @@ const LceOnlineView = memo(function LceOnlineView({ const [incomingReqs, setIncomingReqs] = useState([]); const [outgoingReqs, setOutgoingReqs] = useState([]); const [isHosting, setIsHosting] = useState(false); - const [_hostStatus, setHostStatus] = useState(""); - const [isDiscovering, setIsDiscovering] = useState(false); const [isAddingFriend, setIsAddingFriend] = useState(false); const [addFriendUsername, setAddFriendUsername] = useState(""); const addFriendInputRef = useRef(null); @@ -80,14 +78,12 @@ const LceOnlineView = memo(function LceOnlineView({ const handleStartHosting = async () => { playPressSound(); - setIsDiscovering(true); - setHostStatus("Starting relay..."); try { - await TauriService.stunDiscover(); - } catch {} - setIsHosting(true); - setHostStatus("Relay ready"); - setIsDiscovering(false); + await TauriService.startHostRelay(lceOnlineService.accessToken ?? "", 25565); + setIsHosting(true); + } catch (e: unknown) { + setErrorModal(e instanceof Error ? e.message : "Failed to start relay"); + } }; const handleStopHosting = async () => { @@ -98,7 +94,6 @@ const LceOnlineView = memo(function LceOnlineView({ console.warn("Stop hosting failed", e); } setIsHosting(false); - setHostStatus(""); }; const handleAuth = async () => { @@ -129,22 +124,20 @@ const LceOnlineView = memo(function LceOnlineView({ const menuItems = useMemo(() => { const items: MenuItem[] = []; if (currentTab === "friends") { - if (!isDiscovering) { - if (!isHosting) { - items.push({ - id: "host_game", - type: "button", - label: "Host Game", - onClick: handleStartHosting, - }); - } else { - items.push({ - id: "stop_hosting", - type: "button", - label: "Stop Hosting", - onClick: handleStopHosting, - }); - } + if (!isHosting) { + items.push({ + id: "host_game", + type: "button", + label: "Host Game", + onClick: handleStartHosting, + }); + } else { + items.push({ + id: "stop_hosting", + type: "button", + label: "Stop Hosting", + onClick: handleStopHosting, + }); } items.push({ id: "add_friend", @@ -201,7 +194,6 @@ const LceOnlineView = memo(function LceOnlineView({ outgoingReqs, playPressSound, isHosting, - isDiscovering, ]); const tabs: ("friends" | "requests")[] = ["friends", "requests"]; diff --git a/src/services/LceOnlineService.ts b/src/services/LceOnlineService.ts index 8de40ac..9298cae 100644 --- a/src/services/LceOnlineService.ts +++ b/src/services/LceOnlineService.ts @@ -52,38 +52,32 @@ export class LceOnlineService { } async login(username: string, password: string): Promise { - const res = await this.request<{ body: string }>( + const res = await this.request( "POST", "/login", `${username}:${password}`, AUTH_BASE_URL, ); - console.log("auth login response", res); - const pretoken = - res && typeof res === "object" && "body" in res - ? (res as any).body - : typeof res === "string" - ? res - : ""; - const token = pretoken.split(":")[1]; + const text = typeof res === "string" ? res : ""; + if (!text.startsWith("-")) { + throw new Error(text || "Login failed"); + } + const token = text.split(":")[1]; this.loginWithToken(token, username); } async register(username: string, password: string): Promise { - const res = await this.request<{ body: string }>( + const res = await this.request( "POST", "/register", `${username}:${password}`, AUTH_BASE_URL, ); - console.log("auth register response", res); - const pretoken = - res && typeof res === "object" && "body" in res - ? (res as any).body - : typeof res === "string" - ? res - : ""; - const token = pretoken.split(":")[1]; + const text = typeof res === "string" ? res : ""; + if (!text.startsWith("-")) { + throw new Error(text || "Registration failed"); + } + const token = text.split(":")[1]; this.loginWithToken(token, username); } @@ -190,19 +184,31 @@ export class LceOnlineService { } async sendFriendRequest(target: string): Promise { - await this.request("POST", "/sendrequest", target); + const res = await this.request("POST", "/sendrequest", target); + if (typeof res === "string" && res !== "Successfully Sent Friend Request") { + throw new Error(res); + } } async acceptFriendRequest(from: string): Promise { - await this.request("POST", "/acceptrequest", from); + const res = await this.request("POST", "/acceptrequest", from); + if (typeof res === "string" && res !== "1") { + throw new Error(res); + } } async declineFriendRequest(from: string): Promise { - await this.request("POST", "/declinerequest", from); + const res = await this.request("POST", "/declinerequest", from); + if (typeof res === "string" && res !== "1") { + throw new Error(res); + } } async removeFriend(from: string): Promise { - await this.request("POST", "/removefriend", from); + const res = await this.request("POST", "/removefriend", from); + if (typeof res === "string") { + throw new Error(res); + } } }