Advertisement
Guest User

MoodlightData.cs

a guest
Jan 21st, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using System.Collections.Generic;
  5.  
  6. using Plus.Database.Interfaces;
  7.  
  8.  
  9.  
  10. namespace Plus.HabboHotel.Items.Data.Moodlight
  11. {
  12. public class MoodlightData
  13. {
  14. public int ItemId;
  15. public int CurrentPreset;
  16. public bool Enabled;
  17.  
  18. public List<MoodlightPreset> Presets;
  19.  
  20. public MoodlightData(int ItemId)
  21. {
  22. this.ItemId = ItemId;
  23.  
  24. DataRow Row = null;
  25.  
  26. using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  27. {
  28. dbClient.SetQuery("SELECT enabled,current_preset,preset_one,preset_two,preset_three FROM room_items_moodlight WHERE item_id = '" + ItemId + "' LIMIT 1");
  29. Row = dbClient.getRow();
  30. }
  31.  
  32. if (Row == null)
  33. {
  34. using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  35. {
  36. dbClient.RunQuery("INSERT INTO `room_items_moodlight` (item_id,enabled,current_preset,preset_one,preset_two,preset_three) VALUES (" + ItemId + ",0,1,'#000000,255,0','#000000,255,0','#000000,255,0')");
  37. dbClient.SetQuery("SELECT enabled,current_preset,preset_one,preset_two,preset_three FROM room_items_moodlight WHERE item_id=" + ItemId + " LIMIT 1");
  38. Row = dbClient.getRow();
  39. }
  40. }
  41.  
  42. Enabled = PlusEnvironment.EnumToBool(Row["enabled"].ToString());
  43. CurrentPreset = Convert.ToInt32(Row["current_preset"]);
  44. Presets = new List<MoodlightPreset>();
  45.  
  46. Presets.Add(GeneratePreset(Convert.ToString(Row["preset_one"])));
  47. Presets.Add(GeneratePreset(Convert.ToString(Row["preset_two"])));
  48. Presets.Add(GeneratePreset(Convert.ToString(Row["preset_three"])));
  49. }
  50.  
  51. public void Enable()
  52. {
  53. Enabled = true;
  54.  
  55. using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  56. {
  57. dbClient.RunQuery("UPDATE room_items_moodlight SET enabled = 1 WHERE item_id = '" + ItemId + "' LIMIT 1");
  58. }
  59. }
  60.  
  61. public void Disable()
  62. {
  63. Enabled = false;
  64.  
  65. using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  66. {
  67. dbClient.RunQuery("UPDATE room_items_moodlight SET enabled = 0 WHERE item_id = '" + ItemId + "' LIMIT 1");
  68. }
  69. }
  70.  
  71. public void UpdatePreset(int Preset, string Color, int Intensity, bool BgOnly, bool Hax = false)
  72. {
  73. if (!IsValidColor(Color) || !IsValidIntensity(Intensity) && !Hax)
  74. {
  75. return;
  76. }
  77.  
  78. string Pr;
  79.  
  80. switch (Preset)
  81. {
  82. case 3:
  83.  
  84. Pr = "three";
  85. break;
  86.  
  87. case 2:
  88.  
  89. Pr = "two";
  90. break;
  91.  
  92. case 1:
  93. default:
  94.  
  95. Pr = "one";
  96. break;
  97. }
  98.  
  99. using (IQueryAdapter dbClient = PlusEnvironment.GetDatabaseManager().GetQueryReactor())
  100. {
  101. dbClient.SetQuery("UPDATE room_items_moodlight SET preset_" + Pr + " = '@color," + Intensity + "," + PlusEnvironment.BoolToEnum(BgOnly) + "' WHERE item_id = '" + ItemId + "' LIMIT 1");
  102. dbClient.AddParameter("color", Color);
  103. dbClient.RunQuery();
  104. }
  105.  
  106. GetPreset(Preset).ColorCode = Color;
  107. GetPreset(Preset).ColorIntensity = Intensity;
  108. GetPreset(Preset).BackgroundOnly = BgOnly;
  109. }
  110.  
  111. public static MoodlightPreset GeneratePreset(string Data)
  112. {
  113. String[] Bits = Data.Split(',');
  114.  
  115. if (!IsValidColor(Bits[0]))
  116. {
  117. Bits[0] = "#000000";
  118. }
  119.  
  120. return new MoodlightPreset(Bits[0], int.Parse(Bits[1]), PlusEnvironment.EnumToBool(Bits[2]));
  121. }
  122.  
  123. public MoodlightPreset GetPreset(int i)
  124. {
  125. i--;
  126.  
  127. if (Presets[i] != null)
  128. {
  129. return Presets[i];
  130. }
  131.  
  132. return new MoodlightPreset("#000000", 255, false);
  133. }
  134.  
  135. public static bool IsValidColor(string ColorCode)
  136. {
  137. switch (ColorCode)
  138. {
  139. case "#000000":
  140. case "#0053F7":
  141. case "#EA4532":
  142. case "#82F349":
  143. case "#74F5F5":
  144. case "#E759DE":
  145. case "#F2F851":
  146.  
  147. return true;
  148.  
  149. default:
  150.  
  151. return false;
  152. }
  153. }
  154.  
  155. public static bool IsValidIntensity(int Intensity)
  156. {
  157. if (Intensity < 0 || Intensity > 255)
  158. {
  159. return false;
  160. }
  161.  
  162. return true;
  163. }
  164.  
  165. public string GenerateExtraData()
  166. {
  167. MoodlightPreset Preset = GetPreset(CurrentPreset);
  168. var SB = new StringBuilder();
  169.  
  170. SB.Append(Enabled == true ? 2 : 1);
  171.  
  172. SB.Append(",");
  173. SB.Append(CurrentPreset);
  174. SB.Append(",");
  175.  
  176. SB.Append(Preset.BackgroundOnly == true ? 2 : 1);
  177.  
  178. SB.Append(",");
  179. SB.Append(Preset.ColorCode);
  180. SB.Append(",");
  181. SB.Append(Preset.ColorIntensity);
  182. return SB.ToString();
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement