C# 时间管理器和服务器时间同步

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;


namespace SGGame
{
    public enum TIMER_CATEGORY
    {
        ACTIVITY_DUNGEON = 1,
        DUNGEON_LOOT,
        ARENA_RECORD,
        COIN_MINE,
        WORLD_BOSS,
        HUNTING,
        SP_RECOVER,
        RED_DOT,
    }


    public class Schedule
    {
        private static Dictionary<string, bool> _Routines = new Dictionary<string, bool>();

        public static void New(string code, TimePeriod period, Action start_action, Action end_action)
        {
            if (_Routines.ContainsKey(code) && _Routines[code])
            {
                GameLog.LogError("[SCHEDULE]Code not allowed: " + code);
                return;
            }

            _Routines[code] = true;

            MainClient.StartCoroutine(CoExecute(code, period, start_action, end_action));
        }

        private static IEnumerator CoExecute(string code, TimePeriod period, Action start_action, Action end_action)
        {
            if (period.IsOver) yield break;

            if (period.IsSoon) yield return new WaitForSeconds((float)Math.Ceiling(period.Delta.TotalSeconds));

            if (!_Routines[code]) yield break;
            if (start_action != null) start_action();

            yield return new WaitForSeconds((float)period.Delta.TotalSeconds);

            if (!_Routines[code]) yield break;
            if (end_action != null) end_action();

            _Routines.Remove(code);
        }

        public static void End(string code)
        {
            if (!_Routines.ContainsKey(code)) return;

            _Routines[code] = false;
        }
    }


    public class GameTime
    {
        public static DateTime Now { get { return DateTime.Now - GameManager.ProxyManager.MainProxy.ServerTimeLag; } }
        public static DateTime Today
        {
            get
            {
                DateTime now = Now;
                return new DateTime(now.Year, now.Month, now.Day);
            }
        }

        public static bool IsBetween(int second_1, int second_2)
        {
            int current_seconds = (int)(Now - Today).TotalSeconds;
            return current_seconds > second_1 && current_seconds < second_2;
        }
    }


    public class TimePeriod
    {
        private DateTime Now { get { return DateTime.Now - GameManager.ProxyManager.MainProxy.ServerTimeLag; } }
        private DateTime Today { get { return new DateTime(Now.Year, Now.Month, Now.Day); } }
        private DateTime Start;
        private DateTime End;

        private int Code;
        /// <summary>
        /// 1. activity dungeon
        /// 2. dungeon loot
        /// </summary>
        public  int Type; // TIMER_CATEGORY

        public TimeSpan Interval { get { return End - Start; } }
        public TimeSpan Delta
        {
            get
            {
                if (IsOver) return Now - End;
                return IsSoon ? Start - Now : End - Now;
            }
        }

        public string DeltaFormat
        {
            get
            {
                // TODO: HARD CODE
                TimeSpan span = Delta;
                if (span.Days > 0)   return span.Days  + (IsOver ? ConfigManager.String("@Title_Text_247") : "天");
                if (span.Hours > 0 ) return span.Hours + (IsOver ? ConfigManager.String("@Title_Text_246") : "小时");
                return span.Minutes + (IsOver ? ConfigManager.String("@Title_Text_245") : "分钟");
            }
        }

        public bool IsOver { get { return End   < Now; } }
        public bool IsSoon { get { return Start > Now; } }

        public TimePeriod(int period)
        {
            Start = Now;
            End   = Now.AddSeconds(period);
        }

        public TimePeriod(DateTime start, DateTime end)
        {
            Start = start;
            End   = end;
        }

        public TimePeriod(int start, int end)
        {
            Start = Today.AddSeconds(start);
            End   = Today.AddSeconds(end);
        }

        public TimePeriod(string start)
        {
            Start = Now;
            int[] timeCode = start.Split('-').ToInt();
            End = new DateTime(timeCode[0], timeCode[1], timeCode[2], timeCode[3], timeCode[4], timeCode[5]);
        }

        public TimePeriod(string start, string end)
        {
            int [] timeCode = start.Split('-').ToInt();
            Start = new DateTime(timeCode[0], timeCode[1], timeCode[2], timeCode[3], timeCode[4], timeCode[5]);

            timeCode = end.Split('-').ToInt();
            End   = new DateTime(timeCode[0], timeCode[1], timeCode[2], timeCode[3], timeCode[4], timeCode[5]);
        }

        public TimePeriod(string start, string end, string weekday)
        {
            int [] week_code   = weekday.Split(',').ToInt();
            int    day_of_week = (int)Now.DayOfWeek;
            foreach (int code in week_code)
            {
                // TODO XYS: maybe handle time period over days in future
                if (code < day_of_week) continue;

                Start = Today.AddDays(code - day_of_week);
                End   = Start;

                int [] timeCode = start.Split('-').ToInt();
                Start = Start.AddSeconds(timeCode[0] * 3600 + timeCode[1] * 60 + timeCode[2]);

                timeCode = end.Split('-').ToInt();
                End = End.AddSeconds(timeCode[0] * 3600 + timeCode[1] * 60 + timeCode[2]);

                break;
            }
        }

        public void Init(int code, TIMER_CATEGORY category) { Init(code, (int)category); } // use this instead
        // legacy function
        public void Init(int code, int type)
        {
            Code = code;
            Type = type;

            //if (IsOver) return;

            if (!Timers.ContainsKey(type)) Timers.Add(type, new Data<TimePeriod>());
            Timers[type][Code] = this;
        }

        // timers
        private static Dictionary<int, Data<TimePeriod>> Timers = new Dictionary<int, Data<TimePeriod>>();
        public  static void Clear(TIMER_CATEGORY category) { Clear((int)category); }
        public  static void Clear(int type) { Timers.Remove(type); }
        public  static void Remove(int code, TIMER_CATEGORY category) { Remove(code, (int)category); }
        public  static void Remove(int code, int type) { if (Timers.ContainsKey(type)) Timers[type].Entries.Remove(code); }

        public  static TimePeriod Get(int code, TIMER_CATEGORY category) { return Get(code, (int)category); }
        public  static TimePeriod Get(int code, int type)
        {
            if (!Timers.ContainsKey(type)) return null;
            return Timers[type][code];
        }

        // tool functions
        /// get date time from time stamp
        public  static DateTime Convert(int timestamp)
        { return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).AddSeconds(timestamp); }

        public  static bool IsPassed(int timestamp)
        { return (DateTime.Now - GameManager.ProxyManager.MainProxy.ServerTimeLag) > Convert(timestamp); }
    }
}

转载于:https://www.cnblogs.com/mengdi520123/p/4288324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值