Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. using System.Runtime.Remoting.Messaging;
  2. using MySql.Data.MySqlClient;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.SocialPlatforms.Impl;
  7.  
  8. public class ScoreBdd
  9. {
  10. public static float[] myScores;
  11. public static float[] allScores;
  12.  
  13. public static void loadMyScores()
  14. {
  15. myScores = new float[25];
  16. for (int i = 0; i < myScores.Length; i++)
  17. myScores[i] = PlayerPrefs.GetFloat("ScoreLvl" + (i + 1), -1);
  18. }
  19.  
  20. public static void loadAllScores(ref int loadedScore)
  21. {
  22. allScores = new float[25];
  23. for (int i = 0; i < allScores.Length; i++)
  24. {
  25. allScores[i] = getAllBest(i + 1);
  26. loadedScore++;
  27. }
  28. }
  29.  
  30.  
  31. public static bool AddScore(int id, int lvl, float score)
  32. {
  33. MySqlConnection con = new MySqlConnection("host=51.38.179.180;user=kiuser;password=EioKnSzZq7Tc0jIf;database=kiugame;");
  34. string level = "lvl" + lvl;
  35.  
  36. if (!ScoreExist(id))
  37. {
  38. string sql3 = "INSERT INTO kiugame.levelscore(id, " + level + ") VALUES('" + id + "', '" + score + "');";
  39. MySqlCommand cmd3 = new MySqlCommand(sql3, con);
  40. con.Open();
  41. cmd3.ExecuteNonQuery();
  42. con.Close();
  43.  
  44.  
  45. ActualiseWorldScoreOnline(id);
  46. return true;
  47. }
  48.  
  49.  
  50. string sql = "SELECT id, " + level + " FROM levelscore";
  51. MySqlCommand cmd = new MySqlCommand(sql, con);
  52.  
  53. con.Open();
  54. MySqlDataReader reader = cmd.ExecuteReader();
  55.  
  56. bool continuer = true;
  57. float tempScore = -1;
  58.  
  59. while (continuer && reader.Read())
  60. {
  61. int tempId = (int) reader["id"];
  62.  
  63. if (id == tempId)
  64. {
  65. if(reader[level].GetType().Equals(typeof(float)))
  66. tempScore = (float) reader[level];
  67.  
  68. continuer = false;
  69. con.Close();
  70. }
  71. }
  72.  
  73. if (tempScore < score)
  74. {
  75. string sql2 = "UPDATE kiugame.levelscore SET " + level + " = " + score + " WHERE id=" + id + ";";
  76. MySqlCommand cmd2 = new MySqlCommand(sql2, con);
  77. con.Open();
  78. cmd2.ExecuteNonQuery();
  79. con.Close();
  80.  
  81. ActualiseWorldScoreOnline(id);
  82. return true;
  83. }
  84.  
  85. con.Close();
  86. return false;
  87. }
  88.  
  89. public static void ActualiseWorldScoreOnline(int id)
  90. {
  91. MySqlConnection con = new MySqlConnection("host=51.38.179.180;user=kiuser;password=EioKnSzZq7Tc0jIf;database=kiugame;");
  92.  
  93. float totalScore = 0;
  94.  
  95. for (int i = 1; i <= 5; i++)
  96. {
  97. float score = 0;
  98.  
  99. for (int j = (i - 1) * 5 ; j < i * 5; j++)
  100. if (myScores[j] > 0)
  101. score += myScores[j];
  102.  
  103. totalScore += score;
  104.  
  105. string sql = "UPDATE kiugame.levelscore SET world" + i + " = " + score + " WHERE id=" + id + ";";
  106.  
  107. MySqlCommand cmd = new MySqlCommand(sql, con);
  108. con.Open();
  109. cmd.ExecuteNonQuery();
  110. con.Close();
  111. }
  112.  
  113. string sql2 = "UPDATE kiugame.levelscore SET totalscore = " + totalScore + " WHERE id=" + id + ";";
  114.  
  115. MySqlCommand cmd2 = new MySqlCommand(sql2, con);
  116. con.Open();
  117. cmd2.ExecuteNonQuery();
  118. con.Close();
  119. }
  120.  
  121. public static bool ScoreExist(int id)
  122. {
  123. string sql = "SELECT id FROM levelscore";
  124. MySqlConnection con = new MySqlConnection("host=51.38.179.180;user=kiuser;password=EioKnSzZq7Tc0jIf;database=kiugame;");
  125. MySqlCommand cmd = new MySqlCommand(sql, con);
  126. con.Open();
  127.  
  128. MySqlDataReader reader = cmd.ExecuteReader();
  129.  
  130. while (reader.Read())
  131. {
  132. int tempId = (int) reader["id"];
  133.  
  134. if (id == tempId)
  135. {
  136. con.Close();
  137. return true;
  138. }
  139. }
  140.  
  141. con.Close();
  142. return false;
  143. }
  144.  
  145.  
  146. public static float getAllBest(int lvl)
  147. {
  148. string level = "lvl" + lvl;
  149. string sql = "SELECT " + level + " FROM levelscore";
  150. MySqlConnection con = new MySqlConnection("host=51.38.179.180;user=kiuser;password=EioKnSzZq7Tc0jIf;database=kiugame;");
  151. MySqlCommand cmd = new MySqlCommand(sql, con);
  152. con.Open();
  153.  
  154. MySqlDataReader reader = cmd.ExecuteReader();
  155.  
  156. float score = -1;
  157.  
  158. while (reader.Read())
  159. {
  160. float tempScore = -1;
  161. if (reader[level].GetType().Equals(typeof(float)))
  162. tempScore = (float) reader[level];
  163.  
  164. if (tempScore > score)
  165. score = tempScore;
  166. }
  167.  
  168. con.Close();
  169.  
  170. if (score > 0)
  171. return score;
  172.  
  173. return 0;
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement