Files
vkv/app/Http/Controllers/EvaluationRuleSetController.php
Zdeněk Burda 41e3ce6f25 Initial commit
2026-01-09 21:26:40 +01:00

203 lines
9.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\EvaluationRuleSet;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class EvaluationRuleSetController extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
public function __construct()
{
// zápis jen pro přihlášené
$this->middleware('auth:sanctum')->only(['store', 'update', 'destroy']);
}
/**
* Seznam rulesetů (stránkovaně).
*/
public function index(Request $request): JsonResponse
{
$perPage = (int) $request->get('per_page', 100);
$items = EvaluationRuleSet::query()
->orderBy('name')
->paginate($perPage);
return response()->json($items);
}
/**
* Vytvoření nového rulesetu.
* Autorizace přes EvaluationRuleSetPolicy@create.
*/
public function store(Request $request): JsonResponse
{
$this->authorize('create', EvaluationRuleSet::class);
$data = $this->validateData($request);
$item = EvaluationRuleSet::create($data);
return response()->json($item, 201);
}
/**
* Detail rulesetu.
*/
public function show(EvaluationRuleSet $evaluationRuleSet): JsonResponse
{
$evaluationRuleSet->load(['evaluationRuns']);
return response()->json($evaluationRuleSet);
}
/**
* Update (partial).
* Autorizace přes EvaluationRuleSetPolicy@update.
*/
public function update(Request $request, EvaluationRuleSet $evaluationRuleSet): JsonResponse
{
$this->authorize('update', $evaluationRuleSet);
$data = $this->validateData($request, partial: true);
$evaluationRuleSet->fill($data);
$evaluationRuleSet->save();
return response()->json($evaluationRuleSet);
}
/**
* Smazání rulesetu.
* Autorizace přes EvaluationRuleSetPolicy@delete.
*/
public function destroy(EvaluationRuleSet $evaluationRuleSet): JsonResponse
{
$this->authorize('delete', $evaluationRuleSet);
$evaluationRuleSet->delete();
return response()->json(null, 204);
}
/**
* Validace vstupů pro store/update.
*/
protected function validateData(Request $request, bool $partial = false): array
{
$required = $partial ? 'sometimes' : 'required';
$data = $request->validate([
'name' => [$required, 'string', 'max:100'],
'code' => [$required, 'string', 'max:50'],
'description' => ['sometimes', 'nullable', 'string'],
'scoring_mode' => [$required, 'in:DISTANCE,FIXED_POINTS'],
'points_per_qso' => ['sometimes', 'integer', 'min:0'],
'points_per_km' => ['sometimes', 'numeric', 'min:0'],
'use_multipliers' => ['sometimes', 'boolean'],
'multiplier_type' => [$required, 'in:NONE,WWL,DXCC,SECTION,COUNTRY'],
'dup_qso_policy' => [$required, 'in:ZERO_POINTS,PENALTY'],
'nil_qso_policy' => [$required, 'in:ZERO_POINTS,PENALTY'],
'no_counterpart_log_policy' => ['sometimes', 'in:INVALID,ZERO_POINTS,FLAG_ONLY,PENALTY'],
'not_in_counterpart_log_policy' => ['sometimes', 'in:INVALID,ZERO_POINTS,FLAG_ONLY,PENALTY'],
'unique_qso_policy' => ['sometimes', 'in:INVALID,ZERO_POINTS,FLAG_ONLY'],
'busted_call_policy' => [$required, 'in:ZERO_POINTS,PENALTY'],
'busted_exchange_policy'=> [$required, 'in:ZERO_POINTS,PENALTY'],
'busted_serial_policy' => ['sometimes', 'in:ZERO_POINTS,PENALTY'],
'busted_locator_policy' => ['sometimes', 'in:ZERO_POINTS,PENALTY'],
'penalty_dup_points' => ['sometimes', 'integer', 'min:0'],
'penalty_nil_points' => ['sometimes', 'integer', 'min:0'],
'penalty_busted_call_points' => ['sometimes', 'integer', 'min:0'],
'penalty_busted_exchange_points' => ['sometimes', 'integer', 'min:0'],
'penalty_busted_serial_points' => ['sometimes', 'integer', 'min:0'],
'penalty_busted_locator_points' => ['sometimes', 'integer', 'min:0'],
'dupe_scope' => ['sometimes', 'in:BAND,BAND_MODE'],
'callsign_normalization' => ['sometimes', 'in:STRICT,IGNORE_SUFFIX'],
'distance_rounding' => ['sometimes', 'in:FLOOR,ROUND,CEIL'],
'min_distance_km' => ['sometimes', 'nullable', 'integer', 'min:0'],
'require_locators' => ['sometimes', 'boolean'],
'out_of_window_policy' => ['sometimes', 'in:IGNORE,ZERO_POINTS,PENALTY,INVALID'],
'penalty_out_of_window_points' => ['sometimes', 'integer', 'min:0'],
'exchange_type' => ['sometimes', 'in:SERIAL,WWL,SERIAL_WWL,CUSTOM'],
'exchange_requires_wwl' => ['sometimes', 'boolean'],
'exchange_requires_serial' => ['sometimes', 'boolean'],
'exchange_requires_report' => ['sometimes', 'boolean'],
'exchange_pattern' => ['sometimes', 'nullable', 'string', 'max:200'],
'ignore_slash_part' => ['sometimes', 'boolean'],
'ignore_third_part' => ['sometimes', 'boolean'],
'letters_in_rst' => ['sometimes', 'boolean'],
'rst_ignore_third_char' => ['sometimes', 'boolean'],
'discard_qso_rec_diff_call' => ['sometimes', 'boolean'],
'discard_qso_sent_diff_call' => ['sometimes', 'boolean'],
'discard_qso_rec_diff_rst' => ['sometimes', 'boolean'],
'discard_qso_sent_diff_rst' => ['sometimes', 'boolean'],
'discard_qso_rec_diff_code' => ['sometimes', 'boolean'],
'discard_qso_sent_diff_code' => ['sometimes', 'boolean'],
'discard_qso_rec_diff_serial' => ['sometimes', 'boolean'],
'discard_qso_sent_diff_serial' => ['sometimes', 'boolean'],
'discard_qso_rec_diff_wwl' => ['sometimes', 'boolean'],
'discard_qso_sent_diff_wwl' => ['sometimes', 'boolean'],
'busted_rst_policy' => ['sometimes', 'in:ZERO_POINTS,PENALTY'],
'penalty_busted_rst_points' => ['sometimes', 'integer', 'min:0'],
'match_tiebreak_order' => ['sometimes', 'nullable', 'array'],
'match_require_locator_match' => ['sometimes', 'boolean'],
'match_require_exchange_match' => ['sometimes', 'boolean'],
'multiplier_scope' => ['sometimes', 'in:PER_BAND,OVERALL'],
'multiplier_source' => ['sometimes', 'in:VALID_ONLY,ALL_MATCHED'],
'wwl_multiplier_level' => ['sometimes', 'in:LOCATOR_2,LOCATOR_4,LOCATOR_6'],
'checklog_matching' => ['sometimes', 'boolean'],
'out_of_window_dq_threshold' => ['sometimes', 'nullable', 'integer', 'min:1'],
'time_diff_dq_threshold_percent' => ['sometimes', 'nullable', 'integer', 'min:1', 'max:100'],
'time_diff_dq_threshold_sec' => ['sometimes', 'nullable', 'integer', 'min:1'],
'bad_qso_dq_threshold_percent' => ['sometimes', 'nullable', 'integer', 'min:1', 'max:100'],
'time_tolerance_sec' => ['sometimes', 'nullable', 'integer', 'min:0'],
'require_unique_qso' => ['sometimes', 'boolean'],
'allow_time_shift_one_hour' => ['sometimes', 'boolean'],
'time_shift_seconds' => ['sometimes', 'nullable', 'integer', 'min:0'],
'time_mismatch_policy' => ['sometimes', 'nullable', 'in:INVALID,ZERO_POINTS,FLAG_ONLY,PENALTY'],
'allow_time_mismatch_pairing' => ['sometimes', 'boolean'],
'time_mismatch_max_sec' => ['sometimes', 'nullable', 'integer', 'min:0'],
'callsign_suffix_max_len' => ['sometimes', 'nullable', 'integer', 'min:1'],
'callsign_levenshtein_max' => ['sometimes', 'nullable', 'integer', 'min:0', 'max:2'],
'dup_resolution_strategy' => ['sometimes', 'nullable', 'array'],
'operating_window_mode' => ['sometimes', 'in:NONE,BEST_CONTIGUOUS'],
'operating_window_hours' => ['sometimes', 'nullable', 'integer', 'min:1', 'max:24'],
'sixhr_ranking_mode' => ['sometimes', 'in:IARU,CRK'],
'options' => ['sometimes', 'nullable', 'array'],
]);
if (array_key_exists('operating_window_hours', $data) && ! array_key_exists('operating_window_mode', $data)) {
$data['operating_window_mode'] = 'BEST_CONTIGUOUS';
}
if (array_key_exists('operating_window_mode', $data)) {
if ($data['operating_window_mode'] === 'BEST_CONTIGUOUS') {
$data['operating_window_hours'] = 6;
} else {
$data['operating_window_hours'] = null;
}
}
return $data;
}
}