PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/controller/ajax.php

https://bitbucket.org/alvinpd/monsterninja
PHP | 138 lines | 94 code | 20 blank | 24 comment | 28 complexity | dd92b97172011374400467e507d1a57f MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * @file ajax.php
  4. * @brief Ajax request handler for the Monster Ninja application.
  5. *
  6. * @package Monsterninja
  7. * @category Controller
  8. * @author Alvin Difuntorum <alvinpd09@gmail.com>
  9. * @copyright (c) 2010 Monster Ninja Games. All rights reserved.
  10. */
  11. class Controller_Ajax extends Controller_Base
  12. {
  13. public function before()
  14. {
  15. parent::before();
  16. $this->auto_render = false;
  17. }
  18. public function action_score()
  19. {
  20. // This must be an ajax request
  21. if ( !Request::$is_ajax )
  22. {
  23. return;
  24. }
  25. // A user must be already logged in to bookmark games
  26. if ( !Auth::instance()->logged_in() )
  27. {
  28. return;
  29. }
  30. $leaderboard = ORM::factory('leaderboard');
  31. $post = $leaderboard->validate_board($_POST);
  32. if ( $post->check() ) {
  33. // Add the new leadearboard in the leaderboards table if it does not exist yet.
  34. $ld = ORM::factory('leaderboard')
  35. ->where('boardID','=',$post['boardID'])
  36. ->and_where('gameID','=',$post['gameID'])
  37. ->find();
  38. // Save the new leaderboard data if it does not exist
  39. if ( $ld->id == 0 AND $post['submission'] == 'score' ) {
  40. $ld->values($post);
  41. $ld->save();
  42. }
  43. // Save the score data for this game
  44. $score = null;
  45. if ( $post['submission'] == 'score' ) {
  46. $score = ORM::factory('achievement')
  47. ->where('boardID','=',$post['boardID'])
  48. ->and_where('userID','=',$post['userID'])->find();
  49. }
  50. elseif ( $post['submission'] == 'medal' ) {
  51. $score = ORM::factory('achievement')
  52. ->where('gameID','=',$post['gameID'])
  53. ->and_where('userID','=',$post['userID'])->find();
  54. }
  55. if ( $score == null ) {
  56. return;
  57. }
  58. // Determine if a record is already available in the database
  59. if ( $score->id == 0 ) {
  60. $score->plays = 0;
  61. $score->thumbnail_bronze = '';
  62. $score->thumbnail_gold = '';
  63. $score->thumbnail_silver = '';
  64. $score->values($post);
  65. }
  66. // Save the score submission for this game and user
  67. if ( $post['submission'] == 'score' ) {
  68. $score->plays++;
  69. if ( $post['sortOrder'] == 'asc' ) {
  70. // The lower score the better
  71. if ( $post['score'] < $score->score ) {
  72. $score->score = $post['score'];
  73. }
  74. }
  75. elseif ( $post['sortOrder'] == 'desc' ) {
  76. // The higher score the better
  77. if ( $post['score'] > $score->score ) {
  78. $score->score = $post['score'];
  79. }
  80. }
  81. }
  82. // Save the medal submission for this game and user
  83. elseif ( $post['submission'] == 'medal' ) {
  84. if ( stristr($post['name'], 'gold') != FALSE ) {
  85. $score->medal_gold ++;
  86. $score->thumbnail_gold = $post['thumbnail'];
  87. }
  88. elseif ( stristr($post['name'], 'silver') != FALSE ) {
  89. $score->medal_silver ++;
  90. $score->thumbnail_silver = $post['thumbnail'];
  91. }
  92. elseif ( stristr($post['name'], 'bronze') != FALSE ) {
  93. $score->medal_bronze ++;
  94. $score->thumbnail_bronze = $post['thumbnail'];
  95. }
  96. }
  97. else {
  98. /* Nothing to do. */
  99. }
  100. // Save achievement of the user
  101. $score->save();
  102. }
  103. }
  104. public function action_bookmark()
  105. {
  106. // This must be an ajax request
  107. if ( !Request::$is_ajax )
  108. {
  109. return;
  110. }
  111. // A user must be already logged in to bookmark games
  112. if ( !Auth::instance()->logged_in() )
  113. {
  114. return;
  115. }
  116. }
  117. };
  118. // End of file