PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/team_individual.php

https://gitlab.com/robinphillips/risus-web
PHP | 241 lines | 195 code | 21 blank | 25 comment | 45 complexity | c7164c8078c0bfabe49fbedb4e536bf3 MD5 | raw file
  1. <?php
  2. /*
  3. Risus Web. Copyright (c) 2016 Robin Phillips
  4. This software may be modified and distributed under the terms
  5. of the MIT license. See the LICENSE file for details.
  6. Risus: The Anything RPG is written by S. John Ross. Get it from
  7. https://www.drivethrurpg.com/product/170294/
  8. */
  9. require("inc_head_php.php");
  10. require("inc_head_html.php");
  11. require("inc_team.php");
  12. // Get team members
  13. if (isset($_POST["btnSubmit"]) && $_POST["btnSubmit"] != "") {
  14. $teamcliches = array();
  15. $teamdice = array();
  16. // Initialise inappropriate and vengeance to 0. Set to 1 later if required
  17. $teaminappropriate = 0;
  18. $team0vengeance = 0;
  19. $team1vengeance = 0;
  20. // Get "inappropriate" state and ensure that "member" checkbox is set to checked for leader
  21. if (isset($_POST["leader0"])) {
  22. $_POST["member".intval($_POST["leader0"])] = intval($_POST["leader0"]);
  23. if (isset($_POST["inappropriate0"]))
  24. $teaminappropriate = intval($_POST["inappropriate0"]);
  25. if (isset($_POST["vengeance0"]))
  26. $team0vengeance = intval($_POST["vengeance0"]);
  27. }
  28. else {
  29. $_POST["member".intval($_POST["leader1"])] = intval($_POST["leader1"]);
  30. if (isset($_POST["inappropriate1"]))
  31. $teaminappropriate = intval($_POST["inappropriate1"]);
  32. if (isset($_POST["vengeance1"]))
  33. $team1vengeance = intval($_POST["vengeance1"]);
  34. }
  35. // Find team members and add relevant cliché & dice to $teamcliches & $teamdice array
  36. foreach ($_POST as $key=>$value) {
  37. if (substr($key, 0, 6) == "member" && strlen($key) > 6) {
  38. // charid: clicheid
  39. $teamcliches[substr($key, 6)] = $_POST["cliche".intval($value)];
  40. $clichecurrent = $db->querySingle("SELECT current FROM cliches WHERE clicheid=".$_POST["cliche".intval($value)]);
  41. // charid: NumberOfDice
  42. $teamdice[intval($value)] = $clichecurrent;
  43. if (isset($_POST["lucky".intval($value)]))
  44. $teamdice[intval($value)]++;
  45. }
  46. }
  47. $log = "<p>Team vs Individual</p>\n<p>";
  48. $teamtotal = 0;
  49. // Roll team dice
  50. if ($_POST["teamtype"] == "character")
  51. $teamtotal = roll_team_dice ($db, $teamdice, $_POST["leader0"], $team0vengeance, $log);
  52. else
  53. $teamtotal = roll_team_dice ($db, $teamdice, $_POST["leader1"], $team1vengeance, $log);
  54. $log .= "Team total is <strong>$teamtotal</strong>.</p>\n";
  55. // Individual's dice roll
  56. $individual = $db->querySingle("SELECT name FROM characters WHERE charid = ".$_POST["individual"]);
  57. $individualcliche = $db->querySingle("SELECT * FROM cliches WHERE clicheid = ".$_POST["individualcliche"], True);
  58. $log .= "<p>".htmlentities($individual,ENT_QUOTES)." (".htmlentities($individualcliche["cliche"],ENT_QUOTES)." ".$individualcliche["current"].")<br>";
  59. // Inappropriate Clichés and Lucky Shots
  60. if (isset($_POST["inappropriate"]))
  61. $log .= htmlentities($individual,ENT_QUOTES)." is using an Inappropriate Cliché<br>";
  62. if (isset($_POST["lucky"])) {
  63. $individualdice = $individualcliche["current"] +1;
  64. $log .= htmlentities($individual,ENT_QUOTES)." is using a Lucky Shot<br>";
  65. }
  66. else
  67. $individualdice = $individualcliche["current"];
  68. $individualroll = dice_roll($individualdice);
  69. $log .= htmlentities($individual,ENT_QUOTES)." rolls <strong>" . array_sum($individualroll) . "</strong> (";
  70. foreach ($individualroll as $die)
  71. $log .= "$die, ";
  72. // Remove final comma-space
  73. $log = substr($log, 0, -2) . ")</p>\n<p>";
  74. // Work out results
  75. if ($teamtotal == array_sum($individualroll))
  76. $log .= "Draw!";
  77. elseif ($teamtotal > array_sum($individualroll)) {
  78. // Team won
  79. $log .= "Team wins.<br>";
  80. $log .= htmlentities($individual,ENT_QUOTES)." loses ";
  81. if ($teaminappropriate == 1 && !isset($_POST["inappropriate"])) {
  82. $log .= "<i>three points</i>";
  83. $newindividualcliche = $individualcliche["current"] -3;
  84. }
  85. else {
  86. $newindividualcliche = $individualcliche["current"] -1;
  87. $log .= "one point";
  88. }
  89. $log .= " from ".htmlentities($individualcliche["cliche"],ENT_QUOTES);
  90. if ($newindividualcliche <= 0) {
  91. $newindividualcliche = 0;
  92. $log .= "<br><b>".htmlentities($individual,ENT_QUOTES)." is defeated!</b>";
  93. }
  94. // Update individual's cliché
  95. $updateclichesql = "UPDATE cliches SET current = $newindividualcliche WHERE clicheid = ".$_POST["individualcliche"];
  96. $db->exec($updateclichesql);
  97. }
  98. else {
  99. // Individual won
  100. $log .= htmlentities($individual,ENT_QUOTES)." wins.";
  101. // $viewlog is HTML to be displayed but not logged
  102. $viewlog = "Choose a team member to lose ";
  103. if ($teaminappropriate == 0 && isset($_POST["inappropriate"])) {
  104. $viewlog .= "<i>three points:</i>";
  105. $teamclicheloss = 3;
  106. }
  107. else {
  108. $viewlog .= "one point:";
  109. $teamclicheloss = 1;
  110. }
  111. $viewlog .= "<br><select id='updateteamcliche' data-clicheloss='$teamclicheloss'>";
  112. foreach ($teamcliches as $charid=>$clicheid) {
  113. $name = $db->querySingle("SELECT name FROM characters WHERE charid = $charid");
  114. $cliche = $db->querySingle("SELECT cliche, current FROM cliches WHERE clicheid = $clicheid", True);
  115. $viewlog .= "<option value='$clicheid' id='option$clicheid' data-charid='$charid' data-current='".intval($cliche["current"])."'>";
  116. $viewlog .= htmlentities($name, ENT_QUOTES)." (".htmlentities($cliche["cliche"], ENT_QUOTES)." ".intval($cliche["current"]).")";
  117. $viewlog .= "</option>";
  118. }
  119. $viewlog .= "</select>";
  120. $viewlog .= "&nbsp;<button id='selectbutton'>Select</button><br>";
  121. $viewlog .= "<input type = 'checkbox' name='stepforward' id='stepforward' value = '1'> <label for ='stepforward' title='The character takes double damage, but the team leader gets double dice on the next roll' id='lblStepForward'>Character is stepping forward</label>";
  122. }
  123. $log .= "</p>\n";
  124. $viewlog .= "</p>\n";
  125. logdb ($log);
  126. }
  127. ?>
  128. <script>
  129. $(function() {
  130. // Update cliché lists when individual changes
  131. $("#individual").change(function(event){
  132. $('#individualcliche').load('./ajax_clicheoptionlist.php?charid='+$("#individual").val())
  133. })
  134. // Hide results box on rolling dice
  135. $("#btnSubmit").click(function(event) {
  136. $("#resultsdiv").hide()
  137. })
  138. })
  139. </script>
  140. <h1>Combat: Team vs Individual</h1>
  141. <form method="post">
  142. <?php
  143. if (isset($_POST["teamtype"]) && $_POST["teamtype"] == "npc") {
  144. echo "<input type='hidden' id='teamtype' name='teamtype' value='npc'>\n";
  145. echo "<span id='currentteam'>NPC</span> Team (<a id='changeteam' href='#'>Change to Character team</a>)\n";
  146. $showChar = False;
  147. $showNPC = True;
  148. }
  149. else {
  150. echo "<input type='hidden' id='teamtype' name='teamtype' value='character'>\n";
  151. echo "<span id='currentteam'>Character</span> Team (<a id='changeteam' href='#'>Change to NPC team</a>)\n";
  152. $showChar = True;
  153. $showNPC = False;
  154. }
  155. displayCharacters($db, 0, $showChar);
  156. displayCharacters($db, 1, $showNPC);
  157. ?>
  158. <p>vs</p>
  159. <div class="box">
  160. <p class="boxtitle">Individual</p>
  161. <p>
  162. <select name="individual" id="individual">
  163. <?php
  164. if (isset($_POST["individual"]))
  165. $selectedindividual = selectCharacters($db, $_POST["individual"]);
  166. else {
  167. $sql = "SELECT charid FROM characters WHERE active = 1 AND npc = 1 ORDER BY name LIMIT 1";
  168. $selectedindividual = selectCharacters($db, $db->querySingle($sql));
  169. }
  170. ?>
  171. </select>
  172. <select name="individualcliche" id="individualcliche">
  173. <?php
  174. $sql = "SELECT * FROM cliches WHERE cliche_charid = $selectedindividual ORDER BY full DESC";
  175. $cliches = $db->query($sql);
  176. while ($cliche = $cliches->fetchArray(SQLITE3_ASSOC)) {
  177. echo "<option value='".$cliche["clicheid"]."'";
  178. if (isset($_POST["individualcliche"]) && $cliche["clicheid"] == intval($_POST["individualcliche"]))
  179. echo " selected";
  180. echo ">".$cliche["cliche"]." ".$cliche["current"]." ".clichevalue($cliche["full"],$cliche["doublepump"])."</option>";
  181. }
  182. ?>
  183. </select>
  184. </p>
  185. <?php
  186. if (isset($_POST["inappropriate"]))
  187. $check = " checked";
  188. else
  189. $check = "";
  190. ?>
  191. <p class='inappropriate'>
  192. <input name="inappropriate" type="checkbox" id="inappropriate"<?=$check;?>> <label for="inappropriate">This is an Inappropriate Cliché</label>
  193. </p>
  194. <p>
  195. <input name="lucky" type="checkbox" id="lucky"> <label for="lucky">Use a Lucky Shot</label>
  196. </p>
  197. </div>
  198. <p><input type="submit" name="btnSubmit" value="Roll the Dice" id="btnSubmit"></p>
  199. </form>
  200. <?php
  201. if ($log != "")
  202. $style = "";
  203. else
  204. $style = "style='display:none;'";
  205. ?>
  206. <div class='box' id='resultsdiv' <?=$style;?>>
  207. <h2>Results</h2>
  208. <?php
  209. echo "<p>$log</p>\n";
  210. if ($viewlog != "")
  211. $style = "";
  212. else
  213. $style = "style='display:none;'";
  214. echo "<p id='viewlog' $style>$viewlog</p>\n";
  215. ?>
  216. </div>
  217. <?php
  218. require("inc_foot.php");
  219. ?>