下面两个类放到项目中,实现IAntiAddictedServices接口就能用
1.AntiAddictedController .cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Achonor.AntiAddicted
{
public class AntiAddictedController : MonoBehaviour
{
public static AntiAddictedController Instance = null;
private IAntiAddictedServices _antiAddictedServices;
private DateTime _birthTime = DateTime.MinValue;
private DateTime _lastCheckHolidayTime = DateTime.MinValue;
private bool _isHolidayChecking = false;
private bool _isHoliday = false;
private DateTime _startGameTime;
private float _onlineCheckInterval = 0;
private const string PLAYER_IDCARD = "Player_IDCard";
private const string PLAYER_NAME = "Player_Name";
private const string PLAYER_BIRTH = "Player_Birth";
private const string PLAYER_ONLINE_DURATION = "Player_Online_Duration";
private const string PLAYER_TODAY_ONLINE_DURATION = "Player_Today_Online_Duration";
private const string PLAYER_LAST_ONLINE_DATE = "Player_Last_Online_Date";
private const float ONlINECHECKINTERVAL = 60;
private string PlayerIDCard {
get {
return PlayerPrefs.GetString(PLAYER_IDCARD);
}
}
private string PlayerName {
get {
return PlayerPrefs.GetString(PLAYER_NAME);
}
}
private int PlayerAge {
get {
if (DateTime.MinValue == _birthTime) {
string birthStr = PlayerPrefs.GetString(PLAYER_BIRTH);
if (string.IsNullOrEmpty(birthStr)) {
return -1;
}
DateTime birthTime = new DateTime();
if (!DateTime.TryParse(birthStr, out birthTime)) {
return -1;
}
_birthTime = birthTime;
}
int year = DateTime.Now.Year - _birthTime.Year;
if (DateTime.Now < _birthTime.AddYears(year)) {
return year - 1;
}
return year;
}
}
private bool IsHoliday{
get {
if (DateTime.MinValue == _lastCheckHolidayTime) {
if (!_isHolidayChecking) {
StartCoroutine(RequestHoliday());
}
return false;
}
if (!DateTime.Now.Date.Equals(_lastCheckHolidayTime.Date)) {
//重新检测
StartCoroutine(RequestHoliday());
}
return _isHoliday;
}
}
private float UnderageDurationMax {
get {
return _isHoliday ? 10800 : 5400;
}
}
private void Awake() {
Instance = this;
DontDestroyOnLoad(this);
}
private void Start() {
_onlineCheckInterval = 0;
_startGameTime = DateTime.Now;
StartCoroutine(RequestHoliday());
}
private void OnApplicationPause(bool pause) {
OnApplicationFocus(!pause);
}
private void OnApplicationFocus(bool focus) {
Debug.Log("OnApplicationPause focus = " + focus);
if (true == focus) {
//记录开始时间
_startGameTime = DateTime.Now;
}
else if (DateTime.MinValue != _startGameTime) {
int onlineSeconds = (int)(DateTime.Now - _startGameTime).TotalSeconds;
Debug.Log("onlineSeconds = " + onlineSeconds);
Debug.Log("PLAYER_ONLINE_DURATION = " + PlayerPrefs.GetInt(PLAYER_ONLINE_DURATION, 0));
PlayerPrefs.SetInt(PLAYER_ONLINE_DURATION, PlayerPrefs.GetInt(PLAYER_ONLINE_DURATION, 0) + onlineSeconds);
DateTime lastOnlineDate = DateTime.Parse(PlayerPrefs.GetString(PLAYER_LAST_ONLINE_DATE, "1970-01-01"));
if (lastOnlineDate.Date.Equals(DateTime.Now.Date)) {
PlayerPrefs.SetInt(PLAYER_TODAY_ONLINE_DURATION, PlayerPrefs.GetInt(PLAYER_TODAY_ONLINE_DURATION, 0) + onlineSeconds);
}
else {
//今天刚上线
PlayerPrefs.SetString(PLAYER_LAST_ONLINE_DATE, DateTime.Now.ToString("yyyy-MM-dd"));
PlayerPrefs.SetInt(PLAYER_TODAY_ONLINE_DURATION, onlineSeconds);
}
_startGameTime = DateTime.MinValue;
}
}
private void Update() {
_onlineCheckInterval += Time.unscaledDeltaTime;
if (_onlineCheckInterval < ONlINECHECKINTERVAL) {
return;
}
_onlineCheckInterval = 0;
OnlineCheck();
}
public void Login<IServices>(Action callback) where IServices : IAntiAddictedServices, new(){
_antiAddictedServices = new IServices();
string IDCard = PlayerPrefs.GetString(PLAYER_IDCARD, string.Empty);
if (OnlineCheck()) {
if (!string.IsNullOrEmpty(IDCard)) {
CallCallback(callback);
return;
}
OpenLoginUI(callback);
}
}
public void OpenLoginUI(Action callback = null) {
_antiAddictedServices.OpenLoginUI(delegate(LoginResultType resultType) {
if (LoginResultType.Register == resultType) {
_antiAddictedServices.CloseLoginUI();
OpenVerificationUI((result)=> {
if (!result) {
OpenLoginUI(callback);
} else {
callback();
}
});
}
else if (LoginResultType.Tourists == resultType) {
_antiAddictedServices.CloseLoginUI();
CallCallback(callback);
}
});
}
public void OpenVerificationUI(Action<bool> callback = null) {
_antiAddictedServices.OpenVerificationUI((VerificationResultType resultType, string IDCard, string name, Action<bool> callback1) => {
if (VerificationResultType.Closed == resultType) {
_antiAddictedServices.CloseVerificationUI();
CallCallback(callback, false);
CallCallback(callback1, true);
}
else if (VerificationResultType.Verification == resultType) {
Verification(IDCard, name, (result) => {
if (result) {
SaveInfo(IDCard, name);
_antiAddictedServices.CloseVerificationUI();
CallCallback(callback, true);
CallCallback(callback1, true);
}
else {
CallCallback(callback1, false);
}
});
}
});
}
private bool OnlineCheck() {
int playerAge = PlayerAge;
Debug.Log("OnlineCheck playerAge = " + playerAge);
if (18 <= playerAge) {
return true;
}
int onlineSeconds = (int)(DateTime.Now - _startGameTime).TotalSeconds;
if (-1 == playerAge) {
//游客
int onlineSumDuration = PlayerPrefs.GetInt(PLAYER_ONLINE_DURATION, 0) + onlineSeconds;
if (3600 < onlineSumDuration) {
//需要实名注册
OpenVerificationUI();
return false;
}
}
else {
//未成年
int onlineTodaySumDuration = PlayerPrefs.GetInt(PLAYER_TODAY_ONLINE_DURATION, 0) + onlineSeconds;
if (UnderageDurationMax < onlineTodaySumDuration) {
//达到时长上限
_antiAddictedServices.OpenRemindUI(()=> {
Application.Quit();
});
return false;
}
}
return true;
}
private void Verification(string IDCard, string name, Action<bool> callback) {
bool result = false;
if (15 == IDCard.Length) {
result = CheckIDCard15(IDCard);
}
else if (18 == IDCard.Length) {
result = CheckIDCard18(IDCard);
}
callback(result);
}
private void SaveInfo(string IDCard, string name) {
PlayerPrefs.SetString(PLAYER_IDCARD, IDCard);
PlayerPrefs.SetString(PLAYER_NAME, name);
string birthStr = string.Empty;
if (15 == IDCard.Length) {
birthStr = IDCard.Substring(6, 6).Insert(4, "-").Insert(2, "-");
}
else if (18 == IDCard.Length) {
birthStr = IDCard.Substring(6, 8).Insert(6, "-").Insert(4, "-");
}
PlayerPrefs.SetString(PLAYER_BIRTH, birthStr);
}
string _IDAddress = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
private bool CheckIDCard15(string IDCard) {
long IDNumber;
if (long.TryParse(IDCard, out IDNumber) == false || IDNumber < Math.Pow(10, 14)) {
return false;//数字验证
}
if (_IDAddress.IndexOf(IDCard.Remove(2)) == -1) {
return false;//省份验证
}
string birth = IDCard.Substring(6, 6).Insert(4, "-").Insert(2, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false) {
return false;//生日验证
}
return true;//符合15位身份证标准
}
private bool CheckIDCard18(string IDCard) {
long IDNumber = 0;
if (long.TryParse(IDCard.Remove(17), out IDNumber) == false || IDNumber < Math.Pow(10, 16)
|| long.TryParse(IDCard.Replace('x', '0').Replace('X', '0'), out IDNumber) == false) {
return false;//数字验证
}
if (_IDAddress.IndexOf(IDCard.Remove(2)) == -1) {
return false;//省份验证
}
string birth = IDCard.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false) {
return false;//生日验证
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = IDCard.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++) {
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != IDCard.Substring(17, 1).ToLower()) {
return false;//校验码验证
}
return true;//符合GB11643-1999标准
}
private IEnumerator RequestHoliday() {
_isHoliday = false;
_isHolidayChecking = true;
string dateStr = DateTime.Now.ToString("yyyyMMdd");
WWW www = new WWW("http://www.easybots.cn/api/holiday.php?d=" + dateStr);
yield return www;
_isHolidayChecking = false;
if (null != www.error) {
Debug.LogError("PreventAddictedController.RequestHoliday error = " + www.error);
}
else {
_lastCheckHolidayTime = DateTime.Parse(dateStr.Insert(6, "-").Insert(4, "-"));
Debug.Log(www.text);
if (-1 == www.text.IndexOf("\"0\"")) {
//休息日
_isHoliday = true;
}
}
}
private void CallCallback(Action callback) {
if (null != callback) {
callback();
}
}
private void CallCallback(Action<bool> callback, bool value) {
if (null != callback) {
callback(value);
}
}
}
}
2.IAntiAddictedServices.cs
using System;
using System.Collections;
using System.Collections.Generic;
namespace Achonor.AntiAddicted {
public enum LoginResultType {
Register = 0, //注册
Tourists //游客登陆
}
public enum VerificationResultType {
Closed = 0,
Verification,
}
public interface IAntiAddictedServices{
void OpenLoginUI(Action<LoginResultType> callback);
void CloseLoginUI();
void OpenVerificationUI(Action<VerificationResultType, string, string, Action<bool>> callback);
void CloseVerificationUI();
void OpenRemindUI(Action callback);
}
}