使用 PHP 和 MYSQL 实现非自动徽章

发布于 2024-09-24 15:46:31 字数 321 浏览 0 评论 0原文

我有用户表 users,其中存储 post_count 等信息。我想要拥有大约 50 个徽章,将来会更多。

所以,我想要一个页面,网站成员可以在其中获取徽章,而不是像SO那样自动给他徽章。当他点击一个名为“取得‘发表 10 个帖子’徽章”的按钮后,系统会检查他是否已发布 10 个帖子并且还没有此徽章,如果可以,则给他徽章并插入到新的帖子中表徽章的 id 和 user_id 该会员不能接受两次。

但我有这么多徽章,所以我真的需要放置这么多“如果”来检查所有徽章吗?您对此有何建议?如果可能的话,我怎样才能使其更加优化?

谢谢。

I have users' table users, where I store information like post_count and so on. I want to have ~50 badges and it is going to be even more than that in future.

So, I want to have a page where member of website could go and take the badge, not automatically give him it like in SO. And after he clicks a button called smth like "Take 'Made 10 posts' badge" the system checks if he has posted 10 posts and doesn't have this badge already, and if it's ok, give him the badge and insert into the new table the badge's id and user_id that member couldn't take it twice.

But I have so many badges, so do I really need to put so many if's to check for all badges? What would be your suggestion on this? How can I make it more optimal if it's even possible?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2024-10-01 15:46:31

,最佳方案如下:

为用户提供一个对象,其函数返回用户特定的属性/指标,您使用正确的用户 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/key 初始化并从数据库加载依赖项的徽章对象:

<?
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);
    }
    }
        }
        ?>

如果您有需要奇怪计算的非常自定义的批次,您可以扩展到此类。

希望我带你走上正轨。
未经测试并且可能充满语法错误。
欢迎来到面向对象编程的世界。还想做这个吗?

optimal would be IMHO the the following:

have an object for the user with functions that return user specific attributes/metrics that you initialise with the proper user id (you probably wanna make this a singleton/static for some elements...):

<?
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
 }
}
?>

have a badge object that is initialised with an id/key and loads dependencies from your database:

<?
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;
 }
}
?>

then you could have a class that controls the awarding of batches (you can also do it inside user...)
and checks dependencies and prints failed dependencies etc...

  <?
        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);
    }
    }
        }
        ?>

you can extend to this class if you have very custom batches that require weird calculations.

hope i brought you on the right track.
untested andp robably full of syntax errors.
welcome to the world of object oriented programming. still wanna do this?

断桥再见 2024-10-01 15:46:31

也许可以将信息放入表格中并进行检查?如果它是基于帖子数量,则有 badge_namepost_count 字段并以这种方式检查?

Maybe throw the information into a table and check against that? If it's based on the number of posts, have fields for badge_name and post_count and check that way?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文