smart-interactive-display/Assets/GadGame/Scripts/Network/P4PGraphqlManager.cs

234 lines
7.2 KiB
C#
Raw Normal View History

2024-05-21 11:33:09 -07:00
using System;
2024-05-22 00:36:04 -07:00
using System.Net.WebSockets;
2024-05-21 11:33:09 -07:00
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
2024-05-21 04:32:10 -07:00
using GadGame.Singleton;
using GraphQlClient.Core;
2024-05-21 11:33:09 -07:00
using GraphQlClient.EventCallbacks;
2024-05-21 04:32:10 -07:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Networking;
2024-05-22 00:36:04 -07:00
using ZXing;
using ZXing.QrCode;
2024-05-21 04:32:10 -07:00
namespace GadGame.Network
{
2024-05-21 11:33:09 -07:00
2024-05-22 00:36:04 -07:00
struct DataReceive
2024-05-21 04:32:10 -07:00
{
2024-05-22 00:36:04 -07:00
public JObject[] errors;
public JObject data;
2024-05-21 04:32:10 -07:00
}
2024-05-22 00:36:04 -07:00
2024-05-21 04:32:10 -07:00
public class P4PGraphqlManager : PersistentSingleton<P4PGraphqlManager>
{
2024-05-22 00:36:04 -07:00
[SerializeField] private GraphApi _graphApi;
2024-05-21 04:32:10 -07:00
[SerializeField] private string _machineMac;
[SerializeField] private string _promotionId;
2024-05-21 11:33:09 -07:00
private DateTime _startTime;
2024-05-22 00:36:04 -07:00
private string _userId;
private string _userAccessToken;
private string _machineAccessToken;
2024-05-21 11:33:09 -07:00
public Action<OnSubscriptionDataReceived> OnGuestUpdatedSubscription;
2024-05-21 04:32:10 -07:00
2024-05-21 11:33:09 -07:00
protected override async void Awake()
2024-05-21 04:32:10 -07:00
{
base.Awake();
2024-05-21 11:33:09 -07:00
await LoginMachine();
await CreateGuest();
await JoinPromotion();
await UniTask.Delay(5000);
await SubmitGameSession(1000);
GetQrLink();
2024-05-21 04:32:10 -07:00
}
2024-05-21 11:33:09 -07:00
private void OnEnable()
2024-05-21 04:32:10 -07:00
{
2024-05-21 11:33:09 -07:00
OnSubscriptionDataReceived.RegisterListener(OnGuestUpdated);
}
private void OnDisable()
{
OnSubscriptionDataReceived.UnregisterListener(OnGuestUpdated);
}
private void OnGuestUpdated(OnSubscriptionDataReceived dataReceived)
{
2024-05-22 00:36:04 -07:00
Debug.Log(dataReceived.data);
2024-05-21 11:33:09 -07:00
OnGuestUpdatedSubscription?.Invoke(dataReceived);
2024-05-21 04:32:10 -07:00
}
2024-05-21 11:33:09 -07:00
private DataReceive GetData(string data)
2024-05-21 04:32:10 -07:00
{
2024-05-21 11:33:09 -07:00
var json = HttpHandler.FormatJson(data);
return JsonConvert.DeserializeObject<DataReceive>(json);
}
private async Task<bool> LoginMachine()
{
2024-05-22 00:36:04 -07:00
var query = _graphApi.GetQueryByName("LoginAsGameMachine", GraphApi.Query.Type.Mutation);
2024-05-21 04:32:10 -07:00
query.SetArgs(new
{
input = new
{
macAddress = _machineMac,
password = "Abc@123"
}
});
2024-05-22 00:36:04 -07:00
var request = await _graphApi.Post(query);
2024-05-21 11:33:09 -07:00
if (request.result == UnityWebRequest.Result.Success)
{
var receive = GetData(request.downloadHandler.text);
2024-05-22 00:36:04 -07:00
if (receive.data != null)
2024-05-21 11:33:09 -07:00
{
2024-05-22 00:36:04 -07:00
var loginDetail = receive.data["loginAsGameMachine"]!.ToObject<LoginDetails>();
_machineAccessToken = loginDetail.accessToken;
2024-05-21 11:33:09 -07:00
return true;
}
}
return false;
2024-05-21 04:32:10 -07:00
}
2024-05-21 11:33:09 -07:00
public async Task<bool> CreateGuest()
2024-05-21 04:32:10 -07:00
{
2024-05-22 00:36:04 -07:00
var query = _graphApi.GetQueryByName("CreateGuest", GraphApi.Query.Type.Mutation);
2024-05-21 04:32:10 -07:00
query.SetArgs(new
{
input = new
{
password = "Abc@123"
}
});
2024-05-22 00:36:04 -07:00
var request = await _graphApi.Post(query);
2024-05-21 11:33:09 -07:00
if (request.result == UnityWebRequest.Result.Success)
{
var receive = GetData(request.downloadHandler.text);
if (receive.data != null)
{
2024-05-22 00:36:04 -07:00
var loginDetails = receive.data["createGuest"]!.ToObject<LoginDetails>();
_userId = loginDetails.user.id;
_userAccessToken = loginDetails.accessToken;
2024-05-21 11:33:09 -07:00
return true;
}
}
return false;
2024-05-21 04:32:10 -07:00
}
2024-05-21 11:33:09 -07:00
public async Task<bool> JoinPromotion()
2024-05-21 04:32:10 -07:00
{
2024-05-22 00:36:04 -07:00
var query = _graphApi.GetQueryByName("JoinPromotion", GraphApi.Query.Type.Mutation);
2024-05-21 04:32:10 -07:00
query.SetArgs(new
{
input = new
{
promotionId = _promotionId
}
});
2024-05-22 00:36:04 -07:00
_graphApi.SetAuthToken(_userAccessToken);
var request = await _graphApi.Post(query);
2024-05-21 11:33:09 -07:00
if (request.result == UnityWebRequest.Result.Success)
{
var receive = GetData(request.downloadHandler.text);
if (receive.errors == null)
{
_startTime = DateTime.Now;
return true;
}
}
return false;
2024-05-21 04:32:10 -07:00
}
2024-05-21 11:33:09 -07:00
public async Task<bool> SubmitGameSession(int gameScore)
2024-05-21 04:32:10 -07:00
{
2024-05-21 11:33:09 -07:00
var endTime = DateTime.Now.AddSeconds(-1);
2024-05-22 00:36:04 -07:00
var query = _graphApi.GetQueryByName("SubmitGameSession", GraphApi.Query.Type.Mutation);
2024-05-21 04:32:10 -07:00
query.SetArgs(new
{
input = new
{
2024-05-21 11:33:09 -07:00
playerId = _userId,
promotionId = _promotionId,
startAt = _startTime,
endAt = endTime,
score = gameScore,
2024-05-21 04:32:10 -07:00
}
});
2024-05-22 00:36:04 -07:00
_graphApi.SetAuthToken(_userAccessToken);
var request = await _graphApi.Post(query);
2024-05-21 11:33:09 -07:00
if (request.result == UnityWebRequest.Result.Success)
{
var receive = GetData(request.downloadHandler.text);
if (receive.errors == null)
{
return true;
}
}
return false;
}
public async void GetQrLink()
{
await GuestUpdatedSubscription();
2024-05-21 04:32:10 -07:00
}
2024-05-22 00:36:04 -07:00
private async Task<Texture2D> GuestUpdatedSubscription()
2024-05-21 04:32:10 -07:00
{
2024-05-22 00:36:04 -07:00
var query = _graphApi.GetQueryByName("GuestUpdatedSubscription", GraphApi.Query.Type.Subscription);
2024-05-21 11:33:09 -07:00
query.SetArgs(new
{
2024-05-22 00:36:04 -07:00
guestId = _userId,
});
_graphApi.SetAuthToken(_machineAccessToken);
var socket = await _graphApi.Subscribe(query);
if (socket.State == WebSocketState.Open)
{
var link = $"https://play4promo.online/brands/{_promotionId}/scan-qr?token={_userAccessToken}";
Debug.Log(link);
return EncodeTextToQrCode(link);
}
return null;
}
private Color32 [] Encode(string textForEncoding, int width, int height)
{
BarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
2024-05-21 11:33:09 -07:00
{
2024-05-22 00:36:04 -07:00
Height = height,
Width = width
2024-05-21 11:33:09 -07:00
}
2024-05-22 00:36:04 -07:00
};
return writer.Write(textForEncoding);
}
private Texture2D EncodeTextToQrCode(string input, int width = 256, int height = 256)
{
if (String.IsNullOrWhiteSpace(input))
{
Debug.Log("You should write something");
return null;
}
var texture = new Texture2D(width, height);
Color32 [] convertPixelToTexture = Encode(input, texture.width, texture.height);
texture.SetPixels32(convertPixelToTexture);
texture.Apply();
return texture;
2024-05-21 04:32:10 -07:00
}
}
}