using System;
using System.Collections;
using Ion.Storage;
using System.Data;
using Holo.Source.Virtual.Catalogue;
namespace Holo.Managers
{
///
/// Provides functions and tasks for the item Recycler, which allows users to trade in their items for special items.
///
public static class recyclerManager
{
private static int sessionLength;
private static int sessionExpireLength;
private static int itemMinOwnershipLength;
private static Hashtable sessionRewards;
public static string setupString;
///
/// Initializes the item Recycler, determining the state and creating the setup string.
///
public static void Init()
{
if (Config.getTableEntry("recycler_enable") == "1")
{
Config.enableRecycler = true;
Out.WriteLine("Initializing recycler...");
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
{
DataTable table = dbClient.ReadDataTable("SELECT rclr_cost, rclr_reward FROM system_recycler");
sessionLength = int.Parse(Config.getTableEntry("recycler_session_length"));
sessionExpireLength = int.Parse(Config.getTableEntry("recycler_session_expirelength"));
itemMinOwnershipLength = int.Parse(Config.getTableEntry("recycler_minownertime"));
sessionRewards = new Hashtable();
setupString = "I" + Encoding.encodeVL64(itemMinOwnershipLength) + Encoding.encodeVL64(sessionLength) + Encoding.encodeVL64(sessionExpireLength) + Encoding.encodeVL64(table.Rows.Count);
foreach(DataRow row in table.Rows)
{
itemTemplate Template = catalogueManager.getItem(Convert.ToInt32(row[1]));
if (Template.Sprite != "")
{
sessionRewards.Add(row[0], row[1]);
setupString += Encoding.encodeVL64(Convert.ToInt32(row[0])) + "H" + Template.Sprite + Convert.ToChar(2) + "H" + Encoding.encodeVL64(Template.Length) + Encoding.encodeVL64(Template.Width) + Convert.ToChar(2);
//setupString += Encoding.encodeVL64(rclrCosts[i]) + "H" + Template.cctName + Convert.ToChar(2) + "HJI" + Convert.ToChar(2);
}
}
}
Out.WriteLine("Recycler enabled.");
}
else
{
setupString = "H";
Out.WriteLine("Recycler disabled.");
}
}
#region Session management
///
/// Determines if there exists a reward for this amount of brought in items.
///
/// The amount of items brought in.
public static bool rewardExists(int itemCount)
{
return sessionRewards.ContainsKey(itemCount);
}
///
/// Creates a Recycler session for this user in the users_recycler table, with the userid, the timestamp where the session started and the reward template ID matching this amount of brought of items.
///
/// The ID of the user who requests a Recycler session.
/// The amount of items the user brought in.
public static void createSession(int userID, int itemCount)
{
int rewardTemplateID = ((int)sessionRewards[itemCount]);
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
dbClient.ExecuteQuery("INSERT INTO users_recycler(userid,session_started,session_reward) VALUES ('" + userID + "','" + DateTime.Now.ToString() + "','" + rewardTemplateID + "')");
}
///
/// Deletes the user's session row in users_recycler, and brings items back to the users hand OR deletes them permanently from the database.
///
/// The ID of the user who owns this session.
/// When true, the items will be deleted from database, else the items will return back to the users Hand.
public static void dropSession(int userID, bool dropItems)
{
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
{
dbClient.ExecuteQuery("DELETE FROM users_recycler WHERE userid = '" + userID + "' LIMIT 1");
if (dropItems)
dbClient.ExecuteQuery("DELETE FROM furniture WHERE ownerid = '" + userID + "' AND roomid = '-2'");
else
dbClient.ExecuteQuery("UPDATE furniture SET roomid = '0' WHERE ownerid = '" + userID + "' AND roomid = '-2'");
}
}
///
/// Creates the reward for the user and handles special items such as teleporters, so user receives a pair etc.
///
/// The ID of the user to reward the session of.
public static void rewardSession(int userID)
{
int rewardTemplateID = sessionRewardID(userID);
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
dbClient.ExecuteQuery("INSERT INTO furniture(tid,ownerid) VALUES ('" + sessionRewardID(userID) + "','" + userID + "')");
catalogueManager.handlePurchase(rewardTemplateID, userID, 0, 0, 0);
}
///
/// Returns the amount of passed minutes since the user's session started. If the user hasn't got a session or any other error occurs, 0 is returned.
///
/// The user ID of the session to lookup.
public static int passedMinutes(int userID)
{
try
{
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
{
TimeSpan Span = DateTime.Now - DateTime.Parse(dbClient.ReadString("SELECT session_started FROM users_recycler WHERE userid = '" + userID + "'"));
return Convert.ToInt32(Span.TotalMinutes);
}
}
catch
{
return 0;
}
}
///
/// Returns the session string for a users session to use with the "Dp" packet.
///
/// The ID of the user to retrieve the session string for.
public static string sessionString(int userID)
{
if (Config.enableRecycler == false)
return "H";
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
if (!dbClient.Exists("SELECT * FROM users_recycler WHERE userid = '" + userID + "'"))
return "H";
int minutesPassed = passedMinutes(userID);
if (minutesPassed < sessionLength)
return "IH" + catalogueManager.getItem(sessionRewardID(userID)).Sprite + Convert.ToChar(2) + Encoding.encodeVL64(sessionLength - minutesPassed);
if (minutesPassed > sessionLength)
return "JH" + catalogueManager.getItem(sessionRewardID(userID)).Sprite + Convert.ToChar(2);
if (minutesPassed > sessionExpireLength)
return "K";
return "H";
}
///
/// Returns a bool that indicates if a user has a session.
///
/// The ID of the user to check for sessions.
public static bool sessionExists(int userID)
{
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
return dbClient.Exists("SELECT userid FROM users_recycler WHERE userid = '" + userID + "'");
}
///
/// Returns a bool that indicates if a user's session is ready and hasn't expired yet
///
/// The user ID of the user to lookup the session of.
public static bool sessionReady(int userID)
{
if (sessionExists(userID))
{
int minutesPassed = passedMinutes(userID);
if ((minutesPassed > sessionLength) & (minutesPassed < sessionExpireLength))
return true;
}
return false;
}
///
/// Returns the template ID of reward item of the session of a user.
///
/// The user ID of the session to lookup.
private static int sessionRewardID(int userID)
{
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
return dbClient.ReadInt32("SELECT session_reward FROM users_recycler WHERE userid = '" + userID + "'");
}
#endregion
}
}