77 lines
3.6 KiB
C#
77 lines
3.6 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using CSNetwork;
|
|
|
|
namespace BrewMonster.Network
|
|
{
|
|
public partial class EC_Game
|
|
{
|
|
private static uint m_AbsTickStart;
|
|
private static int m_AbsTimeStart;
|
|
private static int m_iTimeError; // 服务器与本机时间差(秒) // Time error in seconds
|
|
private static int m_iTimeZoneBias; // 服务器时区偏移(秒) // Server timezone bias in seconds
|
|
private static bool m_bServerTimeInited;
|
|
private static float m_dwTickTime; // Logic time of current tick
|
|
private static float m_dwRealTickTime; // Real tick time
|
|
public static int GetTimeZoneBias() { return m_iTimeZoneBias; }
|
|
// 设置时间误差 // Set time error
|
|
public static void SetServerTime(int iSevTime, int iTimeZoneBias)
|
|
{
|
|
Debug.Log($"SetServerTime, iSevTime = {iSevTime}, iTimeZoneBias = {iTimeZoneBias}");
|
|
int iOldTimeError = m_iTimeError;
|
|
int nowUnix = (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
|
m_iTimeError = iSevTime - nowUnix; // 记录与本机的时间差 // store delta with local
|
|
m_iTimeZoneBias = iTimeZoneBias; // 记录服务器时区偏移 // store server timezone bias
|
|
|
|
// 计算服务器本地时间并设置昼夜 // Compute server local time and set time of day
|
|
var serverLocal = DateTimeOffset.FromUnixTimeSeconds((long)iSevTime + iTimeZoneBias);
|
|
int nTimeInDay = serverLocal.Hour * 3600 + serverLocal.Minute * 60 + serverLocal.Second;
|
|
// GetGameRun()->GetWorld()->GetSunMoon()->SetTimeOfTheDay(nTimeInDay / (4.0f * 3600.0f));
|
|
// 设置昼夜时间(原逻辑保留为注释) // Set time of day (original call left commented)
|
|
|
|
// 防沉迷时长修正 // Anti-wallow playtime adjust
|
|
// S2C::player_wallow_info wallowinfo = GetGameRun()->GetWallowInfo();
|
|
// if (wallowinfo.anti_wallow_active)
|
|
// {
|
|
// wallowinfo.play_time += m_iTimeError - iOldTimeError;
|
|
// GetGameRun()->SetWallowInfo(wallowinfo);
|
|
// }
|
|
|
|
// 初始化绝对时间参考点 // Initialize absolute time reference
|
|
m_AbsTimeStart = iSevTime;
|
|
m_AbsTickStart = (uint)(Time.realtimeSinceStartup * 1000.0f);
|
|
m_bServerTimeInited = true;
|
|
Debug.Log($"timeGetTime(), TickStart = {m_AbsTickStart}");
|
|
}
|
|
|
|
public static int GetServerAbsTime()
|
|
{
|
|
// Fallback: if server time was never initialized (SetServerTime not called),
|
|
// return local unix time seconds so task timestamps (usually epoch seconds) still work.
|
|
// This makes wait-time/countdown and timetable logic behave correctly even before server sync.
|
|
if (!m_bServerTimeInited || m_AbsTimeStart == 0)
|
|
{
|
|
return (int)DateTimeOffset.UtcNow.ToUnixTimeSeconds() + m_iTimeError;
|
|
}
|
|
|
|
uint curTick = (uint)(Time.realtimeSinceStartup * 1000.0f);
|
|
|
|
if (curTick < m_AbsTickStart)
|
|
{
|
|
// if player run this game more than 49.71 days...
|
|
uint sec = (((uint)~0u - m_AbsTickStart + 1u) + curTick) / 1000u;
|
|
m_AbsTickStart = curTick;
|
|
m_AbsTimeStart += (int)sec;
|
|
return m_AbsTimeStart;
|
|
}
|
|
else
|
|
{
|
|
uint sec = (curTick - m_AbsTickStart) / 1000u;
|
|
return m_AbsTimeStart + (int)sec;
|
|
}
|
|
}
|
|
|
|
// Get real tick time of current frame
|
|
public static float GetRealTickTime() { return m_dwRealTickTime; }
|
|
}
|
|
} |