1
0
Fork 0

initial commit

This commit is contained in:
Henrik Hautakoski 2021-06-28 17:33:29 +01:00
commit 1e1aa7d461
215 changed files with 35140 additions and 0 deletions

1
database/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.sqlite*

View file

@ -0,0 +1,47 @@
<?php
namespace Database\Factories;
use App\Models\User;
use App\Models\Character;
use App\Models\CharacterProfession;
use App\Warcraft\Classes;
use Illuminate\Database\Eloquent\Factories\Factory;
class CharacterFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Character::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'user_id' => User::factory(),
'name' => $this->faker->unique()->firstName(),
'race' => $this->faker->randomElement(['human', 'dwarf', 'gnome', 'night elf', 'draenei', 'orc', 'troll', 'tauren', 'undead', 'blood elf']),
'gender' => $this->faker->randomElement(['F', 'M']),
'class' => function (array $attributes) {
return (new Classes)->race($attributes['race'])->random();
},
'level' => $this->faker->numberBetween(1, 70),
];
}
public function hasRandomExistingProfessions($count)
{
$professions = CharacterProfession::factory()
->count($count);
return $this->has($professions, 'professions');
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Database\Factories;
use App\Models\CharacterProfession;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Character;
use App\Models\Profession;
class CharacterProfessionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = CharacterProfession::class;
/**
* Array to keep track uf used id's to make
* sure we generate unique ones.
*/
protected static $ids = [];
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'character_id' => Character::factory(),
'profession_id' => function (array $attributes) {
// Abit of a hack to make sure we generate
// unique character_id,profession_id pairs.
$ch_id = $attributes['character_id'];
if (!isset(self::$ids[$ch_id])) {
self::$ids[$ch_id] = [];
}
$id = self::$ids[$ch_id][] = Profession::whereNotIn('id', self::$ids[$ch_id])->get()->random()->id;
return $id;
},
'skill' => $this->faker->numberBetween(1, 375),
];
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Database\Factories;
use App\Models\Item;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
class ItemFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Item::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'external_id' => $this->faker->unique()->randomNumber(),
'name' => $this->faker->unique()->sentence(8),
'slug' => function (array $attributes) {
return Str::slug($attributes['name']);
},
'texture' => $this->faker->numberBetween(1, 999999),
'color' => Str::of($this->faker->hexcolor)->replace('#', '')
];
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Database\Factories;
use App\Models\Profession;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
class ProfessionFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Profession::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->unique()->word,
];
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Database\Factories;
use App\Models\RecipeCategory;
use Illuminate\Database\Eloquent\Factories\Factory;
class RecipeCategoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = RecipeCategory::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->unique()->sentence(3)
];
}
}

View file

@ -0,0 +1,66 @@
<?php
namespace Database\Factories;
use App\Models\Profession;
use App\Models\Item;
use App\Models\Recipe;
use App\Models\RecipeCategory;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
class RecipeFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Recipe::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'profession_id' => Profession::factory(),
'category_id' => RecipeCategory::factory(),
'item_id' => Item::factory(),
//'slug' => function (array $attributes) {
// return Str::slug(Item::find($attributes['item_id'])->name);
//}
];
}
public function randomExistingProfession()
{
return $this->state(function (array $attributes) {
return [
'profession_id' => Profession::all()->random()->id,
];
});
}
public function randomExistingCategory()
{
return $this->state(function (array $attributes) {
return [
'category_id' => RecipeCategory::all()->random()->id,
];
});
}
public function randomExistingItem()
{
return $this->state(function (array $attributes) {
$ids = Recipe::select('item_id')->orderBy('item_id')->get()->pluck('item_id');
return [
'item_id' => Item::whereNotIn('id', $ids)->get()->random()->id,
];
});
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Database\Factories;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'username' => $this->faker->username(),
'role' => $this->faker->randomElement(['user', 'admin']),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
}

View file

@ -0,0 +1,95 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Base extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('username')->unique();
$table->string('password');
$table->enum('role', ['user', 'admin']);
$table->rememberToken();
$table->timestamps();
});
Schema::create('professions', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
});
Schema::create('items', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('external_id')->unique();
$table->string('name')->unique();
$table->string('slug')->unique();
$table->string('color', 9)->nullable();
$table->integer('texture')->nullable();
});
Schema::create('recipe_categories', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
});
Schema::create('recipes', function (Blueprint $table) {
$table->id();
$table->foreignId('profession_id')->constrained();
$table->foreignId('category_id')->constrained('recipe_categories');
$table->foreignId('item_id')->constrained()->comment('crafted item');
$table->unique(['profession_id', 'item_id']);
});
Schema::create('reagents', function (Blueprint $table) {
$table->id();
$table->foreignId('recipe_id')->constrained();
$table->foreignId('item_id')->constrained();
$table->unsignedTinyInteger('quantity')->default(1);
$table->unique(['recipe_id', 'item_id']);
});
Schema::create('characters', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('name', 12)->unique();
$table->enum('race', ['human', 'dwarf', 'gnome', 'night elf', 'draenei', 'orc', 'troll', 'tauren', 'undead', 'blood elf']);
$table->enum('gender', ['F', 'M']);
$table->enum('class', ['warrior', 'hunter', 'rogue', 'mage', 'warlock', 'priest', 'druid', 'shaman', 'paladin']);
$table->unsignedTinyInteger('level')->default(1);
});
Schema::create('character_professions', function (Blueprint $table) {
$table->id();
$table->foreignId('character_id')->constrained()->onDelete('cascade');
$table->foreignId('profession_id')->constrained();
$table->integer('skill');
$table->unique(['character_id', 'profession_id']);
});
Schema::create('character_profession_recipe', function (Blueprint $table) {
$table->id();
$table->foreignId('ch_prof_id')->constrained('character_professions')->onDelete('cascade');
$table->foreignId('recipe_id')->constrained();
$table->unique(['ch_prof_id', 'recipe_id']);
});
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(SettingsSeeders\ProfessionsTableSeeder::class);
$this->call(DevelopmentSeeders\UserTableSeeder::class);
$this->call(DevelopmentSeeders\CharacterTableSeeder::class);
$this->call(DevelopmentSeeders\ItemTableSeeder::class);
$this->call(DevelopmentSeeders\RecipeCategoryTableSeeder::class);
$this->call(DevelopmentSeeders\RecipeTableSeeder::class);
$this->call(DevelopmentSeeders\CharacterRecipesTableSeeder::class);
}
}

View file

@ -0,0 +1,30 @@
<?php
namespace Database\Seeders\DevelopmentSeeders;
use Illuminate\Database\Seeder;
use App\Models\Recipe;
use App\Models\Character;
class CharacterRecipesTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
foreach(Character::all() as $character) {
foreach($character->professions as $ch_prof) {
$recipes = Recipe::where('profession_id', $ch_prof->profession->id)->get()
->random(rand(10, 50));
$ch_prof->recipes()->attach($recipes);
}
}
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Database\Seeders\DevelopmentSeeders;
use Illuminate\Database\Seeder;
use App\Models\Profession;
use App\Models\Character;
use App\Models\CharacterProfession;
class CharacterTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
for($i = 0; $i < 50; $i++) {
Character::factory()
->hasRandomExistingProfessions(2)
->create();
}
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Database\Seeders\DevelopmentSeeders;
use Illuminate\Database\Seeder;
use App\Models\Item;
class ItemTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
Item::factory(1000)->create();
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace Database\Seeders\DevelopmentSeeders;
use Illuminate\Database\Seeder;
use App\Models\RecipeCategory;
class RecipeCategoryTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
RecipeCategory::factory()->count(100)->create();
}
}

View file

@ -0,0 +1,33 @@
<?php
namespace Database\Seeders\DevelopmentSeeders;
use Illuminate\Database\Seeder;
use App\Models\Item;
use App\Models\Recipe;
use App\Models\RecipeCategory;
class RecipeTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
foreach(range(1, 500) as $index) {
$items = rand(1, 5);
$quantity = rand(1, 10);
Recipe::factory()
->randomExistingProfession()
->randomExistingCategory()
->randomExistingItem()
->hasAttached(Item::get()->random($items), ['quantity' => $quantity], 'reagents')
->create();
}
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Database\Seeders\DevelopmentSeeders;
use Illuminate\Database\Seeder;
use App\Models\User;
class UserTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
User::factory()->create([
'username' => 'admin',
'role' => 'admin',
]);
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Database\Seeders\SettingsSeeders;
use Illuminate\Database\Seeder;
use App\Models\Profession;
class ProfessionsTableSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
Profession::insert([
[ 'name' => 'Alchemy' ],
[ 'name' => 'Blacksmith' ],
[ 'name' => 'Cooking' ],
[ 'name' => 'Enchanting' ],
[ 'name' => 'Engineering' ],
[ 'name' => 'Jewelcrafting' ],
[ 'name' => 'Leatherworking' ],
[ 'name' => 'Tailoring' ]
]);
}
}