Skip to content

Commit

Permalink
Merge pull request #45 from baughj/SERVER-159
Browse files Browse the repository at this point in the history
[SERVER-159] Display item quantities in exchange window
  • Loading branch information
baughj committed Jun 5, 2016
2 parents af41c21 + 98209b7 commit df22f84
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 54 deletions.
13 changes: 12 additions & 1 deletion hybrasyl/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public enum LegendColor
Red = 248
}

internal static class ExchangeActions
{
public const byte Initiate = 0x00;
public const byte QuantityPrompt = 0x01;
public const byte ItemUpdate = 0x02;
public const byte GoldUpdate = 0x03;
public const byte Cancel = 0x04;
public const byte Confirm = 0x05;
}

//this is a wip
internal static class OpCodes
{
Expand Down Expand Up @@ -96,7 +106,8 @@ internal static class OpCodes
public const byte MapData = 0x3C;
public const byte UseSkill = 0x3E;
public const byte Cooldown = 0x3F;
public const byte ClickObject = 0x43;
public const byte Exchange = 0x42;
public const byte ClickObject = 0x43;
public const byte CancelCast = 0x48;
public const byte ServerSelect = 0x57;
public const byte MapLoadComplete = 0x58;
Expand Down
14 changes: 7 additions & 7 deletions hybrasyl/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public bool AddItem(User giver, byte slot, byte quantity = 1)

if (quantity > theItem.Count)
{
giver.SendSystemMessage(String.Format("You don't have that many {0} to give!", theItem.Name));
giver.SendSystemMessage($"You don't have that many {theItem.Name} to give!");
return false;
}

Expand All @@ -163,13 +163,13 @@ public bool AddItem(User giver, byte slot, byte quantity = 1)
{
if (giver == _target)
{
_target.SendSystemMessage(String.Format("They can't carry any more {0}", theItem.Name));
_source.SendSystemMessage(String.Format("You can't carry any more {0}.", theItem.Name));
_target.SendSystemMessage($"They can't carry any more {theItem.Name}");
_source.SendSystemMessage($"You can't carry any more {theItem.Name}.");
}
else
{
_source.SendSystemMessage(String.Format("They can't carry any more {0}", theItem.Name));
_target.SendSystemMessage(String.Format("You can't carry any more {0}.", theItem.Name));
_source.SendSystemMessage($"They can't carry any more {theItem.Name}");
_target.SendSystemMessage($"You can't carry any more {theItem.Name}.");
}
return false;
}
Expand Down Expand Up @@ -356,7 +356,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
var output = new object[inventory.Size];
for (byte i = 0; i < inventory.Size; i++)
{
var itemInfo = new Dictionary<String, object>();
var itemInfo = new Dictionary<string, object>();
if (inventory[i] != null)
{
itemInfo["Name"] = inventory[i].Name;
Expand Down Expand Up @@ -580,7 +580,7 @@ private void _RemoveFromIndex(Item item)
}
}

public bool TryGetValue(String name, out Item item)
public bool TryGetValue(string name, out Item item)
{
item = null;
List<Item> itemList;
Expand Down
85 changes: 39 additions & 46 deletions hybrasyl/Objects/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2161,14 +2161,13 @@ public void SetEncryptionParameters(byte[] key, byte seed, string name)
/// <param name="requestor">The user requesting the trade</param>
public void SendExchangeInitiation(User requestor)
{
if (Status.HasFlag(PlayerStatus.InExchange) && requestor.Status.HasFlag(PlayerStatus.InExchange))
if (!Status.HasFlag(PlayerStatus.InExchange) || !requestor.Status.HasFlag(PlayerStatus.InExchange)) return;
Enqueue(new ServerPacketStructures.Exchange
{
var x42 = new ServerPacket(0x42);
x42.WriteByte(0); // show exchange window
x42.WriteUInt32(requestor.Id);
x42.WriteString8(requestor.Name);
Enqueue(x42);
}
Action = ExchangeActions.Initiate,
RequestorId = requestor.Id,
RequestorName = requestor.Name
}.Packet());
}

/// <summary>
Expand All @@ -2177,14 +2176,13 @@ public void SendExchangeInitiation(User requestor)
/// <param name="itemSlot">The item slot containing a stacked item that will be split (client side)</param>
public void SendExchangeQuantityPrompt(byte itemSlot)
{
if (Status.HasFlag(PlayerStatus.InExchange))
{
var x42 = new ServerPacket(0x42);
x42.WriteByte(1); // show quantity prompt
x42.WriteByte(itemSlot); // Slot for which we need quantity info
Enqueue(x42);
}

if (!Status.HasFlag(PlayerStatus.InExchange)) return;
Enqueue(
new ServerPacketStructures.Exchange
{
Action = ExchangeActions.QuantityPrompt,
ItemSlot = itemSlot
}.Packet());
}
/// <summary>
/// Send an exchange update packet for an item to an active exchange participant.
Expand All @@ -2194,17 +2192,17 @@ public void SendExchangeQuantityPrompt(byte itemSlot)
/// <param name="source">Boolean indicating which "side" of the transaction will be updated (source / "left side" == true)</param>
public void SendExchangeUpdate(Item toAdd, byte slot, bool source = true)
{
if (Status.HasFlag(PlayerStatus.InExchange))
if (!Status.HasFlag(PlayerStatus.InExchange)) return;
var update = new ServerPacketStructures.Exchange
{
var x42 = new ServerPacket(0x42); // Update exchange packet
x42.WriteByte(2); // Show item in exchange window
x42.WriteByte((byte)(source ? 0 : 1)); // Update "my" side of the transaction
x42.WriteByte(slot); // Which "exchange slot" to update
x42.WriteUInt16((ushort)(0x8000 + toAdd.Sprite));
x42.WriteByte(toAdd.Color);
x42.WriteString8(toAdd.Name);
Enqueue(x42);
}
Action = ExchangeActions.ItemUpdate,
Side = source,
ItemSlot = slot,
ItemSprite = toAdd.Sprite,
ItemColor = toAdd.Color,
ItemName = toAdd.Stackable && toAdd.Count > 1 ? $"{toAdd.Name} ({toAdd.Count}" : toAdd.Name
};
Enqueue(update.Packet());
}

/// <summary>
Expand All @@ -2214,14 +2212,13 @@ public void SendExchangeUpdate(Item toAdd, byte slot, bool source = true)
/// <param name="source">Boolean indicating which "side" of the transaction will be updated (source / "left side" == true)</param>
public void SendExchangeUpdate(uint gold, bool source = true)
{
if (Status.HasFlag(PlayerStatus.InExchange))
if (!Status.HasFlag(PlayerStatus.InExchange)) return;
Enqueue(new ServerPacketStructures.Exchange
{
var x42 = new ServerPacket(0x42); // Update exchange packet
x42.WriteByte(3); // Update gold in exchange window
x42.WriteByte((byte)(source ? 0 : 1)); // Update "my" side of the transaction
x42.WriteUInt32(gold); // Which "exchange slot" to update
Enqueue(x42);
}
Action=ExchangeActions.GoldUpdate,
Side =source,
Gold =gold
}.Packet());
}

/// <summary>
Expand All @@ -2230,14 +2227,12 @@ public void SendExchangeUpdate(uint gold, bool source = true)
/// <param name="source">The "side" responsible for cancellation (source / "left side" == true)</param>
public void SendExchangeCancellation(bool source = true)
{
if (Status.HasFlag(PlayerStatus.InExchange))
if (!Status.HasFlag(PlayerStatus.InExchange)) return;
Enqueue(new ServerPacketStructures.Exchange
{
var x42 = new ServerPacket(0x42); // Update exchange packet
x42.WriteByte(4); // Exchange cancelled
x42.WriteByte((byte)(source ? 0 : 1)); // Which "side" cancelled the transaction
x42.WriteString8("Exchange was cancelled.");
Enqueue(x42);
}
Action = ExchangeActions.Cancel,
Side =source
}.Packet());
}

/// <summary>
Expand All @@ -2247,14 +2242,12 @@ public void SendExchangeCancellation(bool source = true)

public void SendExchangeConfirmation(bool source = true)
{
if (Status.HasFlag(PlayerStatus.InExchange))
if (!Status.HasFlag(PlayerStatus.InExchange)) return;
Enqueue(new ServerPacketStructures.Exchange
{
var x42 = new ServerPacket(0x42); // Update exchange packet
x42.WriteByte(5); // Exchange confirmed
x42.WriteByte((byte)(source ? 0 : 1)); // Which "side" confirmed the transaction
x42.WriteString8("You exchanged.");
Enqueue(x42);
}
Action = ExchangeActions.Confirm,
Side =source
}.Packet());
}

public void SendInventory()
Expand Down
67 changes: 67 additions & 0 deletions hybrasyl/ServerPacketStructures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,73 @@ internal ServerPacket Packet()

}

internal partial class Exchange
{
private static byte OpCode =
OpCodes.Exchange;

internal byte Action;
internal uint Gold;
internal bool Side;
internal string RequestorName;
internal uint RequestorId;
internal byte ItemSlot;
internal ushort ItemSprite;
internal byte ItemColor;
internal string ItemName;
internal const string CancelMessage = "Exchange was cancelled.";
internal const string ConfirmMessage = "You exchanged.";

internal ServerPacket Packet()
{
ServerPacket packet = new ServerPacket(OpCode);
packet.WriteByte(Action);
switch (Action)
{
case ExchangeActions.Initiate:
{
packet.WriteUInt32(RequestorId);
packet.WriteString8(RequestorName);
}
break;
case ExchangeActions.QuantityPrompt:
{
packet.WriteByte(ItemSlot);
}
break;
case ExchangeActions.ItemUpdate:
{
packet.WriteByte((byte) (Side ? 0 : 1));
packet.WriteByte(ItemSlot);
packet.WriteUInt16((ushort) (0x8000 + ItemSprite));
packet.WriteByte(ItemColor);
packet.WriteString8(ItemName);
}
break;
case ExchangeActions.GoldUpdate:
{
packet.WriteByte((byte) (Side ? 0 : 1));
packet.WriteUInt32(Gold);
}
break;
case ExchangeActions.Cancel:
{
packet.WriteByte((byte) (Side ? 0 : 1));
packet.WriteString8(CancelMessage);

}
break;
case ExchangeActions.Confirm:
{
packet.WriteByte((byte) (Side ? 0 : 1));
packet.WriteString8(ConfirmMessage);
}
break;
}
return packet;
}
}

internal partial class PlaySound
{
private byte OpCode;
Expand Down

0 comments on commit df22f84

Please sign in to comment.