Add inventory support

This commit is contained in:
SilicaAndPina 2020-10-22 19:46:17 +13:00
parent 74aeb84494
commit 0d64c3e22c
13 changed files with 1702 additions and 1568 deletions

File diff suppressed because it is too large Load diff

View file

@ -49,7 +49,6 @@ namespace Horse_Isle_Server
} }
private void updateTimerTick(object state) private void updateTimerTick(object state)
{ {
Server.UpdateArea(this);
Server.UpdateWorld(this); Server.UpdateWorld(this);
Server.UpdatePlayer(this); Server.UpdatePlayer(this);
} }

View file

@ -329,7 +329,7 @@ namespace Horse_Isle_Server
droppedItem.X = reader.GetInt32(0); droppedItem.X = reader.GetInt32(0);
droppedItem.Y = reader.GetInt32(1); droppedItem.Y = reader.GetInt32(1);
droppedItem.DespawnTimer = reader.GetInt32(4); droppedItem.DespawnTimer = reader.GetInt32(4);
ItemInstance instance = new ItemInstance(reader.GetInt32(3),reader.GetInt32(4)); ItemInstance instance = new ItemInstance(reader.GetInt32(3),reader.GetInt32(2));
droppedItem.instance = instance; droppedItem.instance = instance;
itemList.Add(droppedItem); itemList.Add(droppedItem);
} }
@ -338,35 +338,28 @@ namespace Horse_Isle_Server
} }
return itemList.ToArray(); return itemList.ToArray();
} }
public static void AddDroppedItems(DroppedItems.DroppedItem[] items)
public static void AddDroppedItem(DroppedItems.DroppedItem item)
{ {
using (MySqlConnection db = new MySqlConnection(ConnectionString)) using (MySqlConnection db = new MySqlConnection(ConnectionString))
{ {
db.Open(); db.Open();
MySqlTransaction transaction = db.BeginTransaction();
foreach (DroppedItems.DroppedItem item in items) MySqlCommand sqlCommand = db.CreateCommand();
{ sqlCommand.CommandText = "INSERT INTO DroppedItems VALUES(@x, @y, @randomId, @itemId, @despawnTimer)";
sqlCommand.Parameters.AddWithValue("@x", item.X);
sqlCommand.Parameters.AddWithValue("@y", item.Y);
sqlCommand.Parameters.AddWithValue("@randomId", item.instance.RandomID);
sqlCommand.Parameters.AddWithValue("@itemId", item.instance.ItemID);
sqlCommand.Parameters.AddWithValue("@despawnTimer", item.DespawnTimer);
sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
MySqlCommand sqlCommand = db.CreateCommand();
sqlCommand.Transaction = transaction;
sqlCommand.CommandText = "INSERT INTO DroppedItems VALUES(@x, @y, @randomId, @itemId, @despawnTimer)";
sqlCommand.Parameters.AddWithValue("@x", item.X);
sqlCommand.Parameters.AddWithValue("@y", item.Y);
sqlCommand.Parameters.AddWithValue("@randomId", item.instance.RandomID);
sqlCommand.Parameters.AddWithValue("@itemId", item.instance.ItemID);
sqlCommand.Parameters.AddWithValue("@despawnTimer", item.DespawnTimer);
sqlCommand.Prepare();
sqlCommand.ExecuteNonQuery();
sqlCommand.Dispose();
}
transaction.Commit();
} }
} }
public static void AddMail(int toId, string fromName, string subject, string message) public static void AddMail(int toId, string fromName, string subject, string message)
{ {
using (MySqlConnection db = new MySqlConnection(ConnectionString)) using (MySqlConnection db = new MySqlConnection(ConnectionString))

View file

@ -69,7 +69,7 @@ namespace Horse_Isle_Server
if(droppedItemsList[i].instance.RandomID == randomId) if(droppedItemsList[i].instance.RandomID == randomId)
{ {
droppedItemsList.RemoveAt(i); droppedItemsList.RemoveAt(i);
return;
} }
} }
@ -164,6 +164,7 @@ namespace Horse_Isle_Server
droppedItem.DespawnTimer = despawnTimer; droppedItem.DespawnTimer = despawnTimer;
droppedItem.instance = instance; droppedItem.instance = instance;
droppedItemsList.Add(droppedItem); droppedItemsList.Add(droppedItem);
Database.AddDroppedItem(droppedItem);
Logger.DebugPrint("Created Item ID: " + instance.ItemID + " in " + isle.Name + " at: X: " + droppedItem.X + " Y: " + droppedItem.Y); Logger.DebugPrint("Created Item ID: " + instance.ItemID + " in " + isle.Name + " at: X: " + droppedItem.X + " Y: " + droppedItem.Y);
newItems++; newItems++;
break; break;
@ -187,10 +188,6 @@ namespace Horse_Isle_Server
} }
} }
if(newItems > 0)
{
Database.AddDroppedItems(droppedItemsList.ToArray());
}
} }
public static void Init() public static void Init()

View file

@ -206,6 +206,11 @@ namespace Horse_Isle_Server
Messages.LoginMessageForamt = gameData.messages.login_format; Messages.LoginMessageForamt = gameData.messages.login_format;
Messages.LogoutMessageFormat = gameData.messages.logout_format; Messages.LogoutMessageFormat = gameData.messages.logout_format;
// Transport
Messages.CantAffordTransport = gameData.messages.transport.not_enough_money;
Messages.WelcomeToAreaFormat = gameData.messages.transport.welcome_to_format;
// Chat // Chat
Messages.ChatViolationMessageFormat = gameData.messages.chat.violation_format; Messages.ChatViolationMessageFormat = gameData.messages.chat.violation_format;
@ -246,7 +251,6 @@ namespace Horse_Isle_Server
Messages.Seperator = gameData.messages.meta.seperator; Messages.Seperator = gameData.messages.meta.seperator;
Messages.TileFormat = gameData.messages.meta.tile_format; Messages.TileFormat = gameData.messages.meta.tile_format;
Messages.TransportFormat = gameData.messages.meta.transport_format; Messages.TransportFormat = gameData.messages.meta.transport_format;
Messages.InventoryFormat = gameData.messages.meta.inventory_format;
Messages.ExitThisPlace = gameData.messages.meta.exit_this_place; Messages.ExitThisPlace = gameData.messages.meta.exit_this_place;
Messages.BackToMap = gameData.messages.meta.back_to_map; Messages.BackToMap = gameData.messages.meta.back_to_map;
Messages.LongFullLine = gameData.messages.meta.long_full_line; Messages.LongFullLine = gameData.messages.meta.long_full_line;
@ -255,6 +259,7 @@ namespace Horse_Isle_Server
Messages.NothingMessage = gameData.messages.meta.dropped_items.nothing_message; Messages.NothingMessage = gameData.messages.meta.dropped_items.nothing_message;
Messages.ItemsOnGroundMessage = gameData.messages.meta.dropped_items.items_message; Messages.ItemsOnGroundMessage = gameData.messages.meta.dropped_items.items_message;
Messages.GrabItemFormat = gameData.messages.meta.dropped_items.item_format; Messages.GrabItemFormat = gameData.messages.meta.dropped_items.item_format;
Messages.GrabAllItemsButton = gameData.messages.meta.dropped_items.grab_all;
Messages.GrabbedItemMessage = gameData.messages.grab_message; Messages.GrabbedItemMessage = gameData.messages.grab_message;
Messages.GrabAllItemsMessage = gameData.messages.grab_all_message; Messages.GrabAllItemsMessage = gameData.messages.grab_all_message;
@ -264,6 +269,14 @@ namespace Horse_Isle_Server
Messages.South = gameData.messages.meta.nearby.south; Messages.South = gameData.messages.meta.nearby.south;
Messages.West = gameData.messages.meta.nearby.west; Messages.West = gameData.messages.meta.nearby.west;
// Inventory
Messages.InventoryHeaderFormat = gameData.messages.meta.inventory.header_format;
Messages.InventoryItemFormat = gameData.messages.meta.inventory.item_entry;
Messages.ItemInformationButton = gameData.messages.meta.inventory.item_info_button;
Messages.ItemDropItemButton = gameData.messages.meta.inventory.item_drop_button;
// Map Data // Map Data
Map.OverlayTileDepth = gameData.tile_paramaters.overlay_tiles.tile_depth.ToObject<int[]>(); Map.OverlayTileDepth = gameData.tile_paramaters.overlay_tiles.tile_depth.ToObject<int[]>();
@ -294,6 +307,7 @@ namespace Horse_Isle_Server
// Inventory // Inventory
Messages.DefaultInventoryMax = gameData.item.max_carryable; Messages.DefaultInventoryMax = gameData.item.max_carryable;
// Swf // Swf
Messages.WagonCutscene = gameData.transport.wagon_cutscene; Messages.WagonCutscene = gameData.transport.wagon_cutscene;

View file

@ -6,6 +6,17 @@ using System.Threading.Tasks;
namespace Horse_Isle_Server namespace Horse_Isle_Server
{ {
class InventoryItem
{
public InventoryItem()
{
ItemInstances = new List<ItemInstance>();
}
public int ItemId;
public List<ItemInstance> ItemInstances;
}
interface IInventory interface IInventory
{ {
@ -17,7 +28,7 @@ namespace Horse_Isle_Server
get; get;
} }
ItemInstance[] GetItemList(); InventoryItem[] GetItemList();
} }

View file

@ -48,15 +48,28 @@ namespace Horse_Isle_Server
public static string PasswordNotice; public static string PasswordNotice;
public static string CapsNotice; public static string CapsNotice;
// Transport
public static string CantAffordTransport;
public static string WelcomeToAreaFormat;
//Dropped Items //Dropped Items
public static string NothingMessage; public static string NothingMessage;
public static string ItemsOnGroundMessage; public static string ItemsOnGroundMessage;
public static string GrabItemFormat; public static string GrabItemFormat;
public static string GrabAllItemsButton;
public static string GrabAllItemsMessage; public static string GrabAllItemsMessage;
public static string GrabbedItemMessage; public static string GrabbedItemMessage;
public static string GrabbedAllObjectsMessage; public static string GrabbedAllObjectsMessage;
// Inventory
public static string InventoryItemFormat;
public static string InventoryHeaderFormat;
public static string ItemDropItemButton;
public static string ItemInformationButton;
// Meta // Meta
public static string IsleFormat; public static string IsleFormat;
public static string TownFormat; public static string TownFormat;
@ -72,8 +85,7 @@ namespace Horse_Isle_Server
public static string TileFormat; public static string TileFormat;
public static string Seperator; public static string Seperator;
public static string InventoryFormat;
public static string ExitThisPlace; public static string ExitThisPlace;
public static string BackToMap; public static string BackToMap;
public static string LongFullLine; public static string LongFullLine;
@ -95,9 +107,30 @@ namespace Horse_Isle_Server
public static string FormatPlayerInventoryHeaderMeta(int itemCount, int maxItems) public static string FormatPlayerInventoryHeaderMeta(int itemCount, int maxItems)
{ {
return InventoryFormat.Replace("%ITEMCOUNT%", itemCount.ToString()).Replace("%MAXITEMS%", maxItems.ToString()); return InventoryHeaderFormat.Replace("%ITEMCOUNT%", itemCount.ToString()).Replace("%MAXITEMS%", maxItems.ToString());
} }
public static string FormatPlayerInventoryItemMeta(int iconid, int count, string name)
{
return InventoryItemFormat.Replace("%ICONID%", iconid.ToString()).Replace("%COUNT%", count.ToString()).Replace("%TITLE%", name);
}
public static string FormatItemInformationButton(int randomid)
{
return ItemInformationButton.Replace("%RANDOMID%", randomid.ToString());
}
public static string FormatItemDropItemButton(int randomid)
{
return ItemDropItemButton.Replace("%RANDOMID%", randomid.ToString());
}
// Meta // Meta
public static string FormatTileName(string name)
{
return Messages.TileFormat.Replace("%TILENAME%", name);
}
public static string FormatGrabItemMessage(string name, int randomid, int iconid) public static string FormatGrabItemMessage(string name, int randomid, int iconid)
{ {
return GrabItemFormat.Replace("%ICONID%",iconid.ToString()).Replace("%ITEMNAME%", name).Replace("%RANDOMID%", randomid.ToString()); return GrabItemFormat.Replace("%ICONID%",iconid.ToString()).Replace("%ITEMNAME%", name).Replace("%RANDOMID%", randomid.ToString());
@ -227,6 +260,12 @@ namespace Horse_Isle_Server
return WelcomeFormat.Replace("%USERNAME%", username); return WelcomeFormat.Replace("%USERNAME%", username);
} }
// Transport
public static string FormatWelcomeToAreaMessage(string placeName)
{
return WelcomeToAreaFormat.Replace("%PLACE%", placeName);
}
// Disconnect // Disconnect
public static string FormatIdleKickMessage() public static string FormatIdleKickMessage()
{ {

View file

@ -80,7 +80,7 @@ namespace Horse_Isle_Server
{ {
string message = ""; string message = "";
message += Messages.Seperator + buildNearbyString(x, y); message += buildNearbyString(x, y);
// Dropped Items // Dropped Items
DroppedItems.DroppedItem[] Items = DroppedItems.GetItemsAt(x, y); DroppedItems.DroppedItem[] Items = DroppedItems.GetItemsAt(x, y);
@ -94,9 +94,9 @@ namespace Horse_Isle_Server
Item.ItemInformation itemInfo = item.instance.GetItemInfo(); Item.ItemInformation itemInfo = item.instance.GetItemInfo();
message += Messages.FormatGrabItemMessage(itemInfo.Name, item.instance.RandomID, itemInfo.IconId); message += Messages.FormatGrabItemMessage(itemInfo.Name, item.instance.RandomID, itemInfo.IconId);
} }
message += Messages.GrabAllItemsMessage; if(Items.Length > 1)
message += Messages.GrabAllItemsButton;
} }
Logger.DebugPrint(message);
return message; return message;
} }
public static string BuildTransportInfo(Transport.TransportPoint transportPoint) public static string BuildTransportInfo(Transport.TransportPoint transportPoint)
@ -114,13 +114,15 @@ namespace Horse_Isle_Server
{ {
string message = ""; string message = "";
if (specialTile.Title != null) if (specialTile.Code == null)
message += Messages.TileFormat.Replace("%TILENAME%", specialTile.Title); message += buildLocationString(specialTile.X, specialTile.Y)+Messages.Seperator;
else
message += buildLocationString(specialTile.X, specialTile.Y);
if (specialTile.Description != null)
message += Messages.Seperator + specialTile.Description; if (specialTile.Title != null && specialTile.Title != "")
message += Messages.FormatTileName(specialTile.Title) + Messages.Seperator;
if (specialTile.Description != null && specialTile.Description != "")
message += specialTile.Description;
if (specialTile.Code == null) if (specialTile.Code == null)
message += buildCommonInfo(specialTile.X, specialTile.Y); message += buildCommonInfo(specialTile.X, specialTile.Y);
@ -140,6 +142,27 @@ namespace Horse_Isle_Server
{ {
string message = ""; string message = "";
message += Messages.FormatPlayerInventoryHeaderMeta(inv.Count, Messages.DefaultInventoryMax); message += Messages.FormatPlayerInventoryHeaderMeta(inv.Count, Messages.DefaultInventoryMax);
InventoryItem[] items = inv.GetItemList();
foreach(InventoryItem item in items)
{
Item.ItemInformation itemInfo = Item.GetItemById(item.ItemId);
string title = itemInfo.Name;
if (item.ItemInstances.Count > 1 && itemInfo.PluralName != "")
title = itemInfo.PluralName;
message += Messages.FormatPlayerInventoryItemMeta(itemInfo.IconId, item.ItemInstances.Count, title);
int randomId = item.ItemInstances[0].RandomID;
int sortBy = itemInfo.SortBy
if(sortBy == 2) // all items with sort 2 are throwable-
message += Messages.FormatItemDropItemButton(randomId);
message += Messages.FormatItemInformationButton(randomId);
message += "^R1";
}
Logger.DebugPrint(message);
return message; return message;
} }
public static string BuildMetaInfo(int x, int y) public static string BuildMetaInfo(int x, int y)

View file

@ -9,37 +9,74 @@ namespace Horse_Isle_Server
class PlayerInventory : IInventory class PlayerInventory : IInventory
{ {
private User baseUser; private User baseUser;
private List<ItemInstance> instances = new List<ItemInstance>(); private List<InventoryItem> inventoryItems;
public PlayerInventory(User forUser) public PlayerInventory(User forUser)
{ {
inventoryItems = new List<InventoryItem>();
baseUser = forUser; baseUser = forUser;
instances = Database.GetPlayerInventory(baseUser.Id); ItemInstance[] instances = Database.GetPlayerInventory(baseUser.Id).ToArray();
foreach(ItemInstance instance in instances)
{
Add(instance);
}
} }
public int Count public int Count
{ {
get get
{ {
return instances.Count; return inventoryItems.Count;
} }
} }
public void Add(ItemInstance item) public void Add(ItemInstance item)
{ {
instances.Add(item);
Database.AddItemToInventory(baseUser.Id, item); Database.AddItemToInventory(baseUser.Id, item);
foreach (InventoryItem invetoryItem in inventoryItems)
{
if (invetoryItem.ItemId == item.ItemID)
{
invetoryItem.ItemInstances.Add(item);
return;
}
}
InventoryItem inventoryItem = new InventoryItem();
inventoryItem.ItemId = item.ItemID;
inventoryItem.ItemInstances.Add(item);
inventoryItems.Add(inventoryItem);
} }
public ItemInstance[] GetItemList() public InventoryItem[] GetItemList()
{ {
return instances.ToArray(); return inventoryItems.OrderBy(o => o.ItemInstances[0].GetItemInfo().SortBy).ToArray();
} }
public void Remove(ItemInstance item) public void Remove(ItemInstance item)
{ {
instances.Remove(item);
Database.RemoveItemFromInventory(baseUser.Id, item); Database.RemoveItemFromInventory(baseUser.Id, item);
foreach (InventoryItem inventoryItem in inventoryItems)
{
if(item.ItemID == inventoryItem.ItemId)
{
foreach(ItemInstance instance in inventoryItem.ItemInstances)
{
if(instance.RandomID == item.RandomID)
{
inventoryItem.ItemInstances.Remove(instance);
return;
}
}
}
}
Logger.ErrorPrint("Tried to remove item : " + item.RandomID + " from inventory when it was not in it");
} }
} }
} }

View file

@ -339,7 +339,7 @@ namespace Horse_Isle_Server
if (sender.LoggedinUser.Money >= transportLocation.Cost) if (sender.LoggedinUser.Money >= transportLocation.Cost)
{ {
string swfToLoad = Messages.BoatCutscene; string swfToLoad = Messages.BoatCutscene;
if (transportLocation.Type == "WAGON") if (transportLocation.Type == "WAGON")
@ -353,8 +353,16 @@ namespace Horse_Isle_Server
Teleport(sender, transportLocation.GotoX, transportLocation.GotoY); Teleport(sender, transportLocation.GotoX, transportLocation.GotoY);
byte[] welcomeToIslePacket = PacketBuilder.CreateChat(Messages.FormatWelcomeToAreaMessage(transportLocation.LocationTitle), PacketBuilder.CHAT_BOTTOM_RIGHT);
sender.SendPacket(welcomeToIslePacket);
sender.LoggedinUser.Money -= transportLocation.Cost; sender.LoggedinUser.Money -= transportLocation.Cost;
} }
else
{
byte[] cantAfford = PacketBuilder.CreateChat(Messages.FormatWelcomeToAreaMessage(transportLocation.LocationTitle), PacketBuilder.CHAT_BOTTOM_RIGHT);
sender.SendPacket(cantAfford);
}
} }
catch (KeyNotFoundException) catch (KeyNotFoundException)
{ {

Binary file not shown.

Binary file not shown.

Binary file not shown.