using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
namespace IDCGames.API
{
///
/// Match session: group chat + voice (LiveKit or Mumble) via /api/v1/matches.
/// Text real-time: same Reverb channel as group chat (see IDCChatManager + docs/CHAT_API.md).
/// Voice WebRTC: install LiveKit Unity SDK, connect with JoinMatchAsync credentials.
/// REQUIRED: enable VAD before publishing mic (GET /api/v1/voice/config → client_requirements.vad).
///
public class IDCMatchVoiceManager : MonoBehaviour
{
[Header("API")]
public string apiBaseUrl = "http://localhost:8000";
[Header("Voice")]
[Tooltip("livekit or mumble — must match server config")]
public string voiceProvider = "livekit";
public event Action OnMatchVoiceError;
private string _accessToken;
public void SetAccessToken(string token) => _accessToken = token;
public async Task GetVoiceConfigAsync()
{
var data = await GetAsync("/api/v1/voice/config");
return data.client_requirements;
}
/// Host creates match (same match_key as your game server).
public async Task CreateMatchAsync(string matchKey, int[] memberUserIds = null, string label = null)
{
var payload = new Dictionary
{
{ "match_key", matchKey },
{ "member_user_ids", memberUserIds ?? Array.Empty() },
};
if (!string.IsNullOrEmpty(label))
payload["label"] = label;
var json = JsonConvert.SerializeObject(payload);
return await PostAsync("/api/v1/matches", json, expectCreated: true);
}
public async Task GetMatchAsync(string matchKey)
{
return await GetAsync($"/api/v1/matches/{Uri.EscapeDataString(matchKey)}");
}
/// Returns all/team chat channels + optional LiveKit token or Mumble credentials (voice requires teamId).
public async Task JoinMatchAsync(string matchKey, bool voiceEnabled, string teamId = null)
{
if (voiceEnabled && string.IsNullOrWhiteSpace(teamId))
throw new ArgumentException("team_id is required when voice is enabled (match voice is team-only).");
var body = JsonConvert.SerializeObject(new
{
voice_enabled = voiceEnabled,
voice_provider = voiceEnabled ? voiceProvider : null,
team_id = teamId,
});
return await PostAsync(
$"/api/v1/matches/{Uri.EscapeDataString(matchKey)}/join", body);
}
public async Task LeaveMatchAsync(string matchKey)
{
await PostAsync