Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. using LootLogger.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Net.Http;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9.  
  10. namespace LootLogger
  11. {
  12. public class PacketHandler
  13. {
  14. private ILootService lootService;
  15. private HttpClient client;
  16. private const string itemsMappingUrl = "https://pastebin.pl/view/raw/033981c1";
  17. private bool isInitialized = false;
  18. private Dictionary<int, string> itemDictionary = new Dictionary<int, string>();
  19. SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
  20. public PacketHandler(ILootService lootService)
  21. {
  22. this.lootService = lootService;
  23. this.client = new HttpClient();
  24. }
  25.  
  26. public async void OnEvent(byte code, Dictionary<byte, object> parameters)
  27. {
  28. if (!this.isInitialized)
  29. {
  30. await this.InitializeAsync();
  31. }
  32. if (code == 2)
  33. {
  34. return;
  35. }
  36. object val;
  37. parameters.TryGetValue(252, out val);
  38. if (val == null)
  39. {
  40. return;
  41. }
  42. int iCode = 0;
  43. if (!int.TryParse(val.ToString(), out iCode))
  44. {
  45. return;
  46. }
  47. EventCodes eventCode = (EventCodes)iCode;
  48. if (eventCode == EventCodes.OtherGrabbedLoot)
  49. {
  50. this.OnLootPicked(parameters);
  51. }
  52.  
  53. }
  54.  
  55. public void OnResponse(byte operationCode, short returnCode, Dictionary<byte, object> parameters)
  56. {
  57. }
  58.  
  59. public void OnRequest(byte operationCode, Dictionary<byte, object> parameters)
  60. {
  61. int iCode = 0;
  62. if (!int.TryParse(parameters[253].ToString(), out iCode))
  63. {
  64. return;
  65. }
  66. }
  67.  
  68. private void OnLootPicked(Dictionary<byte, object> parameters)
  69. {
  70. try
  71. {
  72. string looter = parameters[2].ToString();
  73. string quantity = parameters[5].ToString();
  74. int itemId = int.Parse(parameters[4].ToString());
  75. string itemName = itemDictionary[itemId];
  76. string deadPlayer = parameters[1].ToString();
  77.  
  78. Loot loot = new Loot
  79. {
  80. Id = itemId,
  81. ItemName = itemName,
  82. Quantity = int.Parse(quantity.ToString()),
  83. PickupTime = DateTime.UtcNow,
  84. BodyName = deadPlayer,
  85. LooterName = looter
  86. };
  87.  
  88. if (!loot.IsTrash)
  89. {
  90. lootService.AddLootForPlayer(loot, looter);
  91. string path = Path.Combine(Directory.GetCurrentDirectory(), "logs.txt");
  92. string line = $"{looter} has looted {quantity}x {itemName} on {deadPlayer}";
  93. Console.WriteLine(line);
  94. using (StreamWriter sw = File.AppendText(path))
  95. {
  96. sw.WriteLine(line);
  97. }
  98. }
  99. }
  100. catch (Exception e)
  101. {
  102. }
  103. }
  104.  
  105. private async Task InitializeAsync()
  106. {
  107. semaphore.Wait();
  108. try
  109. {
  110. var reponse = await this.client.GetAsync(new Uri(itemsMappingUrl));
  111. var content = await reponse.Content.ReadAsStringAsync();
  112. var lines = content.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  113. foreach (var line in lines ?? new string[] { })
  114. {
  115. string[] split = line.Split(new[] { ":" }, StringSplitOptions.None);
  116. itemDictionary.Add(int.Parse(split[0]), split[1]);
  117. }
  118. this.isInitialized = true;
  119. }
  120. catch (Exception e)
  121. {
  122. Debug.WriteLine(e.StackTrace);
  123. }
  124. finally
  125. {
  126. semaphore.Release();
  127. }
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement