250 lines
7.8 KiB
C#
250 lines
7.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using Org.BouncyCastle.Asn1.Crmf;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.SocialPlatforms.Impl;
|
|
|
|
public class UserDataManager : MonoBehaviour
|
|
{
|
|
public const int FRIENDS_REFRESH_RATE = 15;
|
|
public const int INVITES_REFRESH_RATE = 7;
|
|
public const string API_ENDPOINT = "https://vps.playpoolstudios.com/metahunt/api/";
|
|
public static string GetApiUrl(string filename){
|
|
return $"{API_ENDPOINT}{filename}.php";
|
|
}
|
|
public static UserDataManager instance;
|
|
void Awake(){
|
|
if(instance !=null){Destroy(this);return;}
|
|
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
public int UserID {get; private set;}
|
|
public string Username {get; private set;}
|
|
public string DisplayName{get; private set;}
|
|
public int Kills{get; private set;}
|
|
public int Deaths{get; private set;}
|
|
|
|
// public List<string> FriendsRequests{get; private set;}
|
|
public List<Friendship> Friends {get; private set;}
|
|
public List<Friendship> FriendRequests {get; private set;}
|
|
|
|
public List<Invite> Invites{get;private set;}
|
|
|
|
public UnityAction OnFriendsUpdated = ()=>{};
|
|
public UnityAction OnInvitesUpdated = ()=>{};
|
|
|
|
float friend_refresh_timer = 0;
|
|
float invite_refresh_timer =0;
|
|
|
|
void Start(){
|
|
friend_refresh_timer = FRIENDS_REFRESH_RATE;
|
|
invite_refresh_timer = INVITES_REFRESH_RATE;
|
|
}
|
|
|
|
void Update(){
|
|
if(friend_refresh_timer < FRIENDS_REFRESH_RATE){
|
|
friend_refresh_timer +=Time.deltaTime;
|
|
}else{
|
|
RefreshFriends();
|
|
friend_refresh_timer=0;
|
|
}
|
|
|
|
if(invite_refresh_timer < INVITES_REFRESH_RATE){
|
|
invite_refresh_timer += Time.deltaTime;
|
|
}else{
|
|
RefreshInvites();
|
|
invite_refresh_timer =0;
|
|
}
|
|
}
|
|
public void RefreshInvites(){
|
|
StartCoroutine(CoroutineGetInvites());
|
|
}
|
|
|
|
IEnumerator CoroutineGetInvites(){
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("uid", UserID);
|
|
|
|
WWW req = new WWW(GetApiUrl("get_invites"), form);
|
|
yield return req;
|
|
|
|
Debug.Log("Invite response:\n" + req.text);
|
|
HandleInviteResponse(req.text);
|
|
}
|
|
|
|
void HandleInviteResponse(string response){
|
|
Invites = new List<Invite>();
|
|
try{
|
|
Invites = JsonConvert.DeserializeObject<List<Invite>>(response);
|
|
|
|
|
|
}catch(Exception e){
|
|
Debug.LogError("Couldn't parse invites");
|
|
Debug.LogError(e.ToString());
|
|
}
|
|
|
|
OnInvitesUpdated.Invoke();
|
|
}
|
|
|
|
public void RefreshFriends(){
|
|
StartCoroutine(CoroutineGetFriends());
|
|
}
|
|
IEnumerator CoroutineGetFriends(){
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("uid", UserID);
|
|
|
|
// WWW requestsReq = new WWW(GetApiUrl("get_friend_requests"),form);
|
|
// yield return requestsReq;
|
|
|
|
// Debug.Log(requestsReq.text);
|
|
// if(requestsReq.text == "0"){
|
|
// //no one is looking for you!
|
|
// FriendsRequests = new List<string>();
|
|
// }else{
|
|
// FriendsRequests = JsonConvert.DeserializeObject<List<string>>(requestsReq.text);
|
|
// }
|
|
|
|
WWW friendsReq = new WWW(GetApiUrl("get_friends"),form);
|
|
yield return friendsReq;
|
|
Debug.Log(friendsReq.text);
|
|
List<Friendship> allFriends = new List<Friendship>();
|
|
if(friendsReq.text == "0"){
|
|
|
|
}else{
|
|
allFriends = JsonConvert.DeserializeObject<List<Friendship>>(friendsReq.text);
|
|
}
|
|
|
|
FriendRequests = new List<Friendship>();
|
|
Friends = new List<Friendship>();
|
|
foreach(Friendship friendship in allFriends){
|
|
if(friendship.status == 0){
|
|
FriendRequests.Add(friendship);
|
|
}else if(friendship.status == 1){
|
|
Friends.Add(friendship);
|
|
}
|
|
}
|
|
|
|
OnFriendsUpdated.Invoke();
|
|
}
|
|
|
|
public void AcceptFriendRequest(int id, bool deny =false){
|
|
foreach(Friendship request in FriendRequests){
|
|
if(request.id == id){
|
|
FriendRequests.Remove(request);
|
|
OnFriendsUpdated.Invoke();
|
|
|
|
break;
|
|
}
|
|
}
|
|
StartCoroutine(CoroutineAcceptFriendRequest(id,deny));
|
|
}
|
|
|
|
IEnumerator CoroutineAcceptFriendRequest(int id,bool deny){
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("id", id);
|
|
|
|
WWW req = new WWW(GetApiUrl(deny?"block_friend_by_id" :"accept_friend_by_id"),form);
|
|
yield return req;
|
|
|
|
if(req.text == "0"){
|
|
//SUCCESS
|
|
RefreshFriends();
|
|
}
|
|
}
|
|
|
|
public void Login(string username, string password, Action OnLoginSuccess, Action<Exception> OnLoginFailed){
|
|
StartCoroutine(Coroutine_Login(username, password,OnLoginSuccess,OnLoginFailed));
|
|
}
|
|
|
|
private Dictionary<string,string> userJson = new Dictionary<string, string>();
|
|
public string GetObjectFromUserJson(string key){
|
|
if(userJson.ContainsKey(key)){
|
|
return userJson[key];
|
|
}else{
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public IEnumerator Coroutine_Login(string username, string password, Action OnLoginSuccess, Action<Exception> OnLoginFailed){
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("username",username);
|
|
form.AddField("password", password);
|
|
|
|
WWW req = new WWW(GetApiUrl("login"), form);
|
|
yield return req;
|
|
Debug.Log("LoginRes: " +req.text);
|
|
|
|
try{
|
|
Dictionary<string,string> jsonObj = JsonConvert.DeserializeObject<Dictionary<string,string>>(req.text);
|
|
Username = jsonObj["username"];
|
|
UserID = int.Parse(jsonObj["id"]);
|
|
LoginManager.username = Username;
|
|
DisplayName = jsonObj["display_name"];
|
|
Kills = int.Parse(jsonObj["kills"]);
|
|
Deaths = int.Parse(jsonObj["deaths"]);
|
|
|
|
|
|
OnLoginSuccess.Invoke();
|
|
userJson = jsonObj;
|
|
|
|
RefreshFriends();
|
|
}catch(Exception e){
|
|
Debug.LogError("Login failed : " + e.GetType().ToString());
|
|
Debug.LogError(e.Message);
|
|
|
|
OnLoginFailed.Invoke(e);
|
|
}
|
|
}
|
|
|
|
public void Register(string username, string password, Action OnRegisterSuccess, Action<string> OnRegisterFailed){
|
|
StartCoroutine(Coroutine_Reg(username,password, OnRegisterSuccess, OnRegisterFailed));
|
|
}
|
|
|
|
public IEnumerator Coroutine_Reg(string username, string password, Action OnRegisterSuccess, Action<string> OnRegisterFailed){
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("username",username);
|
|
form.AddField("password", password);
|
|
|
|
WWW req = new WWW(GetApiUrl("register"), form);
|
|
Debug.Log(req.url);
|
|
yield return req;
|
|
Debug.Log("RegRes: " +req.text);
|
|
|
|
if(req.text.Contains("0")){
|
|
OnRegisterSuccess.Invoke();
|
|
}else{
|
|
OnRegisterFailed.Invoke(req.text);
|
|
}
|
|
}
|
|
|
|
public void AddKill(){
|
|
if(string.IsNullOrEmpty(Username)){Debug.LogError("Not Logged in to add kills!");return;}
|
|
|
|
StartCoroutine(Coroutine_AddStat("kill",(newVal)=>{Kills= newVal;}));
|
|
}
|
|
|
|
public void AddDeath(){
|
|
if(string.IsNullOrEmpty(Username)){Debug.LogError("Not Logged in to add deaths!");return;}
|
|
|
|
StartCoroutine(Coroutine_AddStat("death",(newVal)=>{Deaths= newVal;}));
|
|
}
|
|
|
|
IEnumerator Coroutine_AddStat(string statname, Action<int> OnNewVal){
|
|
WWWForm form = new WWWForm();
|
|
form.AddField("username",Username);
|
|
|
|
WWW req = new WWW(GetApiUrl("add_"+statname), form);
|
|
yield return req;
|
|
|
|
int newVal = -1;
|
|
if(int.TryParse(req.text, out newVal)){
|
|
OnNewVal.Invoke(newVal);
|
|
}else{
|
|
OnNewVal.Invoke(-1);
|
|
}
|
|
}
|
|
}
|