mirror of
https://github.com/thilo-behnke/wasm-pong.git
synced 2026-08-18 20:56:24 +00:00
join session
This commit is contained in:
@@ -24,19 +24,14 @@ async function createNetworkSession(): Promise<Session> {
|
||||
});
|
||||
}
|
||||
|
||||
const joinSessionPath = "/pong/api/join_session"
|
||||
|
||||
function createJoinLink(sessionId: string, relative = false): string {
|
||||
let link = `${joinSessionPath}?session_id=${sessionId}`;
|
||||
if (!relative) {
|
||||
link = window.location.origin + link;
|
||||
}
|
||||
return link;
|
||||
function createJoinLink(sessionId: string): string {
|
||||
return `${window.location.origin}${window.location.pathname}?session_id=${sessionId}`;
|
||||
}
|
||||
|
||||
async function joinNetworkSession(sessionId): Promise<Session> {
|
||||
return fetch(createJoinLink(sessionId, true), {
|
||||
return fetch("/pong/api/join_session", {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({session_id: sessionId}),
|
||||
headers: [['Content-Type', 'application/json']]
|
||||
})
|
||||
.then(sessionResponseHandler)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import {createEventDispatcher, getContext} from "svelte";
|
||||
import {createEventDispatcher, getContext, onMount} from "svelte";
|
||||
import {Shadow} from 'svelte-loading-spinners'
|
||||
import session from "../api/session";
|
||||
|
||||
export let isLoading = false;
|
||||
|
||||
@@ -11,6 +12,18 @@
|
||||
|
||||
$: disableControls = isLoading;
|
||||
|
||||
onMount(() => {
|
||||
if (!window.location.search.startsWith("?")) {
|
||||
return;
|
||||
}
|
||||
const params = window.location.search.slice(1).split("?").map(p => p.split('=')).reduce((acc, [key, val]) => ({...acc, [key]: val}), {}) as any;
|
||||
console.log(params)
|
||||
if (!params.session_id) {
|
||||
return;
|
||||
}
|
||||
joinSessionId = params.session_id;
|
||||
})
|
||||
|
||||
const localSession = () => {
|
||||
dispatch("local-create")
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
{:else if session.state === SessionState.CLOSED}
|
||||
<h3>game over!</h3>
|
||||
{:else if session.state === SessionState.RUNNING}
|
||||
<NetworkSessionWrapper let:inputs={inputs}>
|
||||
<NetworkSessionWrapper session={session} let:inputs={inputs}>
|
||||
<slot inputs={inputs}></slot>
|
||||
</NetworkSessionWrapper>
|
||||
{:else }
|
||||
|
||||
@@ -111,17 +111,15 @@ export const sessionInputs = (session: Session) => readable([], function(setInpu
|
||||
}
|
||||
if (session.type === SessionType.OBSERVER) {
|
||||
const events = inputEvents(session);
|
||||
const p1Sub = events.subscribe(inputs => {
|
||||
events.subscribe(inputs => {
|
||||
player1Inputs.set(inputs)
|
||||
setInputs([...get(player1Inputs), ...get(player2Inputs)])
|
||||
})
|
||||
const p2Sub = events.subscribe(inputs => {
|
||||
events.subscribe(inputs => {
|
||||
player2Inputs.set(inputs)
|
||||
setInputs([...get(player1Inputs), ...get(player2Inputs)])
|
||||
})
|
||||
return () => {
|
||||
onDestroy(p1Sub);
|
||||
onDestroy(p2Sub);
|
||||
}
|
||||
}
|
||||
throw new Error(`unknown session type ${session.type}`)
|
||||
|
||||
@@ -106,7 +106,7 @@ async fn handle_session_join(
|
||||
if let Err(e) = session_join_res {
|
||||
eprintln!("Failed to join session: {:?}", e);
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.status(StatusCode::CONFLICT)
|
||||
.body(Body::from(e))
|
||||
.unwrap());
|
||||
}
|
||||
@@ -124,6 +124,5 @@ async fn handle_session_join(
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SessionJoinDto {
|
||||
pub session_id: String,
|
||||
pub player: String
|
||||
pub session_id: String
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user