Skip to content

Commit

Permalink
Fixes HM reduction.
Browse files Browse the repository at this point in the history
  • Loading branch information
Soof4 committed Jul 13, 2023
1 parent f3a4f74 commit 9ef0ee8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions PlaytimeRewards/PlaytimeRewards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ namespace PlaytimeRewards {
[ApiVersion(2,1)]
public class PlaytimeRewards : TerrariaPlugin {
public override string Name => "PlaytimeRewards";
public override Version Version => new Version(1, 2, 2);
public override Version Version => new Version(1, 2, 3);
public override string Author => "Soofa";
public override string Description => "Gives players rewards based on how much time they've played on the server.";

private bool isHM;
public static string path = Path.Combine(TShock.SavePath + "/PlaytimeRewardsConfig.json");
public static Config Config = new Config();
public static DateTime lastTime = DateTime.UtcNow;
Expand All @@ -28,10 +29,10 @@ public override void Initialize() {
db = new SqliteConnection(("Data Source=" + Path.Combine(TShock.SavePath, "PlaytimeRewards.sqlite")));
dbManager = new Database.DatabaseManager(db);

ServerApi.Hooks.GameHardmodeTileUpdate.Register(this, OnGameHardmodeTileUpdate);
ServerApi.Hooks.ServerLeave.Register(this, OnServerLeave);
ServerApi.Hooks.WorldStartHardMode.Register(this, OnWorldStartHardMode);
ServerApi.Hooks.ServerJoin.Register(this, OnServerJoin);
GeneralHooks.ReloadEvent += OnReload;
ServerApi.Hooks.GamePostInitialize.Register(this, OnGamePostInitialize);

Commands.ChatCommands.Add(new Command("pr.getreward", GetRewardCmd, "getreward", "gr") {
AllowServer = false,
Expand All @@ -54,6 +55,10 @@ public override void Initialize() {
}
}

private void OnGamePostInitialize(EventArgs args) {
isHM = Main.hardMode;
}

private void UpdateDatabaseCmd(CommandArgs args) {
foreach (var kvp in Config.PlayerList) {
if (!dbManager.SavePlayer(kvp.Key, kvp.Value)) {
Expand Down Expand Up @@ -91,13 +96,17 @@ public void UpdateTime() {
lastTime = DateTime.UtcNow;
}

private void OnGameHardmodeTileUpdate(HandledEventArgs args) {
private void OnWorldStartHardMode(HandledEventArgs args) {
if (isHM) {
return;
}
UpdateTime();
foreach (var kvp in onlinePlayers) {
onlinePlayers[kvp.Key] = (int)(kvp.Value*Config.SwitchToHMMultiplier);
dbManager.SavePlayer(kvp.Key, kvp.Value);
}
TSPlayer.All.SendInfoMessage($"Everyone's playtime has been reduced by {100*(1-Config.SwitchToHMMultiplier)}%.");
isHM = true;
}
private void PlayTimeCmd(CommandArgs args) {
UpdateTime();
Expand Down Expand Up @@ -162,9 +171,10 @@ private void OnReload(ReloadEventArgs e) {
}
protected override void Dispose(bool disposing) {
if(disposing) {
ServerApi.Hooks.WorldStartHardMode.Deregister(this, OnGameHardmodeTileUpdate);
ServerApi.Hooks.WorldStartHardMode.Deregister(this, OnWorldStartHardMode);
ServerApi.Hooks.ServerLeave.Deregister(this, OnServerLeave);
ServerApi.Hooks.ServerJoin.Deregister(this, OnServerJoin);
ServerApi.Hooks.GameWorldConnect.Deregister(this, OnGamePostInitialize);
GeneralHooks.ReloadEvent -= OnReload;
}
base.Dispose(disposing);
Expand Down

0 comments on commit 9ef0ee8

Please sign in to comment.