namespace Holo.Managers { using Holo; using Holo.Virtual.Users; using Ion.Storage; using System; using System.Collections; using System.Runtime.InteropServices; using System.Text; using System.Threading; public static class eventManager { internal static int categoryAmount; private static int deadEventDropInterval; private static Thread deadEventDropper; private static Hashtable[] Events; public static bool categoryOK(int categoryID) { return ((categoryID > 0) && (categoryID <= categoryAmount)); } public static void createEvent(int categoryID, int userID, int roomID, string Name, string Description) { if (!Events[categoryID - 1].Contains(roomID)) { virtualEvent event2 = new virtualEvent(roomID, userID, Name, Description); using (DatabaseClient client = Eucalypt.dbManager.GetClient()) { client.AddParamWithValue("ename", Name); client.AddParamWithValue("edescription", Description); client.runQuery(string.Concat(new object[] { "INSERT INTO events (name,description,userid,roomid,category,date) VALUES (@ename, @edescription, '", userID, "', '", roomID, "', '", categoryID, "', '", DateTime.Now.TimeOfDay.ToString(), "')" })); } Events[categoryID - 1].Add(roomID, event2); } } private static void dropDeadEvents() { while (true) { for (int i = 0; i < categoryAmount; i++) { Hashtable hashtable = (Hashtable) Events[i].Clone(); foreach (virtualEvent event2 in hashtable.Values) { if (!userManager.containsUser(event2.userID)) { using (DatabaseClient client = Eucalypt.dbManager.GetClient()) { client.runQuery("DELETE FROM events WHERE roomid = '" + event2.roomID + "'"); } Events[i].Remove(event2.roomID); continue; } virtualUser user = userManager.getUser(event2.userID); if (user._roomID != event2.roomID) { using (DatabaseClient client2 = Eucalypt.dbManager.GetClient()) { client2.runQuery("DELETE FROM events WHERE roomid = '" + event2.roomID + "'"); } Events[i].Remove(event2.roomID); user._hostsEvent = false; } } } Thread.Sleep(deadEventDropInterval); Out.WriteTrace("Drop dead event loop"); } } public static void editEvent(int categoryID, int roomID, string Name, string Description) { if (Events[categoryID - 1].Contains(roomID)) { virtualEvent event2 = (virtualEvent) Events[categoryID - 1][roomID]; event2.Name = Name; event2.Description = Description; using (DatabaseClient client = Eucalypt.dbManager.GetClient()) { client.AddParamWithValue("name", Name); client.AddParamWithValue("desc", Description); client.runQuery(string.Concat(new object[] { "UPDATE events SET name = @name ,description = @desc ,category = '", categoryID, "',date = '", DateTime.Now.TimeOfDay.ToString(), "' WHERE roomid = '", roomID, "'" })); } Events[categoryID - 1].Remove(roomID); Events[categoryID - 1].Add(roomID, event2); } } public static string getEvent(int roomID) { try { for (int i = 0; i < categoryAmount; i++) { if (Events[i].ContainsKey(roomID)) { virtualEvent event2 = (virtualEvent) Events[i][roomID]; return string.Concat(new object[] { event2.userID, Convert.ToChar(2).ToString(), userManager.getUser(event2.userID)._Username, Convert.ToChar(2).ToString(), event2.roomID, Convert.ToChar(2).ToString(), Holo.Encoding.encodeVL64(i + 1), event2.Name, Convert.ToChar(2).ToString(), event2.Description, Convert.ToChar(2).ToString(), event2.Started, Convert.ToChar(2).ToString() }); } } return "-1"; } catch { return "-1"; } } public static string getEvents(int categoryID) { try { int i = 0; StringBuilder builder = new StringBuilder(); Hashtable hashtable = (Hashtable) Events[categoryID - 1].Clone(); foreach (virtualEvent event2 in hashtable.Values) { virtualUser user = userManager.getUser(event2.userID); if (user != null) { builder.Append(string.Concat(new object[] { event2.roomID, Convert.ToChar(2).ToString(), user._Username, Convert.ToChar(2), event2.Name, Convert.ToChar(2), event2.Description, Convert.ToChar(2), event2.Started, Convert.ToChar(2) })); i++; } } hashtable.Clear(); return (Holo.Encoding.encodeVL64(i) + builder.ToString()); } catch { return "H"; } } public static void Init() { try { deadEventDropper.Abort(); } catch { } categoryAmount = int.Parse(Config.getTableEntry("events_categorycount")); Events = new Hashtable[categoryAmount]; for (int i = 0; i < categoryAmount; i++) { Events[i] = new Hashtable(); } deadEventDropInterval = int.Parse(Config.getTableEntry("events_deadevents_removeinterval")) * 0x3e8; deadEventDropper = new Thread(new ThreadStart(eventManager.dropDeadEvents)); deadEventDropper.Priority = ThreadPriority.BelowNormal; deadEventDropper.Start(); } public static void removeEvent(int roomID) { for (int i = 0; i < categoryAmount; i++) { if (Events[i].ContainsKey(roomID)) { using (DatabaseClient client = Eucalypt.dbManager.GetClient()) { client.runQuery("DELETE FROM events WHERE roomid = '" + roomID + "'"); } Events[i].Remove(roomID); return; } } } [StructLayout(LayoutKind.Sequential)] private struct virtualEvent { internal int roomID; internal int userID; internal string Name; internal string Description; internal string Started; internal virtualEvent(int roomID, int userID, string Name, string Description) { this.roomID = roomID; this.userID = userID; this.Name = Name; this.Description = Description; this.Started = DateTime.Now.ToShortTimeString(); } } } }