PageRenderTime 2576ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Server2011/GWLP-R/GameServer/Packets/FromClient/P043_ChangeWeaponSet.cs

http://gwlp-revived-open-source.googlecode.com/
C# | 204 lines | 170 code | 28 blank | 6 comment | 9 complexity | f6c07881aa91cf8e68925371a976d04f MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using GameServer.Actions;
  4. using GameServer.Enums;
  5. using GameServer.Packets.ToClient;
  6. using GameServer.ServerData;
  7. using GameServer.ServerData.Items;
  8. using ServerEngine;
  9. using ServerEngine.GuildWars.Tools;
  10. using ServerEngine.NetworkManagement;
  11. using ServerEngine.PacketManagement.CustomAttributes;
  12. using ServerEngine.PacketManagement.Definitions;
  13. namespace GameServer.Packets.FromClient
  14. {
  15. [PacketAttributes(IsIncoming = true, Header = 43)]
  16. public class P043_ChangeWeaponSet : IPacket
  17. {
  18. public class PacketSt43 : IPacketTemplate
  19. {
  20. public UInt16 Header { get { return 43; } }
  21. public byte WeaponSet;
  22. }
  23. public void InitPacket(object parser)
  24. {
  25. pParser = (PacketParser<PacketSt43>)parser;
  26. IsInitialized = true;
  27. IsInUse = false;
  28. }
  29. public bool Handler(ref NetworkMessage message)
  30. {
  31. // parse the message
  32. var pack = new PacketSt43();
  33. pParser(pack, message.PacketData);
  34. var chara = GameServerWorld.Instance.Get<DataClient>(message.NetID).Character;
  35. Weaponset newWeaponset = chara.Data.Items.Weaponsets[pack.WeaponSet];
  36. byte numNewWeapons = GetWeaponsInfo(newWeaponset);
  37. Weaponset activeWeaponset = chara.Data.Items.ActiveWeaponset;
  38. byte numActiveWeapons = GetWeaponsInfo(activeWeaponset);
  39. int slotsRequired = numActiveWeapons - numNewWeapons;
  40. bool hasEnoughSpace = (chara.Data.Items.GetNumFreeSlots() >= slotsRequired);
  41. if (hasEnoughSpace)
  42. {
  43. // update the weaponset selection
  44. chara.Data.Items.ActiveWeaponset = newWeaponset;
  45. var updateActiveWeaponset = new NetworkMessage(chara.Data.NetID)
  46. {
  47. PacketTemplate = new P318_UpdateActiveWeaponset.PacketSt318
  48. {
  49. ItemStreamID = 1,
  50. ActiveWeaponSlot = (byte)chara.Data.Items.ActiveWeaponset.Number
  51. }
  52. };
  53. QueuingService.PostProcessingQueue.Enqueue(updateActiveWeaponset);
  54. SwitchEquipmentSlot(chara, activeWeaponset.LeadHand, newWeaponset.LeadHand, 0);
  55. SwitchEquipmentSlot(chara, activeWeaponset.OffHand, newWeaponset.OffHand, 1);
  56. chara.Data.SaveToWeaponsetsDB();
  57. var map = GameServerWorld.Instance.Get<DataMap>(chara.Data.MapID);
  58. map.Data.ActionQueue.Enqueue(
  59. new SendToAllClients(
  60. new P099_UpdateAgentEquipment.PacketSt99
  61. {
  62. AgentID = chara.Data.AgentID.Value,
  63. EquipmentSlot = 0,
  64. ItemLocalID = (uint)newWeaponset.LeadHand.Data.ItemLocalID
  65. }
  66. ).Execute);
  67. map.Data.ActionQueue.Enqueue(
  68. new SendToAllClients(
  69. new P099_UpdateAgentEquipment.PacketSt99
  70. {
  71. AgentID = chara.Data.AgentID.Value,
  72. EquipmentSlot = 1,
  73. ItemLocalID = (uint)newWeaponset.OffHand.Data.ItemLocalID
  74. }
  75. ).Execute);
  76. }
  77. else
  78. {
  79. var equipBag2 = new NetworkMessage(chara.Data.NetID)
  80. {
  81. PacketTemplate = new P306_EquipBag2.PacketSt306
  82. {
  83. StorageID = 3,
  84. BagLocalID = (uint)chara.Data.Items.ActiveWeaponset.Number
  85. }
  86. };
  87. QueuingService.PostProcessingQueue.Enqueue(equipBag2);
  88. // no inventory space
  89. var chatMessage = new NetworkMessage(chara.Data.NetID)
  90. {
  91. PacketTemplate = new P081_GeneralChatMessage.PacketSt81
  92. {
  93. Message = "Your inventory is full.".ToGW()
  94. }
  95. };
  96. QueuingService.PostProcessingQueue.Enqueue(chatMessage);
  97. var chatNoOwner = new NetworkMessage(chara.Data.NetID)
  98. {
  99. PacketTemplate = new P082_GeneralChatNoOwner.PacketSt82
  100. {
  101. Data1 = 1,
  102. Data2 = 7
  103. }
  104. };
  105. QueuingService.PostProcessingQueue.Enqueue(chatNoOwner);
  106. }
  107. return true;
  108. }
  109. private static byte GetWeaponsInfo(Weaponset weaponset)
  110. {
  111. byte numWeapons = 0;
  112. if (weaponset.LeadHand.Data.PersonalItemID > 0) numWeapons++;
  113. if (weaponset.OffHand.Data.PersonalItemID > 0) numWeapons++;
  114. return numWeapons;
  115. }
  116. private static void SwitchEquipmentSlot(DataCharacter chara, Item activeWeapon, Item newWeapon, byte equipmentSlot)
  117. {
  118. if (activeWeapon.Data.PersonalItemID == newWeapon.Data.PersonalItemID) return; // it's not the same weapon
  119. bool hasActiveWeapon = activeWeapon.Data.PersonalItemID > 0;
  120. bool hasNewWeapon = newWeapon.Data.PersonalItemID > 0;
  121. if (hasNewWeapon)
  122. {
  123. if (hasActiveWeapon)
  124. {
  125. // new and active so swap them
  126. activeWeapon.Data.Storage = newWeapon.Data.Storage;
  127. activeWeapon.Data.Slot = newWeapon.Data.Slot;
  128. activeWeapon.SaveToDB();
  129. newWeapon.Data.Storage = ItemStorage.Equiped;
  130. newWeapon.Data.Slot = equipmentSlot;
  131. newWeapon.SaveToDB();
  132. chara.Data.Items.Equipment[(AgentEquipment)equipmentSlot] = newWeapon;
  133. var swapItems = new NetworkMessage(chara.Data.NetID)
  134. {
  135. PacketTemplate = new P328_SwapItems.PacketSt328
  136. {
  137. ItemStreamID = 1,
  138. MovedItemLocalID = (uint)newWeapon.Data.ItemLocalID,
  139. ItemToBeSwappedWithLocalID = (uint)activeWeapon.Data.ItemLocalID
  140. }
  141. };
  142. QueuingService.PostProcessingQueue.Enqueue(swapItems);
  143. }
  144. else
  145. {
  146. // new but no active
  147. newWeapon.Data.Storage = ItemStorage.Equiped;
  148. newWeapon.Data.Slot = equipmentSlot;
  149. newWeapon.SaveToDB();
  150. newWeapon.SendMove(chara.Data.NetID);
  151. chara.Data.Items.Equipment.Add((AgentEquipment)equipmentSlot, newWeapon);
  152. }
  153. }
  154. else
  155. {
  156. if (hasActiveWeapon)
  157. {
  158. // no new but active
  159. ItemStorage storage;
  160. byte slot;
  161. if (chara.Data.Items.GetFirstFreeSlot(out storage, out slot))
  162. {
  163. activeWeapon.Data.Storage = storage;
  164. activeWeapon.Data.Slot = slot;
  165. activeWeapon.SaveToDB();
  166. activeWeapon.SendMove(chara.Data.NetID);
  167. chara.Data.Items.Equipment.Remove((AgentEquipment)equipmentSlot);
  168. }
  169. }
  170. }
  171. }
  172. public bool IsInitialized { get; set; }
  173. public bool IsInUse { get; set; }
  174. private PacketParser<PacketSt43> pParser;
  175. }
  176. }