move Achievement check to AchievementComponent
This commit is contained in:
parent
a8c97c6d8b
commit
db60917439
2 changed files with 180 additions and 151 deletions
|
|
@ -100,8 +100,8 @@
|
||||||
$this->checkPermission($request, $response);
|
$this->checkPermission($request, $response);
|
||||||
|
|
||||||
// Check achievements
|
// Check achievements
|
||||||
$this->checkAchievements($request, $response, 'date');
|
$this->checkAchievements($request, 'date');
|
||||||
$this->checkAchievements($request, $response, 'achievement');
|
$this->checkAchievements($request, 'achievement');
|
||||||
|
|
||||||
// Set Seminary and Character data
|
// Set Seminary and Character data
|
||||||
$this->set('loggedSeminary', self::$seminary);
|
$this->set('loggedSeminary', self::$seminary);
|
||||||
|
|
@ -159,13 +159,12 @@
|
||||||
* Check for newly achieved Achievements.
|
* Check for newly achieved Achievements.
|
||||||
*
|
*
|
||||||
* @param \nre\core\Request $request Current request
|
* @param \nre\core\Request $request Current request
|
||||||
* @param \nre\core\Response $response Current response
|
|
||||||
* @param array $checkConditions Conditions to check
|
* @param array $checkConditions Conditions to check
|
||||||
*/
|
*/
|
||||||
protected function checkAchievements(\nre\core\Request $request, \nre\core\Response $response, $checkConditions=null)
|
protected function checkAchievements(\nre\core\Request $request, $checkConditions=null)
|
||||||
{
|
{
|
||||||
// Do not check MediaController
|
// Do not check MediaController
|
||||||
if($this->request->getParam(0, 'toplevel') != \nre\configs\AppConfig::$defaults['toplevel']) {
|
if($request->getParam(0, 'toplevel') != \nre\configs\AppConfig::$defaults['toplevel']) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -174,145 +173,12 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set conditions to check
|
// Check Achievements
|
||||||
if(!is_null($checkConditions) && !is_array($checkConditions)) {
|
$achievements = $this->Achievement->checkAchievements(self::$seminary['id'], self::$character['id'], $checkConditions);
|
||||||
$checkConditions = array($checkConditions);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get unachieved Achievments
|
// Add notifications
|
||||||
$achievements = $this->Achievements->getUnachhievedAchievementsForCharacter(self::$seminary['id'], self::$character['id']);
|
|
||||||
if(in_array('user', self::$character['characterroles'])) {
|
|
||||||
$achievements = array_merge($achievements, $this->Achievements->getUnachievedOnlyOnceAchievementsForSeminary(self::$seminary['id']));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check conditions
|
|
||||||
foreach($achievements as &$achievement)
|
foreach($achievements as &$achievement)
|
||||||
{
|
{
|
||||||
// Check condition to test
|
|
||||||
if(!is_null($checkConditions) && !in_array($achievement['condition'], $checkConditions)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check deadline
|
|
||||||
if(!is_null($achievement['deadline']) && $achievement['deadline'] < date('Y-m-d H:i:s')) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get conditions
|
|
||||||
$conditions = array();
|
|
||||||
$progress = 0;
|
|
||||||
switch($achievement['condition'])
|
|
||||||
{
|
|
||||||
// Date conditions
|
|
||||||
case 'date':
|
|
||||||
$conditionsDate = $this->Achievements->getAchievementConditionsDate($achievement['id']);
|
|
||||||
foreach($conditionsDate as &$condition)
|
|
||||||
{
|
|
||||||
$conditions[] = array(
|
|
||||||
'func' => 'checkAchievementConditionDate',
|
|
||||||
'params' => array(
|
|
||||||
$condition['select']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
// Character conditions
|
|
||||||
case 'character':
|
|
||||||
$conditionsCharacter = $this->Achievements->getAchievementConditionsCharacter($achievement['id']);
|
|
||||||
foreach($conditionsCharacter as &$condition)
|
|
||||||
{
|
|
||||||
$conditions[] = array(
|
|
||||||
'func' => 'checkAchievementConditionCharacter',
|
|
||||||
'params' => array(
|
|
||||||
$condition['field'],
|
|
||||||
$condition['value'],
|
|
||||||
self::$character['id']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
// Quest conditions
|
|
||||||
case 'quest':
|
|
||||||
$conditionsQuest = $this->Achievements->getAchievementConditionsQuest($achievement['id']);
|
|
||||||
foreach($conditionsQuest as &$condition)
|
|
||||||
{
|
|
||||||
$conditions[] = array(
|
|
||||||
'func' => 'checkAchievementConditionQuest',
|
|
||||||
'params' => array(
|
|
||||||
$condition['field'],
|
|
||||||
$condition['count'],
|
|
||||||
$condition['value'],
|
|
||||||
$condition['status'],
|
|
||||||
$condition['groupby'],
|
|
||||||
$condition['quest_id'],
|
|
||||||
self::$character['id']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
// Achievement conditions
|
|
||||||
case 'achievement':
|
|
||||||
$conditionsAchievement = $this->Achievements->getAchievementConditionsAchievement($achievement['id']);
|
|
||||||
foreach($conditionsAchievement as &$condition)
|
|
||||||
{
|
|
||||||
$conditions[] = array(
|
|
||||||
'func' => 'checkAchievementConditionAchievement',
|
|
||||||
'params' => array(
|
|
||||||
$condition['field'],
|
|
||||||
$condition['count'],
|
|
||||||
$condition['value'],
|
|
||||||
$condition['groupby'],
|
|
||||||
$condition['meta_achievement_id'],
|
|
||||||
self::$character['id']
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not achieve Achievements without conditions
|
|
||||||
if(empty($conditions)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check conditions
|
|
||||||
$achieved = ($achievement['all_conditions'] == 1);
|
|
||||||
foreach($conditions as &$condition)
|
|
||||||
{
|
|
||||||
// Calculate result of condition
|
|
||||||
$result = call_user_func_array(
|
|
||||||
array(
|
|
||||||
$this->Achievements,
|
|
||||||
$condition['func']
|
|
||||||
),
|
|
||||||
$condition['params']
|
|
||||||
);
|
|
||||||
|
|
||||||
// The overall result and abort if possible
|
|
||||||
if($achievement['all_conditions'])
|
|
||||||
{
|
|
||||||
if(!$result) {
|
|
||||||
$achieved = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if($result) {
|
|
||||||
$achieved = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Achievement achieved
|
|
||||||
if($achieved)
|
|
||||||
{
|
|
||||||
// Set status
|
|
||||||
$this->Achievements->setAchievementAchieved($achievement['id'], self::$character['id']);
|
|
||||||
|
|
||||||
// Add notification
|
|
||||||
$this->Notification->addNotification(
|
$this->Notification->addNotification(
|
||||||
\hhu\z\controllers\components\NotificationComponent::TYPE_ACHIEVEMENT,
|
\hhu\z\controllers\components\NotificationComponent::TYPE_ACHIEVEMENT,
|
||||||
$achievement['title'],
|
$achievement['title'],
|
||||||
|
|
@ -321,7 +187,6 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $models = array('achievements');
|
public $models = array('achievements', 'characterroles');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -34,6 +34,170 @@
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check for newly achieved Achievements.
|
||||||
|
*
|
||||||
|
* @param int $seminaryId ID of Seminary to check Achievements for
|
||||||
|
* @param int $characterId ID of Character to check Achievements for
|
||||||
|
* @param array $checkConditions Conditions to check
|
||||||
|
* @return array List of newly achieved achievements
|
||||||
|
*/
|
||||||
|
public function checkAchievements($seminaryId, $characterId, $checkConditions=null)
|
||||||
|
{
|
||||||
|
// Set conditions to check
|
||||||
|
if(!is_null($checkConditions) && !is_array($checkConditions)) {
|
||||||
|
$checkConditions = array($checkConditions);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get unachieved Achievments
|
||||||
|
$achievements = $this->Achievements->getUnachhievedAchievementsForCharacter($seminaryId, $characterId);
|
||||||
|
// Merge “only-once” Achievements
|
||||||
|
if(in_array('user', $this->Characterroles->getCharacterrolesForCharacterById($characterId))) {
|
||||||
|
$achievements = array_merge($achievements, $this->Achievements->getUnachievedOnlyOnceAchievementsForSeminary($seminaryId));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check conditions
|
||||||
|
$achievedAchievements = array();
|
||||||
|
foreach($achievements as &$achievement)
|
||||||
|
{
|
||||||
|
// Check condition to test
|
||||||
|
if(!is_null($checkConditions) && !in_array($achievement['condition'], $checkConditions)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check deadline
|
||||||
|
if(!is_null($achievement['deadline']) && $achievement['deadline'] < date('Y-m-d H:i:s')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get conditions
|
||||||
|
$conditions = array();
|
||||||
|
$progress = 0;
|
||||||
|
switch($achievement['condition'])
|
||||||
|
{
|
||||||
|
// Date conditions
|
||||||
|
case 'date':
|
||||||
|
$conditionsDate = $this->Achievements->getAchievementConditionsDate($achievement['id']);
|
||||||
|
foreach($conditionsDate as &$condition)
|
||||||
|
{
|
||||||
|
$conditions[] = array(
|
||||||
|
'func' => 'checkAchievementConditionDate',
|
||||||
|
'params' => array(
|
||||||
|
$condition['select']
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// Character conditions
|
||||||
|
case 'character':
|
||||||
|
$conditionsCharacter = $this->Achievements->getAchievementConditionsCharacter($achievement['id']);
|
||||||
|
foreach($conditionsCharacter as &$condition)
|
||||||
|
{
|
||||||
|
$conditions[] = array(
|
||||||
|
'func' => 'checkAchievementConditionCharacter',
|
||||||
|
'params' => array(
|
||||||
|
$condition['field'],
|
||||||
|
$condition['value'],
|
||||||
|
$characterId
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// Quest conditions
|
||||||
|
case 'quest':
|
||||||
|
$conditionsQuest = $this->Achievements->getAchievementConditionsQuest($achievement['id']);
|
||||||
|
foreach($conditionsQuest as &$condition)
|
||||||
|
{
|
||||||
|
$conditions[] = array(
|
||||||
|
'func' => 'checkAchievementConditionQuest',
|
||||||
|
'params' => array(
|
||||||
|
$condition['field'],
|
||||||
|
$condition['count'],
|
||||||
|
$condition['value'],
|
||||||
|
$condition['status'],
|
||||||
|
$condition['groupby'],
|
||||||
|
$condition['quest_id'],
|
||||||
|
$characterId
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
// Achievement conditions
|
||||||
|
case 'achievement':
|
||||||
|
$conditionsAchievement = $this->Achievements->getAchievementConditionsAchievement($achievement['id']);
|
||||||
|
foreach($conditionsAchievement as &$condition)
|
||||||
|
{
|
||||||
|
$conditions[] = array(
|
||||||
|
'func' => 'checkAchievementConditionAchievement',
|
||||||
|
'params' => array(
|
||||||
|
$condition['field'],
|
||||||
|
$condition['count'],
|
||||||
|
$condition['value'],
|
||||||
|
$condition['groupby'],
|
||||||
|
$condition['meta_achievement_id'],
|
||||||
|
$characterId
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not achieve Achievements without conditions
|
||||||
|
if(empty($conditions)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check conditions
|
||||||
|
$achieved = ($achievement['all_conditions'] == 1);
|
||||||
|
foreach($conditions as &$condition)
|
||||||
|
{
|
||||||
|
// Calculate result of condition
|
||||||
|
$result = call_user_func_array(
|
||||||
|
array(
|
||||||
|
$this->Achievements,
|
||||||
|
$condition['func']
|
||||||
|
),
|
||||||
|
$condition['params']
|
||||||
|
);
|
||||||
|
|
||||||
|
// The overall result and abort if possible
|
||||||
|
if($achievement['all_conditions'])
|
||||||
|
{
|
||||||
|
if(!$result) {
|
||||||
|
$achieved = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if($result) {
|
||||||
|
$achieved = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Achievement achieved
|
||||||
|
if($achieved)
|
||||||
|
{
|
||||||
|
// Set status
|
||||||
|
$this->Achievements->setAchievementAchieved($achievement['id'], $characterId);
|
||||||
|
|
||||||
|
// Add to list
|
||||||
|
$achievedAchievements[] = $achievement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Return newly achieved Achievements
|
||||||
|
return $achievedAchievements;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue