PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/action.php

https://bitbucket.org/mikel3377/webgame
PHP | 162 lines | 138 code | 23 blank | 1 comment | 31 complexity | f5f49c7d63561dffcae895f91dd4352b MD5 | raw file
  1. <?php
  2. include('lib/applicationlib.php');
  3. include('lib/pathfinding.php');
  4. if(isset($_POST['action']) && isset($_POST['gameid']))
  5. {
  6. $gameid = mysql_real_escape_string( $_POST['gameid']);
  7. $s = GameDataService::GetInstance();
  8. $game = $s->GetGameByID( $gameid );
  9. $updateList=array();
  10. if($_POST['action']=="move" && isset( $_POST['unitid']) && isset($_POST['x']) && isset($_POST['y']))
  11. {
  12. $unitid = mysql_real_escape_string( $_POST['unitid']);
  13. $x = mysql_real_escape_string( $_POST['x']);
  14. $y = mysql_real_escape_string( $_POST['y']);
  15. $newParams = array('x' => $x, 'y' => $y);
  16. HandleTargetedAbility( $unitid, 'move', $newParams, $game, $updateList);
  17. echo json_encode( array( 'status' => $updateList ) );
  18. return;
  19. }else if( $_POST['action']=="targeted ability" && isset( $_POST['abilityname'] ) && isset( $_POST['unitid'] ) && isset($_POST['targetx']) && isset($_POST['targety']) && isset($_POST['x']) && isset($_POST['y']))
  20. {
  21. $unitid = mysql_real_escape_string( $_POST['unitid']);
  22. $abilityname = mysql_real_escape_string( $_POST['abilityname']);
  23. $targetx = mysql_real_escape_string( $_POST['targetx']);
  24. $targety = mysql_real_escape_string( $_POST['targety']);
  25. $x = mysql_real_escape_string( $_POST['x']);
  26. $y = mysql_real_escape_string( $_POST['y']);
  27. if (UnitHasMoved($unitid, $x, $y)){
  28. $newParams = array('x' => $x, 'y' => $y);
  29. HandleTargetedAbility( $unitid, 'move', $newParams, $game, $updateList);
  30. }
  31. $newParams = array('x' => $targetx, 'y' => $targety);
  32. if (isset($_POST['param1']) ){
  33. $newParams['param1'] = $_POST['param1'];
  34. }
  35. HandleTargetedAbility( $unitid, $abilityname, $newParams, $game, $updateList);
  36. echo json_encode( array( 'status' => $updateList) );
  37. return;
  38. }
  39. else if( $_POST['action']=="endturn" )
  40. {
  41. EndTurn($game, $updateList);
  42. echo json_encode( array( 'status' => $updateList ) );
  43. return;
  44. }
  45. echo json_encode( array( 'status' => "Invalid action parameters") );
  46. return;
  47. }
  48. function HandleTargetedAbility( $unitid, $abilityname, $newParams, $game, &$updateList){
  49. $unitrow = mysql_fetch_array(GetThisUnit($unitid));
  50. $ability = UnitConfiguration::GetAbility($unitrow['type'],$abilityname);
  51. if ($ability){
  52. $ability->Apply($game, $unitrow, $newParams, $updateList);
  53. } else{
  54. die( json_encode( array( 'status' => 'unit ' . $unitid . ' doesnt have ability ' . $abilityname ) ) );
  55. }
  56. }
  57. function EndTurn( $game, &$updateList)
  58. {
  59. $user = Application::GetCurrentUser();
  60. $gameid = $game->ID;
  61. $query = "SELECT ownerid FROM units WHERE gameid = $game->ID GROUP BY ownerid";
  62. $result = mysql_query($query) or die( json_encode( array( 'status' => mysql_error())));
  63. if( $game->ActiveUser != $user->ID )
  64. {
  65. die( json_encode( array( 'status' => "You can only end your own turn $game->ActiveUser $user->ID" ) ) );
  66. }
  67. $next = false;
  68. $first = true;
  69. $firstid = -1;
  70. //$status = "";
  71. while($row = mysql_fetch_array($result))
  72. {
  73. if( $first )
  74. {
  75. $first =false;
  76. $firstid = $row['ownerid'];
  77. }
  78. if( $next )
  79. {
  80. $newid = $row['ownerid'];
  81. $query = "UPDATE games SET activeuser=$newid WHERE id = $game->ID";
  82. mysql_query($query) or die( mysql_error());
  83. $query = "INSERT INTO gameupdates (gameid, type, param1 ) VALUES ($game->ID, 3, $newid )";
  84. mysql_query($query) or die( array( 'status' => mysql_error()) );
  85. EndOfPlayerTurn($game->ActiveUser, $game, $updateList );
  86. return;
  87. }
  88. if( $row['ownerid'] == $game->ActiveUser )
  89. $next = true;
  90. }
  91. $newid = $firstid;
  92. $query = "UPDATE games SET activeuser=$newid WHERE id = $game->ID";
  93. mysql_query($query) or die( array( 'status' => mysql_error()) );
  94. $query = "INSERT INTO gameupdates (gameid, type, param1 ) VALUES ($gameid, 3, $newid )";
  95. mysql_query($query) or die( array( 'status' => mysql_error()) );
  96. EndOfPlayerTurn($game->ActiveUser, $game, $updateList );
  97. }
  98. function UnitHasMoved($id, $x, $y){
  99. $query = "SELECT x, y FROM units WHERE id = $id";
  100. $result = mysql_query($query) or die( json_encode( array( 'status' =>mysql_error())));
  101. $row = mysql_fetch_row($result);
  102. if ($x == $row[0] && $y == $row[1]){
  103. return FALSE;
  104. }
  105. return TRUE;
  106. }
  107. function EndOfPlayerTurn($id,$game, &$updateList){
  108. $MyUnits = GetMyUnits($id,$game->ID);
  109. $s = GameDataService::GetInstance();
  110. $map = $s->GetGameMap( $game );
  111. Harvest($id,$game,$map,$MyUnits);
  112. mysql_data_seek($MyUnits, 0);
  113. ApplyAuras($game, $MyUnits, $updateList);
  114. $query = "UPDATE units SET moves = moverange, actionused = FALSE WHERE units.gameid = $game->ID AND ownerid = $game->ActiveUser";
  115. mysql_query($query) or die( json_encode( array( 'status' => mysql_error())));
  116. }
  117. function Harvest($id,$game,$map,$units){
  118. $dr=0;
  119. while ($row = mysql_fetch_array($units)){
  120. $t = $map->Cells[$row['x']][$row['y']]->Type;
  121. $tile = TileConfiguration::GetByID( $t );
  122. $unit = UnitConfiguration::GetByID($row['type']);
  123. $dr=$dr+$tile->GatherSpeed*$unit->GatherSpeed;
  124. }
  125. $query="UPDATE usergames SET resources = resources + $dr WHERE gameid = $game->ID AND userid = $id";
  126. mysql_query( $query ) or die( array( 'status' => mysql_error()) );
  127. }
  128. function ApplyAuras($game, $units, &$updateList){
  129. while ($row = mysql_fetch_array($units)){
  130. $unit = UnitConfiguration::GetById($row['type']);
  131. $ability = $unit->GetAbility($row['type'],'lifeaura');
  132. if ($ability)
  133. {
  134. $newParams = 'herp';
  135. $ability->Apply($game, $row, $newParams, $updateList);
  136. }
  137. }
  138. }
  139. ?>