Guest User

mine.php

a guest
Jan 31st, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.35 KB | None | 0 0
  1. <?php
  2. /*
  3. TheMasterGeneral's Mining Mod
  4. Mine up items, flakes, etc.
  5. Copyright: 2016
  6. License: DBAD
  7. Filename: mine.php
  8. */
  9. //Gotta make sure the user is legitimate and not a bot.
  10. $macropage=('mine');
  11. require('globals.php');
  12. /*
  13. Testing to see if the user already exists in the databse,
  14. and if not, adding them to the database.
  15. */
  16. $UIDB=$db->query("SELECT * FROM `tmg_mining` WHERE `userid` = {$userid}");
  17. if (!($db->num_rows($UIDB)))
  18. {
  19. $db->query("INSERT INTO `tmg_mining`
  20. (`userid`, `max_miningpower`, `miningpower`, `miningxp`,
  21. `buyable_power`, `mining_level`)
  22. VALUES ('{$userid}', '100', '100', '0', '1', '1');");
  23. }
  24. //Assign their information to a variable we can access later.
  25. $MUS=($db->fetch_row($db->query("SELECT * FROM `tmg_mining` WHERE `userid` = {$userid} LIMIT 1")));
  26. //Causes a person to level up, when needed. Called on each page load while mining.
  27. mining_levelup();
  28. echo "<h2>Dangerous Mines</h2><hr />";
  29. //Blocks access to players who are in jail/hospital.
  30. if ($ir['hospital'])
  31. {
  32. echo "Only the healthy folk can go mining.";
  33. die($h->endpage());
  34. }
  35. if ($ir['jail'])
  36. {
  37. echo "Only the innocent folk can go mining.";
  38. die($h->endpage());
  39. }
  40. if (!isset($_GET['action']))
  41. {
  42. $_GET['action'] = '';
  43. }
  44. switch ($_GET['action'])
  45. {
  46. case 'mine':
  47. mine();
  48. break;
  49. case 'buypower':
  50. buypower();
  51. break;
  52. default:
  53. home();
  54. break;
  55. }
  56. function home()
  57. {
  58. global $MUS,$db,$h;
  59. $mineen = min((int) ($MUS['miningpower'] / $MUS['max_miningpower'] * 100), 100);
  60. $minexp = min((int) ($MUS['miningxp'] / $MUS['xp_needed'] * 100), 100);
  61. $mineenp = 100 - $mineen;
  62. $minexpp = 100 - $minexp;
  63. echo "Welcome to the dangerous mines, brainless fool.
  64. Riches are avaliable for you, if you have the skill.
  65. Each mine has its own requirements, and could even have
  66. a special pickaxe that you need to use.<br />
  67. <br />
  68. You currently have a mining level of {$MUS['mining_level']}.<br />
  69. Mining Power - {$MUS['miningpower']} / {$MUS['max_miningpower']}<br />
  70. <img src='yellowbar.png' width='$mineen' height='15' /><img src='redbar.png' width='$mineenp' height='15' /><br />
  71. Mining XP - {$MUS['miningxp']} / {$MUS['xp_needed']}<br />
  72. <img src='yellowbar.png' width='$minexp' height='15' /><img src='redbar.png' width='$minexpp' height='15' /><br />
  73. <u>Avaliable Mines:</u><br />";
  74. $minesql=$db->query("SELECT * FROM `tmg_mines_data` ORDER BY `mine_level` ASC");
  75. while ($mines = $db->fetch_row($minesql))
  76. {
  77. $CityName=$db->fetch_single($db->query("SELECT `cityname` FROM `cities` WHERE `cityid` = {$mines['mine_location']}"));
  78. echo"[<a href='?action=mine&spot={$mines['mine_id']}'>{$CityName} - Level {$mines['mine_level']}</a>]<br />";
  79. }
  80.  
  81. echo "<br /><br />
  82. [<a href='?action=buypower'>Buy Power</a>]";
  83.  
  84. }
  85. function buypower()
  86. {
  87. global $userid,$db,$ir,$MUS,$h;
  88. $CostForPower = $MUS['mining_level']*75+10+$MUS['mining_level']; //Cost formula, in IQ.
  89. if (isset($_POST['sets']) && ($_POST['sets'] > 0))
  90. {
  91. $sets=abs((int) $_POST['sets']);
  92. $totalcost=$sets*$CostForPower;
  93. if ($sets > $MUS['buyable_power'])
  94. {
  95. echo "You are trying to buy more sets than you currently have available.";
  96. die($h->endpage());
  97. }
  98. elseif (($ir['IQ'] < $totalcost))
  99. {
  100. echo "You need " . number_format($totalcost) . " IQ for the amount of sets
  101. you wish to buy. You only have " . number_format($ir['IQ']) . ".";
  102.  
  103. }
  104. else
  105. {
  106. $db->query("UPDATE `userstats` SET `IQ` = `IQ` - '{$totalcost}' WHERE
  107. `userid` = {$userid}");
  108. $db->query("UPDATE `tmg_mining` SET `buyable_power` = `buyable_power` -
  109. '$sets', `max_miningpower` = `max_miningpower` + ($sets*10) WHERE `userid` = {$userid}");
  110. echo "You have successfully traded " . number_format($totalcost) . "
  111. IQ for {$sets} sets of 10 mining power! Your new maximum mining power is
  112. " . number_format($MUS['max_miningpower'] + ($sets*10)) . "";
  113. }
  114. }
  115. else
  116. {
  117. echo "You can currently buy {$MUS['buyable_power']} sets of 10 Mining Power.
  118. Each set will cost you " . number_format($CostForPower) . " IQ each. So, how
  119. many sets would you like to purchase?";
  120. echo "<br />
  121. <form method='post'>
  122. <input type='number' min='1' max='{$MUS['buyable_power']}' name='sets' required='1'>
  123. <br />
  124. <input type='submit' value='Buy Power'>
  125. </form>";
  126. }
  127. }
  128. function mine()
  129. {
  130. global $db,$MUS,$ir,$userid;
  131. if (!isset($_GET['spot']) || empty($_GET['spot']))
  132. {
  133. echo "Invalid mining spot.";
  134. }
  135. else
  136. {
  137. $spot=abs((int) $_GET['spot']);
  138. $mineinfo=$db->query("SELECT * FROM `tmg_mines_data` WHERE `mine_id` = {$spot}");
  139. //Testing for if the mine itself is in the database. If not,
  140. //Give them a friendly error, rather than quitting out to SQL.
  141. if (!($db->num_rows($mineinfo)))
  142. {
  143. echo "This mine does not exist.";
  144. }
  145. else
  146. {
  147. //If the mine is found, assign its data to a variable
  148. //for easy access later.
  149. $MSI=$db->fetch_row($mineinfo);
  150. if ($MUS['mining_level'] < $MSI['mine_level'])
  151. {
  152. echo "You don't have a high enough mining level to mine here.
  153. You need a mining level of {$MSI['mine_level']}, whereas you
  154. only have a mining level of {$MUS['mining_level']}.";
  155. }
  156. elseif ($ir['location'] != $MSI['mine_location'])
  157. {
  158. echo "You can only go mining in this mine if you're in the same
  159. location.";
  160. }
  161. elseif ($ir['IQ'] < $MSI['mine_iq'])
  162. {
  163. echo "Your IQ is too low to use this mine. You need
  164. " . number_format($MSI['mine_iq']) . " IQ, whereas you only have
  165. " . number_format($ir['IQ']) . ".";
  166. }
  167. elseif ($MUS['miningpower'] < $MSI['mine_power_use'])
  168. {
  169. echo "You do not have enough mining power to mine here. You need
  170. {$MSI['mine_power_use']} and you only have {$MUS['miningpower']}.";
  171. }
  172. else
  173. {
  174. //Fallback in case XP gain isn't updated later on.
  175. if (!isset($xpgain))
  176. {
  177. $xpgain = 0;
  178. }
  179. //If the user's IQ level is between the minimum and
  180. //33% higher than minimum, they get the least amount of
  181. //Positive outputs.
  182. if ($ir['IQ'] <= $MSI['mine_iq']+($MSI['mine_iq']*.3))
  183. {
  184. $Rolls=mt_rand(1,5);
  185. }
  186. //If the user's IQ level is between 30% higher than minimum
  187. //yet less than 60%, give them an increased chance for positive
  188. //outputs.
  189. elseif ($ir['IQ'] >= $MSI['mine_iq']+($MSI['mine_iq']*.3) && ($ir['IQ'] <= $MSI['mine_iq']+($MSI['mine_iq']*.6)))
  190. {
  191. $Rolls=mt_rand(1,10);
  192. }
  193. //If the user's IQ level is above 60% of the mine's minimum IQ,
  194. //Give them more rolls, and a chance to get the gem item.
  195. else
  196. {
  197. $Rolls=mt_rand(1,15);
  198. }
  199. //Rolls 1-3 are negative.
  200. if ($Rolls <= 3)
  201. {
  202. $NegRolls=mt_rand(1,3);
  203. //Three different outputs for the negative.
  204. $NegTime=mt_rand(50,100)*($MUS['mining_level']*.5);
  205. //Hospital
  206. if ($NegRolls == 1)
  207. {
  208. echo "While mining, you hit a gas pocket and ignite
  209. an explosion. You are found later inside the rubble,
  210. barely breathing.";
  211. //$db->query("UPDATE `users` SET `hospital`=`hospital`+{$NegTime},
  212. //`hospreason` = 'Gas Pocket' WHERE `userid` = {$userid}");
  213. }
  214. //Jail
  215. elseif ($NegRolls == 2)
  216. {
  217. echo "While mining, you and another miner spot the same vein with
  218. a gem in it. After a few minutes of bickering of who saw it first,
  219. he punches you, and a brawl begins. The cops see this and arrest you
  220. both.";
  221. //$db->query("UPDATE `users` SET `jail`=`jail`+{$NegTime},
  222. //`jail_reason` = 'Fighting Over a Gem' WHERE `userid` = {$userid}");
  223. }
  224. //Nothing found
  225. else
  226. {
  227. echo "Unlucky for you. While mining you found nothing.";
  228. }
  229. }
  230. //All the positive rolls. 4-14
  231. elseif ($Rolls >= 3 && $Rolls <= 14)
  232. {
  233. $PosRolls=mt_rand(1,3);
  234. //Positive has 3 rolls.
  235. //Output #1
  236. if ($PosRolls == 1)
  237. {
  238. $FlakeName=$db->fetch_single($db->query("SELECT `itmname` FROM `items` WHERE `itmid` = {$MSI['mine_copper_item']}"));
  239. $flakes=mt_rand($MSI['mine_copper_min'],$MSI['mine_copper_max']);
  240. echo "You struck ore! Swing your pick away! After a few minutes,
  241. you managed to grab yourself " . number_format($flakes) . " {$FlakeName}(s)
  242. from this vein.";
  243. item_add($userid,$MSI['mine_copper_item'],$flakes);
  244. $xpgain=$flakes*0.1;
  245.  
  246. }
  247. //Output #2
  248. elseif ($PosRolls == 2)
  249. {
  250. $FlakeName=$db->fetch_single($db->query("SELECT `itmname` FROM `items` WHERE `itmid` = {$MSI['mine_silver_item']}"));
  251. $flakes=mt_rand($MSI['mine_copper_min'],$MSI['mine_copper_max']);
  252. echo "You struck ore! Swing your pick away! After a few minutes,
  253. you managed to grab yourself " . number_format($flakes) . " {$FlakeName}(s)
  254. from this vein.";
  255. item_add($userid,$MSI['mine_silver_item'],$flakes);
  256. $xpgain=$flakes*0.25;
  257. }
  258. //Output #3
  259. else
  260. {
  261. $FlakeName=$db->fetch_single($db->query("SELECT `itmname` FROM `items` WHERE `itmid` = {$MSI['mine_gold_item']}"));
  262. $flakes=mt_rand($MSI['mine_copper_min'],$MSI['mine_copper_max']);
  263. echo "You struck ore! Swing your pick away! After a few minutes,
  264. you managed to grab yourself " . number_format($flakes) . " {$FlakeName}(s)
  265. from this vein.";
  266. item_add($userid,$MSI['mine_gold_item'],$flakes);
  267. $xpgain=$flakes*0.5;
  268. }
  269. }
  270. //Rolled a 15 to get a gem.
  271. else
  272. {
  273. $GemItemName=$db->fetch_single($db->query("SELECT `itmname` FROM `items` WHERE `itmid` = {$MSI['mine_gem_item']}"));
  274. echo "While mining, you expertly find a {$GemItemName} and mine it out slowly. Congratulations, man!";
  275. item_add($userid,$MSI['mine_gem_item'],1);
  276. $xpgain=1;
  277. }
  278. //Displayed at the end so players don't have to refresh.
  279. //Also adds mining xp, and removes mining power when needed.
  280. echo"<hr />
  281. [<a href='?action=mine&spot={$spot}'>Mine Again</a>]<br />
  282. [<a href='mine.php'>Pack it Up</a>]";
  283. $db->query("UPDATE `tmg_mining` SET `miningxp`=`miningxp`+ {$xpgain}, `miningpower`=`miningpower`-'{$MSI['mine_power_use']}' WHERE `userid` = {$userid}");
  284. }
  285. }
  286. }
  287. }
  288. //Function for leveling up while mining.
  289. function mining_levelup()
  290. {
  291. global $db,$userid,$MUS;
  292. //Mining XP needed for a level up.
  293. $MUS['xp_needed'] = (int) (($MUS['mining_level'] + 1) * ($MUS['mining_level'] + 1) * ($MUS['mining_level'] + 1) * 4.4);
  294. //If the user's mining XP is higher than what's needed
  295. if ($MUS['miningxp'] >= $MUS['xp_needed'])
  296. {
  297. $expu = $MUS['miningxp'] - $MUS['xp_needed'];
  298. $MUS['mining_level'] += 1;
  299. $MUS['miningxp'] = $expu;
  300. $MUS['buyable_power'] += 1;
  301. $MUS['xp_needed'] =
  302. (int) (($MUS['mining_level'] + 1) * ($MUS['mining_level'] + 1)
  303. * ($MUS['mining_level'] + 1) * 4.4);
  304. $db->query("UPDATE `tmg_mining` SET `mining_level` = `mining_level` + 1, `miningxp` = {$expu},
  305. `buyable_power` = `buyable_power` + 1 WHERE `userid` = {$userid}");
  306. }
  307. }
  308. $h->endpage();
Add Comment
Please, Sign In to add comment