using System; using System.Threading; using System.Collections; using Holo.Managers; using Holo.Virtual.Rooms; namespace Holo.Virtual.Users { /// /// Provides management for the statuses of a virtual user. /// public class virtualRoomUserStatusManager { #region Declares /// /// The ID of the user that uses this status manager. /// private int userID; /// /// The ID of the room the user that uses this status manager is in. /// private int roomID; /// /// Contains the status strings. /// private Hashtable _Statuses; /// /// The thread that handles the carrying, drinking and finally vanishing the item in the virtual room. /// private Thread itemCarrier; private delegate void statusVoid(string Key, string Value, int Length); #endregion #region Constructors/destructors public virtualRoomUserStatusManager(int userID, int roomID) { this.userID = userID; this.roomID = roomID; _Statuses = new Hashtable(); } /// /// Empties the status manager and destructs all inside objects. /// internal void Clear() { try { itemCarrier.Abort(); _Statuses.Clear(); _Statuses = null; } catch { } } #endregion #region Partner objects /// /// The parent virtualUser object of this status manager. /// private virtualUser User { get { return userManager.getUser(userID); } } /// /// The virtualRoom object where the parent virtual user of this status manager is in. /// private virtualRoom Room { get { return roomManager.getRoom(roomID); } } /// /// The virtualRoomUser object of the parent virtual user of this status manager. /// private virtualRoomUser roomUser { get { return userManager.getUser(userID).roomUser; } } #endregion #region Status management /// /// Adds a status key and a value to the status manager. If the status is already inside, then the previous one will be removed. /// /// The key of the status. /// The value of the status. internal void addStatus(string Key, string Value) { if (_Statuses.ContainsKey(Key)) _Statuses.Remove(Key); _Statuses.Add(Key, Value); } /// /// Removes a certain status from the status manager. /// /// The key of the status to remove. internal void removeStatus(string Key) { try { if (_Statuses.ContainsKey(Key)) _Statuses.Remove(Key); } catch { } } /// /// Returns a bool that indicates if a certain status is in the status manager. /// /// The key of the status to check. internal bool containsStatus(string Key) { return _Statuses.ContainsKey(Key); } /// /// Refreshes the status of the parent virtual user in the virtual room. /// internal void Refresh() { roomUser.Refresh(); } /// /// Returns the status string of all the statuses currently in the status manager. /// public override string ToString() { string Output = ""; Hashtable status = (Hashtable)_Statuses.Clone(); try { foreach (string Key in status.Keys) { Output += Key; string Value = (string)_Statuses[Key]; if (Value != "") Output += " " + Value; Output += "/"; } } catch { } return Output; } #endregion #region Statuses /// /// Makes the user carry a drink/item in the virtual room. Starts a thread that uses config-defined values. The thread will handle the animations of the sips etc, and finally the drop. /// /// The item to carry. internal void carryItem(string Item) { dropCarrydItem(); _Statuses.Remove("dance"); itemCarrier = new Thread(new ParameterizedThreadStart(itemCarrierLoop)); itemCarrier.Priority = ThreadPriority.Lowest; itemCarrier.Start(Item); } /// /// Immediately stops carrying an item. /// internal void dropCarrydItem() { if (itemCarrier != null && itemCarrier.IsAlive) itemCarrier.Abort(); removeStatus("carryd"); removeStatus("drink"); } #endregion #region Status handlers /// /// Adds a status, keeps it for a specified amount of time [in ms] and removes the status again. Refreshes at add and remove. /// /// /// /// internal void handleStatus(string Key, string Value, int Length) { if (_Statuses.ContainsKey(Key)) _Statuses.Remove(Key); new statusVoid(HANDLESTATUS).BeginInvoke(Key, Value, Length, null, null); } private void HANDLESTATUS(string Key, string Value, int Length) { try { _Statuses.Add(Key, Value); roomUser.Refresh(); Thread.Sleep(Length); _Statuses.Remove(Key); roomUser.Refresh(); } catch { } } /// /// Ran on thread with lowest priority. Handles the carrying and drinking of an item in the virtual room. /// /// The object that will be converted in a string to serve as the item being carryd. private void itemCarrierLoop(object s) { string carrydItem = (string)s; for(int i = 1; i <= Config.Statuses_itemCarrying_SipAmount; i++) { addStatus("carryd",carrydItem); roomUser.Refresh(); Thread.Sleep(Config.Statuses_itemCarrying_SipInterval); _Statuses.Remove("carryd"); addStatus("drink",carrydItem); roomUser.Refresh(); Thread.Sleep(Config.Statuses_itemCarrying_SipDuration); _Statuses.Remove("drink"); } roomUser.Refresh(); } #endregion } }