Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add warps #816

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions app/Http/Controllers/Api/v2/Minecraft/MinecraftWarpController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Http\Controllers\Api\v2\Minecraft;

use App\Http\Controllers\ApiController;
use App\Models\MinecraftWarp;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

final class MinecraftWarpController extends ApiController
{
public function index(Request $request)
{
return MinecraftWarp::orderBy('name', 'asc')->get();
}

public function store(Request $request)
{
$request->validate([
'name' => ['required', 'string', 'alpha_dash', Rule::unique('minecraft_warps')],
'world' => 'required|string',
'x' => 'required|integer',
'y' => 'required|integer',
'z' => 'required|integer',
'pitch' => 'required|numeric',
'yaw' => 'required|numeric',
]);

return MinecraftWarp::create($request->all());
}

public function update(Request $request, MinecraftWarp $warp)
{
$request->validate([
'name' => ['required', 'string', 'alpha_dash', Rule::unique('minecraft_warps')->ignore($warp)],
'world' => 'required|string',
'x' => 'required|integer',
'y' => 'required|integer',
'z' => 'required|integer',
'pitch' => 'required|numeric',
'yaw' => 'required|numeric',
]);

$warp->update($request->all());

return $warp;
}

public function destroy(Request $request, MinecraftWarp $warp)
{
$warp->delete();

return response()->json();
}
}
94 changes: 94 additions & 0 deletions app/Http/Controllers/Panel/MinecraftWarpController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Http\Controllers\Panel;

use App\Http\Controllers\WebController;
use App\Models\MinecraftWarp;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class MinecraftWarpController extends WebController
{
public function index(Request $request): MinecraftWarp|Factory|View
{
$warps = MinecraftWarp::orderBy('name', 'asc')
->paginate(100);

return view('admin.minecraft-warps.index')->with(compact('warps'));
}

public function create(Request $request): Application|Factory|View
{
$warp = new MinecraftWarp();

return view('admin.minecraft-warps.create')
->with(compact('warp'));
}

public function store(Request $request): RedirectResponse
{
$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'alpha_dash', Rule::unique('minecraft_warps')],
'world' => 'required|string',
'x' => 'required|integer',
'y' => 'required|integer',
'z' => 'required|integer',
'pitch' => 'required|numeric',
'yaw' => 'required|numeric',
]);

if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}

MinecraftWarp::create($request->all());

return redirect(route('front.panel.minecraft.warps.index'));
}

/**
* Show the form for editing the specified resource.
*/
public function edit(MinecraftWarp $warp): MinecraftWarp|Factory|View
{
return view('admin.minecraft-warps.edit')
->with(compact('warp'));
}

public function update(Request $request, MinecraftWarp $warp): RedirectResponse
{
$validator = Validator::make($request->all(), [
'name' => ['required', 'string', 'alpha_dash', Rule::unique('minecraft_warps')->ignore($warp)],
'world' => 'required|string',
'x' => 'required|integer',
'y' => 'required|integer',
'z' => 'required|integer',
'pitch' => 'required|numeric',
'yaw' => 'required|numeric',
]);

if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}

$warp->update($request->all());

return redirect(route('front.panel.minecraft.warps.index'));
}

public function destroy(Request $request, MinecraftWarp $warp): RedirectResponse
{
$warp->delete();

return redirect(route('front.panel.minecraft.warps.index'));
}
}
32 changes: 32 additions & 0 deletions app/Models/MinecraftWarp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Models;

use App\Core\Utilities\Traits\HasStaticTable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class MinecraftWarp extends Model
{
use HasFactory;
use HasStaticTable;

protected $table = 'minecraft_warps';

protected $fillable = [
'name',
'world',
'x',
'y',
'z',
'pitch',
'yaw',
'created_at',
'updated_at',
];

protected $casts = [
'pitch' => 'decimal:1',
'yaw' => 'decimal:1',
];
}
25 changes: 25 additions & 0 deletions database/factories/WarpFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Factories;

use App\Models\MinecraftWarp;

class WarpFactory extends Factory
{
protected $model = MinecraftWarp::class;

public function definition()
{
return [
'name' => strtolower($this->faker->unique()->firstName),
'world' => $this->faker->randomElement(['creative', 'survival', 'monarch']),
'x' => $this->faker->numberBetween(0, 20000),
'y' => $this->faker->numberBetween(0, 20000),
'z' => $this->faker->numberBetween(0, 20000),
'pitch' => $this->faker->randomFloat(nbMaxDecimals: 1, min: 0, max: 100),
'yaw' => $this->faker->randomFloat(nbMaxDecimals: 1, min: 0, max: 100),
'created_at' => $this->faker->dateTime,
'updated_at' => $this->faker->dateTime,
];
}
}
40 changes: 40 additions & 0 deletions database/migrations/2024_10_28_110159_create_warps.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('minecraft_warps', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('world');
$table->double('x');
$table->double('y');
$table->double('z');
$table->float('pitch');
$table->float('yaw');
$table->timestamps();

$table->index('name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('minecraft_warps');
}
};
8 changes: 7 additions & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Seeders;

use App\Models\MinecraftConfig;
use App\Models\MinecraftWarp;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
Expand All @@ -20,6 +21,11 @@ public function run()
$this->call(ServerTokenSeeder::class);
$this->call(ShowcaseWarpSeeder::class);

MinecraftConfig::factory()->create();
MinecraftConfig::factory()
->create();

MinecraftWarp::factory()
->count(40)
->create();
}
}
7 changes: 7 additions & 0 deletions resources/views/admin/layouts/_sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
<span>Minecraft</span>
</h6>
<ul class="nav flex-column mb-2">
<li class="nav-item">
<a class="nav-link {{ request()->is('panel/minecraft/warps*') ? 'active' : '' }} "
href="{{ route('front.panel.minecraft.warps.index') }}">
<i class="fas fa-flag fa-fw"></i>
Warps
</a>
</li>
<li class="nav-item">
<a class="nav-link {{ request()->is('panel/showcase-warps*') ? 'active' : '' }} "
href="{{ route('front.panel.showcase-warps.index') }}">
Expand Down
47 changes: 47 additions & 0 deletions resources/views/admin/minecraft-warps/_form.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="row mb-3">
<label for="name" class="col-sm-3 col-form-label horizontal-label">Warp Name</label>
<div class="col-sm-9">
<input type="text" id="name" name="name" class="form-control" placeholder="warp_name" value="{{ old('name', $warp->name) }}">
</div>
</div>
<div class="row mb-3">
<label for="world" class="col-sm-3 col-form-label horizontal-label">World</label>
<div class="col-sm-9">
<input type="text" id="world" name="world" class="form-control" placeholder="creative" value="{{ old('world', $warp->world) }}">
</div>
</div>
<div class="row mb-3">
<label for="x" class="col-sm-3 col-form-label horizontal-label">X</label>
<div class="col-sm-9">
<input type="number" id="x" name="x" class="form-control" placeholder="0" value="{{ old('x', $warp->x) }}">
</div>
</div>
<div class="row mb-3">
<label for="y" class="col-sm-3 col-form-label horizontal-label">Y</label>
<div class="col-sm-9">
<input type="number" id="y" name="y" class="form-control" placeholder="0" value="{{ old('y', $warp->y) }}">
</div>
</div>
<div class="row mb-3">
<label for="z" class="col-sm-3 col-form-label horizontal-label">Z</label>
<div class="col-sm-9">
<input type="number" id="z" name="z" class="form-control" placeholder="0" value="{{ old('z', $warp->z) }}">
</div>
</div>
<div class="row mb-3">
<label for="pitch" class="col-sm-3 col-form-label horizontal-label">Pitch</label>
<div class="col-sm-9">
<input type="text" id="pitch" name="pitch" class="form-control" placeholder="0.0" value="{{ old('pitch', $warp->pitch) }}">
</div>
</div>
<div class="row mb-3">
<label for="yaw" class="col-sm-3 col-form-label horizontal-label">Yaw</label>
<div class="col-sm-9">
<input type="text" id="yaw" name="yaw" class="form-control" placeholder="0.0" value="{{ old('yaw', $warp->yaw) }}">
</div>
</div>
<div class="row mb-3">
<div class="col-sm-9 ms-auto">
<button type="submit" class="btn btn-primary">{{ $buttonText }}</button>
</div>
</div>
16 changes: 16 additions & 0 deletions resources/views/admin/minecraft-warps/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@extends('admin.layouts.admin')

@section('title', 'Create Warp')

@section('body')
<div class="row">
<div class="col-md-8">
@include('admin._errors')
<form action="{{ route('front.panel.minecraft.warps.store') }}" method="post">
@csrf

@include('admin.minecraft-warps._form', ['buttonText' => 'Create'])
</form>
</div>
</div>
@endsection
27 changes: 27 additions & 0 deletions resources/views/admin/minecraft-warps/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@extends('admin.layouts.admin')

@section('title', 'Edit Warp #' . $warp->getKey())

@section('toolbar')
<div class="btn-toolbar">
<form method="post" action="{{ route('front.panel.minecraft.warps.destroy', $warp) }}">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-outline-danger"><i class="fas fa-trash"></i> Delete</button>
</form>
</div>
@endsection

@section('body')
<div class="row">
<div class="col-md-8">
@include('admin._errors')
<form action="{{ route('front.panel.minecraft.warps.update', $warp) }}" method="post">
@csrf
@method('PUT')

@include('admin.minecraft-warps._form', ['buttonText' => 'Save'])
</form>
</div>
</div>
@endsection
Loading
Loading