Hello guys, i think this will be my first real contribution to the forum. By the way, it's nothing so hard to do heheh.
I've been working on my WCProject and on the Battle System that is based on Demons Source, i noticed that they have those classes implemented.
Firstly, i tried using what most sources use, DateTime and Environment.TickCount, I don't know if this class will save or spend more resources, but i don't need to rewrite Update and Reset codes all the time, and it got a lot easier to code timers with this now.
Note: This do not execute anything automatically, just stores the time.
First, you need a Unix Timestamp class, something to return Seconds and Milliseconds.
I will provide my code, if you have anything better to contribute, i will edit here.
Unix Timestamp.cs
*
Below, i have the TimeOut and TimeOutMS classes.
TimeOut hasn't been tested yet, but i am using TimeOutMS on my Meele Attack, and it's working like a boss.
I will provide commented code and examples later. ^_^
Hope it helps anyone #00
Almost forgot...
Calculations does contains this
I've been working on my WCProject and on the Battle System that is based on Demons Source, i noticed that they have those classes implemented.
Firstly, i tried using what most sources use, DateTime and Environment.TickCount, I don't know if this class will save or spend more resources, but i don't need to rewrite Update and Reset codes all the time, and it got a lot easier to code timers with this now.
Note: This do not execute anything automatically, just stores the time.
First, you need a Unix Timestamp class, something to return Seconds and Milliseconds.
I will provide my code, if you have anything better to contribute, i will edit here.
Unix Timestamp.cs
Code:
using System;
namespace Core.Common
{
public static class UnixTimestamp
{
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
public const int TIME_SECONDS_MINUTE = 60;
public const int TIME_SECONDS_HOUR = 60 * TIME_SECONDS_MINUTE;
public const int TIME_SECONDS_DAY = 24 * TIME_SECONDS_HOUR;
public static DateTime ToDateTime(uint timestamp)
{
return UnixEpoch.AddSeconds(timestamp);
}
public static int Timestamp()
{
return Convert.ToInt32((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToUniversalTime()).TotalSeconds);
}
public static long LongTimestamp()
{
return Convert.ToInt64((DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToUniversalTime()).TotalMilliseconds);
}
public static int Timestamp(DateTime time)
{
return Convert.ToInt32((time - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToUniversalTime()).TotalSeconds);
}
public static long LongTimestamp(DateTime time)
{
return Convert.ToInt64((time - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToUniversalTime()).TotalMilliseconds);
}
public static int MonthDayStamp()
{
return Convert.ToInt32((DateTime.Now - new DateTime(DateTime.Now.Year, 1, 1, 0, 0, 0, 0).ToUniversalTime()).TotalSeconds);
}
public static int MonthDayStamp(DateTime time)
{
return Convert.ToInt32((time - new DateTime(DateTime.Now.Year, 1, 1, 0, 0, 0, 0).ToUniversalTime()).TotalSeconds);
}
public static int DayOfTheMonthStamp()
{
return Convert.ToInt32((DateTime.Now - new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0, 0).ToUniversalTime()).TotalSeconds);
}
public static int DayOfTheMonthStamp(DateTime time)
{
return Convert.ToInt32((time - new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0, 0).ToUniversalTime()).TotalSeconds);
}
}
}
Below, i have the TimeOut and TimeOutMS classes.
TimeOut hasn't been tested yet, but i am using TimeOutMS on my Meele Attack, and it's working like a boss.
Code:
// World Conquer Project - Phoenix Project Based
// Source Development by Felipe Vieira (FTW! Masters)
// Source Infrastructure by Gareth Jensen (Akarui)
//
// Computer User: Administrador
// File Created by: Felipe Vieira Vendramini
// zfserver - MsgServer - TimeOut.cs
// File Created: 2015/01/13 00:53
// This is the UnixTimestamp location
using Core.Common;
namespace MsgServer.Structures
{
public sealed class TimeOut
{
private int _updateTime;
private int _interval;
public TimeOut(int nInterval)
{
_interval = nInterval;
_updateTime = 0;
}
public int clock() { return UnixTimestamp.Timestamp(); }
public bool Update() { _updateTime = clock(); return true; }
public bool IsTimeOut() { return clock() >= _updateTime + _interval; }
public bool ToNextTime()
{
if (IsTimeOut())
return Update();
return false;
}
public void SetInterval(int nSecs) { _interval = nSecs; }
public void Startup(int nSecs) { _interval = nSecs; Update(); }
public bool TimeOver()
{
if (IsActive() && IsTimeOut()) return Clear();
return false;
}
public bool IsActive() { return _updateTime != 0; }
public bool Clear() { _updateTime = _interval = 0; return true; }
public void IncInterval(int nSecs, int nLimit) { _interval = Calculations.CutOverflow(_interval + nSecs, nLimit); }
public void DecInterval(int nSecs) { _interval = Calculations.CutTrail(_interval - nSecs, 0); }
public bool IsTimeOut(int nSecs) { return clock() >= _updateTime + nSecs; }
public bool ToNextTime(int nSecs) { if (IsTimeOut(nSecs)) return Update(); return false; }
public bool TimeOver(int nSecs) { if (IsActive() && IsTimeOut(nSecs)) return Clear(); return false; }
public bool ToNextTick(int nSecs)
{
if (IsTimeOut(nSecs))
{
if (clock() >= _updateTime + nSecs * 2)
return Update();
_updateTime += nSecs;
return true;
}
return false;
}
public int GetRemain() { return _updateTime != 0 ? Calculations.CutRange(_interval - ((int)clock() - (int)_updateTime), 0, _interval) : 0; }
public int GetInterval() { return _interval; }
}
public sealed class TimeOutMS
{
private long _updateTime;
private int _interval;
public TimeOutMS(int nInterval)
{
_interval = nInterval;
_updateTime = 0;
}
public long clock() { return UnixTimestamp.LongTimestamp(); }
public bool Update() { _updateTime = clock(); return true; }
public bool IsTimeOut() { return clock() >= _updateTime + _interval; }
public bool ToNextTime()
{
if (IsTimeOut())
return Update();
return false;
}
public void SetInterval(int nMilliSecs) { _interval = nMilliSecs; }
public void Startup(int nMilliSecs) { _interval = nMilliSecs; Update(); }
public bool TimeOver()
{
if (IsActive() && IsTimeOut()) return Clear();
return false;
}
public bool IsActive() { return _updateTime != 0; }
public bool Clear() { _updateTime = _interval = 0; return true; }
public void IncInterval(int nMilliSecs, int nLimit) { _interval = Calculations.CutOverflow(_interval + nMilliSecs, nLimit); }
public void DecInterval(int nMilliSecs) { _interval = Calculations.CutTrail(_interval - nMilliSecs, 0); }
public bool IsTimeOut(int nMilliSecs) { return clock() >= _updateTime + nMilliSecs; }
public bool ToNextTime(int nMilliSecs) { if (IsTimeOut(nMilliSecs)) return Update(); return false; }
public bool TimeOver(int nMilliSecs) { if (IsActive() && IsTimeOut(nMilliSecs)) return Clear(); return false; }
public bool ToNextTick(int nMilliSecs)
{
if (IsTimeOut(nMilliSecs))
{
if (clock() >= _updateTime + nMilliSecs * 2)
return Update();
_updateTime += nMilliSecs;
return true;
}
return false;
}
public int GetRemain() { return _updateTime != 0 ? Calculations.CutRange(_interval - ((int) clock() - (int)_updateTime),0 ,_interval) : 0; }
public int GetInterval() { return _interval; }
}
}
I will provide commented code and examples later. ^_^
Hope it helps anyone #00
Almost forgot...
Calculations does contains this
Code:
public static long CutTrail(long x, long y) { return (x >= y) ? x : y; }
public static long CutOverflow(long x, long y) { return (x <= y) ? x : y; }
public static long CutRange(long n, long min, long max) { return (n < min) ? min : ((n > max) ? max : n); }
public static int CutTrail(int x, int y) { return (x >= y) ? x : y; }
public static int CutOverflow(int x, int y) { return (x <= y) ? x : y; }
public static int CutRange(int n, int min, int max) { return (n < min) ? min : ((n > max) ? max : n); }
public static short CutTrail(short x, short y) { return (x >= y) ? x : y; }
public static short CutOverflow(short x, short y) { return (x <= y) ? x : y; }
public static short CutRange(short n, short min, short max) { return (n < min) ? min : ((n > max) ? max : n); }
public static ulong CutTrail(ulong x, ulong y) { return (x >= y) ? x : y; }
public static ulong CutOverflow(ulong x, ulong y) { return (x <= y) ? x : y; }
public static ulong CutRange(ulong n, ulong min, ulong max) { return (n < min) ? min : ((n > max) ? max : n); }
public static uint CutTrail(uint x, uint y) { return (x >= y) ? x : y; }
public static uint CutOverflow(uint x, uint y) { return (x <= y) ? x : y; }
public static uint CutRange(uint n, uint min, uint max) { return (n < min) ? min : ((n > max) ? max : n); }
public static ushort CutTrail(ushort x, ushort y) { return (x >= y) ? x : y; }
public static ushort CutOverflow(ushort x, ushort y) { return (x <= y) ? x : y; }
public static ushort CutRange(ushort n, ushort min, ushort max) { return (n < min) ? min : ((n > max) ? max : n); }
public static byte CutTrail(byte x, byte y) { return (x >= y) ? x : y; }
public static byte CutOverflow(byte x, byte y) { return (x <= y) ? x : y; }
public static byte CutRange(byte n, byte min, byte max) { return (n < min) ? min : ((n > max) ? max : n); }