public override void NetSend(Item item, BinaryWriter writer) { if (this.NeedsSaving(item)) { writer.Write((Int32)SystemHelpers.TimeStampInSeconds() - this.TimestampInSeconds); } }
//// public override void NetReceive(Item item, BinaryReader reader) { if (this.NeedsSaving(item)) { this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds() - reader.ReadInt32(); } }
public override TagCompound Save() { return(new TagCompound { { "stack", this.StoredItemStackSize }, { "type", this.StoredItemType }, { "duration", (int)(SystemHelpers.TimeStampInSeconds() - this.TimestampInSeconds) } }); }
//////////////// public void SetTimeLeftByPercent(int maxElapsedSeconds, float timeLeftPercent) { float elapsedPercent = 1f - timeLeftPercent; int elapsedTicks = (int)((float)maxElapsedSeconds * elapsedPercent); int elapsedSeconds = elapsedTicks / 60; long now = SystemHelpers.TimeStampInSeconds(); this.TimestampInSeconds = now - elapsedSeconds; }
public override TagCompound Save(Item item) { if (this.NeedsSaving(item)) { return(new TagCompound { { "duration", (int)(SystemHelpers.TimeStampInSeconds() - this.TimestampInSeconds) } }); } return(new TagCompound()); }
//////////////// public override void Load(TagCompound tag) { if (!tag.ContainsKey("stack")) { return; } this.StoredItemStackSize = tag.GetInt("stack"); this.StoredItemType = tag.GetInt("type"); this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds() - tag.GetInt("duration"); }
//////////////// public void ResetTimestampAndMaxStackSize(Item item) { if (this.NeedsSaving(item)) { item.maxStack = 1; if (this.TimestampInSeconds == 0) { this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds(); } } }
public override void Load(Item item, TagCompound tags) { if (this.NeedsSaving(item)) { this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds(); if (tags.ContainsKey("duration")) { this.TimestampInSeconds -= tags.GetInt("duration"); } } }
public bool ComputeElapsedTicks(out int elapsedTicks) { if (this.StoredItemStackSize == 0) { elapsedTicks = 0; return(false); } var mymod = (StarvationMod)this.mod; long now = SystemHelpers.TimeStampInSeconds(); int elapsedSeconds = (int)(now - this.TimestampInSeconds); elapsedTicks = elapsedSeconds * 60; //float elapsedTicksScaled = (float)elapsedTicks * mymod.Config.TupperwareSpoilageRateScale; return(true); }
//////////////// public bool SetTimeLeftByPercent(Item item, float timeLeftPercent) { int maxElapsedTicks; if (!this.ComputeMaxElapsedTicks(item, out maxElapsedTicks)) { throw new ModHelpersException("Could not compute max elapsed ticks."); } float elapsedPercent = 1f - timeLeftPercent; int elapsedTicks = (int)((float)maxElapsedTicks * elapsedPercent); int elapsedSeconds = elapsedTicks / 60; long now = SystemHelpers.TimeStampInSeconds(); this.TimestampInSeconds = now - elapsedSeconds; return(true); }
private static void DoWorkToValidateServer(ServerBrowserEntry server_data, string hash) { string hash_base = ""; bool found = false; for (int i = 0; i < 1000000; i++) { hash_base = i + ""; string test_hash = SystemHelpers.ComputeSHA256Hash(hash_base); if (hash == test_hash) { found = true; break; } } if (!found) { LogHelpers.Log("Server browser processing failed; no matching hash."); return; } var output_obj = new ServerBrowserWorkProof { ServerIP = server_data.ServerIP, Port = server_data.Port, WorldName = server_data.WorldName, HashBase = hash_base }; string json_str = JsonConvert.SerializeObject(output_obj, Formatting.None); byte[] json_bytes = Encoding.UTF8.GetBytes(json_str); Thread.Sleep(1500); NetHelpers.NetHelpers.MakePostRequestAsync(ServerBrowserReporter.URL, json_bytes, delegate(string output) { LogHelpers.Log("Server browser processing complete."); }, delegate(Exception e, string output) { LogHelpers.Log("Server browser reply returned error: " + e.ToString()); }); ServerBrowserReporter.LastSendTimestamp = SystemHelpers.TimeStampInSeconds(); }
//////////////// public bool ComputeElapsedTicks(Item item, out int elapsedTicks) { if (!this.NeedsSaving(item)) { elapsedTicks = 0; return(false); } if (this.TimestampInSeconds == 0) { elapsedTicks = 0; return(false); } var mymod = (StarvationMod)this.mod; long now = SystemHelpers.TimeStampInSeconds(); int elapsedSeconds = (int)(now - this.TimestampInSeconds); elapsedTicks = (int)elapsedSeconds * 60; return(true); }
public TimeSpan GetTimeSpan() { long now = SystemHelpers.TimeStampInSeconds(); long total_seconds = now - this.Created; long max_seconds = 60 * 60 * 24 * 30; long seconds = total_seconds; if (total_seconds > max_seconds) { seconds = max_seconds; } long minutes = seconds / 60; long hours = minutes / 60; minutes -= (hours * 60); seconds -= (minutes * 60) + (hours * 60 * 60); var span = new TimeSpan((int)hours, (int)minutes, (int)seconds); return(span); }
public override void NetRecieve(BinaryReader reader) { this.StoredItemStackSize = reader.ReadInt32(); this.StoredItemType = reader.ReadInt32(); this.TimestampInSeconds = SystemHelpers.TimeStampInSeconds() - reader.ReadInt32(); }
public override void NetSend(BinaryWriter writer) { writer.Write((int)this.StoredItemStackSize); writer.Write((int)this.StoredItemType); writer.Write((int)(SystemHelpers.TimeStampInSeconds() - this.TimestampInSeconds)); }
//////////////// public static bool IsHammering() { return((SystemHelpers.TimeStampInSeconds() - ServerBrowserReporter.LastSendTimestamp) <= 10); }
//////////////// public static void AnnounceServer() { HamstarHelpersMod mymod = HamstarHelpersMod.Instance; Version vers = mymod.Version; int port = Netplay.ListenPort; if (mymod.Config.ServerBrowserCustomPort != -1) { port = mymod.Config.ServerBrowserCustomPort; } int pvp = 0; bool[] team_checks = new bool[10]; ServerBrowserReporter server_browser = mymod.ServerBrowser; for (int i = 0; i < Main.player.Length; i++) { Player player = Main.player[i]; if (player == null || !player.active) { continue; } if (player.hostile) { pvp++; } team_checks[player.team] = true; } int team_count = 0; for (int i = 1; i < team_checks.Length; i++) { if (team_checks[i]) { team_count++; } } try { var server_data = new ServerBrowserEntry(); server_data.ServerIP = NetHelpers.NetHelpers.GetPublicIP(); server_data.Port = port; server_data.IsPassworded = Netplay.ServerPassword != ""; server_data.Motd = Main.motd; server_data.WorldName = Main.worldName; server_data.WorldProgress = InfoHelpers.GetVanillaProgress(); server_data.WorldEvent = string.Join(", ", InfoHelpers.GetCurrentVanillaEvents().ToArray()); server_data.MaxPlayerCount = Main.maxNetPlayers; server_data.PlayerCount = Main.ActivePlayersCount; server_data.PlayerPvpCount = pvp; server_data.TeamsCount = team_count; server_data.AveragePing = mymod.ServerBrowser.AveragePing; server_data.Mods = new Dictionary <string, string>(); server_data.Version = (vers.Major * 1000000) + (vers.Minor * 10000) + (vers.Build * 100) + vers.Revision; foreach (Mod mod in ModLoader.LoadedMods) { if (mod.File == null) { continue; } server_data.Mods[mod.DisplayName] = mod.Version.ToString(); } string json_str = JsonConvert.SerializeObject(server_data, Formatting.None); byte[] json_bytes = Encoding.UTF8.GetBytes(json_str); Action <string> on_response = delegate(string output) { ServerBrowserReporter.HandleServerAnnounceOutputAsync(server_data, output); }; Action <Exception, string> on_error = delegate(Exception e, string output) { LogHelpers.Log("Server browser returned error: " + e.ToString()); }; NetHelpers.NetHelpers.MakePostRequestAsync(ServerBrowserReporter.URL, json_bytes, on_response, on_error); ServerBrowserReporter.LastSendTimestamp = SystemHelpers.TimeStampInSeconds(); } catch (Exception e) { LogHelpers.Log("AnnounceServer - " + e.ToString()); return; } }