|
|
@@ -21,15 +21,16 @@ namespace OpenCards.Service.Center
|
|
|
|
|
|
private async Task OnTokenExchangeStartAsync()
|
|
|
{
|
|
|
+ DateTime gameNow = TimeUtils.GetGameNow();
|
|
|
+
|
|
|
_tokenExchangeGlobalMapping = new TokenExchangeGlobalDataMapping(PersistenceConstants.TYPE_TOKEN_EXCHANGE_GLOBAL, serverID, this);
|
|
|
await _tokenExchangeGlobalMapping.LoadOrCreateDataAsync(() => new TokenExchangeGlobalData()
|
|
|
{
|
|
|
UnitPrice = 2.00f,
|
|
|
- LastAdjustTime = TimeUtils.CurrentTimeMs,
|
|
|
- NextAdjustTime = CalcNextAdjustTime(TimeUtils.CurrentTimeMs),
|
|
|
+ LastAdjustTime = TimeUtils.ToUnixMs(gameNow),
|
|
|
+ NextAdjustTime = TimeUtils.ToUnixMs(CalcNextAdjustTime(gameNow)),
|
|
|
PeriodBuyCount = 0,
|
|
|
PeriodSellCount = 0,
|
|
|
-
|
|
|
});
|
|
|
|
|
|
_tokenExchangeAdjustTime = this.Provider.CreateTimer(
|
|
|
@@ -75,8 +76,7 @@ namespace OpenCards.Service.Center
|
|
|
rsp.s2c_code = Response.CODE_ERROR;
|
|
|
return rsp;
|
|
|
}
|
|
|
- //await _tokenExchangeLock.WaitAsync();
|
|
|
-
|
|
|
+ await _tokenExchangeLock.WaitAsync();
|
|
|
try
|
|
|
{
|
|
|
var global = _tokenExchangeGlobalMapping.Data;
|
|
|
@@ -115,7 +115,7 @@ namespace OpenCards.Service.Center
|
|
|
rsp.s2c_code = Response.CODE_ERROR;
|
|
|
return rsp;
|
|
|
}
|
|
|
- //await _tokenExchangeLock.WaitAsync();
|
|
|
+ await _tokenExchangeLock.WaitAsync();
|
|
|
try
|
|
|
{
|
|
|
var global = _tokenExchangeGlobalMapping.Data;
|
|
|
@@ -145,10 +145,8 @@ namespace OpenCards.Service.Center
|
|
|
{
|
|
|
_tokenExchangeLock.Release();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private bool ValidateServerId(string reqServerId)
|
|
|
{
|
|
|
return string.IsNullOrEmpty(reqServerId) || reqServerId == this.serverID;
|
|
|
@@ -157,24 +155,20 @@ namespace OpenCards.Service.Center
|
|
|
/// <summary>
|
|
|
/// 返回下一个调价时间点, 当日12点或者次日00:00 (当天24点)
|
|
|
/// </summary>
|
|
|
- /// <param name="nowMs"></param>
|
|
|
+ /// <param name="nowTicks"></param>
|
|
|
/// <returns></returns>
|
|
|
- private long CalcNextAdjustTime(long nowMs)
|
|
|
+ private DateTime CalcNextAdjustTime(DateTime gameNow)
|
|
|
{
|
|
|
- var now = TimeUtils.TimeStampToDateTime(nowMs);
|
|
|
- var dayStart = now.GetDayStart();
|
|
|
+ var dayStart = gameNow.Date;
|
|
|
var noon = dayStart.AddHours(12);
|
|
|
var midnight = dayStart.AddDays(1);
|
|
|
|
|
|
- long noonMs = (long)(noon - TimeUtils.GetUTCStartTime()).TotalMilliseconds;
|
|
|
- long midnightMs = (long)(midnight - TimeUtils.GetUTCStartTime()).TotalMilliseconds;
|
|
|
-
|
|
|
- if (nowMs < noonMs)
|
|
|
- return noonMs;
|
|
|
- if (nowMs < midnightMs)
|
|
|
- return midnightMs;
|
|
|
+ if (gameNow < noon)
|
|
|
+ return noon;
|
|
|
+ if (gameNow < midnight)
|
|
|
+ return midnight;
|
|
|
|
|
|
- return noonMs + TimeUtils.OndDayTimeMs;
|
|
|
+ return dayStart.AddDays(1).AddHours(12);
|
|
|
}
|
|
|
|
|
|
private async Task OnTokenExchangeAdjustTime(object state)
|
|
|
@@ -186,15 +180,18 @@ namespace OpenCards.Service.Center
|
|
|
if (global == null)
|
|
|
return;
|
|
|
|
|
|
- long now = TimeUtils.CurrentTimeMs;
|
|
|
- if (now < global.NextAdjustTime)
|
|
|
+ DateTime gameNow = TimeUtils.GetGameNow();
|
|
|
+ long nowMs = TimeUtils.ToUnixMs(gameNow);
|
|
|
+
|
|
|
+ DateTime nextAdjustTime = TimeUtils.FromUnixMs(global.NextAdjustTime);
|
|
|
+ if (gameNow < nextAdjustTime)
|
|
|
return;
|
|
|
|
|
|
float newPrice = CalcAdjustedUnitPrice(global.UnitPrice, global.PeriodBuyCount, global.PeriodSellCount);
|
|
|
|
|
|
global.UnitPrice = newPrice;
|
|
|
- global.LastAdjustTime = now;
|
|
|
- global.NextAdjustTime = CalcNextAdjustTime(now);
|
|
|
+ global.LastAdjustTime = nowMs;
|
|
|
+ global.NextAdjustTime = TimeUtils.ToUnixMs(CalcNextAdjustTime(gameNow));
|
|
|
global.PeriodBuyCount = 0;
|
|
|
global.PeriodSellCount = 0;
|
|
|
|
|
|
@@ -203,7 +200,7 @@ namespace OpenCards.Service.Center
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
-
|
|
|
+ _tokenExchangeLock.Release();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -241,12 +238,6 @@ namespace OpenCards.Service.Center
|
|
|
== Math.Round(serverPrice, 2, MidpointRounding.AwayFromZero);
|
|
|
}
|
|
|
|
|
|
- //[RpcHandler(typeof(GetTokenExchangeGlobalRequest), typeof(GetTokenExchangeGlobalResponse))]
|
|
|
- //public Task<GetTokenExchangeGlobalResponse> rpc_Handle(GetTokenExchangeGlobalRequest req)
|
|
|
- //{
|
|
|
- // return OnGetTokenExchangeGlobalRequest(req);
|
|
|
- //}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
}
|