Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.84 KB | None | 0 0
  1. namespace TroZenA.Database
  2. {
  3. using TroZenA;
  4. using TroZenA.Game;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9.  
  10. public static class JiangHu
  11. {
  12. public static Dictionary<ushort, Atribut> Atributes = new Dictionary<ushort, Atribut>();
  13. public static Dictionary<byte, List<byte>> CultivateStatus = new Dictionary<byte, List<byte>>();
  14. private static byte[] FreeCourse = new byte[] { 10, 20, 0x1a, 0x1f, 0x34 };
  15. private static ushort[] FreeCourseInCastle = new ushort[] { 0x126, 0x271, 0x341, 0x3e8, 0x61a };
  16. public static bool fullload = false;
  17. private static ushort[] GetPoints = new ushort[] { 100, 120, 150, 200, 300, 500 };
  18. private static double[] MinutesInCastle = new double[] { 31.25, 31.25, 32.0, 32.2, 30.0 };
  19. private static ushort[] MinutesOnTalent = new ushort[] { 0x3e8, 500, 0x180, 0x142, 0xc0 };
  20. private static ushort[] GetAlignmentExtraPoints = new ushort[9] { 0, 10, 13, 15, 18, 21, 25, 30, 50 };
  21. public static ushort AlignmentExtraPoints(byte amount)
  22. {
  23. return GetAlignmentExtraPoints[Math.Min(8, (int)amount)];
  24. }
  25. public static ushort GetFreeCourse(byte Talent)
  26. {
  27. if (Talent == 0)
  28. {
  29. Talent = 1;
  30. }
  31. return FreeCourse[Math.Min(4, Talent - 1)];
  32. }
  33.  
  34. public static ushort GetFreeCourseInCastle(byte Talent)
  35. {
  36. if (Talent == 0)
  37. {
  38. Talent = 1;
  39. }
  40. return FreeCourseInCastle[Math.Min(4, Talent - 1)];
  41. }
  42.  
  43. public static double GetMinutesInCastle(byte Talent)
  44. {
  45. if (Talent == 0)
  46. {
  47. Talent = 1;
  48. }
  49. return MinutesInCastle[Math.Min(4, Talent - 1)];
  50. }
  51.  
  52. public static ushort GetMinutesOnTalent(byte Talent)
  53. {
  54. if (Talent == 0)
  55. {
  56. Talent = 1;
  57. }
  58. return MinutesOnTalent[Math.Min(4, Talent - 1)];
  59. }
  60.  
  61. public static ushort GetPower(ushort UID)
  62. {
  63. if (Atributes.ContainsKey(UID))
  64. {
  65. return Atributes[UID].Power;
  66. }
  67. return 0;
  68. }
  69.  
  70. public static ushort GetStatusPoints(byte Level)
  71. {
  72. if (Level == 0)
  73. {
  74. Level = 1;
  75. }
  76. return GetPoints[Math.Min(5, Level - 1)];
  77. }
  78.  
  79. public static void LoadStatus()
  80. {
  81. try
  82. {
  83. TroZenA.Database.Read read;
  84. uint count;
  85. uint num2;
  86. string[] strArray;
  87. using (read = new TroZenA.Database.Read(@"database\JianghuAttributes.txt"))
  88. {
  89. if (read.Reader(true))
  90. {
  91. count = (uint)read.Count;
  92. for (num2 = 0; num2 < count; num2++)
  93. {
  94. strArray = read.ReadString("").Split(new char[] { ' ' });
  95. Atribut atribut = new Atribut
  96. {
  97. Type = byte.Parse(strArray[1]),
  98. Level = byte.Parse(strArray[2]),
  99. Power = ushort.Parse(strArray[3])
  100. };
  101. if (((atribut.Type == 10) || (atribut.Type == 11)) || (atribut.Type == 12))
  102. {
  103. atribut.Power = (ushort)(atribut.Power * 10);
  104. }
  105. ushort key = ValueToRoll(atribut.Type, atribut.Level);
  106. Atributes.Add(key, atribut);
  107. }
  108. }
  109. }
  110. using (read = new TroZenA.Database.Read(@"database\JingHuCultivateStatus.txt"))
  111. {
  112. if (read.Reader(true))
  113. {
  114. count = (uint)read.Count;
  115. for (num2 = 0; num2 < count; num2++)
  116. {
  117. strArray = read.ReadString("").Split(new char[] { ' ' });
  118. byte num4 = byte.Parse(strArray[0]);
  119. byte num5 = byte.Parse(strArray[1]);
  120. List<byte> list = new List<byte>();
  121. for (byte i = 0; i < num5; i = (byte)(i + 1))
  122. {
  123. list.Add(byte.Parse(strArray[2 + i]));
  124. }
  125. CultivateStatus.Add(num4, list);
  126. }
  127. }
  128. }
  129. }
  130. catch (Exception exception)
  131. {
  132. TroZenA.Console.WriteLine(exception.ToString());
  133. }
  134. }
  135.  
  136. public static void LoadJiangHuOLD()
  137. {
  138. using (Read r = new Read("database\\JiangHu.txt"))
  139. {
  140. if (r.Reader())
  141. {
  142. int count = r.Count;
  143. for (uint x = 0; x < count; x++)
  144. {
  145. string data = r.ReadString("");
  146. if (data != null)
  147. {
  148. Game.JiangHu jiang = new Game.JiangHu(0);
  149. jiang.Load(data);
  150. Game.JiangHu.JiangHuClients.TryAdd(jiang.UID, jiang);
  151. Game.JiangHu.JiangHuRanking.UpdateRank(jiang);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. public static void LoadJiangHu()
  158. {
  159. if (System.IO.File.Exists("database\\JiangHu.txt"))
  160. {
  161. LoadJiangHuOLD();
  162. foreach (var ijiang in Game.JiangHu.JiangHuClients.Values)
  163. {
  164. var jiang = ijiang as Game.JiangHu;
  165. if (jiang != null)
  166. {
  167. if (jiang.UID == 0)
  168. continue;
  169. using (var cmd = new MySqlCommand(MySqlCommandType.SELECT))
  170. {
  171. cmd.Select("jiang").Where("UID", jiang.UID);
  172. using (MySqlReader rdr = new MySqlReader(cmd))
  173. {
  174. if (!rdr.Read())
  175. {
  176. using (var command = new MySqlCommand(MySqlCommandType.INSERT))
  177. {
  178. command.Insert("jiang").Insert("UID", jiang.UID).Insert("OwnName", jiang.OwnName);
  179. command.Execute();
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. SaveJiangHu();
  187. System.IO.File.Delete("database\\JiangHu.txt");
  188. }
  189. else
  190. {
  191. using (var cmd = new MySqlCommand(MySqlCommandType.SELECT))
  192. {
  193. cmd.Select("jiang");
  194. using (MySqlReader rdr = new MySqlReader(cmd))
  195. {
  196. while (rdr.Read())
  197. {
  198. Game.JiangHu jiang = new Game.JiangHu(0);
  199. jiang.UID = rdr.ReadUInt32("UID");
  200. jiang.OwnName = rdr.ReadString("OwnName");
  201. jiang.CustomizedName = rdr.ReadString("CustomizedName");
  202. byte[] data = rdr.ReadBlob("Powers");
  203. if (data.Length > 0)
  204. {
  205. using (var stream = new System.IO.MemoryStream(data))
  206. using (var reader = new System.IO.BinaryReader(stream))
  207. {
  208. jiang.Deserialize(reader);
  209. }
  210. }
  211. Game.JiangHu.JiangHuClients.TryAdd(jiang.UID, jiang);
  212. jiang.CreateStatusAtributes(null);
  213. }
  214. }
  215. }
  216. }
  217.  
  218. }
  219. public static void New(Client.GameClient client)
  220. {
  221. if (client.Entity.MyJiang == null)
  222. return;
  223. using (var cmd = new MySqlCommand(MySqlCommandType.SELECT))
  224. {
  225. cmd.Select("jiang").Where("UID", client.Entity.UID);
  226. using (MySqlReader rdr = new MySqlReader(cmd))
  227. {
  228. if (!rdr.Read())
  229. {
  230. using (var command = new MySqlCommand(MySqlCommandType.INSERT))
  231. {
  232. command.Insert("jiang").Insert("UID", client.Entity.UID).Insert("CustomizedName", client.Entity.MyJiang.CustomizedName).Insert("OwnName", client.Entity.MyJiang.OwnName);
  233. command.Execute();
  234. }
  235. }
  236. }
  237. }
  238. }
  239. public static void SaveJiangHu()
  240. {
  241. foreach (var ijiang in Game.JiangHu.JiangHuClients.Values)
  242. {
  243. var jiang = ijiang as Game.JiangHu;
  244. if (jiang != null)
  245. {
  246. if (jiang.UID == 0)
  247. continue;
  248. try
  249. {
  250. MemoryStream stream = new MemoryStream();
  251. BinaryWriter writer = new BinaryWriter(stream);
  252. (jiang as Game.JiangHu).Serialize(writer);
  253. ///////////
  254. string SQL = "UPDATE `jiang` SET Powers=@Powers,OwnName=@OwnName,CustomizedName=@CustomizedName where UID = " + jiang.UID + " ;";
  255. byte[] rawData = stream.ToArray();
  256. using (var conn = DataHolder.MySqlConnection)
  257. {
  258. conn.Open();
  259. using (var cmd = new MySql.Data.MySqlClient.MySqlCommand())
  260. {
  261. cmd.Connection = conn;
  262. cmd.CommandText = SQL;
  263. cmd.Parameters.AddWithValue("@Powers", rawData);
  264. cmd.Parameters.AddWithValue("@OwnName", jiang.OwnName);
  265. cmd.Parameters.AddWithValue("@CustomizedName", jiang.CustomizedName);
  266. cmd.ExecuteNonQuery();
  267. }
  268. }
  269. }
  270. catch (Exception e)
  271. {
  272. System.Console.WriteLine(e);
  273. }
  274.  
  275. }
  276. }
  277. }
  278.  
  279. public static void SaveJiangHu(Client.GameClient client)
  280. {
  281. try
  282. {
  283. MemoryStream stream = new MemoryStream();
  284. BinaryWriter writer = new BinaryWriter(stream);
  285. client.Entity.MyJiang.Serialize(writer);
  286. ///////////
  287. string SQL = "UPDATE `jiang` SET Powers=@Powers,OwnName=@OwnName,CustomizedName=@CustomizedName where UID = " + client.Entity.UID + " ;";
  288. byte[] rawData = stream.ToArray();
  289. using (var conn = DataHolder.MySqlConnection)
  290. {
  291. conn.Open();
  292. using (var cmd = new MySql.Data.MySqlClient.MySqlCommand())
  293. {
  294. cmd.Connection = conn;
  295. cmd.CommandText = SQL;
  296. cmd.Parameters.AddWithValue("@Powers", rawData);
  297. cmd.Parameters.AddWithValue("@OwnName", client.Entity.MyJiang.OwnName);
  298. cmd.Parameters.AddWithValue("@CustomizedName", client.Entity.MyJiang.CustomizedName);
  299. cmd.ExecuteNonQuery();
  300. }
  301. }
  302. }
  303. catch (Exception e)
  304. {
  305. System.Console.WriteLine(e);
  306. }
  307.  
  308. }
  309. public static ushort ValueToRoll(byte typ, byte level)
  310. {
  311. return (ushort)(typ + (level * 0x100));
  312. }
  313.  
  314. public class Atribut
  315. {
  316. public byte Level;
  317. public ushort Power;
  318. public byte Type;
  319. }
  320. }
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement