getCards.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. // require_once 'debug.php';
  3. require_once 'mysql.php';
  4. session_start();
  5. $con = new DominionConnection();
  6. if ($con !== false)
  7. {
  8. $setArray = array ();
  9. $bannedArray = array ();
  10. if ($_POST ['baseCheck'] == "y")
  11. {
  12. $setArray [] = "Dominion";
  13. }
  14. if ($_POST ['intrCheck'] == "y")
  15. {
  16. $setArray [] = "Intrigue";
  17. }
  18. if ($_POST ['seasCheck'] == "y")
  19. {
  20. $setArray [] = "Seaside";
  21. }
  22. if ($_POST ['alchCheck'] == "y")
  23. {
  24. $setArray [] = "Alchemy";
  25. }
  26. if ($_POST ['prosCheck'] == "y")
  27. {
  28. $setArray [] = "Prosperity";
  29. }
  30. if ($_POST ['cornCheck'] == "y")
  31. {
  32. $setArray [] = "Cornucopia";
  33. }
  34. if ($_POST ['hintCheck'] == "y")
  35. {
  36. $setArray [] = "Hinterlands";
  37. }
  38. if ($_POST ['darkCheck'] == "y")
  39. {
  40. $setArray [] = "Dark Ages";
  41. }
  42. if ($_POST ['guilCheck'] == "y")
  43. {
  44. $setArray [] = "Guilds";
  45. }
  46. if ($_POST ['promCheck'] == "y")
  47. {
  48. // If Promo is selected, build the promo list
  49. $setArray [] = "Promo";
  50. if ($_POST ['blaMarCheck'] == "n")
  51. {
  52. $bannedArray [] = "Black Market";
  53. }
  54. if ($_POST ['envoCheck'] == "n")
  55. {
  56. $bannedArray [] = "Envoy";
  57. }
  58. if ($_POST ['stasCheck'] == "n")
  59. {
  60. $bannedArray [] = "Stash";
  61. }
  62. if ($_POST ['wallCheck'] == "n")
  63. {
  64. $bannedArray [] = "Walled Village";
  65. }
  66. if ($_POST ['goveCheck'] == "n")
  67. {
  68. $bannedArray [] = "Governor";
  69. }
  70. }
  71. if ($_POST['selectFrom'] != "all")
  72. {
  73. $numToUse;
  74. if ($_POST['selectFrom'] == "someFixed")
  75. {
  76. $numToUse = $_POST['selectFixedAmount'];
  77. }
  78. else
  79. {
  80. $numToUse = mt_rand(1, count($setArray));
  81. }
  82. $key = array_search("Promo", $setArray);
  83. if ($numToUse == 1 && $key !== false)
  84. {
  85. unset($setArray[$key]);
  86. $setArray = array_values($setArray);
  87. }
  88. while (count($setArray) > $numToUse)
  89. {
  90. unset($setArray[mt_rand(0, count($setArray) - 1)]);
  91. $setArray = array_values($setArray);
  92. }
  93. }
  94. $setsWhere = implode("', '", $setArray);
  95. if (strlen($setsWhere) > 0)
  96. {
  97. $setsWhere = "'" . $setsWhere . "'";
  98. }
  99. $cardCount = $_POST ['countInput'];
  100. $banWhere = "";
  101. if (count($bannedArray) > 0)
  102. {
  103. $bannedArray = $con->getIds($bannedArray);
  104. $banWhere = "id NOT IN(" . implode(", ", $bannedArray) . ")";
  105. }
  106. // Get the random ids
  107. // $cardArray = array_merge($cardArray, getRandom($con, $setsWhere, (strlen($where)>0?"$where ".(count($cardArray)>0?"AND ":""):"").(count($cardArray)>0?"id NOT IN(".implode(", ", $cardArray).")":""), $cardCount));
  108. $cardArray = $con->getRandom($setsWhere, $banWhere, $cardCount);
  109. $finalStr = "";
  110. $usePlatinums = false;
  111. $useColonies = false;
  112. $useShelters = false;
  113. if (count($cardArray) > 0)
  114. {
  115. $prosperityCount = 0;
  116. $darkAgesCount = 0;
  117. $result = $con->getCards("id IN(" . implode(", ", $cardArray) . ")");
  118. foreach ( $result as $value )
  119. {
  120. $finalStr .= $value ['cardName'] . "," . $value ['setId'] . "," . $value ['setName'] . "," . $value ['cost'];
  121. if ($value ['isVictory'])
  122. {
  123. $finalStr .= "," . ($_POST ['playerCount'] == 2 ? "8" : "12");
  124. }
  125. $finalStr .= "\n";
  126. if ($value ['setId'] == 5)
  127. {
  128. $prosperityCount++;
  129. }
  130. else if ($value ['setId'] == 8)
  131. {
  132. $darkAgesCount++;
  133. }
  134. }
  135. if ($_POST ['platColoTogether'] == "y")
  136. {
  137. if ($_POST ['platColo'] == "percent")
  138. {
  139. $useColonies = mt_rand(0, 99) < $_POST ['platColoPercent'];
  140. }
  141. else if ($_POST ['platColo'] == "proportion")
  142. {
  143. $useColonies = mt_rand(0, 9) < $prosperityCount;
  144. }
  145. else
  146. {
  147. $useColonies = $_POST ['platColo'] == "always";
  148. }
  149. $usePlatinums = $useColonies;
  150. }
  151. else
  152. {
  153. if ($_POST ['platinum'] == "percent")
  154. {
  155. $usePlatinums = mt_rand(0, 99) < $_POST ['platPercent'];
  156. }
  157. else if ($_POST ['platinum'] == "proportion")
  158. {
  159. $usePlatinums = mt_rand(0, 9) < $prosperityCount;
  160. }
  161. else
  162. {
  163. $usePlatinums = $_POST ['platinum'] == "always";
  164. }
  165. if ($_POST ['colony'] == "percent")
  166. {
  167. $useColonies = mt_rand(0, 99) < $_POST ['coloPercent'];
  168. }
  169. else if ($_POST ['colony'] == "proportion")
  170. {
  171. $useColonies = mt_rand(0, 9) < $prosperityCount;
  172. }
  173. else
  174. {
  175. $useColonies = $_POST ['colony'] == "always";
  176. }
  177. }
  178. if ($_POST ['shelters'] == "percent")
  179. {
  180. $useShelters = mt_rand(0, 99) < $_POST ['shelPercent'];
  181. }
  182. else if ($_POST ['shelters'] == "proportion")
  183. {
  184. $useShelters = mt_rand(0, 9) < $darkAgesCount;
  185. }
  186. else
  187. {
  188. $useShelters = $_POST ['shelters'] == "always";
  189. }
  190. }
  191. $finalStr = substr($finalStr, 0, -1);
  192. $finalStr .= ";";
  193. $doubleTreasure = $_POST ['playerCount'] >= 5;
  194. $extraVictory = $_POST ['playerCount'] > 2;
  195. $finalStr .= "Copper,1,Dominion,0".($doubleTreasure?",x2":"");
  196. $finalStr .= "\nSilver,1,Dominion,3".($doubleTreasure?",x2":"");
  197. $finalStr .= "\nGold,1,Dominion,6".($doubleTreasure?",x2":"");
  198. if ($usePlatinums)
  199. {
  200. $finalStr .= "\nPlatinum,5,Prosperity,9";
  201. }
  202. $finalStr .= "\nEstate,1,Dominion,2,".($extraVictory?"12":"8");
  203. if (!$useShelters)
  204. {
  205. $finalStr .= "+".$_POST ['playerCount']."x3";
  206. }
  207. $finalStr .= "\nDuchy,1,Dominion,5,".($extraVictory?"12":"8");
  208. $finalStr .= "\nProvince,1,Dominion,8,".($extraVictory?($_POST ['playerCount'] >= 5?$_POST ['playerCount']*3:"12"):"8");
  209. if ($useColonies)
  210. {
  211. $finalStr .= "\nColony,5,Prosperity,11,".($extraVictory?"12":"8");
  212. }
  213. $finalStr .= "\nCurse,1,Dominion,0,".(10 * ($_POST['playerCount'] - 1));
  214. if ($useShelters)
  215. {
  216. $finalStr .= "\nShelters,8,Dark Ages,1,".$_POST ['playerCount']."x3";
  217. }
  218. $required = $con->getCards("id IN(" . implode(", ", $con->getRequired($cardArray)) . ")", false);
  219. //Handle black market deck if set to
  220. $needBlackMarket = false;
  221. if ($_POST ['autoBlackMarket'] == "y")
  222. {
  223. for ($i = 0; $i < count($required); $i++)
  224. {
  225. if ($required[$i]['cardName'] == "Black Market Deck")
  226. {
  227. $needBlackMarket = true;
  228. unset($required[$i]);
  229. $setArray = array_values($required);
  230. break;
  231. }
  232. }
  233. }
  234. foreach ( $required as $value )
  235. {
  236. $finalStr .= "\n" . $value ['cardName'] . "," . $value ['setId'] . "," . $value ['setName'] . "," . $value ['cost'];
  237. }
  238. if ($needBlackMarket)
  239. {
  240. $deck = $con->getCards("id NOT IN(" . implode(", ", $cardArray) . ")", true, $setsWhere);
  241. $finalStr .= ";";
  242. foreach ( $deck as $value )
  243. {
  244. $finalStr .= $value ['cardName'] . "," . $value ['setId'] . "," . $value ['setName'] . "," . $value ['cost'] . "\n";
  245. }
  246. $finalStr = substr($finalStr, 0, -1);
  247. }
  248. echo $finalStr;
  249. }
  250. else
  251. {
  252. echo "Failed!";
  253. }
  254. /*
  255. * define("baneId", executeQuery($con, "SELECT id FROM nonKingdom WHERE name='Bane';")[0]['id']); define("youngWitchId", executeQuery($con, "SELECT id FROM kingdomCards WHERE name='Young Witch';")[0]['id']); define("blackMarketDeckId", executeQuery($con, "SELECT id FROM nonKingdom WHERE name='Black Market Deck';")[0]['id']); define("alchemyId", executeQuery($con, "SELECT id FROM sets WHERE name='Alchemy';")[0]['id']); $bane = true; $blackMarket = true; function getRequired($con, $where) {//Get the required cards from the database return executeQuery($con, "SELECT tmpRequired.id FROM ( ( (SELECT DISTINCT n.id, n.setId FROM nonKingdom n) AS tmpRequired INNER JOIN ( (SELECT k.requires1 AS required FROM kingdomCards k WHERE ".(strlen($where)>0?"$where AND ":"")."k.requires1 IS NOT NULL) UNION (SELECT k.requires2 AS required FROM kingdomCards k WHERE ".(strlen($where)>0?"$where AND ":"")."k.requires2 IS NOT NULL) ) AS tmpCards ON tmpCards.required = tmpRequired.id ) JOIN (SELECT id, name FROM sets s) AS tmpSets ON tmpRequired.setId = tmpSets.id) GROUP BY tmpRequired.id;" ); } function fillRequired($con, $setWhere, &$ids, $where) {//Fill the an array with ids of all the required cards $i = 0; $required = array(); $result = getRequired($con, "id IN(" . implode(", ", $ids) . ")"); foreach ($result as $value) { if ($value['id'] == baneId && isset($_POST['autoBane'])) {//What if array returns nothing? $GLOBALS['bane'] = getRandom($con, $setWhere, (isset($where) && strlen($where)>0?"$where AND ":"")."(id NOT IN(".implode(", ", $ids).")) AND cost IN(2, 3) ", 1); if (count($GLOBALS['bane']) == 0) { $baneResult = getRandom($con, $setWhere, (isset($where) && strlen($where)>0?"$where AND ":"")."(id IN(".implode(", ", $ids).")) AND cost IN(2, 3) ", 1); if ($baneResult !== false) { $GLOBALS['bane'] = $baneResult; $ids[array_search($GLOBALS['bane'][0], $ids)] = getRandom($con, $setWhere, (isset($where) && strlen($where)>0?"$where AND ":"")."(id NOT IN(".implode(", ", $ids)."))", 1)[0]; } else {echo "alert('Could not find a proper bane.');";} } $GLOBALS['bane'] = $GLOBALS['bane'][0]; $baneArray = array($GLOBALS['bane']); $required = array_merge($required, fillRequired($con, $setWhere, $baneArray, $where)); } else if ($value['id'] == blackMarketDeckId && isset($_POST['virtualBlackMarket'])) { if (false !== strpos($setWhere, "'Cornucopia'") && !in_array(youngWitchId, $ids)) { $ids[] = youngWitchId; $required = fillRequired($con, $setWhere, $ids, $where); unset($ids[array_search(youngWitchId, $ids)]); $ids = array_values($ids); $GLOBALS['blackMarket'] = getCards($con, "id NOT IN(".implode(", ", $ids).")".($GLOBALS['bane']!=true?" AND id != $bane":""), true, $setWhere); break; } else {$GLOBALS['blackMarket'] = getCards($con, "id NOT IN(".implode(", ", $ids).")".($GLOBALS['bane']!=true?" AND id != $bane":""), true, $setWhere);} } else {$required[] = $value['id'];} } return $required; } //Build set where //Build set array $setArray = array(); $promoArray = array(); //Pick which sets to use if (isset($_POST['selectFrom']) && $_POST['selectFrom'] != "all") { $numToUse; if ($_POST['selectFrom'] == "someFixed") {$numToUse = $_POST['setCount'];} else {$numToUse = mt_rand(1, count($setArray));} $key = array_search("'Promo'", $setArray); if ($numToUse == 1 && $key !== false) { unset($setArray[$key]); $setArray = array_values($setArray); } while (count($setArray) > $numToUse) { unset($setArray[mt_rand(0, count($setArray) - 1)]); $setArray = array_values($setArray); } } //Prevent unselected promo cards from being chosen $where = ""; if (count($promoArray) > 0) {$where = "(setId != 10 OR name IN(".implode(", ", $promoArray).")) ";} //Fetch the ids of the requested cards //Prep to get the cards $cardCount = isset($_POST['cardNum'])?$_POST['cardNum']:0; $cardArray = array(); //Get the cards $requestedArray = array(); foreach ($_POST as $key => $value) { if (substr($key, 0, strlen("required-")) == "required-" && $value != '') {$requestedArray[] = $value;} } $alchRequested = 0; if (count($requestedArray) > 0) { $result = getCards($con, "name IN('" . implode("', '", $requestedArray) . "')"); foreach ($result as $value) { $cardArray[] = $value['id']; if ($value['setId'] == 4) {$alchRequested++;} $cardCount--; } } $alchIndex = array_search("'Alchemy'", $setArray); if ($alchIndex !== false && isset($_POST['alchLimit'])) { $alchCount = mt_rand(min(3, $cardCount), min(5, $cardCount)); $alchCount = max($alchCount - $alchRequested, 0); $result = getRandom($con, "id=".alchemyId, (count($cardArray)>0?"id NOT IN(".implode(", ", $cardArray).")":""), $alchCount); foreach ($result as $value) { $cardArray[] = $value; $cardCount--; } unset($setArray[$alchIndex]); $setArray = array_values($setArray); } $setsWhere = "name IN(".implode(", ", $setArray).")"; //Get the random ids $cardArray = array_merge($cardArray, getRandom($con, $setsWhere, (strlen($where)>0?"$where ".(count($cardArray)>0?"AND ":""):"").(count($cardArray)>0?"id NOT IN(".implode(", ", $cardArray).")":""), $cardCount)); //Get cards for the required ids $requiredArray = fillRequired($con, $setsWhere, $cardArray, $where); //Print the result to the Javascript if (count($cardArray)>0) { $result = getCards($con, "id IN(".implode(", ", $cardArray).")"); $i = 0; foreach ($result as $value) {echo "cards[" . $i++ . "] = new Card('" . str_replace("'", "\'", $value['cardName']) . "', " . $value['setId'] . ", '" . $value['setName'] . "', " . $value['cost'] . ($value['isVictory']?($_POST['playerNum'] == 2?", '8'":", '12'"):"") .");\n";} } $i = 0; if ($bane !== true) { $result = getCards($con, "id=$bane", true)[0]; echo "required[" . $i++ . "] = new Card('" . str_replace("'", "\'", $result['cardName']) . "', " . $result['setId'] . ", '" . $result['setName'] . "', " . $result['cost'] . ", 'Bane');\n"; } $treasureNote = ($_POST['playerNum'] >= 5?", 'x2'":""); echo "required[" . $i++ . "] = new Card('Copper', 1, 'Dominion', 0$treasureNote);\n"; echo "required[" . $i++ . "] = new Card('Silver', 1, 'Dominion', 3$treasureNote);\n"; echo "required[" . $i++ . "] = new Card('Gold', 1, 'Dominion', 6$treasureNote);\n"; echo "required[" . $i++ . "] = new Card('Curse', 1, 'Dominion', 0, '".(10 * ($_POST['playerNum'] - 1))."');\n"; $darkAgesCount; if (isset($_POST['dark']) && $_POST['shelters'] == "proportion") {$darkAgesCount = executeQuery($con, "SELECT count(*) FROM kingdomCards WHERE id IN(".implode(", ", $cardArray).") AND setId=8 GROUP BY setId;")[0]['count(*)'];} $useShelters = false; if ($_POST['shelters'] == "always" || ($_POST['shelters'] == "percent" && mt_rand(1, 100) <= intval($_POST['shelPercent'])) || ($_POST['shelters'] == "proportion" && mt_rand(1, intval($_POST['cardNum'])) <= $darkAgesCount)) {$useShelters = true;} if ($useShelters) { echo "required[" . $i++ . "] = new Card('Estate', 1, 'Dominion', 2, '".($_POST['playerNum']>2?"12":"8")."');\n"; echo "required[" . $i++ . "] = new Card('Shelters', 8, 'Dark Ages', 1, '".$_POST['playerNum']." Sets');\n"; } else {echo "required[" . $i++ . "] = new Card('Estate', 1, 'Dominion', 2, '".($_POST['playerNum']>2?"12":"8")." + 3x".$_POST['playerNum']."');\n";} if ($_POST['playerNum'] > 2) { echo "required[" . $i++ . "] = new Card('Duchy', 1, 'Dominion', 5, '12');\n"; if ($_POST['playerNum'] == 5) {echo "required[" . $i++ . "] = new Card('Province', 1, 'Dominion', 8, '15');\n";} else if ($_POST['playerNum'] == 6) {echo "required[" . $i++ . "] = new Card('Province', 1, 'Dominion', 8, '18');\n";} else {echo "required[" . $i++ . "] = new Card('Province', 1, 'Dominion', 8, '12');\n";} } else { echo "required[" . $i++ . "] = new Card('Duchy', 1, 'Dominion', 5, '8');\n"; echo "required[" . $i++ . "] = new Card('Province', 1, 'Dominion', 8, '8');\n"; } $prosperityCount; if (isset($_POST['pros']) && ($_POST['platinum'] == "proportion" || $_POST['colony'] == "proportion")) {$prosperityCount = executeQuery($con, "SELECT count(*) FROM kingdomCards WHERE id IN(".implode(", ", $cardArray).") AND setId=5 GROUP BY setId;")[0]['count(*)'];} if (isset($_POST['platColoTogether'])) { if ($_POST['platColo'] == "always" || ($_POST['platColo'] == "percent" && mt_rand(1, 100) <= intval($_POST['platColoPercent'])) || ($_POST['platColo'] == "proportion" && mt_rand(1, intval($_POST['cardNum'])) <= $prosperityCount)) { echo "required[" . $i++ . "] = new Card('Platinum', 5, 'Prosperity', 9);\n"; echo "required[" . $i++ . "] = new Card('Colony', 5, 'Prosperity', 11, ".($_POST['playerNum']>2?"'12'":"'8'").");\n"; } } else { if ($_POST['platinum'] == "always" || ($_POST['platinum'] == "percent" && mt_rand(1, 100) <= intval($_POST['platPercent'])) || ($_POST['platinum'] == "proportion" && mt_rand(1, intval($_POST['cardNum'])) <= $prosperityCount)) {echo "required[" . $i++ . "] = new Card('Platinum', 5, 'Prosperity', 9);\n";} if ($_POST['colony'] == "always" || ($_POST['colony'] == "percent" && mt_rand(1, 100) <= intval($_POST['coloPercent'])) || ($_POST['colony'] == "proportion" && mt_rand(1, intval($_POST['cardNum'])) <= $prosperityCount)) {echo "required[" . $i++ . "] = new Card('Colony', 5, 'Prosperity', 11, ".($_POST['playerNum']>2?"'12'":"'8'").");\n";} } if (count($requiredArray)>0) { $result = getCards($con, "id IN(".implode(", ", $requiredArray).")", false); foreach ($result as $value) {echo "required[" . $i++ . "] = new Card('" . str_replace("'", "\'", $value['cardName']) . "', " . $value['setId'] . ", '" . $value['setName'] . "', " . $value['cost'] . ");\n";} } if ($blackMarket !== true) { shuffle($blackMarket); $i = 0; foreach ($blackMarket as $value) {echo "blackMarketDeck[" . $i++ . "] = new Card('" . str_replace("'", "\'", $value['cardName']) . "', " . $value['setId'] . ", '" . $value['setName'] . "', " . $value['cost'] . ");\n";} }
  256. */
  257. ?>