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