1
Fork 0
mirror of https://github.com/RYGhub/kei.git synced 2024-12-04 22:14:29 +00:00
kei/Assets/Scripts/Network.cs
2019-12-10 02:02:36 +01:00

64 lines
1.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif
[System.Serializable]
public class KeiResponse {
public string emotion;
public string text;
}
public class Network : MonoBehaviour
{
// USE HTTPS IN ANDROID OR IT WON'T WORK
public string url = "https://ryg.steffo.eu/api/kei";
public string kpid;
public string convid;
public string previous = "";
private Sender sender;
void Awake() {
sender = GetComponent<Sender>();
}
void Start() {
kpid = PlayerPrefs.GetString("kpid", "");
if(kpid == "") {
kpid = System.Guid.NewGuid().ToString();
PlayerPrefs.SetString("kpid", kpid);
}
convid = System.Guid.NewGuid().ToString();
StartCoroutine(PostRequest("", "true"));
}
public IEnumerator PostRequest(string message, string first) {
WWWForm form = new WWWForm();
form.AddField("kpid", kpid);
form.AddField("convid", convid);
form.AddField("message", message);
form.AddField("first", first);
form.AddField("previous", previous);
UnityWebRequest request = UnityWebRequest.Post(url, form);
yield return request.SendWebRequest();
if(request.isNetworkError) {
Debug.LogError("Error! " + request.error);
}
else {
try {
KeiResponse kr = JsonUtility.FromJson<KeiResponse>(request.downloadHandler.text);
previous = kr.text;
sender.Change(kr.text, kr.emotion);
}
catch (System.ArgumentException) {
Debug.LogError(request.downloadHandler.text);
}
}
}
}