using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GameServer.Enums;
using ServerEngine;
using ServerEngine.DataManagement.DataWrappers;
using ServerEngine.GuildWars.DataBase;
using ServerEngine.GuildWars.DataWrappers.Clients;
using ServerEngine.GuildWars.Tools;
using ServerEngine.PacketManagement.StaticConvert;
using ServerEngine.NetworkManagement;
using GameServer.Packets.ToClient;
namespace GameServer.ServerData.Items
{
public class Item
{
private object objLock = new object();
private ItemData data;
///
/// Creates a new instance of the class.
///
public Item()
{
lock (objLock)
data = new ItemData();
}
///
/// This property holds all the data of the item.
/// This also provides thread-safe access.
///
public ItemData Data
{
get { lock (objLock) return data; }
set { lock (objLock) data = value; }
}
///
/// Sends the general packet for the item
///
public void SendGeneral(NetID netID)
{
var itemPacket = new NetworkMessage(netID)
{
PacketTemplate = new P343_ItemGeneral.PacketSt343
{
LocalID = (uint)Data.ItemLocalID,
FileID = Data.GameItemFileID,
ItemType = (byte)Data.Type,
Data2 = 32,
DyeColor = (ushort)Data.DyeColor,
Data4 = 0,
CanBeDyed = 1,
Flags = (uint)Data.Flags,
MerchantPrice = Data.MerchantPrice,
ItemID = (uint)Data.GameItemID,
Quantity = (uint)Data.Quantity,
NameHash = Data.Name.ToGW(),
NumStats = (byte)Data.Stats.Count,
Stats = Data.Stats.Select(x => x.Compile()).ToArray()
}
};
QueuingService.PostProcessingQueue.Enqueue(itemPacket);
}
///
/// Sends the owner packet for the item
///
public void SendOwner(NetID netID)
{
// if its customized...
if (Data.CreatorName != null) //failcheck
{
if (Data.CreatorName.Length > 0)
{
var itemOwner = new NetworkMessage(netID)
{
PacketTemplate = new P304_ItemOwnerName.PacketSt304
{
ItemLocalID = (uint)Data.ItemLocalID,
CharName = Data.CreatorName
}
};
QueuingService.PostProcessingQueue.Enqueue(itemOwner);
}
}
}
///
/// Sends the profession packet for the item
///
public void SendProfession(NetID netID)
{
// some items have unnecessary restrictments...
if (Data.Profession > 0)
{
var itemProfession = new NetworkMessage(netID)
{
PacketTemplate = new P336_ItemProfession.PacketSt336
{
ItemLocalID = (uint)Data.ItemLocalID,
Profession = Data.Profession
}
};
QueuingService.PostProcessingQueue.Enqueue(itemProfession);
}
}
///
/// Sends the location/page packet for the item
///
public void SendLocation(NetID netID)
{
// if the item is owned by someone send its inventory location
if (Data.OwnerAccID.Value > 0)
{
if (Data.Storage == ItemStorage.Equiped && Data.Type == ItemType.Bag) // special handling for bags
{
var itemPage = new NetworkMessage(netID)
{
PacketTemplate = new P309_ItemPagePacket.PacketSt309
{
ItemStreamID = 1,
StorageType = 1,
StorageID = (byte)(Data.Slot - (int)AgentEquipment.Backpack),
PageID = (ushort)(Data.Slot - (int)AgentEquipment.Backpack),
Slots = (byte)GetBagSize(),
ItemLocalID = (uint)Data.ItemLocalID
}
};
QueuingService.PostProcessingQueue.Enqueue(itemPage);
}
else
{
var itemLocation = new NetworkMessage(netID)
{
PacketTemplate = new P308_ItemLocation.PacketSt308
{
ItemStreamID = 1,
ItemLocalID = (uint)Data.ItemLocalID,
PageID = (ushort)Data.Storage,
UserSlot = (byte)Data.Slot
}
};
QueuingService.PostProcessingQueue.Enqueue(itemLocation);
}
}
}
///
/// Sends the spawn packet for the item
///
public void SendSpawn(NetID netID)
{
var spawnPacket = new NetworkMessage(netID)
{
PacketTemplate = new P021_SpawnObject.PacketSt21
{
AgentID = (uint)Data.ItemAgentID,
Data1 = (uint)Data.ItemLocalID,
Data2 = 4,
Data3 = 0,
PosX = Data.Position.X,
PosY = Data.Position.Y,
Plane = (ushort)Data.Position.PlaneZ,
Data4 = 1,
Rotation = 0,
Data5 = 1,
Speed = 0,
Data12 = 1,
Data13 = 0x34000000
}
};
QueuingService.PostProcessingQueue.Enqueue(spawnPacket);
}
///
/// Sends the move packet for the item
///
public void SendMove(NetID netID)
{
var movePacket = new NetworkMessage(netID)
{
PacketTemplate = new P321_MoveItem.PacketSt321
{
ItemStreamID = 1,
ItemLocalID = (uint)Data.ItemLocalID,
NewPageID = (ushort)Data.Storage,
NewSlot = (byte)Data.Slot
}
};
QueuingService.PostProcessingQueue.Enqueue(movePacket);
}
///
/// This generates the item packets and automatically sends them
/// to the specified client
///
public void SendPackets(NetID netID)
{
// send all packets from this item to 'netID'
SendGeneral(netID);
SendOwner(netID);
SendProfession(netID);
SendLocation(netID);
}
///
/// Returns the size of this bag (if this item is no bag, this will return 0)
///
public int GetBagSize()
{
// all bags have the 'Slots' stat as the first stat, so get the frist one
var firstStat = data.Stats.First();
// and check if it is Slots
return firstStat.Stat == ItemStatEnums.Slots ? firstStat.Value2 : 0;
}
///
/// Generates a new item object, containing the basic item data copied from the MasterData table
/// Note that this item has now owner (accID, charID)! Do NOT save this to database as is!
///
public static Item CreateItemStubFromDB(int dbItemID, int itemLocalID)
{
// get the database stuff
using (var db = (MySQL)DataBaseProvider.GetDataBase())
{
// get the master data
var masterDatas = from im in db.itemsMasterData
where im.itemID == dbItemID
select im;
// failcheck
if (masterDatas.Count() == 0) return null;
var masterData = masterDatas.First();
// create the item
var tmpItem = new Item
{
Data = new ItemData
{
ItemLocalID = itemLocalID,
ItemID = dbItemID,
GameItemID = masterData.gameItemID,
GameItemFileID = (uint)masterData.gameItemFileID,
Name = masterData.name,
Type = (ItemType)Enum.ToObject(typeof(ItemType), masterData.itemType),
}
};
return tmpItem;
}
}
///
/// Fill an item with values from the database, and set a new itemLocalID
/// Note that CharID will be set to 0 if it is 0 in the db!
///
public static Item LoadFromDB(itemsPerSonALData personalDataBaseItem, int itemLocalID)
{
// get the database stuff
using (var db = (MySQL)DataBaseProvider.GetDataBase())
{
// make things easier for dblinq ;D
var tmpID = personalDataBaseItem.itemID;
// get the master data
var masterDatas = from im in db.itemsMasterData
where im.itemID == tmpID
select im;
// failcheck
if (masterDatas.Count() == 0) return null;
var masterData = masterDatas.First();
// create the item
var tmpItem = new Item
{
Data = new ItemData
{
ItemLocalID = itemLocalID,
ItemID = personalDataBaseItem.itemID,
GameItemID = masterData.gameItemID,
GameItemFileID = (uint)masterData.gameItemFileID,
PersonalItemID = personalDataBaseItem.personalItemID,
OwnerAccID = new AccID((uint)personalDataBaseItem.accountID),
OwnerCharID = new CharID((uint)personalDataBaseItem.charID),
Name = masterData.name,
Type = (ItemType)Enum.ToObject(typeof(ItemType), masterData.itemType),
DyeColor = personalDataBaseItem.dyeColor,
Flags = personalDataBaseItem.flags,
Quantity = personalDataBaseItem.quantity,
Storage = (ItemStorage)Enum.ToObject(typeof(ItemStorage), personalDataBaseItem.storage),
Slot = personalDataBaseItem.slot,
CreatorCharID = personalDataBaseItem.creatorCharID ?? 0,
CreatorName = personalDataBaseItem.creatorName,
Profession = (byte)masterData.profession
}
};
// add the stats
var ms = new MemoryStream(personalDataBaseItem.stats);
for (var i = 0; i < (personalDataBaseItem.stats.Length/4); i++)
{
// read data
var buf = new byte[4];
RawConverter.ReadByteAr(ref buf, ms, 4);
// create & add the stat
tmpItem.Data.Stats.Add(new ItemStat(buf));
}
// finally, return the generated item
return tmpItem;
}
}
///
/// Update / Add this item to the db.
/// Returns the PersonalItemID of this item
///
public void SaveToDB()
{
// get the database stuff
using (var db = (MySQL)DataBaseProvider.GetDataBase())
{
// get the db item
var items = from im in db.itemsPerSonALData
where im.personalItemID == Data.PersonalItemID
select im;
var existsAlready = false;
var item = new itemsPerSonALData();
// check if it exists
if (items.Count() != 0)
{
existsAlready = true;
item = items.First();
}
// determine the char id (0 if the item lies in storage)
var charID = ((int)Data.Storage >= 5 && (int)Data.Storage <= 14) ? 0 : Data.OwnerCharID.Value;
// update the item
item.personalItemID = Data.PersonalItemID;
item.itemID = Data.ItemID;
item.accountID = (int)Data.OwnerAccID.Value;
item.charID = (int)charID;
item.dyeColor = Data.DyeColor;
item.flags = Data.Flags;
item.quantity = Data.Quantity;
item.storage = (int)Data.Storage;
item.slot = Data.Slot;
item.creatorCharID = Data.CreatorCharID;
item.creatorName = Data.CreatorName;
// change the stats
var tmpList = new List();
foreach (var stat in Data.Stats.Select(x => x.Compile()))
{
tmpList.AddRange(BitConverter.GetBytes(stat));
}
item.stats = tmpList.ToArray();
// finally, change the database
if (!existsAlready) db.itemsPerSonALData.InsertOnSubmit(item);
// submit changes, inserting the item (if necessary) or updating the old one
db.SubmitChanges();
Data.PersonalItemID = item.personalItemID;
}
}
///
/// Try to delete the item from the db.
///
///
public bool DeleteFromDB()
{
// get the database stuff
using (var db = (MySQL)DataBaseProvider.GetDataBase())
{
var dbItems = from i in db.itemsPerSonALData
where i.personalItemID == Data.PersonalItemID
select i;
// check if we found any items
if (dbItems.Count() == 0) return false;
var dbItem = dbItems.First();
// delete the item:
db.itemsPerSonALData.DeleteOnSubmit(dbItem);
db.SubmitChanges();
return true;
}
}
public Item Clone()
{
Item clone = new Item();
clone.Data = data.Clone();
clone.Data.PersonalItemID = 0;
return clone;
}
}
public class ItemData
{
///
/// Creates a new instance of the class.
///
public ItemData()
{
OwnerAccID = new AccID(0);
OwnerCharID = new CharID(0);
Stats = new List();
}
///
/// The so called 'Item-Glue', is a generated local ID for the loaded Items
///
public int ItemLocalID { get; set; }
///
/// The AgentID of an item if it is on the ground.
///
public int ItemAgentID { get; set; }
///
/// This ID is only used in the database 'items_masterdata'
///
public int ItemID { get; set; }
///
/// The price the merchant offers for the item
///
public uint MerchantPrice { get; set; }
///
/// The gw-internal ItemID
///
public int GameItemID { get; set; }
///
/// The file id (also called file-hash within the gw.dat-explorers)
/// of the item-3d-model file
///
public uint GameItemFileID { get; set; }
///
/// This id is only used within the database 'items_personaldata' and
/// 'items_personalstats'
///
public long PersonalItemID { get; set; }
///
/// The account id of the owner
///
public AccID OwnerAccID { get; set; }
///
/// The character id of the owner (THIS MAY BE '0'!)
///
public CharID OwnerCharID { get; set; }
///
/// The name of the item, without 'mods'
///
public string Name { get; set; }
///
/// The type of the item, see the enum
///
public ItemType Type { get; set; }
///
/// The profession of the item to make it limited to one profession
///
public byte Profession { get; set; }
///
/// The color of the item (if it is dyed)
///
public int DyeColor { get; set; }
///
/// These flags have effects on the item's visual stuff
///
public int Flags { get; set; }
///
/// Determines the quantity of the item-stack (1 = only one item :P)
///
public int Quantity { get; set; }
///
/// Determines the storage of the item.
///
public ItemStorage Storage { get; set; }
///
/// Determines the slot of the storage the item lies in.
///
public int Slot { get; set; }
///
/// Determines the char id of the char the item is bound to.
/// Note that this char can also be already deleted.
///
public int CreatorCharID { get; set; }
///
/// The name of the char the item is bound to.
///
public string CreatorName { get; set; }
///
/// This property holds all item stats.
///
public List Stats { get; set; }
///
/// This property holds the items coords if on the ground.
///
public GWVector Position { get; set; }
public ItemData Clone()
{
return (ItemData)this.MemberwiseClone();
}
public void SetFlag(ItemFlagEnums flag, bool value)
{
if (value)
{
Flags |= (int)flag;
}
else
{
Flags &= ~(int)flag;
}
}
public bool GetFlag(ItemFlagEnums flag)
{
return (Flags & (int)flag) == (int)flag;
}
}
}