game-village-politics-backend/database/migrations/2026_10_28_200000_create_ga...

108 lines
4.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('worlds', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug')->unique();
// Overrides for config/game/*, e.g. {"world": {"speed": 100}}.
$table->json('config')->nullable();
$table->dateTime('started_at')->nullable();
$table->dateTime('ends_at')->nullable();
$table->timestamps();
});
Schema::create('players', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->foreignId('world_id')->constrained()->cascadeOnDelete();
$table->string('name');
$table->unsignedInteger('points')->default(0);
$table->timestamps();
$table->unique(['user_id', 'world_id']);
});
Schema::create('villages', function (Blueprint $table) {
$table->id();
$table->foreignId('world_id')->constrained()->cascadeOnDelete();
// Null for barbarian villages.
$table->foreignId('player_id')->nullable()->constrained()->nullOnDelete();
$table->string('name');
$table->integer('x');
$table->integer('y');
$table->unsignedSmallInteger('grid_w');
$table->unsignedSmallInteger('grid_h');
// Stored at resources_at; the current value is stored + rate × elapsed.
$table->decimal('wood', 14, 4)->default(0);
$table->decimal('clay', 14, 4)->default(0);
$table->decimal('iron', 14, 4)->default(0);
$table->decimal('food', 14, 4)->default(0);
$table->dateTime('resources_at', 3);
$table->unsignedInteger('points')->default(0);
$table->timestamps();
$table->unique(['world_id', 'x', 'y']);
});
Schema::create('buildings', function (Blueprint $table) {
$table->id();
$table->foreignId('village_id')->constrained()->cascadeOnDelete();
$table->string('type', 32);
$table->smallInteger('level')->default(0);
$table->unsignedSmallInteger('x');
$table->unsignedSmallInteger('y');
$table->unsignedTinyInteger('priority')->default(5);
$table->timestamps();
});
Schema::create('build_orders', function (Blueprint $table) {
$table->id();
$table->foreignId('village_id')->constrained()->cascadeOnDelete();
// Null while the order builds a new instance.
$table->foreignId('building_id')->nullable()->constrained()->cascadeOnDelete();
$table->string('type', 32);
$table->string('action', 16);
// -1 removes the instance.
$table->smallInteger('target_level');
$table->unsignedSmallInteger('x');
$table->unsignedSmallInteger('y');
$table->json('cost');
$table->unsignedTinyInteger('builder')->nullable();
$table->dateTime('starts_at', 3)->nullable();
$table->dateTime('finishes_at', 3)->nullable();
$table->timestamps();
});
Schema::create('game_events', function (Blueprint $table) {
$table->id();
$table->foreignId('world_id')->constrained()->cascadeOnDelete();
// The village whose row is locked while the event runs.
$table->foreignId('village_id')->constrained()->cascadeOnDelete();
$table->string('type', 48);
$table->unsignedBigInteger('subject_id')->nullable();
$table->dateTime('fire_at', 3);
$table->dateTime('processed_at', 3)->nullable();
$table->unsignedSmallInteger('attempts')->default(0);
$table->timestamps();
$table->index(['processed_at', 'fire_at']);
$table->index(['village_id', 'processed_at', 'fire_at']);
});
}
public function down(): void
{
Schema::dropIfExists('game_events');
Schema::dropIfExists('build_orders');
Schema::dropIfExists('buildings');
Schema::dropIfExists('villages');
Schema::dropIfExists('players');
Schema::dropIfExists('worlds');
}
};