PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/php/pbbg/explore.php

http://building-browsergames-tutorial.googlecode.com/
PHP | 150 lines | 133 code | 9 blank | 8 comment | 16 complexity | 237fd0f75aaf9450cab92319916292fb MD5 | raw file
  1. <?php
  2. require_once 'smarty.php';
  3. require_once 'weapon-stats.php';
  4. require_once 'login-check.php';
  5. session_start();
  6. require_once 'config.php'; // our database settings
  7. $conn = mysql_connect($dbhost,$dbuser,$dbpass)
  8. or die('Error connecting to mysql');
  9. mysql_select_db($dbname);
  10. if($_POST) {
  11. if($_POST['action'] == 'Attack') {
  12. require_once 'stats.php'; // player stats
  13. require_once 'monster-stats.php'; // monster stats
  14. // to begin with, we'll retrieve our player and our monster stats
  15. $query = sprintf("SELECT id FROM users WHERE UPPER(username) = UPPER('%s')",
  16. mysql_real_escape_string($_SESSION['username']));
  17. $result = mysql_query($query);
  18. list($userID) = mysql_fetch_row($result);
  19. $player = array (
  20. name => $_SESSION['username'],
  21. attack => getStat('atk',$userID),
  22. defence => getStat('def',$userID),
  23. curhp => getStat('curhp',$userID)
  24. );
  25. $phand = getStat('phand',$userID);
  26. $atk = getWeaponStat('atk',$phand);
  27. $player['attack'] += $atk;
  28. require_once 'armor-stats.php'; // armor stats
  29. $armor = array('atorso','ahead','alegs','aright','aleft');
  30. foreach ($armor as $key) {
  31. $id = getStat($key,$userID);
  32. $defence = getArmorStat('defence',$id);
  33. $player['defence'] += $defence;
  34. }
  35. $query = sprintf("SELECT id FROM monsters WHERE name = '%s'",
  36. mysql_real_escape_string($_POST['monster']));
  37. $result = mysql_query($query);
  38. list($monsterID) = mysql_fetch_row($result);
  39. $monster = array (
  40. name => $_POST['monster'],
  41. attack => getMonsterStat('atk',$monsterID),
  42. defence => getMonsterStat('def',$monsterID),
  43. curhp => getMonsterStat('maxhp',$monsterID)
  44. );
  45. $combat = array();
  46. $turns = 0;
  47. while($player['curhp'] > 0 && $monster['curhp'] > 0 && $turns <= 100) {
  48. if($turns % 2 != 0) {
  49. $attacker = &$monster;
  50. $defender = &$player;
  51. } else {
  52. $attacker = &$player;
  53. $defender = &$monster;
  54. }
  55. $damage = 0;
  56. if($attacker['attack'] > $defender['defence']) {
  57. $damage = $attacker['attack'] - $defender['defence'];
  58. }
  59. $defender['curhp'] -= $damage;
  60. $combat[$turns] = array(
  61. attacker => $attacker['name'],
  62. defender => $defender['name'],
  63. damage => $damage
  64. );
  65. $turns++;
  66. }
  67. setStat('curhp',$userID,$player['curhp']);
  68. if($player['curhp'] > 0) {
  69. // player won
  70. setStat('gc',$userID,getStat('gc',$userID)+getMonsterStat('gc',$monsterID));
  71. $smarty->assign('won',1);
  72. $smarty->assign('gold',getMonsterStat('gc',$monsterID));
  73. $rand = rand(0,100);
  74. $query = sprintf("SELECT item_id FROM monster_items WHERE monster_id = %s AND rarity >= %s ORDER BY RAND() LIMIT 1",
  75. mysql_real_escape_string($monsterID),
  76. mysql_real_escape_string($rand));
  77. $result = mysql_query($query);
  78. list($itemID) = mysql_fetch_row($result);
  79. $query = sprintf("SELECT count(id) FROM user_items WHERE user_id = '%s' AND item_id = '%s'",
  80. mysql_real_escape_string($userID),mysql_real_escape_string($itemID));
  81. $result = mysql_query($query);
  82. list($count) = mysql_fetch_row($result);
  83. if ($count > 0) {
  84. # already has one of the item
  85. $query = sprintf("UPDATE user_items SET quantity = quantity + 1 WHERE user_id = '%s' AND item_id = '%s'",
  86. mysql_real_escape_string($userID),
  87. mysql_real_escape_string($itemID));
  88. } else {
  89. # has none - new row
  90. $query = sprintf("INSERT INTO user_items(quantity,user_id,item_id) VALUES (1,'%s','%s')",
  91. mysql_real_escape_string($userID),
  92. mysql_real_escape_string($itemID));
  93. }
  94. mysql_query($query);
  95. # retrieve the item name, so that we can display it
  96. $query = sprintf("SELECT name FROM items WHERE id = %s",
  97. mysql_real_escape_string($itemID));
  98. $result = mysql_query($query);
  99. list($itemName) = mysql_fetch_row($result);
  100. $smarty->assign('item',$itemName);
  101. $monster_exp = getMonsterStat('exp',$monsterID);
  102. $smarty->assign('exp',$monster_exp);
  103. $exp_rem = getStat('exp_rem',$userID);
  104. $exp_rem -= $monster_exp;
  105. $level_up = 0;
  106. if($exp_rem <= 0) {
  107. // level up!
  108. $exp_rem = 100;
  109. $level_up = 1;
  110. }
  111. $smarty->assign('level_up',$level_up);
  112. setStat('exp_rem',$userID,$exp_rem);
  113. } else {
  114. // monster won
  115. $smarty->assign('lost',1);
  116. }
  117. $smarty->assign('combat',$combat);
  118. } else {
  119. // Running away! Send them back to the main page
  120. header('Location: index.php');
  121. }
  122. } else {
  123. $area_id = $_GET['area'];
  124. $query = sprintf("SELECT monster FROM area_monsters WHERE area = %s ORDER BY RAND() LIMIT 1",
  125. mysql_real_escape_string($area_id));
  126. $result = mysql_query($query);
  127. list($monster_id) = mysql_fetch_row($result);
  128. $query = sprintf("SELECT name FROM monsters WHERE id = %s",
  129. mysql_real_escape_string($monster_id));
  130. $result = mysql_query($query);
  131. list($monster) = mysql_fetch_row($result);
  132. $smarty->assign('monster',$monster);
  133. }
  134. $query = sprintf("SELECT name FROM areas WHERE id = %s",
  135. mysql_real_escape_string($_GET['area']));
  136. $result = mysql_query($query);
  137. list($area_name) = mysql_fetch_row($result);
  138. $smarty->assign('area',$area_name);
  139. $smarty->assign('area_id',$_GET['area']);
  140. $smarty->display('explore.tpl');
  141. ?>