using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
using UnityEngine.SocialPlatforms;
using UnityEngine.Networking;
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
public class packet
{
public int cmd;
}
public class res_join : packet
{
public int errorno;
}
public class res_login : packet
{
public int errorno;
public string nickname;
}
public class res_uploadimage : packet
{
public int errorno;
}
public class res_getimage : packet
{
public int errorno;
public string imagebit;
}
public class req_join : packet
{
public string uid;
public int androidlogin;
public string androidid;
public string nickname;
}
public class req_login : packet
{
public string uid;
public int androidlogin;
public string androidid;
}
public class req_uploadimage : packet
{
public string uid;
public string imagebit;
}
public class req_getimage : packet
{
public string uid;
public int imageid;
}
public class App : MonoBehaviour
{
public Button btnSendImage;
public Button btnGetImage;
public Button btnLoginGuest;
public Button btnLoginGoogle;
public Text txtNickName;
public Text txtNull;
public GameObject welcome;
public Image image;
[SerializeField]
public Sprite exampleImage;
private string serverPath = "http://127.0.0.1:3005";
private string uid;
private string androidId;
// Start is called before the first frame update
void Start()
{
this.SetBtnsEvent();
this.Init();
}
private void Init()
{
this.uid = SystemInfo.deviceUniqueIdentifier;
this.androidId = PlayGamesPlatform.Instance.localUser.id;
Debug.Log(this.uid);
Debug.Log(this.androidId.Length);
if (this.androidId == "")
{
this.Login((resultLogin) =>
{
if (resultLogin == false)
{
this.txtNull.gameObject.SetActive(true);
this.btnLoginGoogle.gameObject.SetActive(true);
this.btnLoginGuest.gameObject.SetActive(true);
}
else
{
this.txtNull.gameObject.SetActive(false);
this.welcome.SetActive(true);
this.btnLoginGoogle.gameObject.SetActive(false);
this.btnLoginGuest.gameObject.SetActive(false);
this.btnSendImage.gameObject.SetActive(true);
this.btnGetImage.gameObject.SetActive(true);
}
});
}
}
private void SetBtnsEvent()
{
//이미지보내기
this.btnSendImage.onClick.AddListener(() =>
{
//this.UploadImage((result) =>
//{
//});
});
//이미지저장
this.btnGetImage.onClick.AddListener(() =>
{
});
//게스트로그인
this.btnLoginGuest.onClick.AddListener(() =>
{
this.Join((resultJoin) =>
{
if(resultJoin)
{
this.Login((resultLogin) =>
{
if (resultLogin)
{
this.txtNull.gameObject.SetActive(false);
this.welcome.SetActive(true);
this.btnLoginGoogle.gameObject.SetActive(false);
this.btnLoginGuest.gameObject.SetActive(false);
this.btnSendImage.gameObject.SetActive(true);
this.btnGetImage.gameObject.SetActive(true);
}
});
}
});
});
//구글로그인
this.btnLoginGoogle.onClick.AddListener(() =>
{
Social.localUser.Authenticate((bool success) =>
{
if(success)
{
this.Join((resultJoin) =>
{
this.Login((resultLogin) =>
{
if (resultLogin)
{
this.txtNull.gameObject.SetActive(false);
this.welcome.SetActive(true);
this.btnLoginGoogle.gameObject.SetActive(false);
this.btnLoginGuest.gameObject.SetActive(false);
this.btnSendImage.gameObject.SetActive(true);
this.btnGetImage.gameObject.SetActive(true);
}
});
}
, true);
}
else
{
Debug.Log("구글 연결실패");
this.txtNull.text = "구글 연동에 실패했습니다.";
}
});
});
}
private void GetImgae(Action<bool> OnCompleteGetImage)
{
var reqGetImage = new req_getimage();
reqGetImage.cmd = 1300;
reqGetImage.uid = this.uid;
reqGetImage.imageid = 1;
var json = JsonConvert.SerializeObject(reqGetImage);
StartCoroutine(this.Post("api/getimage", json, (result) =>
{
var responseResult = JsonConvert.DeserializeObject<res_getimage>(result);
if(responseResult.cmd == 200)
{
Debug.Log("get image 성공");
byte[] imageData = Convert.FromBase64String(responseResult.imagebit);
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false, true);
texture.hideFlags = HideFlags.HideAndDontSave;
texture.filterMode = FilterMode.Point;
texture.LoadImage(imageData);
var rect = new Rect(0, 0, texture.width, texture.height);
this.image.sprite = Sprite.Create(texture, rect, new Vector2(0.5f, 0.5f));
OnCompleteGetImage(true);
}
else
{
Debug.Log("get 실패");
Debug.Log(responseResult.errorno);
OnCompleteGetImage(false);
}
}));
}
private void UploadImage(Action<bool> OnCompleteUpload)
{
var reqUploadImage = new req_uploadimage();
reqUploadImage.cmd = 1400;
reqUploadImage.uid = this.uid;
var sprite = this.exampleImage;
var texture = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height);
var pixels = sprite.texture.GetPixels((int)sprite.textureRect.x, (int)sprite.textureRect.y
, (int)sprite.textureRect.width, (int)sprite.textureRect.height);
texture.SetPixels(pixels);
texture.Apply();
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, texture);
var bytes = ms.ToArray();
var data = Convert.ToBase64String(bytes);
StartCoroutine(this.Post("api/uploadimage", data, (result) =>
{
var responseResult = JsonConvert.DeserializeObject<res_uploadimage>(result);
if(responseResult.cmd == 200)
{
Debug.Log("upload 성공!");
OnCompleteUpload(true);
}
else
{
Debug.Log("업로드실패");
Debug.Log(responseResult.errorno);
OnCompleteUpload(false);
}
}));
}
private void Login(Action<bool> OnCompleteLogin, bool androidLogin = false)
{
var reqLogin = new req_login();
reqLogin.cmd = 1100;
reqLogin.uid = this.uid;
if (androidLogin)
{
reqLogin.androidlogin = 1;
reqLogin.androidid = PlayGamesPlatform.Instance.localUser.id;
}
else
{
reqLogin.androidlogin = 0;
}
var json = JsonConvert.SerializeObject(reqLogin);
StartCoroutine(this.Post("api/login", json, (result) =>
{
var responseResult = JsonConvert.DeserializeObject<res_login>(result);
if (responseResult.cmd == 200)
{
Debug.Log("로그인성공");
this.txtNickName.text = responseResult.nickname;
OnCompleteLogin(true);
}
if (responseResult.errorno == 9001)
{
Debug.Log("uid가 존재하지 않습니다.");
OnCompleteLogin(false);
}
}));
}
private void Join(Action<bool> OnCompleteJoin, bool androidLogin = false)
{
var reqJoin = new req_join();
reqJoin.cmd = 1200;
reqJoin.uid = this.uid;
reqJoin.nickname = "홍길동";
if (androidLogin)
{
reqJoin.androidlogin = 1;
reqJoin.androidid = PlayGamesPlatform.Instance.localUser.id;
}
else
{
reqJoin.androidlogin = 0;
reqJoin.androidid = "x";
}
var json = JsonConvert.SerializeObject(reqJoin);
StartCoroutine(this.Post("api/join", json, (result) =>
{
var responseResult = JsonConvert.DeserializeObject<res_join>(result);
if (responseResult.cmd == 200)
{
Debug.Log("회원가입성공");
OnCompleteJoin(true);
}
if (responseResult.errorno == 1062)
{
Debug.Log("중복아이디");
OnCompleteJoin(false);
}
}));
}
private IEnumerator Post(string uri, string data, Action<string> OnResponse)
{
var url = string.Format("{0}/{1}", this.serverPath, uri);
var req = new UnityWebRequest(url, "POST");
byte[] body = Encoding.UTF8.GetBytes(data);
req.uploadHandler = new UploadHandlerRaw(body);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
OnResponse(req.downloadHandler.text);
}
}