예제 #1
0
 public static function process()
 {
     theme_features::check_referer();
     theme_features::check_nonce();
     $output = [];
     $type = isset($_REQUEST['type']) && is_string($_REQUEST['type']) ? $_REQUEST['type'] : null;
     $target_id = isset($_REQUEST['target']) && is_numeric($_REQUEST['target']) ? $_REQUEST['target'] : null;
     switch ($type) {
         case 'get-target':
             /**
              * check login
              */
             $current_user_id = self::check_login();
             /**
              * check times
              */
             self::check_max_times();
             /**
              * get target
              */
             $target = self::check_target($target_id);
             $output = ['status' => 'success', 'points' => theme_custom_point::get_point($target_id), 'avatar' => theme_cache::get_avatar_url($target_id), 'name' => esc_html($target->display_name), 'msg' => ___('Target locked, bomb is ready.')];
             die(theme_features::json_format($output));
             /**
              * bomb
              */
         /**
          * bomb
          */
         case 'bomb':
             /**
              * check login
              */
             $current_user_id = self::check_login();
             /**
              * check times
              */
             self::check_max_times();
             /**
              * get target
              */
             $target = self::check_target($target_id);
             /**
              * check points
              */
             $points = isset($_REQUEST['points']) && is_numeric($_REQUEST['points']) ? $_REQUEST['points'] : null;
             if (!$points || !in_array($points, self::get_point_values())) {
                 die(theme_features::json_format(['status' => 'error', 'code' => 'invaild_point_value', 'msg' => ___('Sorry, the point value is invaild.'), 'points' => self::get_point_values()]));
             }
             /**
              * check target points
              */
             $target_points = theme_custom_point::get_point($target_id);
             if ($points > $target_points) {
                 die(theme_features::json_format(['status' => 'error', 'code' => 'target_points_not_enough', 'msg' => sprintf(___('Sorry, the target %s is not enough to bear your bomb.'), theme_custom_point::get_point_name())]));
             }
             /**
              * check attacker points
              */
             $attacker_id = theme_cache::get_current_user_id();
             $attacker_points = theme_custom_point::get_point($attacker_id);
             if ($points > $attacker_points) {
                 die(theme_features::json_format(['status' => 'error', 'code' => 'attacker_points_not_enough', 'msg' => sprintf(___('Sorry, your %s is not enough to bomb target.'), theme_custom_point::get_point_name())]));
             }
             /**
              * pass 
              */
             $says = isset($_REQUEST['says']) && is_string($_REQUEST['says']) ? mb_substr($_REQUEST['says'], 0, 30) : false;
             /**
              * define $hit
              */
             $hit = false;
             if (mt_rand(0, 100) <= self::get_victory_percent()) {
                 $hit = true;
             }
             /**
              * define data
              */
             $data = ['attacker-id' => $current_user_id, 'target-id' => $target_id, 'says' => $says, 'points' => $points, 'hit' => $hit];
             /** add history for target */
             self::add_history_for_target($data);
             /** add history for attacker */
             self::add_history_for_attacker($data);
             //self::add_noti_for_target($current_user_id,$target_id,$points,$hit);
             /**
              * new target points
              */
             $target_extra_points = self::get_extra_points_for_target($hit, $points);
             $new_target_points = $target_points + $target_extra_points;
             /**
              * new attacker points
              */
             $attacker_extra_points = self::get_extra_points_for_attacker($hit, $points);
             $new_attacker_points = $attacker_points + $attacker_extra_points;
             /** update attacker points */
             theme_custom_point::update_user_points($attacker_id, $new_attacker_points);
             /** update target points */
             theme_custom_point::update_user_points($target_id, $new_target_points);
             $target_name = '<a href="' . theme_cache::get_author_posts_url($target_id) . '" target="_blank" class="author">' . esc_html($target->display_name) . '</a>';
             /**
              * hit target
              */
             if ($hit) {
                 $output['msg'] = sprintf(___('Bombing successfully! Your bomb hit %1$s, you got %2$s %3$s. Target remaining %4$s %3$s.'), $target_name, '<strong class="plus">+' . $attacker_extra_points . '</strong>', theme_custom_point::get_point_name(), $new_target_points);
                 /**
                  * miss target
                  */
             } else {
                 $output['msg'] = sprintf(___('Unlucky! %1$s miss your attack, you lost %2$s %3$s and remaining %4$s %3$s.'), $target_name, '<strong class="mins">' . $attacker_extra_points . '</strong>', theme_custom_point::get_point_name(), $new_attacker_points);
             }
             $output['hit'] = $hit;
             $output['status'] = 'success';
             /**
              * set times
              */
             self::set_times(self::get_times() + 1);
             die(theme_features::json_format($output));
         default:
             die(theme_features::json_format(['status' => 'error', 'code' => 'invaild_type_param', 'msg' => ___('Sorry, type param is invaild.')]));
     }
 }
 public static function action_update_postmeta($meta_id, $object_id, $meta_key, $meta_value)
 {
     if ($meta_key !== self::$post_meta_key) {
         return;
     }
     /** only run once */
     static $i = 0;
     if ($i !== 0) {
         return;
     }
     ++$i;
     if (self::is_max_times($object_id)) {
         $user_id = theme_cache::get_post($object_id)->post_author;
         $new_point = theme_custom_point::get_point($user_id) + self::get_points_value();
         /** update user point */
         theme_custom_point::update_user_points($user_id, $new_point);
         /** add history */
         self::add_history_views_per_hundred($object_id);
         /** add noti */
         //self::add_noti_views_per_hundred($object_id);
         /** reset times */
         self::reset_times($object_id);
         return;
     }
     /**
      * if new post, reset times
      */
     if (self::get_times($object_id) == 0) {
         self::reset_times($object_id);
     }
 }