using System;
using System.Data;
using System.Text;
using System.Collections;
using System.Threading;
using Ion.Storage;
using Holo.Source;
using Holo.Source.Virtual.Catalogue;
using System.Collections.Generic;
namespace Holo.Managers
{
///
/// Manager for catalogue page caching, catalogue item templates, catalogue purchase handling and few other catalogue related tasks.
///
public static class catalogueManager
{
private static List Pages;
private static List Products;
private static List Deals;
private static List itemTemplates;
///
/// Initializes the catalogue manager, (re)caching all the pages and item templates.
///
public static void Init(bool Update)
{
Pages = new List();
Products = new List();
Deals = new List();
itemTemplates = new List();
Out.WriteLine("Caching catalogue...");
using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
{
foreach (DataRow row in dbClient.ReadDataTable("SELECT * FROM furniture_templates").Rows)
itemTemplates.Add(itemTemplate.Parse(row));
Out.WriteLine("Cached " + itemTemplates.Count + " item templates!");
foreach (DataRow row in dbClient.ReadDataTable("SELECT * FROM catalogue_products").Rows)
Products.Add(catalogueProduct.Parse(row));
Out.WriteLine("Cached " + Products.Count + " catalogue products!");
foreach (DataRow row in dbClient.ReadDataTable("SELECT * FROM catalogue_deals").Rows)
Deals.Add(catalogueDeal.Parse(row));
Out.WriteLine("Cached " + Deals.Count + " catalogue deal items!");
foreach (DataRow row in dbClient.ReadDataTable("SELECT * FROM catalogue_pages ORDER BY orderid ASC").Rows)
Pages.Add(cataloguePage.Parse(row));
Out.WriteLine("Cached " + Pages.Count + " catalogue pages!");
}
if (Update)
Thread.CurrentThread.Abort();
}
public static List getProducts(int pageID)
{
List products = new List();
foreach (catalogueProduct product in Products)
if (product.mPageID == pageID)
products.Add(product);
return products;
}
public static List getDeals(int productID)
{
List deals = new List();
foreach (catalogueDeal deal in Deals)
if (deal.mProductID == productID)
deals.Add(deal);
return deals;
}
public static catalogueProduct getProduct(string sprite, int pageID)
{
foreach (catalogueProduct product in getProducts(pageID))
if (getItem(product.mTemplateID).Sprite == sprite)
return product;
return null;
}
public static itemTemplate getItem(int ID)
{
foreach (itemTemplate item in itemTemplates)
if (item.ID == ID)
return item;
return null;
}
public static itemTemplate getItem(string sprite)
{
foreach (itemTemplate item in itemTemplates)
if (item.Sprite == sprite)
return item;
return null;
}
public static List getPages()
{
return Pages;
}
public static cataloguePage getPage(string indexname)
{
foreach (cataloguePage page in Pages)
if (page.IndexName == indexname)
return page;
return null;
}
public static itemTemplate generatePresent()
{
int r = new Random().Next(1, 7);
return getItem("present_gen" + r);
}
///
/// Handles special actions at purchase in the catalogue, such as decoration variables for items and items who are sold in pairs.
///
/// The template ID of the item being purchased.
/// The ID of the user that receives the item in his/her Hand.
/// The target room ID of this item. 0 = inhand, -1 = in presentbox, -2 = in Recycler
/// The wallpaper/floor value for wallpaper/floor purchases.
/// If the item is bought as present, then specify the ID of the present box item. If not, specify 0.
public static void handlePurchase(int templateID, int receiverID, int roomID, double decorID, int presentBoxID)
{
itemTemplate template = getItem(templateID);
bool handlePresentbox = true;
switch (template.Sprite.Split(' ')[0])
{
case "wallpaper":
case "floor":
case "landscape":
{
int itemID = lastItemID;
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
dbClient.ExecuteQuery("UPDATE furniture SET var = '" + decorID + "' WHERE id = '" + itemID + "' LIMIT 1");
break;
}
case "roomdimmer":
{
int itemID = lastItemID;
string defaultPreset = "1,#000000,155";
string defaultSetPreset = "1,1,1,#000000,155";
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
{
dbClient.ExecuteQuery("INSERT INTO furniture_moodlight(id,roomid,preset_cur,preset_1,preset_2,preset_3) VALUES ('" + itemID + "','0','1','" + defaultPreset + "','" + defaultPreset + "','" + defaultPreset + "')");
dbClient.ExecuteQuery("UPDATE furniture SET var = '" + defaultSetPreset + "' WHERE id = '" + itemID + "' LIMIT 1");
}
break;
}
case "door":
case "doorB":
case "doorC":
case "doorD":
case "teleport_door":
{
int itemID1 = lastItemID;
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
{
dbClient.ExecuteQuery("INSERT INTO furniture(tid,ownerid,roomid,teleportid) VALUES ('" + templateID + "','" + receiverID + "','" + roomID + "','" + itemID1 + "')");
int itemID2 = lastItemID;
dbClient.ExecuteQuery("UPDATE furniture SET teleportid = '" + itemID2 + "' WHERE id = '" + itemID1 + "' LIMIT 1");
if (presentBoxID > 0)
{
dbClient.ExecuteQuery("INSERT INTO furniture_presents(id,itemid) VALUES ('" + presentBoxID + "','" + itemID1 + "')");
dbClient.ExecuteQuery("INSERT INTO furniture_presents(id,itemid) VALUES ('" + presentBoxID + "','" + itemID2 + "')");
}
}
handlePresentbox = false;
break;
}
case "post.it":
case "post.it.vd":
{
int itemID = lastItemID;
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
dbClient.ExecuteQuery("UPDATE furniture SET var = '20' WHERE id = '" + itemID + "' LIMIT 1");
break;
}
default:
{
if (stringManager.getStringPart(template.Sprite, 0, 10) == "sound_set_")
{
int itemID = lastItemID;
int soundSet = int.Parse(template.Sprite.Substring(10));
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
dbClient.ExecuteQuery("UPDATE furniture SET soundmachine_soundset = '" + soundSet + "' WHERE id = '" + itemID + "' LIMIT 1");
}
else if (template.Sprite.Contains("wallpaper"))
{
int itemID = lastItemID;
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
dbClient.ExecuteQuery("UPDATE furniture SET var = '" + decorID + "' WHERE id = '" + itemID + "' LIMIT 1");
}
break;
}
}
if (presentBoxID > 0 && handlePresentbox)
{
using (DatabaseClient dbClient = (Eucalypt.dbManager.GetClient()))
dbClient.ExecuteQuery("INSERT INTO furniture_presents(id,itemid) VALUES ('" + presentBoxID + "','" + lastItemID + "')");
}
}
public static string tradeItemList(int[] itemIDs)
{
FuseBuilder List = new FuseBuilder();
for (int i = 0; i < itemIDs.Length; i++)
{
if (itemIDs[i] == 0)
continue;
DataRow dRow;
using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
{
dRow = dbClient.ReadDataRow("SELECT tid,var FROM furniture WHERE id = '" + itemIDs[i] + "'");
}
int templateID = Convert.ToInt32(dRow["tid"]);
itemTemplate Template = getItem(templateID);
List.AppendString("SI", 30);
List.AppendString(itemIDs[i], 30);
List.AppendString(i, 30);
if (Template.TypeID > 0) // Flooritem
{
List.AppendString("S", 30);
List.AppendString(itemIDs[i], 30);
List.AppendString(Template.Sprite, 30);
List.AppendString(Template.Length, 30);
List.AppendString(Template.Width, 30);
List.AppendString(dRow["var"], 30);
List.AppendString(Template.Colour, 30);
List.AppendString(i, 30);
List.Append("/");
}
else
{
string Sprite = Template.Sprite;
string Colour = Template.Colour;
if (Template.posterString != null)
{
Sprite = Template.posterString[0];
Colour = Template.posterString[1];
}
List.AppendString("I", 30);
List.AppendString(itemIDs[i], 30);
List.AppendString(Sprite, 30);
if (Sprite == "post.it" || Sprite == "post.it.vd")
List.AppendString(dRow["var"], 30);
else
List.AppendString(Colour, 30);
List.AppendString(i, 30);
List.Append("/");
}
}
return List.ToString();
}
///
/// Checks if the wallposition for a wallitem is correct, if it is, then the output should be exactly the same as the input. If not, then the wallposition is invalid.
///
/// The original wallposition. [input]
public static string wallPositionOK(string wallPosition)
{
//:w=3,2 l=9,63 l
try
{
string[] posD = wallPosition.Split(' ');
if (posD[2] != "l" && posD[2] != "r")
return "";
string[] widD = posD[0].Substring(3).Split(',');
int widthX = int.Parse(widD[0]);
int widthY = int.Parse(widD[1]);
if (widthX < 0 || widthY < 0 || widthX > 200 || widthY > 200)
return "";
string[] lenD = posD[1].Substring(2).Split(',');
int lengthX = int.Parse(lenD[0]);
int lengthY = int.Parse(lenD[1]);
if (lengthX < 0 || lengthY < 0 || lengthX > 200 || lengthY > 200)
return "";
return ":w=" + widthX + "," + widthY + " " + "l=" + lengthX + "," + lengthY + " " + posD[2];
}
catch
{
return "";
}
}
///
/// Returns the last purchased/created item in the 'furniture' table of the HoloDB.
///
public static int lastItemID
{
get
{
using (DatabaseClient dbClient = Eucalypt.dbManager.GetClient())
{
return dbClient.ReadInt32("SELECT MAX(id) FROM furniture");
}
}
}
}
}