tiagogouvea / phpgamification
PHPGamification 是一个通用的游戏化 PHP 框架,声称它简单且客观。
Requires
- php: >=5.0.0
This package is not auto-updated.
Last update: 2024-09-28 17:43:54 UTC
README
PHPGamification 是一个 通用的游戏化 PHP 框架,声称它简单且客观。
特性
- 快速将完整的游戏化引擎集成到您的项目中
- 处理 积分、等级 和 徽章
- 访问存储的用户 警报 和 日志 以轻松理解用户得分
- 使用您自己的 用户数据库,无需更改您的表结构
- 创建回调以在用户获得积分或徽章时使用
示例代码
/** Instantiate **/ $gamification = new PHPGamification::getInstance(); $gamification->setDAO(new DAO('my_host', 'my_databse', 'my_user', 'my_pass')); /** Badges definitions */ $gamification->addBadge('newbee', 'Newbee', 'You logged in, congratulations!'); $gamification->addBadge('addict', 'Addict', 'You have logged in 10 times'); $gamification->addBadge('professional_writer', 'Professional Writer', 'You must write a book! 50 posts!!'); /** Levels definitions */ $gamification->addLevel(0, 'No Star'); $gamification->addLevel(1000, 'Five stars', 'grant_five_stars_badge');// Execute event: grant_five_stars_badge $gamification->addLevel(2000, '2K points!'); /** Events definitions */ // Welcome to our network! (disallow reachRequiredRepetitions) $event = new Event(); $event->setAlias('join_network') ->setEachPointsGranted(10) ->setAllowRepetitions(false); // Just one time $gamification->addEvent($event); // Each Login/Logged in 10 times (25 points each time, 50 points when reach 10 times) $event = new Event(); $event->setAlias('login') ->setEachPointsGranted(25) ->setEachBadgeGranted($gamification->getBadgeByAlias('newbee')) ->setReachRequiredRepetitions(10) ->setReachPointsGranted(50) ->setReachBadgeGranted($gamification->getBadgeByAlias('addict')); $gamification->addEvent($event); // Each post to blog/You wrote 5 post to your blog (100 points each + badge, 1000 points reach) $event = new Event(); $event->setAlias('post_to_blog') ->setEachPointsGranted(150) ->setEachCallback("MyOtherClass::myPostToBlogCallBackFunction") ->setReachRequiredRepetitions(50) ->setReachBadgeGranted($gamification->getBadgeByAlias('professional_writer')); $gamification->addEvent($event); /** Using it */ $gamification->setUserId(1); $gamification->executeEvent('join_network'); $gamification->executeEvent('login'); for ($i=0; $i<9; $i++) $gamification->executeEvent('login'); $gamification->executeEvent('post_to_blog', array('YourPostId'=>11)); /** Getting user data */ echo "<pre>"; var_dump($gamification->getUserAllData()); echo "</pre>"; /** Getting users ranking */ echo "<pre>"; var_dump($gamification->getUsersPointsRanking(); echo "</pre>";
安装
克隆、下载或将它添加到您的 composer.json 文件中
{ "require": { "tiagogouvea/phpgamification": "*" } }
或者调用
composer require tiagogouvea/phpgamification
使用
在 /sample/ 文件夹中,您可以查看简单的直观代码。这是您开始使用 PHPGamification 的起点。
设置您的游戏化规则
获取 PHPGamification 引擎以构建对象
$gamification = new PHPGamification::getInstance(); $gamification->setDAO(new DAO('my_host', 'my_databse', 'my_user', 'my_pass'));
您可以为您的 DAO 设置自己的设置:实现 DAOInterface 并使用 $gamification->setDAO(); 设置实例。
等级和徽章
只需告诉您的游戏中有哪些等级和徽章,就像在示例文件中一样。
事件
事件可能只发生一次或多次。创建事件时,您可以设置在每次发生时或用户达到所需重复次数时授予积分和徽章。
-
每次:每次调用事件时发生
-
达到:仅在用户达到所需重复次数时发生
-
您还可以使用 setAllowRepetitions(false) 来告诉事件只能由用户调用一次。
回调
使用回调方法来改进用户与您的游戏化系统的交互或验证用户是否真的需要获得积分/徽章。回调可以在两个时刻调用
- 每个回调:每次执行事件时都会运行
- 达到回调:仅在用户达到所需事件重复次数时运行
在调用您的回调方法时,请记住返回 true 以继续事件,或返回 false 以阻止事件授予积分或徽章。这可以用于在给予用户积分或徽章之前在您的业务逻辑中验证一些数据。
运行您的游戏化引擎
设置正在处理的 用户 Id
$gamification->setUserId($yourUserId);
每次您想在游戏化环境中发生某些事情时,都必须调用 一个事件
$gamification->executeEvent('login',array('more_data'=>'to_your_callback'));
您可以通过调用 getUserScores()、getUserBadges()、showUserLog() 和 getUserEvents() 或 $gamification->getUserAllData() 来检索有关某些用户的任何信息,或一起返回所有信息
var_dump($gamification->getUserScores());
保持用户的参与度
利用事件回调在用户获得新徽章或升级时向用户发送电子邮件,例如。
待办事项
如果您想合作,请创建一个分支并提交您的 pull requests!
- 在您征服新等级时允许回调(将 ReachCallback 移动到徽章和等级?)
- 修复自动加载以仅与 composer 一起使用
- 创建一个 iframe 调用来让人们在自己的博客上显示他们的积分和徽章
联系
Tiago Gouvêa