重构猜谜活动并统一聊天室答题通知
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:猜成语题目模型
|
||||
*
|
||||
* 对应 idioms 表,每条记录是一道成语题目 + 谜语提示。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Idiom extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'answer',
|
||||
'hint',
|
||||
'is_active',
|
||||
'sort',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'is_active' => 'boolean',
|
||||
'sort' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:猜谜活动题库模型
|
||||
*
|
||||
* 对应 idioms 表,统一承载成语题与脑筋急转弯题目。
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* 类功能:统一管理猜谜活动的题目、答案、提示与题型。
|
||||
*/
|
||||
class Riddle extends Model
|
||||
{
|
||||
/**
|
||||
* 属性功能:显式绑定历史题库表名,避免类名重命名后推导到错误表。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'idioms';
|
||||
|
||||
/**
|
||||
* 常量功能:声明成语题题型标识。
|
||||
*/
|
||||
public const TYPE_IDIOM = 'idiom';
|
||||
|
||||
/**
|
||||
* 常量功能:声明脑筋急转弯题型标识。
|
||||
*/
|
||||
public const TYPE_BRAIN_TEASER = 'brain_teaser';
|
||||
|
||||
/**
|
||||
* 方法功能:声明允许批量赋值的题库字段。
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'type',
|
||||
'answer',
|
||||
'hint',
|
||||
'is_active',
|
||||
'sort',
|
||||
];
|
||||
|
||||
/**
|
||||
* 方法功能:定义题库字段的类型转换规则。
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'is_active' => 'boolean',
|
||||
'sort' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:返回系统支持的全部题型。
|
||||
*
|
||||
* @return array<int, string>
|
||||
*/
|
||||
public static function supportedTypes(): array
|
||||
{
|
||||
return [
|
||||
self::TYPE_IDIOM,
|
||||
self::TYPE_BRAIN_TEASER,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:判断给定题型是否属于系统支持范围。
|
||||
*/
|
||||
public static function isSupportedType(string $type): bool
|
||||
{
|
||||
return in_array($type, self::supportedTypes(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:根据题型返回面向用户的中文名称。
|
||||
*/
|
||||
public static function labelForType(string $type): string
|
||||
{
|
||||
return match ($type) {
|
||||
self::TYPE_BRAIN_TEASER => '脑筋急转弯',
|
||||
default => '猜成语',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:返回后台表单可直接使用的题型键值对。
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function typeOptions(): array
|
||||
{
|
||||
return collect(self::supportedTypes())
|
||||
->mapWithKeys(fn (string $type): array => [$type => self::labelForType($type)])
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:返回题型对应的活动标题。
|
||||
*/
|
||||
public static function activityLabelForType(string $type): string
|
||||
{
|
||||
return '猜谜活动·'.self::labelForType($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:按题型筛选题库记录。
|
||||
*/
|
||||
public function scopeOfType(Builder $query, string $type): Builder
|
||||
{
|
||||
return $query->where('type', self::isSupportedType($type) ? $type : self::TYPE_IDIOM);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,9 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:猜成语游戏回合模型
|
||||
* 文件功能:猜谜活动回合模型
|
||||
*
|
||||
* 每次出题对应一个回合,记录题目、状态、奖励和获胜者。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
* 每次出题对应一个回合,记录题型、题目、状态、奖励和获胜者。
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
@@ -16,10 +12,17 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* 类功能:记录猜成语每一轮的题目、奖励与结算状态。
|
||||
* 类功能:记录猜谜活动每一轮的题型、奖励与结算状态。
|
||||
*/
|
||||
class IdiomGameRound extends Model
|
||||
class RiddleGameRound extends Model
|
||||
{
|
||||
/**
|
||||
* 属性功能:显式绑定历史回合表名,避免类名重命名后推导到错误表。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'idiom_game_rounds';
|
||||
|
||||
/**
|
||||
* 方法功能:声明可批量赋值的回合字段。
|
||||
*
|
||||
@@ -28,6 +31,7 @@ class IdiomGameRound extends Model
|
||||
protected $fillable = [
|
||||
'room_id',
|
||||
'idiom_id',
|
||||
'quiz_type',
|
||||
'status',
|
||||
'reward_gold',
|
||||
'reward_exp',
|
||||
@@ -45,6 +49,8 @@ class IdiomGameRound extends Model
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'room_id' => 'integer',
|
||||
'idiom_id' => 'integer',
|
||||
'reward_gold' => 'integer',
|
||||
'reward_exp' => 'integer',
|
||||
'started_at' => 'datetime',
|
||||
@@ -53,10 +59,10 @@ class IdiomGameRound extends Model
|
||||
}
|
||||
|
||||
/**
|
||||
* 方法功能:关联本回合对应的成语题目。
|
||||
* 方法功能:关联本回合对应的猜谜题目。
|
||||
*/
|
||||
public function idiom(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Idiom::class);
|
||||
return $this->belongsTo(Riddle::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user