Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "reward maker" ext #1194

Draft
wants to merge 13 commits into
base: develop
Choose a base branch
from
79 changes: 79 additions & 0 deletions app/Console/Commands/UpdateRewardMaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Console\Commands;

use App\Models\ObjectReward;
use App\Models\Prompt\PromptReward;
use DB;
use Illuminate\Console\Command;

class UpdateRewardMaker extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update-reward-maker';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update reward maker for develop';

/**
* Execute the console command.
*/
public function handle() {
dd(DB::table('prompt_rewards'));
// while this will convert prompt rewards to the new system
// the first half if for those who used the existing ext and have existing data

$rewards = ObjectReward::all();

foreach ($rewards as $reward) {
switch ($reward->object_type) {
case 'Questline':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely don't include questlines in this in the final PR

$objmodel = 'App\Models\Questline\Questline';
break;
case 'Prompt':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also dont need to do this at all and can instead use an object relation on the ObjectReward model

$objmodel = 'App\Models\Prompt\Prompt';
break;
}

switch ($reward->recipient_type) {
case 'User':
$key = 'objectRewards';
break;
case 'Character':
$key = 'objectCharacterRewards';
break;
}

$reward->update([
'rewardable_type' => getAssetModelString(strtolower($reward->rewardable_type)),
'object_type' => $objmodel,
'reward_key' => $key,
]);
}

foreach (PromptReward::all() as $promptreward) {
$rewardmodel = getAssetModelString(strtolower($promptreward->rewardable_type));

$newreward = ObjectReward::create([
'object_id' => $promptreward->prompt_id,
'object_type' => 'App\Models\Prompt\Prompt',
'rewardable_type' => $rewardmodel,
'rewardable_id' => $promptreward->rewardable_id,
'quantity' => $promptreward->quantity,
'recipient_type' => 'User',
'reward_key' => 'objectRewards',
]);

if (!$newreward) {
$this->error('Error. Skipping prompt reward for prompt: '.$promptreward->prompt->name);
}
}
}
}
18 changes: 10 additions & 8 deletions app/Helpers/AssetHelpers.php
Original file line number Diff line number Diff line change
@@ -89,31 +89,31 @@ function getAssetKeys($isCharacter = false) {
*/
function getAssetModelString($type, $namespaced = true) {
switch ($type) {
case 'items':
case 'items': case 'item':
if ($namespaced) {
return '\App\Models\Item\Item';
} else {
return 'Item';
}
break;

case 'currencies':
case 'currencies': case 'currency':
if ($namespaced) {
return '\App\Models\Currency\Currency';
} else {
return 'Currency';
}
break;

case 'raffle_tickets':
case 'raffle_tickets': case 'raffle':
if ($namespaced) {
return '\App\Models\Raffle\Raffle';
} else {
return 'Raffle';
}
break;

case 'loot_tables':
case 'loot_tables': case 'loottable':
if ($namespaced) {
return '\App\Models\Loot\LootTable';
} else {
@@ -171,11 +171,12 @@ function createAssetsArray($isCharacter = false) {
*
* @param array $first
* @param array $second
* @param mixed $isCharacter
*
* @return array
*/
function mergeAssetsArrays($first, $second) {
$keys = getAssetKeys();
function mergeAssetsArrays($first, $second, $isCharacter = false) {
$keys = getAssetKeys($isCharacter);
foreach ($keys as $key) {
foreach ($second[$key] as $item) {
addAsset($first, $item['asset'], $item['quantity']);
@@ -253,11 +254,12 @@ function getDataReadyAssets($array, $isCharacter = false) {
* Use the data attribute after json_decode()ing it.
*
* @param array $array
* @param mixed $isCharacter
*
* @return array
*/
function parseAssetData($array) {
$assets = createAssetsArray();
function parseAssetData($array, $isCharacter = false) {
$assets = createAssetsArray($isCharacter);
foreach ($array as $key => $contents) {
$model = getAssetModelString($key);
if ($model) {
16 changes: 16 additions & 0 deletions app/Helpers/Helpers.php
Original file line number Diff line number Diff line change
@@ -472,3 +472,19 @@ function faVersion() {

return asset($directory.'/'.$version.'.min.css');
}

/**
* Get the object's rewards.
*
* @param mixed $object
* @param mixed $key
* @param mixed $recipient
*/
function objectRewards($object, $key, $recipient) {
return App\Models\ObjectReward::where([
['object_type', get_class($object)],
['object_id', $object->id],
['recipient_type', $recipient],
['reward_key', $key],
])->get();
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/Data/PromptController.php
Original file line number Diff line number Diff line change
@@ -210,7 +210,7 @@ public function getEditPrompt($id) {
public function postCreateEditPrompt(Request $request, PromptService $service, $id = null) {
$id ? $request->validate(Prompt::$updateRules) : $request->validate(Prompt::$createRules);
$data = $request->only([
'name', 'prompt_category_id', 'summary', 'description', 'start_at', 'end_at', 'hide_before_start', 'hide_after_end', 'is_active', 'rewardable_type', 'rewardable_id', 'quantity', 'image', 'remove_image', 'prefix', 'hide_submissions', 'staff_only',
'name', 'prompt_category_id', 'summary', 'description', 'start_at', 'end_at', 'hide_before_start', 'hide_after_end', 'is_active', 'image', 'remove_image', 'prefix', 'hide_submissions', 'staff_only',
]);
if ($id && $service->updatePrompt(Prompt::find($id), $data, Auth::user())) {
flash('Prompt updated successfully.')->success();
49 changes: 49 additions & 0 deletions app/Http/Controllers/Admin/Data/RewardController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Controllers\Admin\Data;

use App\Http\Controllers\Controller;
use App\Services\RewardManager;
use Illuminate\Http\Request;

class RewardController extends Controller {
/*
|--------------------------------------------------------------------------
| Admin / Reward Maker Controller
|--------------------------------------------------------------------------
|
| Handles creation/editing of rewards
|
*/

/**
* Edit reward.
*
* @param int|null $id
* @param mixed $model
*
* @return \Illuminate\Http\RedirectResponse
*/
public function editReward(Request $request, RewardManager $service, $model, $id) {
$decodedmodel = urldecode(base64_decode($model));
// check model + id combo exists
$object = $decodedmodel::find($id);
if (!$object) {
throw new \Exception('Invalid object.');
}

$data = $request->only([
'rewardable_type', 'rewardable_id', 'reward_quantity', 'recipient_type', 'reward_key',
]);

if ($id && $service->editRewards($object, $data)) {
flash('Rewards updated successfully.')->success();
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}

return redirect()->back();
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/SubmissionController.php
Original file line number Diff line number Diff line change
@@ -152,7 +152,7 @@ public function getClaim($id) {
* @return \Illuminate\Http\RedirectResponse
*/
public function postSubmission(Request $request, SubmissionManager $service, $id, $action) {
$data = $request->only(['slug', 'character_rewardable_quantity', 'character_rewardable_id', 'character_rewardable_type', 'character_currency_id', 'rewardable_type', 'rewardable_id', 'quantity', 'staff_comments']);
$data = $request->only(['slug', 'character_rewardable_quantity', 'character_rewardable_id', 'character_rewardable_type', 'character_currency_id', 'rewardable_type', 'rewardable_id', 'quantity', 'staff_comments', 'character_is_focus']);
if ($action == 'reject' && $service->rejectSubmission($request->only(['staff_comments']) + ['id' => $id], Auth::user())) {
flash('Submission rejected successfully.')->success();
} elseif ($action == 'cancel' && $service->cancelSubmission($request->only(['staff_comments']) + ['id' => $id], Auth::user())) {
6 changes: 3 additions & 3 deletions app/Http/Controllers/Users/SubmissionController.php
Original file line number Diff line number Diff line change
@@ -212,7 +212,7 @@ public function postNewSubmission(Request $request, SubmissionManager $service,
$request->only([
'url', 'prompt_id', 'comments', 'slug', 'character_rewardable_type', 'character_rewardable_id', 'character_rewardable_quantity',
'rewardable_type', 'rewardable_id', 'quantity', 'stack_id', 'stack_quantity', 'currency_id', 'currency_quantity',
'gallery_submission_id',
'gallery_submission_id', 'character_is_focus',
]),
Auth::user(),
false,
@@ -257,13 +257,13 @@ public function postEditSubmission(Request $request, SubmissionManager $service,
if ($submit && $service->editSubmission($submission, $request->only([
'url', 'prompt_id', 'comments', 'slug', 'character_rewardable_type', 'character_rewardable_id', 'character_rewardable_quantity',
'rewardable_type', 'rewardable_id', 'quantity', 'stack_id', 'stack_quantity', 'currency_id', 'currency_quantity',
'gallery_submission_id',
'gallery_submission_id', 'character_is_focus',
]), Auth::user(), false, $submit)) {
flash('Draft submitted successfully.')->success();
} elseif ($service->editSubmission($submission, $request->only([
'url', 'prompt_id', 'comments', 'slug', 'character_rewardable_type', 'character_rewardable_id', 'character_rewardable_quantity',
'rewardable_type', 'rewardable_id', 'quantity', 'stack_id', 'stack_quantity', 'currency_id', 'currency_quantity',
'gallery_submission_id',
'gallery_submission_id', 'character_is_focus',
]), Auth::user())) {
flash('Draft saved successfully.')->success();

46 changes: 46 additions & 0 deletions app/Models/ObjectReward.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of having an object reward model why not just have the stored assets array?

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Models;

class ObjectReward extends Model {
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'object_id', 'object_type', 'rewardable_id', 'rewardable_type', 'quantity', 'recipient_type', 'reward_key',
];

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'object_rewards';

/**********************************************************************************************
RELATIONS
**********************************************************************************************/

/**
* Get the object.
*/
public function object() {
return $this->morphTo(__FUNCTION__, 'object_type', 'object_id');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the purpose of the __FUNCTION__ ?

}

/**
* Get the reward attached to the prompt reward.
*/
public function reward() {
return $this->morphTo(__FUNCTION__, 'rewardable_type', 'rewardable_id');
}

/**
* Get the reward type so we don't have to do the no-no of model names in forms.
*/
public function rewardType() {
return class_basename($this->rewardable_type);
}
}
19 changes: 17 additions & 2 deletions app/Models/Prompt/Prompt.php
Original file line number Diff line number Diff line change
@@ -78,8 +78,23 @@ public function category() {
/**
* Get the rewards attached to this prompt.
*/
public function rewards() {
return $this->hasMany(PromptReward::class, 'prompt_id');
public function objectRewards() {
return $this->hasMany('App\Models\ObjectReward', 'object_id')->where([
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally this would use the ::class

['object_type', get_class($this)],
['recipient_type', 'User'],
['reward_key', 'objectRewards'],
]);
}

/**
* Get the rewards attached to this prompt.
*/
public function objectCharacterRewards() {
return $this->hasMany('App\Models\ObjectReward', 'object_id')->where([
['object_type', get_class($this)],
['recipient_type', 'Character'],
['reward_key', 'objectCharacterRewards'],
]);
}

/**********************************************************************************************
2 changes: 1 addition & 1 deletion app/Models/Submission/SubmissionCharacter.php
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ class SubmissionCharacter extends Model {
* @var array
*/
protected $fillable = [
'submission_id', 'character_id', 'data',
'submission_id', 'character_id', 'data', 'is_focus',
];

/**
Loading