using System;
using System.Threading.Tasks;
using UnityEngine;
namespace IDCGames.API
{
///
/// Minimal production smoke test for match voice (LiveKit token from panel).
/// 1. Assign apiBaseUrl = https://gamepanel.idcgames.com
/// 2. Set gameId + gamepanelSecretToken (from game admin; do not ship in client builds).
/// 3. Role Host: creates match. Role Guest: set matchKey from host log.
/// 4. Install LiveKit Unity SDK and uncomment Connect in OnLiveKitCredentials for real audio.
///
public class IDCMatchVoiceProductionSmokeTest : MonoBehaviour
{
public enum SmokeRole { HostCreateAndJoin, GuestJoinOnly }
[Header("Production API")]
public string apiBaseUrl = "https://gamepanel.idcgames.com";
public int gameId = 1;
[Tooltip("gamepanel_secret_token — editor/tests only")]
public string gamepanelSecretToken;
[Header("Match")]
public SmokeRole role = SmokeRole.HostCreateAndJoin;
public string matchKey;
public string teamId = "alpha";
public string voiceProvider = "livekit";
[Header("Sandbox user (unique per run)")]
public string platformUserSuffix;
private IDCMatchVoiceManager _voice;
private async void Start()
{
if (string.IsNullOrEmpty(platformUserSuffix))
platformUserSuffix = Guid.NewGuid().ToString("N").Substring(0, 6);
_voice = gameObject.AddComponent();
_voice.apiBaseUrl = apiBaseUrl;
_voice.voiceProvider = voiceProvider;
try
{
var token = await SandboxLoginAsync();
_voice.SetAccessToken(token);
var cfg = await _voice.GetVoiceConfigAsync();
Debug.Log($"[VoiceSmoke] VAD required: {cfg.vad?.required}");
if (role == SmokeRole.HostCreateAndJoin)
{
matchKey = "unity-voice-" + platformUserSuffix;
await _voice.CreateMatchAsync(matchKey, Array.Empty(), "Unity voice smoke");
Debug.Log($"[VoiceSmoke] Created match_key={matchKey}");
}
if (string.IsNullOrEmpty(matchKey))
{
Debug.LogError("[VoiceSmoke] Set matchKey for GuestJoinOnly");
return;
}
var join = await _voice.JoinMatchAsync(matchKey, true, teamId);
Debug.Log($"[VoiceSmoke] Join OK — all chat id={join.text?.all?.conversation_id} team id={join.text?.team?.conversation_id}");
Debug.Log($"[VoiceSmoke] LiveKit url={join.voice?.url} room={join.voice?.room_name} tokenLen={join.voice?.token?.Length ?? 0}");
if (string.IsNullOrEmpty(join.voice?.token))
{
Debug.LogError("[VoiceSmoke] No token — check LIVEKIT_ENABLED and LIVEKIT_* on server .env");
return;
}
await OnLiveKitCredentials(join.voice);
}
catch (Exception ex)
{
Debug.LogError("[VoiceSmoke] " + ex.Message);
}
}
private async Task SandboxLoginAsync()
{
var platformUserId = (role == SmokeRole.HostCreateAndJoin ? "host_" : "guest_") + platformUserSuffix;
var username = (role == SmokeRole.HostCreateAndJoin ? "Host" : "Guest") + platformUserSuffix;
var body = new LoginBody
{
platform = "sandbox",
token = "sandbox_" + platformUserId,
user_id = platformUserId,
game_id = gameId,
gamepanel_secret_token = gamepanelSecretToken,
username = username,
details = "basic",
};
using var req = new UnityEngine.Networking.UnityWebRequest(
apiBaseUrl.TrimEnd('/') + "/api/v1/auth/third-party/login", "POST");
var json = JsonUtility.ToJson(body);
var bytes = System.Text.Encoding.UTF8.GetBytes(json);
req.uploadHandler = new UnityEngine.Networking.UploadHandlerRaw(bytes);
req.downloadHandler = new UnityEngine.Networking.DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
var op = req.SendWebRequest();
while (!op.isDone)
await Task.Yield();
if (req.result != UnityEngine.Networking.UnityWebRequest.Result.Success)
throw new Exception(req.downloadHandler.text);
var wrapper = JsonUtility.FromJson(req.downloadHandler.text);
return wrapper.access_token;
}
private Task OnLiveKitCredentials(MatchJoinVoice cred)
{
// With LiveKit Unity SDK installed:
// var room = new LiveKit.Room();
// await room.Connect(cred.url, cred.token);
// Enable mic only when VAD opens (see GetVoiceConfigAsync).
Debug.Log("[VoiceSmoke] HTTP credentials OK. Connect LiveKit SDK for audio (second build as Guest, same matchKey + teamId).");
return Task.CompletedTask;
}
[Serializable]
private class LoginBody
{
public string platform;
public string token;
public string user_id;
public int game_id;
public string gamepanel_secret_token;
public string username;
public string details;
}
[Serializable]
private class LoginWrapper
{
public bool success;
public string access_token;
}
}
}