135 lines
3.6 KiB
C#
135 lines
3.6 KiB
C#
/*
|
|
using asap.core;
|
|
using game;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
public class RedeemCodeService
|
|
{
|
|
public class CheckRedemptionKeyRequest
|
|
{
|
|
public string playerID;
|
|
public string Key { get; set; }
|
|
}
|
|
|
|
public class CheckRedemptionKeyResponse
|
|
{
|
|
/// <summary>
|
|
/// RedeemSucc:DropID RegisterDayNotEnough:NeedDays
|
|
/// </summary>
|
|
public int Code;
|
|
public ECheckValidateResult Result;
|
|
}
|
|
|
|
public enum ECheckValidateResult
|
|
{
|
|
invalid = 0,
|
|
success,
|
|
hasRedeemed,
|
|
expired,
|
|
NotFound,
|
|
MaxRedeemCount,
|
|
BusyServer,
|
|
RegisterDayNotEnough,
|
|
}
|
|
|
|
|
|
#region Field&&Property ༼ つ ◕_◕ ༽つ--ฅʕ•̫͡•ʔฅ
|
|
|
|
public bool IsUnlock = false;
|
|
|
|
public DateTime nextRedeemTime;
|
|
|
|
private HttpClient _funcClient;
|
|
private string _funcKey;
|
|
private IConfig _config;
|
|
|
|
#endregion
|
|
|
|
|
|
#region Function ( =∩王∩= )m~~o(=•ェ•=)m
|
|
|
|
public RedeemCodeService(IConfig config)
|
|
{
|
|
this._config = config;
|
|
var url = config.Get<string>(GConstant.K_FuncUrl, "");
|
|
_funcKey = config.Get<string>(GConstant.K_FuncKey, "");
|
|
if(string.IsNullOrEmpty(_funcKey)||string.IsNullOrEmpty(url))
|
|
{
|
|
Debug.LogError("FuncUrl or FuncKey is Null");
|
|
return;
|
|
}
|
|
|
|
_funcClient = new HttpClient();
|
|
_funcClient.BaseAddress = new System.Uri(url);
|
|
_funcClient.DefaultRequestHeaders.Add("x-functions-key", _funcKey);
|
|
_funcClient.Timeout = TimeSpan.FromSeconds(15);
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns>0:success 1:invalide 2:hasUsed 3:timeExpired</returns>
|
|
public async Task<CheckRedemptionKeyResponse> CheckRedemptionKeyValidate(string key)
|
|
{
|
|
var response = new CheckRedemptionKeyResponse();
|
|
// var cdKeys = GContext.container.Resolve<Tables>().TbCDKey.DataMap;
|
|
|
|
// var dropID = -1;
|
|
|
|
if ( string.IsNullOrEmpty(key) )
|
|
{
|
|
response.Result = ECheckValidateResult.invalid;
|
|
return response;
|
|
}
|
|
var result = await CheckRedmptionKeyVallidateInServer(key);
|
|
|
|
return result;
|
|
}
|
|
|
|
private async Task<CheckRedemptionKeyResponse> CheckRedmptionKeyVallidateInServer(string key)
|
|
{
|
|
var checkValidateRequest = new CheckRedemptionKeyRequest
|
|
{
|
|
Key = key,
|
|
playerID = GContext.container.Resolve<IUserService>().UserId,
|
|
};
|
|
|
|
//Request
|
|
var reqJson = JsonConvert.SerializeObject(checkValidateRequest);
|
|
var reqContent = new StringContent(reqJson, Encoding.UTF8, "application/json");
|
|
|
|
var postResponse= await _funcClient.PostAsync("api/CheckRedeemCodeValiad", reqContent);
|
|
|
|
//Response
|
|
var respJson = "";
|
|
|
|
if ( postResponse.IsSuccessStatusCode )
|
|
{
|
|
respJson= await postResponse.Content.ReadAsStringAsync();
|
|
}
|
|
|
|
//error
|
|
else
|
|
{
|
|
UnityEngine.Debug.LogError($"[RedemptionKey error] {postResponse.StatusCode}");
|
|
UnityEngine.Debug.LogError($"[RedemptionKey content][{respJson}]");
|
|
}
|
|
if ( string.IsNullOrEmpty(respJson) )
|
|
{
|
|
var response = new CheckRedemptionKeyResponse();
|
|
response.Result = ECheckValidateResult.invalid;
|
|
return response;
|
|
}
|
|
|
|
//success
|
|
return JsonConvert.DeserializeObject<CheckRedemptionKeyResponse>(respJson);
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
*/
|