- Create narrator_texts table migration with context/hero_type indexes
- Add NarratorText model with getRandomText() for variant selection
- Add NarratorTextSeeder with 30+ texts for 11 contexts
- Implement vouvoiement (recruteur) vs tutoiement (client/dev)
- Create NarratorController with GET /api/narrator/{context}
- Add useFetchNarratorText composable for frontend
Contexts: intro, transitions, hints, encouragements, contact_unlocked, welcome_back
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class NarratorText extends Model
|
|
{
|
|
protected $fillable = [
|
|
'context',
|
|
'text_key',
|
|
'variant',
|
|
'hero_type',
|
|
];
|
|
|
|
public function scopeForContext(Builder $query, string $context): Builder
|
|
{
|
|
return $query->where('context', $context);
|
|
}
|
|
|
|
public function scopeForHero(Builder $query, ?string $heroType): Builder
|
|
{
|
|
if ($heroType) {
|
|
return $query->where(function ($q) use ($heroType) {
|
|
$q->where('hero_type', $heroType)
|
|
->orWhereNull('hero_type');
|
|
});
|
|
}
|
|
|
|
return $query->whereNull('hero_type');
|
|
}
|
|
|
|
public static function getRandomText(string $context, ?string $heroType = null): ?self
|
|
{
|
|
$query = static::forContext($context);
|
|
|
|
if ($heroType) {
|
|
// Priorité aux textes spécifiques au héros, sinon textes génériques
|
|
$heroSpecific = (clone $query)->where('hero_type', $heroType)->inRandomOrder()->first();
|
|
if ($heroSpecific) {
|
|
return $heroSpecific;
|
|
}
|
|
}
|
|
|
|
return $query->whereNull('hero_type')->inRandomOrder()->first();
|
|
}
|
|
}
|