Files
Main/Assets/Plugins/References/FuncellSDK/SDK/Extra/SDKStatistics/SDKStatisticsPC.cs

383 lines
16 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
#if UNITY_STANDALONE_WIN
using BestHTTP;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Timers;
using UnityEngine;
using UnityEngine.Gonbest.MagicCube;
namespace Thousandto.CoreSDK
{
public class SDKStatisticsPC : SDKStatistics
{
public SDKStatisticsPC()
{
SendHeartBeats(null, null);
SendOpenGameMsg();
//间隔5分钟调用一次做心跳
Timer timer = new Timer(300000);
//到达时间执行心跳
timer.Elapsed += new ElapsedEventHandler(SendHeartBeats);
timer.Start();
}
public override void StatisticalEvent(DataTypes funcType, string jsonParam)
{
switch (funcType)
{
case DataTypes.DATA_USER_REGISTER:
break;
case DataTypes.DATA_LOGIN:
SendStatisticsParams("203");
break;
case DataTypes.DATA_CREATE_ROLE:
SendStatisticsParams("202");
break;
case DataTypes.DATA_ENTER_GAME:
break;
case DataTypes.DATA_ROLE_LEVELUP:
SendStatisticsParams("205");
break;
case DataTypes.DATA_TUTORIAL:
break;
case DataTypes.DATA_SERVER_ROLE_INFO:
break;
case DataTypes.DATA_RECHARGE:
SendOrderParams();
break;
case DataTypes.DATA_RES_DOWNLOAD_FINISHED:
break;
case DataTypes.DATA_EXIT_GAME:
break;
case DataTypes.CUSTOM_EVENT:
break;
}
}
private void SendStatisticsParams(string opcode)
{
Dictionary<string, string> statisticsDataDict = new Dictionary<string, string>();
statisticsDataDict.Add("accountid", SDKCacheData.UserID);
statisticsDataDict.Add("actorguid", SDKCacheData.RoleID);
statisticsDataDict.Add("actorname", SDKCacheData.RoleName);
statisticsDataDict.Add("gserverid", SDKCacheData.ServerID);
//玩家所在大区id | 1 |
statisticsDataDict.Add("gzoneid", SDKCacheData.QQZoneID);
statisticsDataDict.Add("actorlevel", SDKCacheData.RoleLevel);
statisticsDataDict.Add("party", SDKCacheData.RoleGuidName);
statisticsDataDict.Add("game_version", AppPersistData.AppVersion);
statisticsDataDict.Add("role_sex", SDKCacheData.RoleSex);
statisticsDataDict.Add("role_combat", SDKCacheData.FightPower);
statisticsDataDict.Add("map_id", SDKCacheData.MapId);
//角色创建202 角色登录203 角色升级205
statisticsDataDict.Add("opcode", opcode);
var dict = GetUniversalParamsDict();
var iter = dict.GetEnumerator();
try
{
while (iter.MoveNext())
{
statisticsDataDict.Add(iter.Current.Key, iter.Current.Value);
}
}
finally
{
iter.Dispose();
}
DebugLog("SDKStatisticsPC SendStatisticsParams: ", statisticsDataDict);
HttpPost(STATISTICS_URL, statisticsDataDict);
}
/// <summary>
/// 组装通用的带参数据
/// </summary>
/// <returns></returns>
private Dictionary<string, string> GetUniversalParamsDict()
{
Dictionary<string, string> universalDict = new Dictionary<string, string>();
universalDict.Add("xlogtime", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString());
universalDict.Add("appver", AppPersistData.AppVersion);
universalDict.Add("mobilemodel", SystemInfo.operatingSystem);
universalDict.Add("brand", SystemInfo.deviceName);
var resolutions = Screen.resolutions;
if (resolutions != null && resolutions.Length > 0)
{
//设置为显示器支持的最大分辨率
var size = resolutions[resolutions.Length - 1];
universalDict.Add("screen", string.Format("{0}*{1}", size.width, size.width));
}
universalDict.Add("idfa", SystemInfo.deviceName);
universalDict.Add("gaid", SystemInfo.deviceName);
universalDict.Add("os", SystemInfo.operatingSystem);
universalDict.Add("osv", SystemInfo.operatingSystem);
universalDict.Add("imei", SystemInfo.operatingSystem);
universalDict.Add("device_id", SystemInfo.deviceUniqueIdentifier);
universalDict.Add("appid", AppPersistData.AppVersion);
universalDict.Add("channel_id", SDKCacheData.ChannelID);
universalDict.Add("gameid", SDKCacheData.SecID);
universalDict.Add("sourcetype", SDKCacheData.SourceId);
universalDict.Add("sourceid", SDKCacheData.CPDid);
universalDict.Add("nettype", Application.internetReachability.ToString());
universalDict.Add("ip", GetHostIp());
universalDict.Add("mac", GetMacAddress());
return universalDict;
}
private void SendOrderParams()
{
Dictionary<string, string> orderDict = new Dictionary<string, string>();
orderDict.Add("accountid", SDKCacheData.UserID);
orderDict.Add("actorguid", SDKCacheData.RoleID);
orderDict.Add("actorname", SDKCacheData.RoleName);
orderDict.Add("actorlevel", SDKCacheData.RoleLevel);
orderDict.Add("gzoneid", SDKCacheData.QQZoneID);
orderDict.Add("roleviplevel", SDKCacheData.RoleRealmLv);
orderDict.Add("orderid", SDKCacheData.OrderNo);
orderDict.Add("paymethod", "");
orderDict.Add("money", SDKCacheData.UserID);
orderDict.Add("money_type", "");
orderDict.Add("gamemoney", SDKCacheData.ItemPrice);
orderDict.Add("productid", SDKCacheData.ItemId);
orderDict.Add("productname", SDKCacheData.ItemName);
orderDict.Add("productnum", SDKCacheData.BuyNum);
//opcode 定值 210
orderDict.Add("opcode", "210");
var dict = GetUniversalParamsDict();
var iter = dict.GetEnumerator();
try
{
while (iter.MoveNext())
{
orderDict.Add(iter.Current.Key, iter.Current.Value);
}
}
finally
{
iter.Dispose();
}
DebugLog("SDKStatisticsPC SendOrderParams: ", orderDict);
HttpPost(STATISTICS_URL, orderDict);
}
/// <summary>
/// 给平台那边做心跳操作
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
private void SendHeartBeats(object source, ElapsedEventArgs e)
{
Dictionary<string, string> heartbeatsDict = new Dictionary<string, string>();
heartbeatsDict.Add("accountid", SDKCacheData.UserID);
heartbeatsDict.Add("actorguid", SDKCacheData.RoleID);
heartbeatsDict.Add("actorname", SDKCacheData.RoleName);
heartbeatsDict.Add("gserverid", SDKCacheData.ServerID);
heartbeatsDict.Add("firstacttime", Time.realtimeSinceStartup.ToString());
heartbeatsDict.Add("lastacttime", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString());
heartbeatsDict.Add("gzoneid", SDKCacheData.QQZoneID);
heartbeatsDict.Add("opcode", "204");
var dict = GetUniversalParamsDict();
var iter = dict.GetEnumerator();
try
{
while (iter.MoveNext())
{
heartbeatsDict.Add(iter.Current.Key, iter.Current.Value);
}
}
finally
{
iter.Dispose();
}
DebugLog("SDKStatisticsPC SendHeartBeats: ", heartbeatsDict);
HttpPost(STATISTICS_URL, heartbeatsDict);
}
private void SendOpenGameMsg()
{
bool isFirstOpen = PlayerPrefs.GetInt("IsFirstOpenGame") == 1;
if (!isFirstOpen)
{
PlayerPrefs.SetInt("IsFirstOpenGame", 1);
}
Dictionary<string, string> openGameDict = new Dictionary<string, string>();
if (isFirstOpen)
{
openGameDict.Add("is_first_active", "1");
openGameDict.Add("opcode", "100");
openGameDict.Add("os_version", SystemInfo.operatingSystem);
openGameDict.Add("operator", SystemInfo.operatingSystem);
openGameDict.Add("network", "WIFI");
openGameDict.Add("ip", GetHostIp());
openGameDict.Add("serial", SystemInfo.deviceUniqueIdentifier);
openGameDict.Add("cpu_count", SystemInfo.processorCount.ToString());
openGameDict.Add("cpu_freq", SystemInfo.processorFrequency.ToString());
openGameDict.Add("cpu_model", SystemInfo.processorType);
openGameDict.Add("cpu_mem", SystemInfo.systemMemorySize.ToString());
openGameDict.Add("total_space", "");
openGameDict.Add("free_space", "");
openGameDict.Add("sdk_version", "2.0");
openGameDict.Add("country", "CN");
openGameDict.Add("languages", "CN");
//{"bootTime":"开机时间(单位秒)","brightness":"屏幕亮度(用数字区分亮度)","macAddr":"mac地址","mockGps":"是否允许模拟定位","SimSerialNumber":"设备序列号"}
openGameDict.Add("device_info", "");
openGameDict.Add("mobile_no", "");
openGameDict.Add("imei", "");
openGameDict.Add("imsi", "");
openGameDict.Add("sim_no", "");
openGameDict.Add("net_provider", "");
openGameDict.Add("oaid", "");
openGameDict.Add("app_contract_list", "");
openGameDict.Add("gaid", "");
openGameDict.Add("ipm", "1");
openGameDict.Add("logchn", "device");
openGameDict.Add("appid", SDKCacheData.AppID);
openGameDict.Add("channel_id", SDKCacheData.ChannelID);
openGameDict.Add("platform", "2");
openGameDict.Add("source_id", SDKCacheData.CPDid);
openGameDict.Add("device_id", SystemInfo.deviceUniqueIdentifier);
openGameDict.Add("app_version", SDKCacheData.AppVersion);
openGameDict.Add("is_emu", "1");
openGameDict.Add("os_type", SystemInfo.operatingSystem);
openGameDict.Add("brand", SystemInfo.deviceName);
openGameDict.Add("model", SystemInfo.operatingSystem);
var resolutions = Screen.resolutions;
if (resolutions != null && resolutions.Length > 0)
{
//设置为显示器支持的最大分辨率
var size = resolutions[resolutions.Length - 1];
openGameDict.Add("screen", string.Format("{0}*{1}", size.width, size.width));
}
}
else
{
openGameDict.Add("is_first_active", "0");
openGameDict.Add("opcode", "200");
openGameDict.Add("click_id", "1");
openGameDict.Add("click_name", "点击启动客户端");
var dict = GetUniversalParamsDict();
var iter = dict.GetEnumerator();
try
{
while (iter.MoveNext())
{
openGameDict.Add(iter.Current.Key, iter.Current.Value);
}
}
finally
{
iter.Dispose();
}
}
DebugLog("SDKStatisticsPC SendOpenGameMsg: ", openGameDict);
HttpPost(STATISTICS_URL, openGameDict);
}
private string GetMacAddress()
{
string physicalAddress = "";
NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adaper in nice)
{
if (adaper.Description == "en0")
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
break;
}
else
{
physicalAddress = adaper.GetPhysicalAddress().ToString();
if (physicalAddress != "")
{
break;
};
}
}
return physicalAddress.Replace("-", ".");
}
private string GetHostIp()
{
string ip = "localhost";
try
{
IPHostEntry iphostInfo = Dns.GetHostEntry(Dns.GetHostName());
ip = null;
for (int i = 0; i < iphostInfo.AddressList.Length; ++i)
{
var ipAddress = iphostInfo.AddressList[i];
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
{
ip = ipAddress.ToString();
if (ip.EndsWith(".1"))
{
ip = null;
continue;
}
break;
}
}
}
catch (System.Exception ex)
{
UnityEngine.Debug.LogException(ex);
}
return ip;
}
private void HttpPost(string Url, Dictionary<string, string> postDataDict, Action<string> callback = null)
{
HTTPRequest request = new HTTPRequest(new Uri(Url),
HTTPMethods.Post,
delegate (HTTPRequest originalRequest, HTTPResponse response)
{
string responseStr = response.DataAsText;
Debug.Log("登陆返回:" + responseStr);
if (callback != null)
{
callback(responseStr);
}
}
);
request.AddHeader("ContentType", "application/x-www-form-urlencoded");
request.AddHeader("Charset", "utf-8");
var iter = postDataDict.GetEnumerator();
while (iter.MoveNext())
{
var value = iter.Current.Value;
if(value == null)
{
value = string.Empty;
}
request.AddField(iter.Current.Key, value);
}
request.Send();
}
private void DebugLog(string logTitle, Dictionary<string, string> pramsDict)
{
StringBuilder sb = new StringBuilder();
sb.Append(logTitle);
var dataIter = pramsDict.GetEnumerator();
while (dataIter.MoveNext())
{
var value = dataIter.Current.Value;
if (value == null)
{
value = string.Empty;
}
sb.AppendFormat("{0}:{1} ", dataIter.Current.Key, value);
}
Debug.Log(sb.ToString());
}
}
}
#endif