public void Handle(Character player, InteractionCode action, Packet inPacket) { switch (action) { case InteractionCode.Open: if (player.ID != this.OwnerID) { throw new HackException("Opening foreign hired merchant."); } else if (!this.Opened) { this.Opened = true; this.Position = player.Position; player.Map.HiredMerchants.Add(this); } break; case InteractionCode.AddItem: case InteractionCode.PutItem: { Item item = player.Items[inPacket.ReadByte(), inPacket.ReadSByte()]; short bundles = inPacket.ReadShort(); short perBundle = inPacket.ReadShort(); int price = inPacket.ReadInt(); short quantity = (short)(bundles * perBundle); if (perBundle < 0 || perBundle * bundles > 2000 || bundles < 0 || price < 0) { throw new HackException("Illegal quantity of items in hired merchant."); } if (quantity > item.Quantity) { return; } else if (quantity < item.Quantity) { item.Quantity -= quantity; item.Update(); } else if (quantity == item.Quantity) { player.Items.Remove(item, true); } this.Items.Add(new PlayerShopItem(item.MapleID, perBundle, quantity, price)); this.Update(); } break; case InteractionCode.RemoveItem: case InteractionCode.TakeItemBack: if (this.OwnerID == player.ID) { PlayerShopItem item = this.Items[inPacket.ReadShort()]; if (item.Quantity > 0) { player.Items.Add(new Item(item.MapleID, item.Quantity)); } this.Items.Remove(item); this.Update(); } else { throw new HackException("Removing items from a foreign hired merchant."); } break; case InteractionCode.Exit: if (this.Visitors[0] == player || this.Visitors[1] == player || this.Visitors[2] == player) { for (int i = 0; i < 3; i++) { if (this.Visitors[i] == player) { this.Visitors[i] = null; player.HiredMerchant = null; using (Packet outPacket = new Packet(MapleServerOperationCode.PlayerInteraction)) { outPacket.WriteByte(0x0A); if (i > 0) { outPacket.WriteByte((byte)(i + 1)); } this.Broadcast(outPacket); } break; } } } break; case InteractionCode.MaintenanceOff: if (this.OwnerID == player.ID) { this.InMaintenance = false; } else { throw new HackException("Turning off maintenance from a foreign hired merchant."); } break; case InteractionCode.Buy: { PlayerShopItem item = this.Items[inPacket.ReadByte()]; short quantity = inPacket.ReadShort(); if (this.OwnerID == player.ID) { throw new HackException("Buying items from own hired merchant."); } if (quantity > item.Quantity || player.Meso < item.MerchantPrice * quantity) { return; } item.Quantity -= quantity; this.Meso += item.MerchantPrice * quantity; this.Update(); player.Meso -= item.MerchantPrice * quantity; player.Items.Add(new Item(item.MapleID, item.Quantity)); } break; case InteractionCode.Chat: string chat = inPacket.ReadString(); using (Packet outPacket = new Packet(MapleServerOperationCode.PlayerInteraction)) { outPacket.WriteBytes(6, 8); for (int i = 0; i < 3; i++) { if (this.Visitors[i] == player) { outPacket.WriteByte((byte)(i + 1)); break; } } outPacket.WriteString(player.Name + " : " + chat); this.Broadcast(outPacket); } break; case InteractionCode.MerchantClose: if (this.OwnerID == player.ID) { this.Close(player, true); } else { throw new HackException("Closing a foreign hired merchant."); } break; } }
public void Handle(Character player, InteractionCode action, Packet inPacket) { switch (action) { case InteractionCode.Open: { this.Owner.Map.PlayerShops.Add(this); this.Opened = true; } break; case InteractionCode.AddItem: { byte type = inPacket.ReadByte(); sbyte slot = (sbyte)inPacket.ReadShort(); short bundles = inPacket.ReadShort(); short perBundle = inPacket.ReadShort(); int price = inPacket.ReadInt(); short quantity = (short)(bundles * perBundle); Item item = player.Items[type, slot]; if (perBundle < 0 || perBundle * bundles > 2000 || bundles < 0 || price < 0) { throw new HackException("Illegal quantity of items in player shop."); } if (quantity > item.Quantity) { throw new HackException("Trading more items than available."); } if (quantity < item.Quantity) { item.Quantity -= quantity; item.Update(); } else { player.Items.Remove(item, true); } PlayerShopItem shopItem = new PlayerShopItem(item.MapleID, bundles, quantity, price); this.Items.Add(shopItem); this.UpdateItems(); } break; case InteractionCode.RemoveItem: if (this.Owner == player) { PlayerShopItem targetItem = this.Items[inPacket.ReadShort()]; if (targetItem.Quantity > 0) { this.Owner.Items.Add(new Item(targetItem.MapleID, targetItem.Quantity)); } this.Items.Remove(targetItem); this.UpdateItems(); } else { throw new HackException("Removing items from a foreign shop."); } break; case InteractionCode.Exit: if (this.Owner == player) { this.Close(); } else { this.RemoveVisitor(player); } break; case InteractionCode.Buy: { PlayerShopItem purchase = this.Items[inPacket.ReadByte()]; short quantity = inPacket.ReadShort(); if (this.Owner == player) { throw new HackException("Buying items from own shop."); } if (quantity > purchase.Quantity) { throw new HackException("Buying a higher ammount of items than allowed."); } if (player.Meso < purchase.MerchantPrice * quantity) { throw new HackException("Buying items without enough mesos."); } purchase.Quantity -= quantity; player.Meso -= purchase.MerchantPrice * quantity; this.Owner.Meso += purchase.MerchantPrice * quantity; player.Items.Add(new Item(purchase.MapleID, quantity)); this.UpdateItems(); bool noItemLeft = true; foreach (PlayerShopItem shopItem in this.Items) { if (shopItem.Quantity > 0) { noItemLeft = false; break; } } if (noItemLeft) { this.Close(); } } break; case InteractionCode.Chat: { string chat = inPacket.ReadString(); using (Packet outPacket = new Packet(MapleServerOperationCode.PlayerInteraction)) { outPacket.WriteBytes(6, 8); byte sender = 0; for (int i = 0; i < 3; i++) { if (this.Visitors[i] == player) { sender = (byte)(i + 1); } } outPacket.WriteByte(sender); outPacket.WriteString(player.Name + " : " + chat); this.Broadcast(outPacket); } } break; } }