feat(chat): 完善五子棋功能,包含AI对战、PvP邀请、断线重连及界面美化

This commit is contained in:
2026-03-12 08:35:21 +08:00
parent b9c703b755
commit 1c42f05e20
17 changed files with 2740 additions and 6 deletions

View File

@@ -0,0 +1,89 @@
<?php
/**
* 文件功能:五子棋对局记录表迁移
*
* 存储 PvP玩家对战 PvE人机对战两种模式的对局信息
* 包含棋盘状态、落子历史、对局结果及奖励记录。
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* 创建 gomoku_games 表。
*/
public function up(): void
{
Schema::create('gomoku_games', function (Blueprint $table) {
$table->id();
// 对战模式pvp玩家对玩家| pve人机对战
$table->string('mode', 10)->index();
// 所在房间 ID用于广播邀请和战报
$table->unsignedBigInteger('room_id')->index();
// 黑棋玩家(先手,也是发起方)
$table->unsignedBigInteger('player_black_id');
// 白棋玩家PvP 时为接受方PvE 时为 null
$table->unsignedBigInteger('player_white_id')->nullable();
// AI 难度1=简单 2=普通 3=困难 4=专家PvE 时使用)
$table->tinyInteger('ai_level')->nullable();
// 对局状态waiting | playing | finished | cancelled
$table->string('status', 20)->default('waiting')->index();
// 15×15 棋盘状态二维数组0=空 1=黑 2=白)
$table->json('board');
// 当前行棋方1=黑棋 2=白棋/AI
$table->tinyInteger('current_turn')->default(1);
// 胜者1=黑棋胜 2=白棋/AI胜 0=平局 null=未结束
$table->tinyInteger('winner')->nullable();
// 落子历史(用于战绩回放)[{row, col, color, at}]
$table->json('moves_history')->nullable();
// 奖励金币(对局结束时写入,记录实际到账金额)
$table->unsignedInteger('reward_gold')->default(0);
// PvE 入场费(对局开始时扣除,平局/认输时结算)
$table->unsignedInteger('entry_fee')->default(0);
// 邀请过期时间waiting 状态下,超时自动 cancelled
$table->timestamp('invite_expires_at')->nullable();
// 对局开始时间
$table->timestamp('started_at')->nullable();
// 对局结束时间
$table->timestamp('finished_at')->nullable();
$table->timestamps();
// 外键约束
$table->foreign('room_id')->references('id')->on('rooms')->onDelete('cascade');
$table->foreign('player_black_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('player_white_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* 回滚。
*/
public function down(): void
{
Schema::dropIfExists('gomoku_games');
}
};

View File

@@ -0,0 +1,41 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class GomokuConfigSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$configs = [
// PvP 配置
['type' => 'gomoku', 'key' => 'pvp_reward', 'value' => '80'],
['type' => 'gomoku', 'key' => 'pvp_invite_timeout', 'value' => '60'],
['type' => 'gomoku', 'key' => 'pvp_move_timeout', 'value' => '60'],
['type' => 'gomoku', 'key' => 'pvp_ready_timeout', 'value' => '30'],
// PvE AI 难度入口费
['type' => 'gomoku', 'key' => 'pve_fee_level_1', 'value' => '0'],
['type' => 'gomoku', 'key' => 'pve_fee_level_2', 'value' => '10'],
['type' => 'gomoku', 'key' => 'pve_fee_level_3', 'value' => '30'],
['type' => 'gomoku', 'key' => 'pve_fee_level_4', 'value' => '80'],
// PvE AI 难度胜利奖励
['type' => 'gomoku', 'key' => 'pve_reward_level_1', 'value' => '20'],
['type' => 'gomoku', 'key' => 'pve_reward_level_2', 'value' => '50'],
['type' => 'gomoku', 'key' => 'pve_reward_level_3', 'value' => '120'],
['type' => 'gomoku', 'key' => 'pve_reward_level_4', 'value' => '300'],
];
foreach ($configs as $config) {
\App\Models\GameConfig::updateOrCreate(
['type' => $config['type'], 'key' => $config['key']],
['value' => $config['value']]
);
}
}
}