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

226 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-06-19 02:32:17 -07:00
using GadGame.Event.Customs;
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-06-18 23:34:19 -07:00
// [SerializeField] private string _machineMac;
2024-05-21 04:32:10 -07:00
[SerializeField] private string _promotionId;
2024-06-19 02:32:17 -07:00
[SerializeField] private GuestEvent _guestUpdatedSubscription;
2024-05-21 04:32:10 -07:00
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
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 04:06:59 -07:00
var Jobj = JObject.Parse(dataReceived.data);
2024-05-24 01:16:15 -07:00
Debug.Log(Jobj);
2024-05-22 04:06:59 -07:00
var data = Jobj["payload"]!["data"]!["guestUpdatedSubscription"]!.ToObject<Guest>();
2024-06-19 02:32:17 -07:00
_guestUpdatedSubscription.Raise(data);
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);
}
2024-06-18 23:34:19 -07:00
public async Task<bool> LoginMachine(string _machineMac)
2024-05-21 11:33:09 -07:00
{
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,
2024-06-18 23:34:19 -07:00
password = "Sangta@123"
2024-05-21 04:32:10 -07:00
}
});
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
{
2024-06-18 23:34:19 -07:00
password = "Sangta@123"
2024-05-21 04:32:10 -07:00
}
});
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-06-27 01:08:10 -07:00
Debug.Log(_userAccessToken);
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-06-27 01:08:10 -07:00
var endTime = DateTime.Now;
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
2024-05-22 04:06:59 -07:00
_graphApi.SetAuthToken(_machineAccessToken);
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.errors == null)
{
return true;
}
}
return false;
}
2024-05-22 04:06:59 -07:00
public async Task<Texture2D> GetQrLink()
2024-05-21 11:33:09 -07:00
{
2024-05-22 04:06:59 -07:00
return 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)
{
2024-06-27 01:08:10 -07:00
var imageNameFile = UdpSocket.Instance.DataReceived.FileName;
var link = $"https://play4promo.online/brands/{_promotionId}/scan-qr?token={_userAccessToken}&img={imageNameFile}";
2024-05-22 00:36:04 -07:00
Debug.Log(link);
return EncodeTextToQrCode(link);
}
return null;
}
2024-06-19 02:32:17 -07:00
private Color32 [] Encode(string textForEncoding, int width, int height)
2024-05-22 00:36:04 -07:00
{
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
}
}
}