using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using KTGMGemClient;
|
using LitJson;
|
using UnityEngine;
|
|
public class FinalHttp : MonoBehaviour
|
{
|
Action<List<HttpGetInfo>, int, HttpGetInfo> callBack;
|
int _limit;
|
public void Init(Action<List<HttpGetInfo>, int, HttpGetInfo> ac, int limit)
|
{
|
isSending = false;
|
callBack = ac;
|
_limit = limit;
|
}
|
private JsonData loginData;
|
private bool isSending = false;
|
void LoadImei()
|
{
|
if (Application.platform == RuntimePlatform.Android)
|
{
|
//GetetDeviceIMEI();//获取安卓手机IMEI
|
|
GameConfig.Imei = TDAA_SDKManager.Ins.GetDeviceId();//使用设备ID
|
}
|
else if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
|
{
|
//为了方便测试
|
GameConfig.Imei = PlayerPrefs.GetString("GemBattlePlayerNickName");
|
Debug.Log("这里是电脑版,直接设置Imei为昵称" + GameConfig.Imei);
|
}
|
|
Debug.Log("设置imei0:" + GameConfig.Imei);
|
}
|
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="nickname"></param>
|
/// <param name="score"></param>
|
/// <param name="level"></param>
|
public void SendPost(string nickname, int score, int level, string waveInfo)
|
{
|
if (!GameConfig.useSDK)
|
LoadImei();
|
Debug.Log("开始Post" + nickname);
|
|
string url = "http://9377-big-data.sbk-h5.com:8600/users/addrank";
|
Dictionary<string, object> keyValues = new Dictionary<string, object>();
|
keyValues.Add("username", GameConfig.Imei);//唯一ID
|
keyValues.Add("nickname", nickname);//昵称
|
keyValues.Add("score", score);//分数
|
keyValues.Add("level", level);//等级,默认1就行
|
keyValues.Add("extra", waveInfo);//1关 1波
|
|
|
|
HttpHelper.Request(this, url, HttpHelper.MethodType.POST, keyValues, delegate (object value)
|
{
|
if (value != null)
|
LoadPostTxt(value.ToString());
|
else
|
{
|
Debug.LogError("连接错误");
|
callBack?.Invoke(null, 0, null);
|
}
|
|
}, HttpHelper.DownloadHanlderType.kHttpTEXT);
|
|
}
|
string playerPrefs_NickName = "GemBattlePlayerNickName";
|
|
private void LoadPostTxt(string value)
|
{
|
Debug.Log(value);
|
loginData = JsonMapper.ToObject(value);
|
if (loginData["error"].ToString() != "0")
|
{
|
Debug.LogError("失败了,这里是否需要重置名字???");
|
//PlayerPrefs.SetString(playerPrefs_NickName, "");
|
callBack?.Invoke(null, 0, null);
|
}
|
else if (loginData["msg"].ToString() == "OK")
|
{
|
Debug.Log("成功了");
|
SendGet();
|
}
|
}
|
|
/// <summary>
|
/// 下载排行榜
|
/// </summary>
|
private void SendGet()
|
{
|
string url = "http://9377-big-data.sbk-h5.com:8600/users/getrank";//9377-big-data.sbk-h5.com:8600/users/getrank?username=0001&limit=100
|
Dictionary<string, object> keyValues = new Dictionary<string, object>();
|
keyValues.Add("username", GameConfig.Imei);
|
keyValues.Add("limit", _limit);
|
|
HttpHelper.Request(this, url, HttpHelper.MethodType.GET, keyValues, delegate (object value)
|
{
|
if (value != null)
|
LoadGetTxt(value.ToString());
|
else
|
{
|
Debug.LogError("连接错误");
|
callBack?.Invoke(null, 0, null);
|
}
|
}, HttpHelper.DownloadHanlderType.kHttpTEXT);
|
|
}
|
|
private void LoadGetTxt(string value)
|
{
|
JsonData getData = JsonMapper.ToObject(value);
|
int myrank = int.Parse(getData["myrank"].ToString());
|
Debug.Log("当前排名:" + myrank);
|
|
JsonData mydata = getData["mydata"];
|
HttpGetInfo mydataHttpInfo = new HttpGetInfo(mydata);
|
|
JsonData rank = getData["rank"];
|
List<HttpGetInfo> allHttpGetInfoLis = new List<HttpGetInfo>();
|
HttpGetInfo info;
|
//string tmpStr;
|
foreach (JsonData item in rank)
|
{
|
info = new HttpGetInfo(item);
|
// info.id = int.Parse(item["id"].ToString());
|
// info.username = item["username"].ToString();
|
// info.nickname = item["nickname"].ToString();
|
// info.score = int.Parse(item["score"].ToString());
|
// info.level = int.Parse(item["level"].ToString());
|
|
// tmpStr = item["extra"].ToString();
|
// tmpStr = tmpStr.Remove(0, 1);
|
// tmpStr = tmpStr.Remove(tmpStr.Length - 1, 1);
|
|
// info.waveInfo = tmpStr;
|
// info.create_time = int.Parse(item["create_time"].ToString());
|
allHttpGetInfoLis.Add(info);
|
}
|
|
if (callBack != null)
|
{
|
Debug.Log($"下载到的列表个数:{allHttpGetInfoLis.Count}");
|
callBack(allHttpGetInfoLis, myrank, mydataHttpInfo);
|
}
|
}
|
|
}
|
|
public class HttpGetInfo
|
{
|
public int id;
|
public string username;
|
public string nickname;
|
public int score;
|
public int level;
|
public JsonData extra;
|
public string waveInfo;
|
public int create_time;
|
|
public HttpGetInfo(JsonData data)
|
{
|
string tmpStr;
|
id = int.Parse(data["id"].ToString());
|
username = data["username"].ToString();
|
nickname = data["nickname"].ToString();
|
score = int.Parse(data["score"].ToString());
|
level = int.Parse(data["level"].ToString());
|
|
tmpStr = data["extra"].ToString();
|
tmpStr = tmpStr.Remove(0, 1);
|
tmpStr = tmpStr.Remove(tmpStr.Length - 1, 1);
|
|
waveInfo = tmpStr;
|
create_time = int.Parse(data["create_time"].ToString());
|
}
|
}
|