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

Feat: Default character rewards in prompts #1087

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions app/Helpers/AssetHelpers.php
Original file line number Diff line number Diff line change
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
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', 'rewardable_type', 'rewardable_id', 'quantity', 'image', 'remove_image', 'prefix', 'hide_submissions', 'staff_only', 'character_rewardable_type', 'character_rewardable_id', 'character_quantity',
]);
if ($id && $service->updatePrompt(Prompt::find($id), $data, Auth::user())) {
flash('Prompt updated successfully.')->success();
Expand Down
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
9 changes: 8 additions & 1 deletion app/Models/Prompt/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ public function category() {
* Get the rewards attached to this prompt.
*/
public function rewards() {
return $this->hasMany(PromptReward::class, 'prompt_id');
return $this->hasMany(PromptReward::class, 'prompt_id')->where('earner_type', 'User');
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 earner_type can this just be type?

Copy link
Author

Choose a reason for hiding this comment

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

ABSOLUTELY i wasn't sure if type would be too vague or not but i can swap it out

}

/**
* Get the character rewards attached to this prompt.
*/
public function characterRewards() {
return $this->hasMany(PromptReward::class, 'prompt_id')->where('earner_type', 'Character');
}

/**********************************************************************************************
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Prompt/PromptReward.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class PromptReward extends Model {
* @var array
*/
protected $fillable = [
'prompt_id', 'rewardable_type', 'rewardable_id', 'quantity',
'prompt_id', 'rewardable_type', 'rewardable_id', 'quantity', 'earner_type',
];

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

/**
Expand Down
27 changes: 21 additions & 6 deletions app/Services/PromptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function createPrompt($data, $user) {
$this->handleImage($image, $prompt->imagePath, $prompt->imageFileName);
}

$this->populateRewards(Arr::only($data, ['rewardable_type', 'rewardable_id', 'quantity']), $prompt);
$this->populateRewards(Arr::only($data, ['rewardable_type', 'rewardable_id', 'quantity', 'character_rewardable_type', 'character_rewardable_id', 'character_quantity']), $prompt);

return $this->commitReturn($prompt);
} catch (\Exception $e) {
Expand Down Expand Up @@ -266,7 +266,7 @@ public function updatePrompt($prompt, $data, $user) {
$this->handleImage($image, $prompt->imagePath, $prompt->imageFileName);
}

$this->populateRewards(Arr::only($data, ['rewardable_type', 'rewardable_id', 'quantity']), $prompt);
$this->populateRewards(Arr::only($data, ['rewardable_type', 'rewardable_id', 'quantity', 'character_rewardable_type', 'character_rewardable_id', 'character_quantity']), $prompt);

return $this->commitReturn($prompt);
} catch (\Exception $e) {
Expand All @@ -293,6 +293,7 @@ public function deletePrompt($prompt) {
}

$prompt->rewards()->delete();
$prompt->characterRewards()->delete();
if ($prompt->has_image) {
$this->deleteImage($prompt->imagePath, $prompt->imageFileName);
}
Expand Down Expand Up @@ -378,14 +379,28 @@ private function populateData($data, $prompt = null) {
private function populateRewards($data, $prompt) {
// Clear the old rewards...
$prompt->rewards()->delete();
$prompt->characterRewards()->delete();

if (isset($data['rewardable_type'])) {
foreach ($data['rewardable_type'] as $key => $type) {
PromptReward::create([
'prompt_id' => $prompt->id,
'rewardable_type' => $type,
'rewardable_id' => $data['rewardable_id'][$key],
'quantity' => $data['quantity'][$key],
'prompt_id' => $prompt->id,
'rewardable_type' => $type,
'rewardable_id' => $data['rewardable_id'][$key],
'quantity' => $data['quantity'][$key],
'earner_type' => 'User',
]);
}
}

if (isset($data['character_rewardable_type'])) {
foreach ($data['character_rewardable_type'] as $key => $type) {
PromptReward::create([
'prompt_id' => $prompt->id,
'rewardable_type' => $type,
'rewardable_id' => $data['character_rewardable_id'][$key],
'quantity' => $data['character_quantity'][$key],
'earner_type' => 'Character',
]);
}
}
Expand Down
Loading