Guest User

Untitled

a guest
Dec 15th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. internal class RewardHandler
  2. {
  3. private readonly ConcurrentDictionary<int, Reward> _rewards;
  4. private readonly ConcurrentDictionary<int, List<int>> _rewardLogs;
  5.  
  6. public RewardHandler()
  7. {
  8. _rewards = new ConcurrentDictionary<int, Reward>();
  9. _rewardLogs = new ConcurrentDictionary<int, List<int>>();
  10. }
  11.  
  12. public void Load()
  13. {
  14. if (_rewards.Count > 0)
  15. {
  16. _rewards.Clear();
  17. }
  18.  
  19. if (_rewardLogs.Count > 0)
  20. {
  21. _rewardLogs.Clear();
  22. }
  23.  
  24. using (var dbConnection = Program.Server.DatabaseHandler.Connection)
  25. {
  26. dbConnection.SetQuery("SELECT * FROM `server_rewards` WHERE enabled = '1'");
  27.  
  28. using (var reader = dbConnection.ExecuteReader())
  29. {
  30. var reward = new Reward(
  31. reader.GetDouble("reward_start"),
  32. reader.GetDouble("reward_end"),
  33. reader.GetString("reward_type"),
  34. reader.GetString("reward_data"),
  35. reader.GetString("message")
  36. );
  37.  
  38. if (!_rewards.TryAdd(reader.GetInt32("id"), reward))
  39. {
  40. // TODO: Log a message?
  41. }
  42. }
  43.  
  44. dbConnection.SetQuery("SELECT * FROM `server_reward_logs`");
  45.  
  46. using (var reader = dbConnection.ExecuteReader())
  47. {
  48. var userId = reader.GetInt32("user_id");
  49. var rewardId = reader.GetInt32("reward_id");
  50.  
  51. if (!_rewardLogs.ContainsKey(userId))
  52. _rewardLogs.TryAdd(userId, new List<int>());
  53.  
  54. if (!_rewardLogs[userId].Contains(rewardId))
  55. _rewardLogs[userId].Add(rewardId);
  56. }
  57. }
  58. }
  59.  
  60. public bool PlayerHasReward(int userId, int rewardId)
  61. {
  62. return _rewardLogs.ContainsKey(userId) && _rewardLogs[userId].Contains(rewardId);
  63. }
  64.  
  65. public void CreateLog(int userId, int rewardId)
  66. {
  67. if (!_rewardLogs.ContainsKey(userId))
  68. {
  69. _rewardLogs.TryAdd(userId, new List<int>());
  70. }
  71.  
  72. if (!_rewardLogs[userId].Contains(rewardId))
  73. {
  74. _rewardLogs[userId].Add(rewardId);
  75. }
  76.  
  77. using (var dbConnection = Program.Server.DatabaseHandler.Connection)
  78. {
  79. dbConnection.SetQuery("INSERT INTO `server_reward_logs` (`user_id`, `reward_id`) VALUES (@userId, @rewardId)");
  80. dbConnection.AppendParameter("userId", userId);
  81. dbConnection.AppendParameter("rewardId", rewardId);
  82. dbConnection.ExecuteNonQuery();
  83. }
  84. }
  85.  
  86. public void CheckRewardsForPlayer(Player player)
  87. {
  88. if (player == null)
  89. {
  90. return;
  91. }
  92.  
  93. foreach (var reward in _rewards)
  94. {
  95. if (PlayerHasReward(player.Id, reward.Key))
  96. {
  97. continue;
  98. }
  99.  
  100. if (!reward.Value.IsActive())
  101. {
  102. continue;
  103. }
  104.  
  105. switch (reward.Value.Type)
  106. {
  107. case RewardType.Badge:
  108. player.GetBadgeComponent().GiveBadgeIfNotGot(reward.Value.Data, true);
  109. break;
  110. case RewardType.Credits:
  111. player.Credits += reward.Value.Data.ToInt();
  112. player.SendPacket(new CreditBalanceComposer(player.Credits));
  113. break;
  114. case RewardType.Duckets:
  115. player.Duckets += reward.Value.Data.ToInt();
  116. player.SendPacket(new HabboActivityPointNotificationComposer(player.Duckets, reward.Value.Data.ToInt()));
  117. break;
  118. case RewardType.Diamonds:
  119. player.Diamonds += reward.Value.Data.ToInt();
  120. player.SendPacket(new HabboActivityPointNotificationComposer(player.Diamonds, reward.Value.Data.ToInt(), 5));
  121. break;
  122. }
  123.  
  124. var message = reward.Value.Message;
  125.  
  126. if (string.IsNullOrEmpty(message))
  127. {
  128. player.SendNotification(message);
  129. }
  130.  
  131. CreateLog(player.Id, reward.Key);
  132. }
  133. }
  134. }
  135.  
  136. internal class Reward
  137. {
  138. public double Start;
  139. public double End;
  140. public RewardType Type;
  141. public string Data;
  142. public string Message;
  143.  
  144. public Reward(double start, double end, RewardType type, string data, string message)
  145. {
  146. Start = start;
  147. End = end;
  148. Type = type;
  149. Data = data;
  150. Message = message;
  151. }
  152.  
  153. public bool IsActive()
  154. {
  155. var unixNow = UnixTimestamp.GetNow();
  156. return unixNow >= Start && unixNow <= End;
  157. }
  158. }
Add Comment
Please, Sign In to add comment