PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/abilitylib.php

https://bitbucket.org/mikel3377/webgame
PHP | 196 lines | 149 code | 36 blank | 11 comment | 15 complexity | 8255cbc6c48c4873eb6cd09f4cee2728 MD5 | raw file
  1. <?php
  2. include('validation.php');
  3. /*Ability apply functions. These get called when $ability->Apply() is called
  4. Each one has identical arguments:
  5. $ability is the ability that's being used. It has Params, which is some array of the ability parameters
  6. $game is a gamedata object, or something
  7. $unitrow is an array of unit data (retrived by mysql_fetch_array off of a mysql_query)
  8. $newParams is whatever the Apply function needs to make the ability happend (e.g. for moving, it should have new coords)
  9. $updateList should be modified to include whatever new updates need to be stored when the ability is used
  10. */
  11. function lifeauraApply($ability, $game, $unitrow, $newParams, &$updateList)
  12. {
  13. $range = $ability->Params[0];
  14. $power = $ability->Params[1];
  15. if ($power >= 0){ // healing aura on own units
  16. $newUnits = GetMyUnits($unitrow['ownerid'], $game->ID);
  17. } else { // damage aura on enemies
  18. $newUnits = GetTheirUnits($unitrow['ownerid'], $game->ID);
  19. }
  20. $affected = AllCellsInRange($game,$unitrow['x'],$unitrow['y'],$range,TRUE);
  21. while ($row = mysql_fetch_array($newUnits)){
  22. if ( isset( $affected[$row['x']][$row['y']] ) ){
  23. DamageApply(-1*$power, 0, $row['hp'], $unitrow['id'], $row['id'], $row['maxhp'], $game->ID, $updateList);
  24. }
  25. }
  26. return;
  27. }
  28. function moveApply($ability, $game, $unitrow, $newParams, &$updateList)
  29. {
  30. $x = $newParams['x'];
  31. $y = $newParams['y'];
  32. $unitid = $unitrow['id'];
  33. $user = Application::GetCurrentUser();
  34. $gameid = $game->ID;
  35. $userid = $user->ID;
  36. if (! ValidateMove($userid, $unitrow, $game, $x, $y)){
  37. die(json_encode( array( 'status' => 'Move validation failed') ));
  38. return;
  39. }
  40. $query = "UPDATE units SET x=$x, y=$y, moves=0 WHERE id=$unitid";
  41. mysql_query($query) or die(json_encode( array( 'status' => mysql_error())));
  42. $query = "INSERT INTO gameupdates (gameid, type, param1, param2, param3) VALUES ($gameid, 1, $unitid, $x, $y)";
  43. mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  44. $row=array('type' => 1, 'gameid' => $game->ID, 'id' => 1, 'param1' => $unitid, 'param2' => $x, 'param3' => $y); //NOTE THE UPDATE ID IS ALWAYS 1 HERE!!!!
  45. $updateList[] = GetUpdateObject($row);
  46. return;
  47. }
  48. function attackApply($ability, $game, $unitrow, $newParams, &$updateList){
  49. $targx = $newParams['x'];
  50. $targy = $newParams['y'];
  51. $gameid = $game->ID;
  52. $targ = mysql_fetch_array(GetUnitByLoc($targx, $targy, $gameid));
  53. $user = Application::GetCurrentUser();
  54. $userid = $user->ID;
  55. $targid = $targ['id'];
  56. $unitid = $unitrow['id'];
  57. $s = GameDataService::GetInstance();
  58. $map = $s->GetGameMap( $game );
  59. if (! ValidateAttack($userid, $unitrow, $gameid, $targ)){
  60. die(json_encode( array( 'status' => 'Move validation failed') ));
  61. return;
  62. }
  63. $at = UnitConfiguration::GetByID( (int)$unitrow['type'] );
  64. $tt = UnitConfiguration::GetByID( (int)$targ['type'] );
  65. $t = $map->Cells[$unitrow['x']][$unitrow['y']]->Type;
  66. $tile = TileConfiguration::GetByID( $t );
  67. $defense = $tt->Defense;
  68. $attack = $at->AttackPower;
  69. $defense = $defense + $tile->DefenseBonus;
  70. DamageApply($attack, $defense, (int)$targ['hp'], $unitid, $targid, $targ['maxhp'], $gameid, $updateList);
  71. }
  72. function spawnunitApply($ability, $game, $unitrow, $newParams, &$updateList){
  73. $unitid = $unitrow['id'];
  74. $ownerid = $unitrow['ownerid'];
  75. $type = $newParams['param1'];
  76. $tarx = $newParams['x'];
  77. $tary = $newParams['y'];
  78. $user = Application::GetCurrentUser();
  79. $userid = $user->ID;
  80. $gameid = $game->ID;
  81. $t = UnitConfiguration::GetByID( $type );
  82. $moneyrow = mysql_fetch_array(GetMyMoney($userid, $gameid));
  83. $money = $moneyrow['resources'];
  84. if( $money < $t->ResourceCost)
  85. {
  86. echo json_encode( array( 'status' => "Not enough resources") );
  87. return;
  88. }
  89. //TODO: move this to validation and do more validation
  90. $money = $money - $t->ResourceCost;
  91. $query = "UPDATE usergames SET resources = $money WHERE gameid = $gameid AND userid = $userid";
  92. mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  93. $query = "UPDATE units SET actionused = TRUE WHERE id = $unitid";
  94. mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  95. $id = MySql::CreateUnit( $gameid, $ownerid, $tarx, $tary, $type );
  96. $query = "UPDATE units SET actionused = TRUE, moves = 0 WHERE id = $id";
  97. mysql_query( $query );
  98. $query = "INSERT INTO gameupdates (gameid, type, param1 ) VALUES ($gameid, 4, $id )";
  99. mysql_query($query) or die( array( 'status' => mysql_error()) );
  100. $row=array('type' => 4, 'gameid' => $gameid, 'id' => 1, 'param1' => $id); //NOTE THE UPDATE ID IS ALWAYS 1 HERE!!!!
  101. $updateList[] = GetUpdateObject($row);
  102. }
  103. //a few database utility functions - maybe these should go somewhere else like dbconn?
  104. function GetAllUnits($gameid){
  105. $query = "SELECT * FROM units WHERE gameid=$gameid";
  106. $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  107. return $result;
  108. }
  109. function GetMyUnits($userid, $gameid, $dead = FALSE){
  110. if ($dead){
  111. $query = "SELECT * FROM units WHERE ownerid=$userid AND gameid=$gameid";
  112. } else {
  113. $query = "SELECT * FROM units WHERE ownerid=$userid AND gameid=$gameid AND hp > 0";
  114. }
  115. $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  116. return $result;
  117. }
  118. function GetTheirUnits($userid, $gameid, $dead = FALSE){
  119. if ($dead){
  120. $query = "SELECT * FROM units WHERE ownerid != $userid AND gameid=$gameid";
  121. } else {
  122. $query = "SELECT * FROM units WHERE ownerid != $userid AND gameid=$gameid AND hp > 0";
  123. }
  124. $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  125. return $result;
  126. }
  127. function GetThisUnit($unitid){
  128. $query = "SELECT * FROM units WHERE id = $unitid";
  129. $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  130. return $result;
  131. }
  132. function GetUnitByLoc($x, $y, $gameid){
  133. $query = "SELECT * FROM units WHERE x = $x AND y = $y AND gameid = $gameid";
  134. $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  135. return $result;
  136. }
  137. function GetMyMoney($userid, $gameid){
  138. $query = "SELECT resources FROM usergames WHERE gameid = $gameid AND userid = $userid";
  139. $result = mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  140. return $result;
  141. }
  142. //a few other utility functions
  143. function DamageApply($attack, $defense, $curhp, $unitid, $targid, $maxhp, $gameid, &$updateList){
  144. if ($curhp > 0){
  145. $damage = $attack-$defense;
  146. if ($attack > 0) {
  147. $damage = max($damage,0);
  148. }
  149. $newhp = max( min( $maxhp, $curhp - $damage ), 0 );
  150. $query = "UPDATE units AS Targ, units AS Att
  151. SET Targ.hp = $newhp, Att.actionused = TRUE, Att.moves = 0
  152. WHERE Att.id = $unitid AND Targ.id = $targid";
  153. mysql_query( $query ) or die( json_encode( array( 'status' => mysql_error()) ) );
  154. $query = "INSERT INTO gameupdates (gameid, type, param1, param2, param3) VALUES ($gameid, 2, $unitid, $targid, $damage)";
  155. mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  156. $row=array('type' => 2, 'gameid' => $gameid, 'id' => 1, 'param1' => $unitid, 'param2' => $targid, 'param3' => $damage); //NOTE THE UPDATE ID IS ALWAYS 1 HERE!!!!
  157. $updateList[] = GetUpdateObject($row);
  158. }
  159. }
  160. ?>