Я пытаюсь определить интервал времени (например, время между выходом из системы и входом в систему), но с оговоркой, что часть каждого дня не должна учитываться (например, не считать время между 7 вечера и7 утра в определенный день).Удаление этого времени из полных дней достаточно просто, но как лучше всего проверить часть дня (например, пользователь выходит из системы в 6 вечера и снова в 8 вечера того же дня) и другие пограничные случаи?
ETA: в настоящее время у меня есть установка, которая выглядит следующим образом:
public class HasProductiveHours
{
//These fields should represent an hour of the day (in 24-hour format)
// in which production should start or stop.
public int startProductiveHour;
public int endProductiveHour;
//Resource type enum
public ResourceType resourceType
//How often this resource is collected, in seconds
public float harvestTime
public HasProductiveHours (int startProductiveHour, int endProductiveHour, ResourceType resourceType, int harvestTime)
{
// Other creation-time things
this.startProductiveHour = startProductiveHour;
this.endProductiveHour = endProductiveHour;
this.resourceType = resourceType;
this.harvestTime = harvestTime;
TimeSimulator.Instance.SimulateElapsedTime (this);
}
}
Экземпляры этого класса и его потомков имитируют производство какого-либо ресурса каждый час или около того (в зависимости от ресурса),Существует система для моделирования времени, прошедшего с момента последнего выхода пользователя из системы.
DateTime lastLogoutTime;
public void Logout ()
{
// Other logout things
lastLogoutTime = DateTime.Now;
}
public void SimulateElapsedTime (HasProductiveHours toSimulate)
{
DateTime currentTime = DateTime.Now;
TimeSpan timeSinceLogout = currentTime - lastLogout;
int unproductiveHours = Math.Abs (toSimulate.startProductionHour - toSimulate.endProductionHour) * timeSinceLogout.Days;
timeSinceLogout.Subtract (new TimeSpan (unproductiveHours, 0, 0);
double secondsElapsed = timeSinceLogout.TotalSeconds;
int harvestsElapsed = (int)(secondsElapsed / toSimulate.harvestTime);
PlayerData.Instance.AddResource (toSimulate.resourceType, harvestsElapsed);
}