Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions app/Console/Commands/UpdateRewardMaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace App\Console\Commands;

use App\Models\ObjectReward;
use DB;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;

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() {
if (!Schema::hasTable('prompt_rewards')) {
$this->info('Already ran conversions.');

return;
}

if ($this->confirm('Have you migrated after installation?')) {
$this->info('If you have edited the reward maker to include custom reward types and other object support, ensure that they have been added to the UpdateRewardMaker command file or errors and data loss may occur.');
if ($this->confirm('Have you either edited the UpdateRewardMaker file to include your edits, or not edited the reward maker at all?')) {
$this->info('Converting rewards.');
$rewards = DB::table('prompt_rewards')->get();

foreach ($rewards 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);
}
}
$this->info('Dropping prompt rewards');
Schema::dropIfExists('prompt_rewards');

$this->info('Converting any existing rewards...');

$objrewards = ObjectReward::all();

foreach ($objrewards as $reward) {
switch ($reward->object_type) {
case 'Questline':
$objmodel = 'App\Models\Questline\Questline';
break;
case 'Prompt':
$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,
]);
}

$this->info('Converted rewards successfully. Happy Lorekeeping! :)');
} else {
$this->error('Please edit the file to ensure the conversion goes smoothly.');

return;
}
} else {
$this->info('Migrating DB. After this is complete, run this command again and confirm that you have to continue.');
$this->call('migrate');
$this->info('Migrations complete. Please run the command again to continue.');
}
}
}
18 changes: 10 additions & 8 deletions app/Helpers/AssetHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions app/Helpers/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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();
Expand Down
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
Expand Up @@ -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())) {
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Users/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand Down
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);
}
}
20 changes: 18 additions & 2 deletions app/Models/Prompt/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models\Prompt;

use App\Models\Model;
use App\Models\ObjectReward;
use Carbon\Carbon;

class Prompt extends Model {
Expand Down Expand Up @@ -78,8 +79,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(ObjectReward::class, 'object_id')->where([
['object_type', get_class($this)],
['recipient_type', 'User'],
['reward_key', 'objectRewards'],
]);
}

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

/**********************************************************************************************
Expand Down
Loading