Massively improve CPU Usage.

This commit is contained in:
SilicaAndPina 2022-04-15 00:07:41 +12:00
parent 298c2aca4a
commit 80b0e88e1e
3 changed files with 55 additions and 64 deletions

View file

@ -1,6 +1,7 @@
using HISP.Server; using HISP.Server;
using System; using System;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace HISP.Cli namespace HISP.Cli
@ -10,6 +11,7 @@ namespace HISP.Cli
private static StreamWriter sw = null; private static StreamWriter sw = null;
private static FileStream fs = null; private static FileStream fs = null;
private static string logFile; private static string logFile;
private static EventWaitHandle shutdownHandle;
public static bool ShuttingDown = false; public static bool ShuttingDown = false;
public static string BaseDir; public static string BaseDir;
@ -42,7 +44,7 @@ namespace HISP.Cli
public static void OnShutdown() public static void OnShutdown()
{ {
ShuttingDown = true; shutdownHandle.Set();
} }
public static void LogToFile(bool error, string type,string text) public static void LogToFile(bool error, string type,string text)
{ {
@ -125,9 +127,9 @@ namespace HISP.Cli
Entry.SetShutdownCallback(OnShutdown); Entry.SetShutdownCallback(OnShutdown);
Entry.Start(); Entry.Start();
while (!ShuttingDown) { /* Allow asyncronous operations to happen. */ };
Task.WaitAll(); shutdownHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
shutdownHandle.WaitOne();
} }
} }
} }

View file

@ -158,14 +158,7 @@ namespace HISP.Game
public class Time public class Time
{ {
public double PreciseMinutes; public int Minutes;
public int Minutes
{
get
{
return Convert.ToInt32(Math.Floor(PreciseMinutes));
}
}
public int Days; public int Days;
public int Years; public int Years;
} }
@ -198,13 +191,13 @@ namespace HISP.Game
public static void TickWorldClock() public static void TickWorldClock()
{ {
ServerTime.PreciseMinutes += 0.1; ServerTime.Minutes++;
if (ServerTime.Minutes > 1440) // 1 day if (ServerTime.Minutes > 1440) // 1 day
{ {
ServerTime.Days += 1; ServerTime.Days += 1;
ServerTime.PreciseMinutes = 0.0; ServerTime.Minutes = 0;
Database.DoIntrestPayments(ConfigReader.IntrestRate); Database.DoIntrestPayments(ConfigReader.IntrestRate);
} }
@ -219,7 +212,7 @@ namespace HISP.Game
public static void ReadWorldData() public static void ReadWorldData()
{ {
Logger.DebugPrint("Reading time from database..."); Logger.DebugPrint("Reading time from database...");
ServerTime.PreciseMinutes = Database.GetServerTime(); ServerTime.Minutes = Database.GetServerTime();
ServerTime.Days = Database.GetServerDay(); ServerTime.Days = Database.GetServerDay();
ServerTime.Years = Database.GetServerYear(); ServerTime.Years = Database.GetServerYear();
StartDate = Database.GetServerStartTime(); StartDate = Database.GetServerStartTime();

View file

@ -42,73 +42,69 @@ namespace HISP.Server
/* /*
* Private stuff * Private stuff
*/ */
private static int gameTickSpeed = 480; // Changing this to ANYTHING else will cause desync with the client. private static int gameTickSpeed = 4800; // Changing this to ANYTHING else will cause desync with the client.
private static int totalMinutesElapsed = 0; private static int totalMinutesElapsed = 0;
private static int oneMinute = 1000 * 60; private static int oneMinute = 1000 * 60;
private static Timer gameTimer; // Controls in-game time. private static Timer gameTimer; // Controls in-game time.
private static Timer minuteTimer; // ticks every real world minute. private static Timer minuteTimer; // ticks every real world minute.
private static int lastServerTime = 0;
private static void onGameTick(object state) private static void onGameTick(object state)
{ {
// Tick the game clock.
World.TickWorldClock(); World.TickWorldClock();
if(World.ServerTime.Minutes != lastServerTime)
// Start all events with this RaceEvery set.
Arena.StartArenas(World.ServerTime.Minutes);
// Decrement horse train timer
Database.DecHorseTrainTimeout();
// Write time to database:
Database.SetServerTime(World.ServerTime.Minutes, World.ServerTime.Days, World.ServerTime.Years);
// Ranch Windmill Payments
if (World.ServerTime.Minutes % 720 == 0) // every 12 hours
{ {
lastServerTime = World.ServerTime.Minutes; Logger.DebugPrint("Paying windmill owners . . . ");
foreach (Ranch ranch in Ranch.Ranches)
// Start all events with this RaceEvery set.
Arena.StartArenas(World.ServerTime.Minutes);
// Decrement horse train timer
Database.DecHorseTrainTimeout();
// Write time to database:
Database.SetServerTime(World.ServerTime.Minutes, World.ServerTime.Days, World.ServerTime.Years);
// Ranch Windmill Payments
if (World.ServerTime.Minutes % 720 == 0) // every 12 hours
{ {
Logger.DebugPrint("Paying windmill owners . . . "); int ranchOwner = ranch.OwnerId;
foreach (Ranch ranch in Ranch.Ranches) if (ranchOwner != -1)
{ {
int ranchOwner = ranch.OwnerId; int moneyToAdd = 5000 * ranch.GetBuildingCount(8); // Windmill
if (ranchOwner != -1) if (GameServer.IsUserOnline(ranchOwner))
GameServer.GetUserById(ranchOwner).AddMoney(moneyToAdd);
else
{ {
int moneyToAdd = 5000 * ranch.GetBuildingCount(8); // Windmill try
if (GameServer.IsUserOnline(ranchOwner))
GameServer.GetUserById(ranchOwner).AddMoney(moneyToAdd);
else
{ {
try Database.SetPlayerMoney(Database.GetPlayerMoney(ranchOwner) + moneyToAdd, ranchOwner);
{ }
Database.SetPlayerMoney(Database.GetPlayerMoney(ranchOwner) + moneyToAdd, ranchOwner); catch (OverflowException)
} {
catch (OverflowException) Database.SetPlayerMoney(2147483647, ranchOwner);
{
Database.SetPlayerMoney(2147483647, ranchOwner);
}
} }
} }
} }
} }
if((World.StartDate != -1)) // Birthday tokens
{
int curTime = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
if (curTime >= World.StartDate + 378691200)
{
Logger.InfoPrint("Your server has been running for 12 years! Adding birthday tokens");
Database.SetStartTime(-1);
World.StartDate = -1;
AddItemToAllUsersEvenOffline(Item.BirthdayToken, 10);
}
}
gameTimer.Change(gameTickSpeed, gameTickSpeed);
} }
if((World.StartDate != -1)) // Birthday tokens
{
int curTime = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
if (curTime >= World.StartDate + 378691200)
{
Logger.InfoPrint("Your server has been running for 12 years! Adding birthday tokens");
Database.SetStartTime(-1);
World.StartDate = -1;
AddItemToAllUsersEvenOffline(Item.BirthdayToken, 10);
}
}
gameTimer.Change(gameTickSpeed, gameTickSpeed);
} }
private static void onMinuteTick(object state) private static void onMinuteTick(object state)
{ {