Skip to content

Commit 88e5936

Browse files
Merge remote-tracking branch 'upstream/develop' into feat/multiple-trait-subtypes
2 parents 4cee8d5 + fb10e07 commit 88e5936

25 files changed

+736
-179
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Models\Reward\Reward;
6+
use DB;
7+
use Illuminate\Console\Command;
8+
use Illuminate\Support\Facades\Schema;
9+
10+
class ConvertRewards extends Command {
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'convert-rewards';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Converts existing rewards on general objects (ex. prompts) to the new system.';
24+
25+
/**
26+
* Execute the console command.
27+
*/
28+
public function handle() {
29+
$this->info('************************');
30+
$this->info('* CONVERT REWARDS *');
31+
$this->info('************************'."\n");
32+
33+
$this->line("Converting prompt rewards...\n");
34+
if (Schema::hasTable('prompt_rewards')) {
35+
$promptRewards = DB::table('prompt_rewards')->get();
36+
$bar = $this->output->createProgressBar(count($promptRewards));
37+
$bar->start();
38+
foreach ($promptRewards as $promptReward) {
39+
Reward::create([
40+
'object_model' => 'App\Models\Prompt\Prompt',
41+
'object_id' => $promptReward->prompt_id,
42+
'rewardable_type' => $promptReward->rewardable_type,
43+
'rewardable_id' => $promptReward->rewardable_id,
44+
'quantity' => $promptReward->quantity,
45+
]);
46+
47+
$bar->advance();
48+
}
49+
$bar->finish();
50+
$this->info("\nDone!");
51+
Schema::dropIfExists('prompt_rewards');
52+
} else {
53+
$this->info('No prompt rewards to convert.');
54+
}
55+
56+
// Add other object types here as needed...
57+
58+
$this->info("\nAll done!");
59+
}
60+
}

app/Helpers/AssetHelpers.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ function getAssetModelString($type, $namespaced = true) {
144144
return 'CharacterItem';
145145
}
146146
break;
147+
148+
case 'prompts': case 'prompt':
149+
if ($namespaced) {
150+
return '\App\Models\Prompt\Prompt';
151+
} else {
152+
return 'Prompt';
153+
}
154+
break;
147155
}
148156

149157
return null;
@@ -200,7 +208,10 @@ function addAsset(&$array, $asset, $quantity = 1) {
200208
if (isset($array[$asset->assetType][$asset->id])) {
201209
$array[$asset->assetType][$asset->id]['quantity'] += $quantity;
202210
} else {
203-
$array[$asset->assetType][$asset->id] = ['asset' => $asset, 'quantity' => $quantity];
211+
$array[$asset->assetType][$asset->id] = [
212+
'asset' => $asset,
213+
'quantity' => $quantity,
214+
];
204215
}
205216
}
206217

app/Helpers/Helpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,3 +503,23 @@ function hasUnlockedLimits($object) {
503503

504504
return $service->checkLimits($object);
505505
}
506+
507+
/**
508+
* Returns the given objects rewards, if any.
509+
*
510+
* @param mixed $object
511+
*
512+
* @return bool
513+
*/
514+
function getRewards($object) {
515+
return App\Models\Reward\Reward::where('object_model', get_class($object))->where('object_id', $object->id)->get();
516+
}
517+
518+
/**
519+
* checks if a certain object has any rewards.
520+
*
521+
* @param mixed $object
522+
*/
523+
function hasRewards($object) {
524+
return App\Models\Reward\Reward::where('object_model', get_class($object))->where('object_id', $object->id)->exists();
525+
}

app/Http/Controllers/Admin/Data/PromptController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ public function getEditPrompt($id) {
210210
public function postCreateEditPrompt(Request $request, PromptService $service, $id = null) {
211211
$id ? $request->validate(Prompt::$updateRules) : $request->validate(Prompt::$createRules);
212212
$data = $request->only([
213-
'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',
213+
'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',
214+
'rewardable_type', 'rewardable_id', 'quantity', 'rewardable_recipient',
214215
]);
215216
if ($id && $service->updatePrompt(Prompt::find($id), $data, Auth::user())) {
216217
flash('Prompt updated successfully.')->success();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Services\RewardService;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
9+
10+
class RewardController extends Controller {
11+
/**
12+
* Creates or edits an objects rewards.
13+
*
14+
* @param App\Services\RewardService $service
15+
*
16+
* @return \Illuminate\Http\RedirectResponse
17+
*/
18+
public function postPopulateRewards(Request $request, RewardService $service) {
19+
$data = $request->only([
20+
'object_model', 'object_id',
21+
'rewardable_recipient', 'rewardable_type', 'rewardable_id', 'quantity',
22+
'data',
23+
]);
24+
if ($service->populateRewards($data['object_model'], $data['object_id'], $data, Auth::user())) {
25+
flash('Rewards updated successfully.')->success();
26+
} else {
27+
foreach ($service->errors()->getMessages()['error'] as $error) {
28+
flash($error)->error();
29+
}
30+
}
31+
32+
return redirect()->back();
33+
}
34+
}

app/Http/Controllers/WorldController.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,11 @@ public function getUniversalFeatures(Request $request) {
453453
$features = count($categories) ?
454454
$features->orderByRaw('FIELD(feature_category_id,'.implode(',', $categories->pluck('id')->toArray()).')') :
455455
$features;
456-
$features = $features->orderByRaw('FIELD(rarity_id,'.implode(',', $rarities->pluck('id')->toArray()).')')
457-
->orderBy('has_image', 'DESC')
456+
$features = count($rarities) ?
457+
$features->orderByRaw('FIELD(rarity_id,'.implode(',', $rarities->pluck('id')->toArray()).')') :
458+
$features;
459+
460+
$features->orderBy('has_image', 'DESC')
458461
->orderBy('name')
459462
->get()->groupBy(['feature_category_id', 'id']);
460463

@@ -493,15 +496,17 @@ public function getKitchenSinkFeatures(Request $request) {
493496
$categories = FeatureCategory::orderBy('sort', 'DESC')->get();
494497
$rarities = Rarity::orderBy('sort', 'ASC')->get();
495498

499+
$features = Feature::visible(Auth::user() ?? null);
500+
496501
$features = count($categories) ?
497-
$query = Feature::visible(Auth::user() ?? null)->orderByRaw('FIELD(feature_category_id,'.implode(',', $categories->pluck('id')->toArray()).')')
498-
->orderByRaw('FIELD(rarity_id,'.implode(',', $rarities->pluck('id')->toArray()).')')
499-
->orderBy('has_image', 'DESC')
500-
->orderBy('name')
501-
->get()
502-
->groupBy(['feature_category_id', 'id']) :
503-
$query = Feature::visible(Auth::user() ?? null)->orderByRaw('FIELD(rarity_id,'.implode(',', $rarities->pluck('id')->toArray()).')')
504-
->orderBy('has_image', 'DESC')
502+
$features->orderByRaw('FIELD(feature_category_id,'.implode(',', $categories->pluck('id')->toArray()).')') :
503+
$features;
504+
505+
$features = count($rarities) ?
506+
$features->orderByRaw('FIELD(rarity_id,'.implode(',', $rarities->pluck('id')->toArray()).')') :
507+
$features;
508+
509+
$features->orderBy('has_image', 'DESC')
505510
->orderBy('name')
506511
->get()
507512
->groupBy(['feature_category_id', 'id']);

app/Models/Prompt/Prompt.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Models\Prompt;
44

55
use App\Models\Model;
6+
use App\Models\Reward\Reward;
67
use Carbon\Carbon;
78

89
class Prompt extends Model {
@@ -79,7 +80,7 @@ public function category() {
7980
* Get the rewards attached to this prompt.
8081
*/
8182
public function rewards() {
82-
return $this->hasMany(PromptReward::class, 'prompt_id');
83+
return $this->morphMany(Reward::class, 'object', 'object_model', 'object_id');
8384
}
8485

8586
/**********************************************************************************************

app/Models/Prompt/PromptReward.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

app/Models/Reward/Reward.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace App\Models\Reward;
4+
5+
use App\Models\Model;
6+
7+
class Reward extends Model {
8+
/**
9+
* The attributes that are mass assignable.
10+
*
11+
* @var array
12+
*/
13+
protected $fillable = [
14+
'object_id', 'object_model', 'rewardable_recipient', 'rewardable_id', 'rewardable_type', 'quantity', 'data',
15+
];
16+
17+
/**
18+
* The table associated with the model.
19+
*
20+
* @var string
21+
*/
22+
protected $table = 'rewards';
23+
24+
/**
25+
* The attributes that should be cast to native types.
26+
*
27+
* @var array
28+
*/
29+
protected $casts = [
30+
'data' => 'array',
31+
];
32+
33+
/**********************************************************************************************
34+
35+
RELATIONS
36+
37+
**********************************************************************************************/
38+
39+
/**
40+
* Get the object that this reward is attached to.
41+
*/
42+
public function object() {
43+
return $this->morphTo('object', 'object_model', 'object_id');
44+
}
45+
46+
/**
47+
* Get the reward associated with this entry.
48+
*/
49+
public function reward() {
50+
$model = getAssetModelString(strtolower($this->rewardable_type));
51+
52+
if (!class_exists($model)) {
53+
// Laravel requires a relationship instance to be returned (cannot return null), so returning one that doesn't exist here.
54+
return $this->belongsTo(self::class, 'id', 'item_id')->whereNull('item_id');
55+
}
56+
57+
return $this->belongsTo($model, 'rewardable_id');
58+
}
59+
60+
/**********************************************************************************************
61+
62+
OTHER FUNCTIONS
63+
64+
**********************************************************************************************/
65+
66+
/**
67+
* checks if a certain object has any rewards.
68+
*
69+
* @param mixed $object
70+
*/
71+
public static function hasRewards($object) {
72+
return self::where('object_model', get_class($object))->where('object_id', $object->id)->exists();
73+
}
74+
75+
/**
76+
* get the rewards of a certain object.
77+
*
78+
* @param mixed $object
79+
*/
80+
public static function getRewards($object) {
81+
return self::where('object_model', get_class($object))->where('object_id', $object->id)->get();
82+
}
83+
}

app/Services/LimitService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ class LimitService extends Service {
2727
* edits an limits on an object.
2828
*
2929
* @param mixed $data
30-
* @param bool|true $log
3130
* @param mixed $object_model
3231
* @param mixed $object_id
32+
* @param mixed $data
33+
* @param bool|true $log
3334
*/
3435
public function editLimits($object_model, $object_id, $data, $log = true) {
3536
DB::beginTransaction();

0 commit comments

Comments
 (0)