1

usersなどの情報を保存するusers' table がありますpost_count。50 個までのバッジを取得したいと考えていますが、将来的にはそれ以上になる予定です。

だから、SOのように自動的にバッジを与えるのではなく、ウェブサイトのメンバーがバッジを取得できるページが必要です。そして、彼が「「Made 10 posts」バッジを取得する」のようなボタンをクリックすると、システムは、彼が 10 件の投稿を投稿し、まだこのバッジを持っていないかどうかをチェックし、問題がなければバッジを渡して、新しい投稿に挿入します。メンバーが 2 回取得できなかったバッジの ID と user_id を表にします。

しかし、私は非常に多くのバッジを持っているので、すべてのバッジをチェックするのにそんなに多くの if を入れる必要があるのでしょうか? これについてあなたの提案は何ですか?可能であれば、どうすればより最適化できますか?

ありがとうございました。

4

2 に答える 2

2

最適なのは、次のようなものです。

適切なユーザーIDで初期化するユーザー固有の属性/メトリックを返す関数を持つユーザー用のオブジェクトがあります(おそらく、これをいくつかの要素のシングルトン/静的にしたいでしょう...):

<?
class User {
 public function initUser($id) {
  /* initialise the user. maby load all metrics now, or if they 
  are intensive on demand when the functions are called.
  you can cache them in a class variable*/

 }
 public function getPostCount() {
  // return number of posts
 }
 public function getRegisterDate() {
  // return register date
 }
 public function getNumberOfLogins() {
  // return the number of logins the user has made over time
 }
}
?>

ID/キーで初期化され、データベースから依存関係をロードするバッジ オブジェクトがあります。

<?
class Badge {
 protected $dependencies = array();
 public function initBadge($id) {
  $this->loadDependencies($id);
 }
 protected function loadDependencies() {

  // load data from mysql and store it into dependencies like so:

  $dependencies = array(array(
   'value' => 300,
   'type' => 'PostCount',
   'compare => 'greater',
  ),...);
  $this->dependencies =  $dependencies;

 }
 public function getDependencies() {
  return $this->dependencies;
 }
}
?>

次に、バッチの付与を制御するクラスを作成し(ユーザー内で実行することもできます...)、依存関係をチェックし、失敗した依存関係を出力するなど...

  <?
        class BadgeAwarder {
         protected $badge = null;
         protected $user = null;

         public function awardBadge($userid,$badge) {
          if(is_null($this->badge))  {
           $this->badge = new Badge; // or something else for strange freaky badges, passed by $badge
}
           $this->badge->initBadge($badge);

          if(is_null($this->user))  {
           $this->user = new User;
           $this->user->initUser($userid);
          }
          $allowed = $this->checkDependencies();
          if($allowed === true) {
           // grant badge, print congratulations
          } else if(is_array($failed)) {
           // sorry, you failed tu full fill thef ollowing dependencies: print_r($failed);
          } else {
           echo "error?";
          }
         }
         protected function checkDependencies() {
          $failed = array();
          foreach($this->badge->getDependencies() as $depdency) {
           $value = call_user_func(array($this->badge, 'get'.$depdency['type']));
           if(!$this->compare($value,$depdency['value'],$dependency['compare'])) { 

            $failed[] = $dependency;
           }
          }
          if(count($failed) > 0) {
           return $failed;
          } else {
           return true;
          }
         }
    protected function compare($val1,$val2,$operator) {
     if($operator == 'greater') {
      return ($val1 > $val2);
    }
    }
        }
        ?>

奇妙な計算を必要とする非常にカスタムなバッチがある場合は、このクラスに拡張できます。

私があなたを正しい軌道に乗せたことを願っています。テストされておらず、おそらく構文エラーでいっぱいです。オブジェクト指向プログラミングの世界へようこそ。まだこれをしたいですか?

于 2010-09-24T15:59:48.233 に答える
0

たぶん、情報をテーブルに投げて、それと照合しますか?投稿数に基づいている場合は、とのフィールドがbadge_nameありpost_count、そのようにチェックしますか?

于 2010-09-24T14:50:37.830 に答える