PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/validation.php

https://bitbucket.org/mikel3377/webgame
PHP | 61 lines | 47 code | 12 blank | 2 comment | 9 complexity | 76b46862050314d65aa0295d5a33061a MD5 | raw file
  1. <?php
  2. function ValidateMove($userid, $unitrow, $game, $x, $y){
  3. // TODO : Lots more validation for moves
  4. if( ! ValidateUnitOwner($userid, $unitrow))
  5. {
  6. die(json_encode( array( 'status' => 'That\'s not your unit!' ) ));
  7. return false;
  8. }
  9. if( ! ValidateTurn($userid, $game->ID))
  10. {
  11. die(json_encode( array( 'status' => 'It\'s not your turn!') ));
  12. return false;
  13. }
  14. if( ! ValidateUnitHasMoves($unitrow))
  15. {
  16. die(json_encode( array( 'status' => 'Unit has no moves.') ));
  17. return false;
  18. }
  19. return true;
  20. }
  21. function ValidateAttack($userid, $unitid, $gameid, $targ){
  22. //TODO: Lots more validation for attacks
  23. return true;
  24. }
  25. function ValidateUnitOwner($userid, $unitrow)
  26. {
  27. if ($unitrow['ownerid'] == $userid){
  28. return TRUE;
  29. }
  30. return FALSE;
  31. }
  32. function ValidateTurn($userid, $gameid)
  33. {
  34. $query = "SELECT activeuser FROM games WHERE id = $gameid";
  35. $result = mysql_query($query) or die( json_encode( array( 'status' =>mysql_error())));
  36. $row = mysql_fetch_row($result);
  37. if ($row && $userid == $row[0]){
  38. return TRUE;
  39. }
  40. return FALSE;
  41. }
  42. function ValidateUnitHasMoves($unitrow)
  43. {
  44. if ($unitrow['moves']>0){
  45. return TRUE;
  46. }
  47. return FALSE;
  48. }
  49. ?>