Main/Assets/Code/Logic/BISystem/BISystem.cs
2025-01-25 04:38:09 +08:00

417 lines
15 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Thousandto.Code.Center;
using Thousandto.Code.Logic;
using Thousandto.Core.Base;
using Thousandto.CoreSDK;
using MSG_BI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Thousandto.Cfg.Data;
using System.Text;
using System;
namespace Thousandto.Code.Logic
{
//打点系统
public class BISystem
{
private ValMap _tempValMap = new ValMap();
//点击数据{[0]=1,[1]=1598607932,[2]=2,[3]=1598607955,...} id、时间戳
private int[] _clickInfoArr = new int[256];
//当前可写入的位置
private int _writePos = 0;
//下一次保存到本地的时间戳
private double _nextSaveUTCTime = 0;
//下一次发送到服务器的时间戳
private double _nextReqUTCTime = 0;
//缓存数量+本地存储数量 的最大值
private int _maxCount = 128;
//发送到服务器的时间间隔 秒
private double _reqSpacingTime = 300;
//保存到本地的时间间隔 秒
private double _saveSpacingTime = 10;
//是否检测
private bool _isCheck = false;
//本地保存的角色Id
private ulong _playerPrefsRoleId = 0;
//当前玩家id
private ulong _curRoleId = 0;
public void Initialize()
{
GameCenter.RegFixEventHandle(Global.LogicEventDefine.EID_EVENT_FIRSTENTERMAP, OnFirstEnterMap);
if (PlayerPrefs.HasKey("[Bi_RoleId]"))
{
var strId = PlayerPrefs.GetString("[Bi_RoleId]");
ulong id = 0;
if (ulong.TryParse(strId, out id))
{
_playerPrefsRoleId = id;
}
else
{
PlayerPrefs.DeleteKey("[Bi_RoleId]");
}
}
}
//首次进入场景
private void OnFirstEnterMap(object param, object sender)
{
_curRoleId = GameCenter.GameSceneSystem.GetLocalPlayerID();
if (_playerPrefsRoleId != 0)
{
ReqAllClickEvent();
}
if (_playerPrefsRoleId != _curRoleId)
{
PlayerPrefs.SetString("[Bi_RoleId]", _curRoleId.ToString());
_playerPrefsRoleId = _curRoleId;
}
Start();
}
public void Uninitialize()
{
GameCenter.UnRegFixEventHandle(Global.LogicEventDefine.EID_EVENT_FIRSTENTERMAP, OnFirstEnterMap);
Stop();
_curRoleId = 0;
}
#region//消息
//message ValMap
//{
// required string key = 1; // key
// required string value = 2; // value
//}
// message ReqBiDevice
// {
// enum MsgID { eMsgID = 185201; };
// required int32 appId = 1; // 平台游戏的游戏id,用于区分游戏由sdk获取
// required int32 roleId = 2; // 角色ID
// required string channelId = 3; // 平台唯一的渠道id,用于区分渠道厂商由sdk获取
// required string sourceId = 4; // 游戏包体id由sdk获取
// required string deviceId = 5; // 平台唯一的设备id由sdk获取
// required int32 platform = 6; // 运行平台IOS=1/安卓=2/H5=3
//}
public void ReqBiDevice()
{
var _reqBiDevice = new ReqBiDevice();
_reqBiDevice.device = CreatDevictData();
_reqBiDevice.Send();
}
//创建设备信息
public Device CreatDevictData()
{
var _device = new Device();
//绿岸
// app id
int appId = 0;
if (!string.IsNullOrEmpty(SDKCacheData.AppID))
int.TryParse(SDKCacheData.AppID, out appId);
_device.appId = appId;
// 角色ID
int roleID = 0;
if (!string.IsNullOrEmpty(SDKCacheData.RoleID))
int.TryParse(SDKCacheData.RoleID, out roleID);
_device.roleId = roleID;
// 渠道ID
_device.channelId = SDKCacheData.ChannelID != null ? SDKCacheData.ChannelID : "";
// 游戏包体ID
_device.sourceId = SDKCacheData.SourceId != null ? SDKCacheData.SourceId : "";
// 设备ID
_device.deviceId = SDKCacheData.DevicesId != null ? SDKCacheData.DevicesId : "";
// 运行平台IOS=1/安卓=2/H5=3/web=4/pc=5
_device.platform = Application.platform == RuntimePlatform.IPhonePlayer ? 1 : Application.platform == RuntimePlatform.Android ? 2 : 4;
//通用
// 客户端资源版本6.21.3
_device.app_version = GameGlobalData.RES_VERSION != string.Empty ? GameGlobalData.RES_VERSION :GameCenter.SDKSystem.AppVersion;
// 运营商中国联通
_device.merchant = SDKCacheData.Merchant != null ? SDKCacheData.Merchant : "";
// 网络类型5G
_device.net_type = Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork ? "CarrierData" : "WIFI";
// 屏幕分辨率480*800
_device.screen = string.Format("{0}*{1}", Screen.width, Screen.height);
// 手机操作系统,如android、iOS
string operatingSystem = SystemInfo.operatingSystem;
_device.os = operatingSystem.IndexOf("Android OS") > -1 ? "android" : (operatingSystem.IndexOf("iPhone OS") > -1 ? "ios" : "");
// 操作系统版本号,如2.3.4
if (_device.os == "android")
{
_device.os_version = operatingSystem.Substring(10).Trim();
}
else if (_device.os == "ios")
{
_device.os_version = operatingSystem.Substring(9).Trim();
}
else
{
_device.os_version = "";
}
// 服务器名字
_device.server_name = GameCenter.LuaSystem.Adaptor.GetCurrServerName();// .ServerListSystem.GetCurrentServer().Name;
//第三方
// SDK获取的gameID
int cpgameId = 0;
if (!string.IsNullOrEmpty(SDKCacheData.CPGameId))
int.TryParse(SDKCacheData.CPGameId, out cpgameId);
_device.cpgameId = cpgameId;
// 用户设备IDAndroid系统取IMEI码iOS6.0以前系统取设备号iOS6.0及以后的系统取广告标示符IDFA -Identifier For Advertising, PC可以采用mac地址。请注意不要用MD5加密did字段
_device.cpdid = SDKCacheData.CPDid;
// 设备名称三星GT-S5830
_device.cpdevice_name = SystemInfo.deviceModel;
// 这个是设备的自定义名字
//_device.cpdevice_name = SystemInfo.deviceName;
// SDK获取的平台ID
int platformId = 0;
if (!string.IsNullOrEmpty(SDKCacheData.CPPlatformId))
int.TryParse(SDKCacheData.CPPlatformId, out platformId);
_device.cpplatformId = platformId; // SDK获取的平台ID
// 渠道账号ID
_device.cpuserid = SDKCacheData.CPUserID;
// 渠道账号名
_device.cpuserName = SDKCacheData.CPUserName;
// 游戏简称
_device.cpgameName = SDKCacheData.CPGameName;
// 游戏平台简称
_device.cpPlatformGname = SDKCacheData.CPPlatformGname;
return _device;
}
//// 具体的BI数据内部数据格式由对接人协商负责
//message ReqBi
//{
// enum MsgID { eMsgID = 185202; };
//required int32 roleId = 1; // 角色ID
// required string biName = 2; // BI数据名表名
//repeated ValMap valMaps = 3; // 数据列表key,value
//}
public void ReqBi(string biName, List<ValMap> valMaps)
{
ReqBi reqBi = new ReqBi();
reqBi.biName = biName;
int roleID = 0;
int.TryParse(SDKCacheData.RoleID, out roleID);
reqBi.roleId = roleID;
for (int i = 0; i < valMaps.Count; i++)
{
reqBi.valMaps.Add(valMaps[i]);
}
reqBi.Send();
}
public void ReqBi(string biName, string key, string value = "")
{
ReqBi reqBi = new ReqBi();
reqBi.biName = biName;
int roleID = 0;
int.TryParse(SDKCacheData.RoleID, out roleID);
reqBi.roleId = roleID;
_tempValMap.Clear();
_tempValMap.key = key;
_tempValMap.value = value;
reqBi.valMaps.Add(_tempValMap);
reqBi.Send();
}
public void ReqBi(string biName, params string[] strArr)
{
ReqBi reqBi = new ReqBi();
reqBi.biName = biName;
int roleID = 0;
int.TryParse(SDKCacheData.RoleID, out roleID);
reqBi.roleId = roleID;
int count = strArr.Length;
for (int i = 0; i < count; i += 2)
{
var _map = new ValMap();
_map.key = strArr[i];
if (i + 1 < count)
{
_map.value = strArr[i + 1];
}
else
{
_map.value = "";
}
reqBi.valMaps.Add(_map);
}
reqBi.Send();
}
//发送ui埋点
public void ReqClickEvent(int id)
{
if (DeclareBi.Get(id).IsOpen == 1)
{
if (_writePos > _clickInfoArr.Length)
{
//保存到本地
Save();
_writePos = 0;
}
_clickInfoArr[_writePos++] = id;
_clickInfoArr[_writePos++] = (int)GameCenter.HeartSystem.ServerTime;
}
}
//发送ui埋点
public void ReqAllClickEvent()
{
int playerPrefsCnt = 0;
if (PlayerPrefs.HasKey("[Bi_Count]"))
{
playerPrefsCnt = PlayerPrefs.GetInt("[Bi_Count]");
}
if (playerPrefsCnt + _writePos > 0)
{
List<UIData> uIDatas = new List<UIData>(playerPrefsCnt + _writePos);
if (_writePos > 0)
{
for (int i = 0; i < _writePos; i += 2)
{
UIData uIData = new UIData();
uIData.id = _clickInfoArr[i];
uIData.time = _clickInfoArr[i + 1];
uIDatas.Add(uIData);
}
}
if (playerPrefsCnt > 0)
{
int row = PlayerPrefs.GetInt("[Bi_DataRow]");
for (int i = 0; i < row; i++)
{
var key = string.Format("[Bi_Data{0:D3}]", i + 1);
var s = PlayerPrefs.GetString(key);
if (s != "")
{
var arr = s.Split(',');
var cnt = arr.Length;
for (int j = 0; j < cnt; j += 2)
{
UIData uIData = new UIData();
int id = -1;
int time = -1;
if (int.TryParse(arr[j], out id))
{
uIData.id = id;
}
if (int.TryParse(arr[j + 1], out time))
{
uIData.time = time;
}
if (id != -1 && time != -1)
{
uIDatas.Add(uIData);
}
}
}
PlayerPrefs.DeleteKey(key);
}
}
ReqUiBi reqUiBi = new ReqUiBi();
reqUiBi.roleId = (long)_playerPrefsRoleId;
reqUiBi.uiData = uIDatas;
reqUiBi.Send();
PlayerPrefs.SetInt("[Bi_Count]", 0);
PlayerPrefs.SetInt("[Bi_DataRow]", 0);
}
var t = GameCenter.HeartSystem.ServerTime;
_nextSaveUTCTime = t + _saveSpacingTime;
_nextReqUTCTime = t + _reqSpacingTime;
}
//保存到本地
public void Save()
{
if (_writePos > 0)
{
int cnt = _writePos;
if (PlayerPrefs.HasKey("[Bi_Count]"))
{
cnt = PlayerPrefs.GetInt("[Bi_Count]") + _writePos;
}
if (cnt >= _maxCount)
{
ReqAllClickEvent();
return;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < _writePos; i++)
{
if (i == _writePos - 1)
{
sb.Append(_clickInfoArr[i]);
}
else
{
sb.Append(_clickInfoArr[i] + ",");
}
}
int row = 0;
if (PlayerPrefs.HasKey("[Bi_DataRow]"))
{
row = PlayerPrefs.GetInt("[Bi_DataRow]");
}
PlayerPrefs.SetInt("[Bi_DataRow]", row + 1);
PlayerPrefs.SetString(string.Format("[Bi_Data{0:D3}]", row + 1), sb.ToString());
PlayerPrefs.SetInt("[Bi_Count]", cnt);
PlayerPrefs.Save();
_writePos = 0;
}
var t = GameCenter.HeartSystem.ServerTime;
_nextSaveUTCTime = t + _saveSpacingTime;
}
public void Start()
{
ReqAllClickEvent();
_isCheck = true;
}
public void Stop()
{
_isCheck = true;
}
public void Update(float dt)
{
if (_isCheck)
{
var t = GameCenter.HeartSystem.ServerTime;
if (t >= _nextSaveUTCTime)
{
Save();
}
if (t > _nextReqUTCTime)
{
ReqAllClickEvent();
}
}
}
#endregion
}
}