using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using Xoa.Managers;
using Xoa.Virtual.Rooms;
using System.Collections.Generic;
using Xoa.Source.Managers;
using Xoa;
using Ion.Storage;
namespace Xoa.Virtual.Rooms.Bots
{
///
/// Provides management for the statuses of a virtual user.
///
public class virtualRoomBotStatusManager
{
#region Declares
///
/// The ID of the user that uses this status manager.
///
private int botID;
///
/// The ID of the room the user that uses this status manager is in.
///
private int roomID;
///
/// Contains the status strings.
///
internal 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 virtualRoomBotStatusManager(int botID, int roomID)
{
this.botID = botID;
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 virtualBot Bot
{
get
{
return botManager.getBot(botID);
}
}
///
/// The virtualRoom object where the parent virtual user of this status manager is in.
///
private virtualRoom Room
{
get
{
return roomManager.getRoom(roomID);
}
}
///
/// The virtualroomBot object of the parent virtual user of this status manager.
///
private virtualRoomBot roomBot
{
get
{
return botManager.getBot(botID).roomBot;
}
}
#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 (Key == "carryd" || Key == "drink")
{
if (_Statuses.ContainsKey(Key))
_Statuses.Remove(Key);
_Statuses.Add(Key, Value);
}
else
{
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()
{
roomBot.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);
roomBot.Refresh();
Thread.Sleep(Length);
_Statuses.Remove(Key);
roomBot.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);
roomBot.Refresh();
Thread.Sleep(Config.Statuses_itemCarrying_SipInterval);
_Statuses.Remove("carryd");
addStatus("drink", carrydItem);
roomBot.Refresh();
Thread.Sleep(Config.Statuses_itemCarrying_SipDuration);
_Statuses.Remove("drink");
}
roomBot.Refresh();
}
#endregion
}
}