/old-cb/cb_backend/db_lib.php

https://github.com/jrundquist/i3-character-creator · PHP · 259 lines · 194 code · 34 blank · 31 comment · 27 complexity · c3bde573000a152862dfc626179d7df7 MD5 · raw file

  1. <?php
  2. require_once('character.php');
  3. require_once('card.php');
  4. require_once('deck.php');
  5. //Gets the connection object to the DB.
  6. function get_cbdb_connection() {
  7. $params = array(
  8. "username" => "untol1_testcb",
  9. "password" => "Ch@racter11",
  10. "db_name" => "untol1_testcb"
  11. );
  12. $link = mysql_connect("testsite.untoldthegame.com", $params["username"], $params["password"]);
  13. mysql_select_db($params["db_name"], $link);
  14. return $link;
  15. }
  16. function get_drudb_connection() {
  17. $params = array(
  18. "username" => "untol1_testcb",
  19. "password" => "Ch@racter11",
  20. "db_name" => "untol1_testdru"
  21. );
  22. $link = mysql_connect("testsite.untoldthegame.com", $params["username"], $params["password"]);
  23. mysql_select_db($params["db_name"], $link);
  24. return $link;
  25. }
  26. function get_drudb_uid($sid, $dbconn) {
  27. if(!$dbconn) {
  28. $dbconn = get_drudb_connection();
  29. }
  30. $query = "select uid from drusessions where sid = '$sid'";
  31. $result = mysql_query($query);
  32. if(!$result) {
  33. return False;
  34. }
  35. $toret = "";
  36. while($res = mysql_fetch_assoc($result)) {
  37. $toret = $res["uid"];
  38. }
  39. return $toret;
  40. }
  41. //Gets the deck representation for the passed in userid.
  42. function get_cbdb_deck($userid, $dbconn) {
  43. if(!$dbconn) {
  44. $dbconn = get_cbdb_connection();
  45. }
  46. //$query = "select cardid from Possession where userid = '$userid'";
  47. //$query = "select cardid from UserCards where userid = '$userid'";
  48. $query = "select a.cardid from untol1_testcb.Possession as a";
  49. $query .= " inner join untol1_testcb.cards_lu as b";
  50. $query .= " where a.cardid = b.full_id";
  51. $query .= " and (b.type = '1' or b.type = '2' or b.type = '3')";
  52. $query .= " and a.userid = '$userid'";
  53. $result = mysql_query($query);
  54. if(!$result) {
  55. return False;
  56. }
  57. $toret = array();
  58. while($res = mysql_fetch_assoc($result)) {
  59. $toret[] = $res["cardid"];
  60. }
  61. return $toret;
  62. }
  63. //Gets all of the card attributes for a given cardid.
  64. function get_cbdb_card($cardid, $dbconn) {
  65. if(!$dbconn) {
  66. $dbconn = get_cbdb_connection();
  67. }
  68. $query = "select full_id as cardid, front, type, name, swap, cost, body_atk, body_def, body_bst, mind_atk, mind_def, mind_bst, soul_atk, soul_def, soul_bst, vit, level, action, duration, `range`, card_img from cards_lu where full_id = '$cardid'";
  69. $result = mysql_query($query);
  70. if(!$result) {
  71. return False;
  72. }
  73. $toret = array();
  74. while($res = mysql_fetch_assoc($result)) {
  75. if($res["front"] == "1") {//Then it's the front.
  76. $toret[1] = $res;
  77. }
  78. else {
  79. $toret[0] = $res;
  80. }
  81. }
  82. return $toret;
  83. }
  84. //Gets a given character using the character id.
  85. function get_cbdb_character($charid, $dbconn) {
  86. if(!$dbconn) {
  87. $dbconn = get_cbdb_connection();
  88. }
  89. $query = "select charid, charname, totalup, swapbuffer, chardesc, lastmodified from UserCharacter where charid = $charid";
  90. $result = mysql_query($query);
  91. if(!$result) {
  92. return False;
  93. }
  94. $char = array("charinfo" => mysql_fetch_assoc($result));
  95. //Must also get the correlated cards.
  96. $query = "select cardid from CharacterCards where charid = $charid";
  97. /*
  98. $query = "SELECT a.cardid FROM `untol1_testcb`.`CharacterCards` as a";
  99. $query .= " inner join untol1_testcb.cards_lu as b";
  100. $query .= " where charid = $charid";
  101. $query .= " and a.cardid = b.full_id";
  102. $query .= " and (b.type = '1' or b.type = '2' or b.type = '3')";
  103. */
  104. $result = mysql_query($query);
  105. if(!$result) {
  106. return False;
  107. }
  108. $char["cards"] = array();
  109. while($res = mysql_fetch_assoc($result)) {
  110. $char["cards"][] = $res["cardid"];
  111. }
  112. return $char;
  113. }
  114. //Gets the list of characters for a given user.
  115. function get_cbdb_users_characters($userid, $dbconn) {
  116. if(!$dbconn) {
  117. $dbconn = get_cbdb_connection();
  118. }
  119. $query = "select charid, charname, totalup, swapbuffer, chardesc, lastmodified from UserCharacter where userid = '$userid'";
  120. $result = mysql_query($query);
  121. //Just need a list of the user's characters, so no card retrieval needed.
  122. if(!$result) {
  123. return False;
  124. }
  125. $toret = array();
  126. while($res = mysql_fetch_assoc($result)) {
  127. $toret[] = $res;
  128. }
  129. return $toret;
  130. }
  131. //returns id of the race card for the given character
  132. function get_race_card_id($charID, $dbconn){
  133. if(!$dbconn) {
  134. $dbconn = get_cbdb_connection();
  135. }
  136. $query = "SELECT a.cardid FROM `untol1_testcb`.`CharacterCards` as a ";
  137. $query .= "inner join `untol1_testcb`.`cards_lu` as b ";
  138. $query .= "on a.cardid = b.full_id ";
  139. $query .= "where b.front = 1 ";
  140. $query .= "and b.type = 1 ";
  141. $query .= "and a.charid = '$charID'";
  142. $result = mysql_query($query);
  143. //Just need a list of the user's characters, so no card retrieval needed.
  144. if(!$result) {
  145. return False;
  146. }
  147. $toret = array();
  148. while($res = mysql_fetch_assoc($result)) {
  149. return $res[cardid];
  150. }
  151. }
  152. //returns id of the race card for the given character
  153. function get_race_card_id_two($charID, $dbconn){
  154. if(!$dbconn) {
  155. $dbconn = get_cbdb_connection();
  156. }
  157. $query = "SELECT a.cardid FROM `untol1_testcb`.`CharacterCards` as a ";
  158. $query .= "inner join `untol1_testcb`.`cards_lu` as b ";
  159. $query .= "on a.cardid = b.full_id ";
  160. $query .= "where b.front = 1 ";
  161. $query .= "and b.type = 1 ";
  162. $result = mysql_query($query);
  163. //Just need a list of the user's characters, so no card retrieval needed.
  164. if(!$result) {
  165. return False;
  166. }
  167. $toret = "";
  168. while($res = mysql_fetch_assoc($result)) {
  169. $toret = $toret . " " . $res[cardid];
  170. }
  171. return substr($toret,1);
  172. }
  173. //Includes both updates and first time saves. Saves the passed in
  174. //object representation of a character.
  175. function save_cbdb_character($char, $userid, $dbconn) {
  176. if(!$dbconn) {
  177. $dbconn = get_cbdb_connection();
  178. }
  179. if($char->getCharID() != NULL) {
  180. //Then the character already has an ID, so we simply update the table.
  181. $query = "update UserCharacter set charname = '".$char->getCharName()."', lastmodified = NOW(), totalup = ".$char->getTotalUP().", swapbuffer = ".$char->getCurrentUP().", chardesc = '".$char->getCharDesc()."' where charid = ".$char->getCharID();
  182. }
  183. else {
  184. //Then we must do an insert because the character doesn't exist yet.
  185. $query = "insert into UserCharacter (charname, userid, totalup, swapbuffer, chardesc, lastmodified) values('".$char->getCharName()."', '$userid', ".$char->getTotalUP().", ".$char->getCurrentUP().", '".$char->getCharDesc()."', NOW())";
  186. }
  187. $result = mysql_query($query);
  188. if(!$result) {
  189. return False;
  190. }
  191. if($char->getCharID() == NULL) {
  192. $postquery = "select last_insert_id()";
  193. $postresult = mysql_query($postquery);
  194. $tempcharid = mysql_fetch_assoc($postresult);
  195. $char->setCharID($tempcharid["last_insert_id()"]);
  196. }
  197. //$newchar = array("charinfo" => mysql_fetch_assoc($result));
  198. //Now must save the cards for the character.
  199. //Hackish way to save, will want to optimize later with an insert or update type of insert. Or a custom made insert or update query.
  200. $preinsert = "delete from CharacterCards where charid = ".$char->getCharID();
  201. mysql_query($preinsert);
  202. foreach($char->getAllCards() as $card) {
  203. $query = "insert into CharacterCards (cardid, charid) values('".$card->id."', ".$char->getCharID().")";
  204. $result = mysql_query($query);
  205. if(!$result) {
  206. print "failed to insert all the cards. ";
  207. return False;
  208. }
  209. }
  210. return $char;
  211. }
  212. //Deletes the representation of a character from the DB using the
  213. //character id.
  214. function delete_cbdb_character($charid, $userid, $dbconn) {
  215. if(!$dbconn) {
  216. $dbconn = get_cbdb_connection();
  217. }
  218. //Remove from the character table.
  219. $query = "delete from UserCharacter where charid = $charid and userid = '$userid'";
  220. $result = mysql_query($query);
  221. //Remove the character's card representation.
  222. $query2 = "delete from CharacterCards where charid = $charid";
  223. $result2 = mysql_query($query2);
  224. }
  225. ?>