質問

ユーザーのテーブルがあります users, 、私がのような情報を保存する場所 post_count 等々。私は約50のバッジを持ちたいと思っていますが、それは将来それ以上になるでしょう。

だから、私はウェブサイトのメンバーがバッジに行くことができるページを持ちたいと思っています。そして、彼が「Take 'Made 10 Posts' Badge」のようなSMTHというボタンをクリックした後、システムは10の投稿を投稿していないかどうかをチェックします。メンバーが2回取ることができなかったバッジのIDとuser_idをテーブル。

しかし、私は非常に多くのバッジを持っているので、すべてのバッジをチェックする必要がある場合は本当に多くのものを置く必要がありますか?これについてのあなたの提案は何でしょうか?可能であれば、どうすれば最適にすることができますか?

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

役に立ちましたか?

解決

最適なのは次のことです。

適切なユーザー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);
    }
    }
        }
        ?>

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

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

他のヒント

たぶん情報をテーブルに投げ込み、それに対してチェックしますか?投稿の数に基づいている場合は、 badge_namepost_count そして、そのようにチェックしますか?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top