using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Threading;
using Holo.Virtual.Rooms;
using Ion.Storage;
namespace Holo.Managers
{
///
/// Provides management for virtual rooms, aswell as some misc tasks for rooms.
///
public static class roomManager
{
private struct CYCLESTATUSUTILS
{
private static int mConfigTime;
private static Hashtable mStorage;
private static DateTime mStart;
public static int MAXCYCLECONFIGTIME
{
get
{
return mConfigTime;
}
set
{
mConfigTime = value;
}
}
public static Hashtable SAFEROOMLIST
{
get
{
return mStorage;
}
}
public static void REFRESHROOMLIST()
{
if (mStorage == null)
mStorage = new Hashtable();
if (mStorage.Count != 0)
mStorage.Clear();
mStorage = (Hashtable)_Rooms.Clone();
}
public static DateTime CYCLESTARTTIME
{
get
{
return mStart;
}
set
{
mStart = value;
}
}
public static int GETSLEEPTIME
{
get
{
TimeSpan Gap = DateTime.Now - mStart;
return mConfigTime - Gap.Milliseconds;
}
}
public static int GETSPENTTIME
{
get
{
TimeSpan UsedTime = DateTime.Now - mStart;
return UsedTime.Milliseconds;
}
}
}
#region Declares
///
/// Contains the hooked virtual room objects.
///
private static Hashtable _Rooms = new Hashtable();
public static Hashtable _roomLayOut = new Hashtable();
private static Thread MAINCYCLETHREAD;
private static Thread CycleWatcher;
///
/// The peak amount of rooms that has been in the room manager since start of the emulator.
///
private static int _peakRoomCount;
#endregion
#region Virtual room management
///
/// Adds a virtualRoom class together with the roomID to the roomManager.
///
/// The ID of the room to add..
/// The virtualRoom class of this room.
public static void Init()
{
Out.WriteLine("Starting cyclesystem");
CYCLESTATUSUTILS.MAXCYCLECONFIGTIME = 410;
StartThread();
CycleWatcher = new Thread(new ThreadStart(WatchCycleThread));
CycleWatcher.Start();
Out.WriteLine("Cyclethread ready");
}
private static void StartThread()
{
try
{
if (MAINCYCLETHREAD.IsAlive)
MAINCYCLETHREAD.Abort();
}
catch { }
MAINCYCLETHREAD = new Thread(new ThreadStart(CYCLEALLSTATUSES));
MAINCYCLETHREAD.Start();
}
private static void CYCLEALLSTATUSES()
{
while (true)
{
try
{
if (_Rooms.Count > 0)
{
CYCLESTATUSUTILS.CYCLESTARTTIME = DateTime.Now;
CYCLESTATUSUTILS.REFRESHROOMLIST();
try
{
foreach (virtualRoom MainRoon in CYCLESTATUSUTILS.SAFEROOMLIST.Values)
{
MainRoon.cycleStatuses();
}
}
catch (Exception CriticalError)
{
Out.PrimaryErrorWriter("MAINCYCLETHREADERROR\r\n" + CriticalError.ToString());
}
int SleepTime = CYCLESTATUSUTILS.GETSLEEPTIME;
if (SleepTime > 0)
Thread.Sleep(SleepTime);
}
else
Thread.Sleep(2000);
}
catch (Exception FATALERROR)
{
Out.SecundaryErrorWriter(FATALERROR.ToString());
}
}
}
private static void WatchCycleThread()
{
while (true)
{
if (CYCLESTATUSUTILS.GETSPENTTIME > 3000)
{
try
{
MAINCYCLETHREAD.Abort();
}
catch { }
StartThread();
MAINCYCLETHREAD = null;
}
Thread.Sleep(2000);
}
}
public static void addRoom(int roomID, virtualRoom Room)
{
if (_Rooms.ContainsKey(roomID) == false)
{
_Rooms.Add(roomID, Room);
//Out.WriteLine("Room [" + roomID + ", publicroom: " + Room.isPublicroom.ToString().ToLower() + "] loaded.", Out.logFlags.StandardAction);
if (_Rooms.Count > _peakRoomCount)
_peakRoomCount = _Rooms.Count;
//RoomsLoaded++;
}
}
///
/// Removes a room from the roomManager. [if it exists]
///
/// The ID of the room to remove.
public static void removeRoom(int roomID)
{
//Should be recoded!
if (_Rooms.ContainsKey(roomID))
{
//bool boolPublicroom = ((virtualRoom)_Rooms[roomID]).isPublicroom;
_Rooms.Remove(roomID);
updateRoomVisitorCount(roomID, 0);
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
dbClient.runQuery("UPDATE rooms SET visitors_now = null WHERE id = '" + roomID + "'");
}
//Out.WriteLine("Room [" + roomID + ", publicroom: " + boolPublicroom.ToString().ToLower() + "] destroyed.", Out.logFlags.StandardAction);
GC.Collect();
//RoomsLoaded--;
}
}
///
/// Returns a bool that indicates if the roomManager contains a certain room.
///
/// The ID of the room.
public static bool containsRoom(int roomID)
{
return _Rooms.ContainsKey(roomID);
}
///
/// Returns the current amount of rooms in the roomManager.
///
public static int roomCount
{
get
{
return _Rooms.Count;
//return RoomsLoaded;
}
}
///
/// Returns the peak amount of rooms in the roomManager since boot.
///
public static int peakRoomCount
{
get
{
return _peakRoomCount;
}
}
///
/// Returns a virtualRoom class for a certain room.
///
/// The ID of the room.
public static virtualRoom getRoom(int roomID)
{
if (_Rooms.ContainsKey(roomID))
{
return (virtualRoom)_Rooms[roomID];
}
else
return null;
}
#endregion
#region Misc room related functions
///
/// Updates the inside visitors count in the database for a certain room.
///
/// The ID of the room to update.
/// The new visitors count.
public static void updateRoomVisitorCount(int roomID, int visitorCount)
{
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
dbClient.runQuery("UPDATE rooms SET visitors_now = '" + visitorCount + "' WHERE id = '" + roomID + "' LIMIT 1");
}
}
///
/// Returns the int ID for a certain room state.
///
/// The room state ID.
public static int getRoomState(string State)
{
if (State == "closed")
return 1;
else if (State == "password")
return 2;
else
return 0;
}
///
/// Returns the string state for a certain room state byte.
///
/// The room state ID.
public static string getRoomState(int State)
{
if (State == 1)
return "closed";
else if (State == 2)
return "password";
else
return "open";
}
#endregion
#region Furni related functions
///
/// Updates the inside visitors count in the database for a certain room.
///
/// The ID of the room the item is in.
/// The ID of the item to refresh.
/// The cct name of the item to refresh.
/// The wall position of the item to refresh.
/// The variable of the item to refresh.
/// The variable of the item to refresh.
public static void refreshWallitem(int roomID, int itemID, string cctName, string wallPosition, string itemVariable, int templateID, virtualRoom TheRoom)
{
try
{
templateID = 1002;
if (TheRoom != null)
{
TheRoom.sendData("AU" + itemID + Convert.ToChar(2) + Encoding.encodeVL64(templateID) + "[nO" + wallPosition + Convert.ToChar(2) + itemVariable + Convert.ToChar(2));
}
}
catch (Exception Bug)
{
Out.PrimaryErrorWriter("Moonlightbug:\r\n" + Bug.ToString());
}
}
///
/// Contains functions for the moodlight.
///
public static class moodlight
{
///
/// Returns a string containing the setting data for the moodlight in the room.
///
/// The roomID to get the moodlight for.
public static string getSettings(int roomID)
{
try
{
DataRow dRow;
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
dRow = dbClient.getRow("SELECT preset_cur,preset_1,preset_2,preset_3 FROM furniture_moodlight WHERE roomid = '" + roomID + "'");
}
string settingPack = Encoding.encodeVL64(3) + Encoding.encodeVL64(Convert.ToInt32(dRow["preset_cur"]));
for (int i = 1; i <= 3; i++)
{
string[] curPresetData = dRow[i].ToString().Split(Char.Parse(","));
settingPack += Encoding.encodeVL64(i) + Encoding.encodeVL64(int.Parse(curPresetData[0])) + curPresetData[1] + Convert.ToChar(2) + Encoding.encodeVL64(int.Parse(curPresetData[2]));
}
return settingPack;
}
catch
{
return null;
}
}
///
/// Saves the setting data for the moodlight in the room.
///
/// The roomID to get the moodlight for.
/// The status of the moodlight (on/off).
/// The preset slot that is being used.
/// The status of the background only tick.
/// The colour that is being saved.
/// The alpha value of the darkness level.
public static void setSettings(int roomID, bool isEnabled, int presetID, int bgState, string presetColour, int alphaDarkF, virtualRoom Room)
{
int itemID;
int templateID = 0;
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
itemID = dbClient.getInt("SELECT id FROM furniture_moodlight WHERE roomid = '" + roomID + "' LIMIT 1");
}
string newPresetValue;
if (isEnabled == false)
{
string curPresetValue;
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
curPresetValue = dbClient.getString("SELECT var FROM furniture WHERE id = '" + itemID + "'");
templateID = dbClient.getInt("SELECT tid FROM furniture WHERE id = '" + itemID + "'");
}
if (curPresetValue.Substring(0, 1) == "2")
{
newPresetValue = "1" + curPresetValue.Substring(1);
}
else
{
newPresetValue = "2" + curPresetValue.Substring(1);
}
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
dbClient.runQuery("UPDATE furniture SET var = '" + newPresetValue + "' WHERE id = '" + itemID.ToString() + "' LIMIT 1");
}
}
else
{
newPresetValue = "2" + "," + presetID.ToString() + "," + bgState.ToString() + "," + presetColour + "," + alphaDarkF.ToString();
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
dbClient.AddParamWithValue("var", newPresetValue);
dbClient.AddParamWithValue("preset", bgState.ToString() + ", " + presetColour + ", " + alphaDarkF.ToString());
dbClient.runQuery("UPDATE furniture SET var = @var WHERE id = '" + itemID.ToString() + "' LIMIT 1");
dbClient.runQuery("UPDATE furniture_moodlight SET preset_cur = '" + presetID.ToString() + "',preset_" + presetID.ToString() + "= @preset WHERE id = '" + itemID.ToString() + "' LIMIT 1");
}
}
string wallPosition;
using (DatabaseClient dbClient = frmMain.dbManager.GetClient())
{
wallPosition = dbClient.getString("SELECT wallpos FROM furniture WHERE id = '" + itemID + "'");
}
refreshWallitem(roomID, itemID, "roomdimmer", wallPosition, newPresetValue, templateID, Room);
}
}
#endregion
public static void ShutDownManager()
{
try
{
try
{
lock (_Rooms)
{
foreach (virtualRoom EndRoom in _Rooms)
{
try
{
EndRoom.Crash();
}
catch { }
}
}
_Rooms.Clear();
_Rooms = null;
}
catch { }
}
catch { }
try
{
_roomLayOut.Clear();
_roomLayOut = null;
}
catch { }
}
}
}