open matches listing with available players

This commit is contained in:
2025-11-28 15:51:55 +05:30
parent a4fc04ace3
commit cb3f5fb5cf
3 changed files with 99 additions and 1 deletions

View File

@@ -3,10 +3,12 @@ import {
Session,
Socket,
MatchmakerTicket,
Match,
MatchData,
MatchmakerMatched,
} from "@heroiclabs/nakama-js";
// @ts-ignore
import { ApiMatch } from "@heroiclabs/nakama-js/dist/api.gen"
import React, { createContext, useContext, useState } from "react";
function getOrCreateDeviceId(): string {
@@ -32,6 +34,7 @@ export interface NakamaContextType {
sendMatchData(matchId: string, op: number, data: object): void;
onMatchData(cb: (msg: any) => void): void;
listOpenMatches(): Promise<ApiMatch[]>;
}
export const NakamaContext = createContext<NakamaContextType>(null!);
@@ -128,6 +131,19 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) {
};
}
async function listOpenMatches(): Promise<ApiMatch[]> {
if (!session) {
console.warn("[Nakama] listOpenMatches called before login");
return [];
}
const result = await client.listMatches(session, 10);
console.log("[Nakama] Open matches:", result.matches);
return result.matches ?? [];
}
return (
<NakamaContext.Provider
value={{
@@ -140,6 +156,7 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) {
joinMatch,
sendMatchData,
onMatchData,
listOpenMatches,
}}
>
{children}