Skip to content

Commit

Permalink
fix(characters): add vote data to design update casts, refactor handling
Browse files Browse the repository at this point in the history
  • Loading branch information
itinerare committed Jan 29, 2025
1 parent 0ec3dc5 commit 5dc562c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 56 deletions.
52 changes: 42 additions & 10 deletions app/Models/Character/CharacterDesignUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CharacterDesignUpdate extends Model {
'hash', 'species_id', 'subtype_ids', 'rarity_id',
'has_comments', 'has_image', 'has_addons', 'has_features',
'submitted_at', 'update_type', 'fullsize_hash',
'approval_votes', 'rejection_votes',
'vote_data',
];

/**
Expand All @@ -44,6 +44,7 @@ class CharacterDesignUpdate extends Model {
'submitted_at' => 'datetime',
'subtype_ids' => 'array',
'data' => 'array',
'vote_data' => 'array',
];

/**
Expand Down Expand Up @@ -324,15 +325,6 @@ public function getUrlAttribute() {
return url('designs/'.$this->id);
}

/**
* Gets the voting data of the design update request.
*
* @return string
*/
public function getVoteDataAttribute() {
return collect($this->attributes['vote_data'], true);
}

/**********************************************************************************************
OTHER FUNCTIONS
Expand Down Expand Up @@ -383,4 +375,44 @@ public function displaySubtypes() {

return implode(', ', $result);
}

/**
* Gets the voting data of the gallery submission and performs preliminary processing.
*
* @param bool $withUsers
*
* @return array
*/
public function getVoteData($withUsers = 0) {
$voteData['raw'] = $this->vote_data;

// Only query users if necessary, and condense to one query per submission
if ($withUsers) {
$users = User::whereIn('id', array_keys($voteData['raw']))->select('id', 'name', 'rank_id')->get();
} else {
$users = null;
}

$voteData['raw'] = collect($voteData['raw'])->mapWithKeys(function ($vote, $id) use ($users) {
return [$id => [
'vote' => $vote,
'user' => $users ? $users->where('id', $id)->first() : $id,
]];
});

// Tally approve/reject sums for ease
$voteData['approve'] = $voteData['reject'] = 0;
foreach ($voteData['raw'] as $vote) {
switch ($vote['vote']) {
case 1:
$voteData['reject'] += 1;
break;
case 2:
$voteData['approve'] += 1;
break;
}
}

return $voteData;
}
}
2 changes: 1 addition & 1 deletion app/Services/DesignUpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ public function voteRequest($action, $request, $user) {
$voteData = (isset($request->vote_data) ? collect($request->vote_data, true) : collect([]));
$voteData->get($user->id) ? $voteData->pull($user->id) : null;
$voteData->put($user->id, $vote);
$request->vote_data = $voteData->toJson();
$request->vote_data = $voteData;

$request->save();

Expand Down
2 changes: 1 addition & 1 deletion config/lorekeeper/extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
'character_TH_profile_link' => 0,

// Design Update Voting - Mercury
'design_update_voting' => 0,
'design_update_voting' => 1,

// Item Entry Expansion - Mercury
'item_entry_expansion' => [
Expand Down
18 changes: 2 additions & 16 deletions resources/views/admin/designs/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,6 @@
<div class="logs-table-body">
@foreach ($requests as $r)
<div class="logs-table-row">
@if (config('lorekeeper.extensions.design_update_voting'))
<?php
$rejectSum = 0;
$approveSum = 0;
foreach ($r->voteData as $voter => $vote) {
if ($vote == 1) {
$rejectSum += 1;
}
if ($vote == 2) {
$approveSum += 1;
}
}
?>
@endif
<div class="row flex-wrap">
<div class="col-md-3">
<div class="logs-table-cell">{!! $r->character ? $r->character->displayName : 'Deleted Character [#' . $r->character_id . ']' !!}</div>
Expand All @@ -98,8 +84,8 @@
<div class="col-2 col-md-2">
<div class="logs-table-cell">
<strong>
<span class="text-danger">{{ $rejectSum }}/{{ Settings::get('design_votes_needed') }}</span> :
<span class="text-success">{{ $approveSum }}/{{ Settings::get('design_votes_needed') }}</span>
<span class="text-danger">{{ $r->getVoteData()['reject'] }}/{{ Settings::get('design_votes_needed') }}</span> :
<span class="text-success">{{ $r->getVoteData()['approve'] }}/{{ Settings::get('design_votes_needed') }}</span>
</strong>
</div>
</div>
Expand Down
40 changes: 12 additions & 28 deletions resources/views/character/design/_header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,22 @@
@endif

@if ($request->status != 'Draft' && Auth::user()->hasPower('manage_characters') && config('lorekeeper.extensions.design_update_voting'))
<?php
$rejectSum = 0;
$approveSum = 0;
foreach ($request->voteData as $voter => $vote) {
if ($vote == 1) {
$rejectSum += 1;
}
if ($vote == 2) {
$approveSum += 1;
}
}
?>
<div class="card mb-3">
<div class="card-body">
<h5 class="text-left">{{ $request->status == 'Pending' ? 'Vote' : 'Past Votes' }} on this {{ $request->update_type == 'MYO' ? 'MYO Submission' : 'Design Update' }}
@if ($request->status == 'Pending')
<span class="text-right float-right">
<div class="row">
<div class="col-sm-6 text-center text-danger">
{{ $rejectSum }}/{{ Settings::get('design_votes_needed') }}
{{ $request->getVoteData()['reject'] }}/{{ Settings::get('design_votes_needed') }}
{!! Form::open(['url' => 'admin/designs/vote/' . $request->id . '/reject', 'id' => 'voteRejectForm']) !!}
<button class="btn {{ $request->voteData->get(Auth::user()->id) == 1 ? 'btn-danger' : 'btn-outline-danger' }}" style="min-width:40px;" data-action="reject"><i class="fas fa-times"></i></button>
<button class="btn {{ ($request->getVoteData()['raw']->get(Auth::user()->id)['vote'] ?? 0) == 1 ? 'btn-danger' : 'btn-outline-danger' }}" style="min-width:40px;" data-action="reject"><i class="fas fa-times"></i></button>
{!! Form::close() !!}
</div>
<div class="col-sm-6 text-center text-success">
{{ $approveSum }}/{{ Settings::get('design_votes_needed') }}
{{ $request->getVoteData()['approve'] }}/{{ Settings::get('design_votes_needed') }}
{!! Form::open(['url' => 'admin/designs/vote/' . $request->id . '/approve', 'id' => 'voteApproveForm']) !!}
<button class="btn {{ $request->voteData->get(Auth::user()->id) == 2 ? 'btn-success' : 'btn-outline-success' }}" style="min-width:40px;" data-action="approve"><i class="fas fa-check"></i></button>
<button class="btn {{ ($request->getVoteData()['raw']->get(Auth::user()->id)['vote'] ?? 0) == 2 ? 'btn-success' : 'btn-outline-success' }}" style="min-width:40px;" data-action="approve"><i class="fas fa-check"></i></button>
{!! Form::close() !!}
</div>
</div>
Expand All @@ -60,24 +48,20 @@
<div class="col-md">
<h5>Reject:</h5>
<ul>
@foreach ($request->voteData as $voter => $vote)
@if ($vote == 1)
<li>
{!! App\Models\User\User::find($voter)->displayName !!} {{ $voter == Auth::user()->id ? '(you)' : '' }}
</li>
@endif
@foreach ($request->getVoteData(1)['raw']->where('vote', 1) as $vote)
<li>
{!! $vote['user']->displayName !!}{{ $vote['user']->id == Auth::user()->id ? ' (you)' : '' }}
</li>
@endforeach
</ul>
</div>
<div class="col-md">
<h5>Approve:</h5>
<ul>
@foreach ($request->voteData as $voter => $vote)
@if ($vote == 2)
<li>
{!! App\Models\User\User::find($voter)->displayName !!} {{ $voter == Auth::user()->id ? '(you)' : '' }}
</li>
@endif
@foreach ($request->getVoteData(1)['raw']->where('vote', 2) as $vote)
<li>
{!! $vote['user']->displayName !!}{{ $vote['user']->id == Auth::user()->id ? ' (you)' : '' }}
</li>
@endforeach
</ul>
</div>
Expand Down

0 comments on commit 5dc562c

Please sign in to comment.