using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class NetMgr
{
//单列
private static NetMgr instance=new NetMgr();
public static NetMgr Instance => instance;
private string Url= "http://192.168.2.24:8080/";
//登录获取玩家数据
private string lpginPlayerDataUrl = "http://192.168.2.24:8080/register";
//获取一个玩家模板
private string playerTempUrl = "http://192.168.2.24:8080/fh";
//改变玩家数据
private string changePlayerDataUrl = "http://192.168.2.24:8080/change_player_data";
//获取排行榜数据
private string rankPostUrl = "http://192.168.2.24:8080/get_user_Scends";
//获取总排行榜数据
private string rankAllGetUrl = "http://192.168.2.24:8080/get_allrank_Scends";
//获取所有关卡
private string allLevUrl = "http://192.168.2.24:8080/get_allLev_name";
//获取关卡萌宠信息
private string levPetInfoUrl = "http://192.168.2.24:8080/getLevDataById";
//提现记录上传
private string goldRealPostUrl = "http://192.168.2.24:8080/uploadGoldRealRecord";
//提现记录获取
private string goldRealGetUrl = "http://192.168.2.24:8080/getGoldRealRecord";
//分数上传
private string scoreUploadPostUrl = "http://192.168.2.24:8080/uploadScore";
//公告信息获取
private string noticeMsgUrl = "http://192.168.2.24:8080/getAnnouncement";
//邮件
private string mailUrl = "http://192.168.2.24:8080/claimReward";
//邀请
private string inviteUrl = "http://192.168.2.24:8080/HandleInvite";
//意见反馈
private string ideaUrl = "http://192.168.2.24:8080/get_idea_msg";
private NetMgr()
{
}
///
/// 登录获取的玩家数据
///
///
public IEnumerator PlayerDataPost(LoginMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg,JsonType.JsonUtlity);
using (UnityWebRequest req = new UnityWebRequest(lpginPlayerDataUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查请求结果
if (req.result == UnityWebRequest.Result.Success)
{
// 处理服务器响应
string str = req.downloadHandler.text;
GameDataMgr.Instance.player = JsonMgr.Instance.LoadData(str);
}
else
{
Debug.LogError($"Error: {req.error}"); // 打印错误信息
}
}
}
///
/// 获取一个玩家模板
///
///
public IEnumerator PlayerTempGet()
{
using (UnityWebRequest req = UnityWebRequest.Get(playerTempUrl))
{
// 发送请求并等待响应
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查请求结果
if (req.result == UnityWebRequest.Result.Success)
{
// 处理服务器响应
string str = req.downloadHandler.text;
GameDataMgr.Instance.playerTemp = JsonMgr.Instance.LoadData(str);
}
else
{
Debug.LogError($"Error: {req.error}"); // 打印错误信息
}
}
}
///
/// 玩家数据改变发送消息
///
///
public IEnumerator ChangeDataPost(PlayerData player)
{
string strMsg = JsonMgr.Instance.SaveData(player);
using (UnityWebRequest req = new UnityWebRequest(changePlayerDataUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查请求结果
if (req.result == UnityWebRequest.Result.Success)
{
// 处理服务器响应
string str = req.downloadHandler.text;
GameDataMgr.Instance.player = JsonMgr.Instance.LoadData(str);
//金币变化
UIManager.Instance.GetPanel().UpdateGold();
}
else
{
Debug.LogError($"Error: {req.error}"); // 打印错误信息
}
}
}
///
/// 给服务器发送提现数据
///
///
public IEnumerator GoldRealPost(GoldRealMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg);
using (UnityWebRequest req = new UnityWebRequest(goldRealPostUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查请求结果
if (req.result == UnityWebRequest.Result.Success)
{
}
else
{
Debug.LogError($"Error: {req.error}"); // 打印错误信息
}
}
}
///
/// 获取提现记录
///
///
///
public IEnumerator GoldRealGet(GoldRealMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg);
using (UnityWebRequest req = new UnityWebRequest(goldRealGetUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查请求结果
if (req.result == UnityWebRequest.Result.Success)
{
// 处理服务器响应
string str = req.downloadHandler.text;
GameDataMgr.Instance.goldRealList = JsonMgr.Instance.LoadData>(str);
//更新面板
if (GameDataMgr.Instance.goldRealList.Count > 0)
{
UIManager.Instance.GetPanel().UpdatePanel(GameDataMgr.Instance.goldRealList);
}
}
else
{
Debug.LogError($"Error: {req.error}"); // 打印错误信息
}
}
}
///
/// 获取所有关卡
///
///
public IEnumerator AllLevGet()
{
using (UnityWebRequest req = UnityWebRequest.Get(allLevUrl))
{
// 发送请求并等待响应
yield return req.SendWebRequest();
// 检查请求结果
if (req.result == UnityWebRequest.Result.Success)
{
// 处理服务器响应
string str = req.downloadHandler.text;
GameDataMgr.Instance.levList = JsonMgr.Instance.LoadData>(str);
}
else
{
Debug.LogError($"Error: {req.error}"); // 打印错误信息
}
}
}
///
/// 获取关卡萌宠信息
///
///
public IEnumerator LevPetInfoPost(LevRankMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg,JsonType.JsonUtlity);
using (UnityWebRequest req = new UnityWebRequest(levPetInfoUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
string js = req.downloadHandler.text;
GameDataMgr.Instance.levData = JsonMgr.Instance.LoadData(js);
GameMgr.Instance.CreatePet();
//隐藏关卡状态面板
if (UIManager.Instance.GetPanel()!=null)
{
UIManager.Instance.HidePanel();
}
//隐藏排行榜关卡面板
if (UIManager.Instance.GetPanel() != null)
{
UIManager.Instance.HidePanel();
}
//隐藏通关面板1
if (UIManager.Instance.GetPanel() != null)
{
UIManager.Instance.HidePanel();
}
//隐藏通关面板1
if (UIManager.Instance.GetPanel() != null)
{
UIManager.Instance.HidePanel();
}
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
///
/// 获取关卡排行榜信息
///
///
public IEnumerator RankPost(LevRankMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg,JsonType.JsonUtlity);
using (UnityWebRequest req = new UnityWebRequest(rankPostUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
string js = req.downloadHandler.text;
GameDataMgr.Instance.rankData = JsonMgr.Instance.LoadData>>(js);
if (GameDataMgr.Instance.rankData==null)
{
GameDataMgr.Instance.rankData=new Dictionary>();
GameDataMgr.Instance.rankData.Add("UserList",new List());
}
UIManager.Instance.GetPanel().UpdatePanel(GameDataMgr.Instance.rankData["UserList"]);
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
///
/// 获取总排行榜数据
///
///
public IEnumerator RankAllGet()
{
using (UnityWebRequest req = UnityWebRequest.Get(rankAllGetUrl))
{
// 发送请求并等待响应
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
string js = req.downloadHandler.text;
GameDataMgr.Instance.rankAllData = JsonMgr.Instance.LoadData>>(js);
UIManager.Instance.ShowPanel().UpdatePanel(GameDataMgr.Instance.rankAllData["UserList"]);
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
///
/// 分数上传,获取关卡的排名
///
///
public IEnumerator ScoreUploadPost(ScoreUploadMsg msg,BasePanel pass)
{
string strMsg = JsonMgr.Instance.SaveData(msg,JsonType.JsonUtlity);
using (UnityWebRequest req = new UnityWebRequest(scoreUploadPostUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
string js = req.downloadHandler.text;
BackRankData scoreRank = JsonMgr.Instance.LoadData(js,JsonType.JsonUtlity);
GamePanel gamePanel = UIManager.Instance.GetPanel();
if (pass is Pass1Panel)
{
(pass as Pass1Panel).UpdatePanel(gamePanel.levInt, gamePanel.scoreInt, scoreRank.rankInt);
}
else if (pass is Pass2Panel)
{
(pass as Pass2Panel).UpdatePanel(gamePanel.levInt, gamePanel.scoreInt, scoreRank.rankInt);
}
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
///
/// 获取公告信息
///
///
public IEnumerator NoticeMasgGet()
{
using (UnityWebRequest req = UnityWebRequest.Get(noticeMsgUrl))
{
// 发送请求并等待响应
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
string js = req.downloadHandler.text;
NoticeMsg noticeMsg = JsonMgr.Instance.LoadData(js,JsonType.JsonUtlity);
UIManager.Instance.ShowPanel().UpdatePanel(noticeMsg.content);
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
///
/// 填写邀请
///
///
public IEnumerator InvitePost(InviteMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg,JsonType.JsonUtlity);
using (UnityWebRequest req = new UnityWebRequest(inviteUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
string js = req.downloadHandler.text;
InviteReturnMsg retMsg = JsonMgr.Instance.LoadData(js,JsonType.JsonUtlity);
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
///
/// 邮件
///
///
public IEnumerator MailPost(CommonMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg,JsonType.JsonUtlity);
using (UnityWebRequest req = new UnityWebRequest(mailUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
string js = req.downloadHandler.text;
if (js.Contains("\"openId\""))
{
CommonMsg common= JsonMgr.Instance.LoadData(js, JsonType.JsonUtlity);
}
else if (js.Contains("\"id\""))
{
MailInfo info = new MailInfo();
info.mailData = JsonMgr.Instance.LoadData(js,JsonType.JsonUtlity);
filePath = Path.Combine(Application.persistentDataPath, GameDataMgr.Instance.player.openId);
LoadMailData();
GameDataMgr.Instance.info.Add(info);
SaveMailData(GameDataMgr.Instance.info);
}
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
///
/// 发送意见建议
///
///
public IEnumerator IdeaPost(IdeaMsg msg)
{
string strMsg = JsonMgr.Instance.SaveData(msg,JsonType.JsonUtlity);
Debug.Log(strMsg);
using (UnityWebRequest req = new UnityWebRequest(ideaUrl, "POST"))
{
// 发送请求并等待响应
byte[] jsonToSend = Encoding.UTF8.GetBytes(strMsg);
req.uploadHandler = new UploadHandlerRaw(jsonToSend);
req.downloadHandler = new DownloadHandlerBuffer();
req.SetRequestHeader("Content-Type", "application/json");
yield return req.SendWebRequest();
// 检查是否有错误
if (req.result == UnityWebRequest.Result.Success)
{
}
else
{
Debug.Log("Data sent successfully. Response: " + req.downloadHandler.text);
}
}
}
private string filePath;
// 保存邮件数据到本地
public void SaveMailData(List info)
{
string json = JsonMgr.Instance.SaveData(info);
File.WriteAllText(filePath, json);
}
// 加载本地邮件数据
private void LoadMailData()
{
if (File.Exists(filePath))
{
string json = File.ReadAllText(filePath);
GameDataMgr.Instance.info = JsonMgr.Instance.LoadData>(json);
if (GameDataMgr.Instance.info == null)
{
GameDataMgr.Instance.info = new List();
}
}
else
{
GameDataMgr.Instance.info = new List();
}
}
}