using System.Threading.Tasks; using asap.core; using EventPartnerGatherRequests; using System; using UnityEngine; using Newtonsoft.Json; namespace EventPartner { public class RequestSystem { private readonly string DetailUrl = "detail", InviteUrl = "invite", RefuseUrl = "refuse", AcceptUrl = "accept", BuildUrl = "build"; private const string AutoMatchControlUrl = "automatch/toggle"; private const string AutoMatchStatusUrl = "automatch/status"; private readonly ICustomServerMgr _customServerMgr = GContext.container.Resolve(); private int _eventId; public void Init(IEventPartnerArchitecture arc) { _eventId = arc.Ctx.EventId; } public void Init(int eventId) { _eventId = eventId; } public async Task GetDetail() { var request = new EventPartnerGatherDetailRequest { EventId = _eventId }; var respString = await _customServerMgr.EventPartnerRequest(DetailUrl, request); try { var res = JsonConvert.DeserializeObject(respString); if (res.State != EEventBuildState.Success) { Debug.Log($"[EventPartner] GetDetail Failed with Code {res.State}: {res.Message}"); return null; } Debug.Log($"[EventPartnerStatue]: Detail response:\n {respString}"); return res.Detail; } catch (Exception e) { Debug.LogError(e); return null; } } public async Task Invite(string partnerId) { var request = new EventPartnerGatherPartnerRequest { PartnerId = partnerId, EventId = _eventId }; var respString = await _customServerMgr.EventPartnerRequest(InviteUrl, request); try { var res = JsonConvert.DeserializeObject(respString); if (res.State != EEventBuildState.Success) { Debug.Log($"[EventPartner] Invite Failed with Code {res.State}: {res.ErrorMessage}"); return null; } return res; } catch (Exception e) { Debug.LogError(e); return null; } } public async Task Refuse(string partnerId) { var request = new EventPartnerGatherPartnerRequest { PartnerId = partnerId, EventId = _eventId }; var respStr = await _customServerMgr.EventPartnerRequest(RefuseUrl, request); try { var res = JsonConvert.DeserializeObject(respStr); if (res.State != EEventBuildState.Success) { Debug.Log($"[EventPartner] Refuse Failed with Code {res.State}: {res.ErrorMessage}"); return null; } return res; } catch (Exception e) { Debug.LogError(e); return null; } } public async Task Accept(string partnerId) { var request = new EventPartnerGatherPartnerRequest { PartnerId = partnerId, EventId = _eventId }; var respStr = await _customServerMgr.EventPartnerRequest(AcceptUrl, request); try { var res = JsonConvert.DeserializeObject(respStr); if (res.State != EEventBuildState.Success) { Debug.Log($"[EventPartner] Accept Failed with Code {res.State}: {res.Message}"); return null; } return res; } catch (Exception e) { Debug.LogError(e); return null; } } public async Task Build(string partnerId, int score) { var request = new EventPartnerGatherBuildRequest { PartnerId = partnerId, NewScore = score, EventId = _eventId }; var respStr = await _customServerMgr.EventPartnerRequest(BuildUrl, request); try { var res = JsonConvert.DeserializeObject(respStr); if (res.State != EEventBuildBuildState.Success) { Debug.Log($"[EventPartner] Build Failed with Code {res.State}: {res.Message}"); return null; } return res; } catch (Exception e) { Debug.LogError(e); return null; } } public async Task ControlAutoMatch(bool enable) { try { var request = new AutoMatchToggleRequest { EventId = _eventId, Enabled = enable }; var response = await _customServerMgr.EventPartnerRequest(AutoMatchControlUrl, request); if (string.IsNullOrEmpty(response)) return new AutoMatchToggleResponse { State = EEventBuildState.Failed, Message = "Network error" }; return JsonConvert.DeserializeObject(response); } catch (Exception e) { Debug.LogError($"[EventPartner] ControlAutoMatch error: {e.Message}"); return new AutoMatchToggleResponse { State = EEventBuildState.Failed, Message = e.Message }; } } public async Task GetAutoMatchStatus() { try { var request = new AutoMatchStatusRequest { EventId = _eventId }; var response = await _customServerMgr.EventPartnerRequest(AutoMatchStatusUrl, request); if (string.IsNullOrEmpty(response)) return new AutoMatchStatusResponse { State = EEventBuildState.Failed, Message = "Network error", IsInMatching = false }; return JsonConvert.DeserializeObject(response); } catch (Exception e) { Debug.LogError($"[EventPartner] GetAutoMatchStatus error: {e.Message}"); return new AutoMatchStatusResponse { State = EEventBuildState.Failed, Message = e.Message, IsInMatching = false }; } } } }