123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- using System.Collections.Generic;
- using System.Net.NetworkInformation;
- using System.Net.Sockets;
- using UnityEngine;
- public class DataEyeGA
- {
- public enum PlatformType
- {
- IOS = 1,
- ADR = 2,
- WP = 3
- }
- public enum AccountType
- {
- _Anonymous = 0,
- _Registered = 1,
- _SinaWeibo = 2,
- _QQ = 3,
- _QQWeibo = 4,
- _ND91 = 5,
- _GooglePlay = 6,
- _Facebook = 7,
- Type3 = 8,
- Type4 = 9,
- Type5 = 10,
- Type6 = 11,
- Type7 = 12,
- Type8 = 13,
- Type9 = 14,
- Type10 = 15,
- }
- public enum Gender
- {
- _UNKNOWN = 0,
- _MALE = 1,
- _FEMALE = 2
- }
- public enum NetType
- {
- _3G = 1,
- WIFI = 2,
- OTHER = 3,
- _2G = 4,
- _4G = 14
- }
- public enum TaskType
- {
- GuideLine = 1,
- MainLine = 2,
- BranchLine = 3,
- Daily = 4,
- Activity = 5,
- Other = 6,
- }
- public enum RequestType
- {
- ActOrReg,
- Online,
- Pay,
- VirtualCurrency,
- BuyItem,
- UseItem,
- GetItem,
- Task,
- LevelUp,
- CustomEvent,
- }
- public static bool isInited = false;
- public static bool isAccountSet = false;
- private static Dictionary<RequestType, string> requestDict;
- private static string urlPrefix = "http://ext.gdatacube.net/dc/rest/";
- //必填
- private static string appId;
- private static string accountId;
- private static int platform;
- private static string gameRegion;
- private static string channel;
- //选填
- private static string appVersion;
- private static string roleId;
- private static string roleName;
- private static string roleClass;
- private static string roleRace;
- private static int accountType;
- // private static string mac;
- private static string imei;
- private static int gender;
- private static int age;
- private static string resolution;
- private static string osVersion;
- private static string brand;
- private static string language;
- private static int netType;
- private static string ip;
- private static string country;
- private static string province;
- private static string operators;
- public static void MakeDict()
- {
- if (requestDict == null)
- {
- requestDict = new Dictionary<RequestType, string>();
- requestDict.Add(RequestType.ActOrReg, "actOrReg?");
- requestDict.Add(RequestType.Online, "online?");
- requestDict.Add(RequestType.Pay, "pay?");
- requestDict.Add(RequestType.VirtualCurrency, "coin?");
- requestDict.Add(RequestType.BuyItem, "itemBuy?");
- requestDict.Add(RequestType.UseItem, "itemUse?");
- requestDict.Add(RequestType.GetItem, "itemGet?");
- requestDict.Add(RequestType.Task, "task?");
- requestDict.Add(RequestType.LevelUp, "levelUp?");
- requestDict.Add(RequestType.CustomEvent, "event?");
- }
- }
- public static void Init(string appId, PlatformType platform, string gameRegion, string channel)
- {
- MakeDict();
- DataEyeGA.appId = appId;
- DataEyeGA.platform = platform.GetHashCode();
- DataEyeGA.gameRegion = gameRegion;
- DataEyeGA.channel = channel;
- appVersion = Application.version;
- string hostName = System.Net.Dns.GetHostName();
- ip = System.Net.Dns.GetHostEntry(hostName).AddressList.GetValue(0).ToString();
- imei = SystemInfo.deviceUniqueIdentifier;
- resolution = Screen.width + "*" + Screen.height;
- osVersion = SystemInfo.operatingSystem;
- brand = SystemInfo.deviceModel;
- language = Application.systemLanguage.ToString();
- if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)
- netType = NetType._4G.GetHashCode();
- else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)
- netType = NetType.WIFI.GetHashCode();
- isInited = true;
- }
- public static void SetAccount(string accountId, AccountType accountType, bool sendOnline = false)
- {
- if(!CheckIsInited())
- return;
- DataEyeGA.accountId = accountId;
- DataEyeGA.accountType = accountType.GetHashCode();
- if(sendOnline)
- Online();
- isAccountSet = true;
- }
- public static void ActOrReg()
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("actTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- paramList.Add(new KeyValuePair<string, string>("regTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.ActOrReg, paramList);
- }
- public static void Online(int level = 0)
- {
- if(!CheckIsInited())
- return;
- if (DateUtil.GetRealTimeSinceStart() <= 60)
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("loginTime", DateUtil.GetStandardTimeExistByTimeStamp().ToString()));
- paramList.Add(new KeyValuePair<string, string>("onlineTime", DateUtil.GetRealTimeSinceStart().ToString()));
- paramList.Add(new KeyValuePair<string, string>("level", level.ToString()));
- SendUrl(RequestType.Online, paramList);
- }
- /// <param name="currencyAmount"></param>
- /// <param name="currencyType"></param>
- /// <param name="iapid">付费点</param>
- /// <param name="orderId">订单ID</param>
- public static void Pay(int currencyAmount, string currencyType, string iapid, string orderId)
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("currencyAmount", currencyAmount.ToString()));
- paramList.Add(new KeyValuePair<string, string>("currencyType", currencyType));
- paramList.Add(new KeyValuePair<string, string>("iapid", iapid));
- paramList.Add(new KeyValuePair<string, string>("orderId", orderId));
- paramList.Add(new KeyValuePair<string, string>("payTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.Pay, paramList);
- }
- /// <param name="coinNum"></param>
- /// <param name="coinType">虚拟币类型</param>
- /// <param name="type">获得虚拟的途径</param>
- /// <param name="isGain">是否获得 0 消耗 1 获得</param>
- /// <param name="totalCoin">该玩家手里最终持有的货币数量</param>
- public static void VirtualCurrency(string coinNum, string coinType, string type, bool isGain, int totalCoin)
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("coinNum", coinNum));
- paramList.Add(new KeyValuePair<string, string>("coinType", coinType));
- paramList.Add(new KeyValuePair<string, string>("type", type));
- paramList.Add(new KeyValuePair<string, string>("isGain", isGain ? "1" : "0"));
- paramList.Add(new KeyValuePair<string, string>("totalCoin", totalCoin.ToString()));
- paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.VirtualCurrency, paramList);
- }
- /// <param name="itemId"></param>
- /// <param name="itemType"></param>
- /// <param name="itemCnt">购买的道具数量</param>
- /// <param name="coinNum">消耗虚拟币数量</param>
- /// <param name="coinType">虚拟币种类</param>
- public static void BuyItem(string itemId, string itemType, string itemCnt, string coinNum, string coinType)
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
- paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
- paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
- paramList.Add(new KeyValuePair<string, string>("coinNum", coinNum));
- paramList.Add(new KeyValuePair<string, string>("coinType", coinType));
- paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.BuyItem, paramList);
- }
- /// <param name="itemId"></param>
- /// <param name="itemType"></param>
- /// <param name="itemCnt">购买的道具数量</param>
- /// <param name="reason">道具消耗的途径</param>
- public static void UseItem(string itemId, string itemType, string itemCnt, string reason)
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
- paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
- paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
- paramList.Add(new KeyValuePair<string, string>("reason", reason));
- paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.UseItem, paramList);
- }
- /// <param name="itemId"></param>
- /// <param name="itemType"></param>
- /// <param name="itemCnt">购买的道具数量</param>
- /// <param name="reason">道具获得的途径</param>
- public static void GetItem(string itemId, string itemType, string itemCnt, string reason)
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("itemId", itemId));
- paramList.Add(new KeyValuePair<string, string>("itemType", itemType));
- paramList.Add(new KeyValuePair<string, string>("itemCnt", itemCnt));
- paramList.Add(new KeyValuePair<string, string>("reason", reason));
- paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.GetItem, paramList);
- }
- /// <param name="taskId">任务ID</param>
- /// <param name="taskType">任务类型</param>
- /// <param name="duration">任务耗时</param>
- /// <param name="isSucc">是否失败 0 任务失败 1 任务完成</param>
- /// <param name="reason">任务失败原因</param>
- public static void Task(string taskId, TaskType taskType, int duration, bool isSucc, string reason)
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("taskId", taskId));
- paramList.Add(new KeyValuePair<string, string>("taskType", taskType.GetHashCode().ToString()));
- paramList.Add(new KeyValuePair<string, string>("duration", duration.ToString()));
- paramList.Add(new KeyValuePair<string, string>("isSucc", isSucc ? "1" : "0"));
- paramList.Add(new KeyValuePair<string, string>("reason", reason));
- paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.Task, paramList);
- }
- /// <param name="startLevel">开始等级</param>
- /// <param name="endLevel">结束等级</param>
- /// <param name="interval">升级时长(秒)</param>
- public static void LevelUp(int startLevel, int endLevel, int interval)
- {
- if(!CheckIsInited())
- return;
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("startLevel", startLevel.ToString()));
- paramList.Add(new KeyValuePair<string, string>("endLevel", endLevel.ToString()));
- paramList.Add(new KeyValuePair<string, string>("interval", interval.ToString()));
- paramList.Add(new KeyValuePair<string, string>("msgTime", DateUtil.GetCurrentTimeByStandardTime().ToString()));
- SendUrl(RequestType.LevelUp, paramList);
- }
- /// <summary>
- /// 触发自定义事件
- /// </summary>
- /// <param name="eventId">事件id</param>
- /// <param name="duration">事件耗时(秒)</param>
- /// <param name="keyValueList">参数列表</param>
- public static void CustomEvent(string eventId, int duration, List<KeyValuePair<string, string>> labelMapList)
- {
- if(!CheckIsInited())
- return;
-
- List<KeyValuePair<string, string>> paramList = new List<KeyValuePair<string, string>>();
- paramList.Add(new KeyValuePair<string, string>("eventId", eventId));
- paramList.Add(new KeyValuePair<string, string>("duration", duration.ToString()));
- string labelMap = "{";
- for(int i=0; i<labelMapList.Count; i++)
- labelMap += labelMapList[i].Key + ":" + labelMapList[i].Value;
- labelMap += "}";
- paramList.Add(new KeyValuePair<string, string>("labelMap", labelMap));
- SendUrl(RequestType.CustomEvent, paramList);
- }
- private static bool CheckIsInited(){
- if(isInited)
- return true;
- else{
- Debug.Log("<color=#ff6600>DataEyeGA is not Inited!</color>");
- return false;
- }
- }
- private static string SendUrl(RequestType requestType, List<KeyValuePair<string, string>> specialParamList = null)
- {
- string url = urlPrefix + requestDict[requestType];
- url += "appId=" + appId;
- url += "&accountId=" + accountId;
- url += "&platform=" + platform;
- url += "&gameRegion=" + gameRegion;
- url += "&channel=" + channel;
- url += "&appVersion=" + appVersion;
- url += "&accountType=" + accountType;
- // url += "&mac=" + mac;
- url += "&ip=" + ip;
- url += "&imei=" + imei;
- url += "&resolution=" + resolution;
- url += "&osVersion=" + WWW.EscapeURL(osVersion);
- url += "&brand=" + WWW.EscapeURL(brand);
- url += "&language=" + language;
- url += "&netType=" + netType;
- if(specialParamList != null)
- {
- for(int i=0; i<specialParamList.Count; i++)
- url += "&" + specialParamList[i].Key + "=" + WWW.EscapeURL(specialParamList[i].Value);
- }
- //Debuger.LogError("[DataEye GA]" + url);
- URLRequest.CreateStrURLRequest(url, null, null, URLRequest.Method.GET, true);
- return url;
- }
- }
|