Skip to content
Snippets Groups Projects
Unverified Commit 46d257d0 authored by Maxime FRIESS's avatar Maxime FRIESS :blue_heart:
Browse files

[game] Added game parameters

parent a0b51c28
Branches
Tags
No related merge requests found
Pipeline #107053 failed with stages
in 47 seconds
Showing with 171 additions and 30 deletions
......@@ -229,13 +229,14 @@ public function store(CreateGameRequest $request)
DB::beginTransaction();
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => $request->input("mode"),
]);
foreach ($request->input("users") as $user) {
$user = User::findOrFail($user["id"]);
foreach ($request->input("users") as $u) {
$user = User::findOrFail($u["id"]);
if ($user->server_id === $server->id && $user->server_state === 'connected') {
$game->users()->attach($user->id, []);
$game->users()->attach($user->id, ['team_id' => $u['team_id']]);
} else {
DB::rollBack();
return response()->json([
......@@ -246,6 +247,8 @@ public function store(CreateGameRequest $request)
DB::commit();
$game->start();
return response()->json([
"game" => $game
], 201);
......@@ -305,13 +308,13 @@ public function update(UpdateGameRequest $request, $id)
abort(404);
}
if ($game->state === 'created' && $request->input('state') === 'playing')
/*if ($game->state === 'created' && $request->input('state') === 'playing')
{
$game->start();
return response()->json([
"game" => $game
], 200);
}
}*/
if ($game->state === 'playing' && $request->input('state') === 'finished')
{
......
......@@ -9,16 +9,28 @@
* request="CreateGame",
* required=true,
* @OA\JsonContent(
* required={"users"},
* required={"users","mode"},
* @OA\Property(
* property="mode",
* type="string",
* description="Mode of the game",
* enum={"ffa", "team"}
* ),
* @OA\Property(
* property="users",
* type="array",
* description="Users who participated in the game",
* @OA\Items(
* required={"id","team_id"},
* @OA\Property(
* property="id",
* type="int",
* description="ID of the user"
* ),
* @OA\Property(
* property="team_id",
* type="int",
* description="ID of the team of the user"
* )
* )
* ),
......@@ -46,7 +58,9 @@ public function rules()
{
return [
"users" => "required|array|min:2",
"users.*.id" => "required|exists:users,id|distinct"
"users.*.id" => "required|exists:users,id|distinct",
"users.*.team_id" => "required_if:mode,team|numeric|integer",
"mode" => "required|string|in:ffa,team"
];
}
}
......@@ -13,17 +13,28 @@
* @OA\Property(
* property="state",
* type="string",
* pattern="^playing|finished$"
* enum={"playing", "finished"}
* ),
* @OA\Property(
* property="users",
* type="array",
* description="Users who participated in the game. Only needed when state == finished",
* @OA\Items(
* required={"id","winner", "rank"},
* @OA\Property(
* property="id",
* type="int",
* description="ID of the user"
* ),
* @OA\Property(
* property="winner",
* type="boolean",
* description="Wether the user won or not (can have multiple winners in team mode)"
* ),
* @OA\Property(
* property="rank",
* type="int",
* description="Rank of the user (from 1 to 4). In team mode, the winning team is 1 and the losing team is 2"
* )
* )
* ),
......@@ -50,9 +61,10 @@ public function authorize()
public function rules()
{
return [
'state' => 'required|string|in:playing,finished',
'state' => 'required|string|in:finished',
"users" => "required_if:state,finished|array|min:2",
"users.*.id" => "required_with:users|exists:users,id|distinct"
"users.*.id" => "required_with:users|exists:users,id|distinct",
"users.*.rank" => "required_with:users|numeric|integer",
];
}
}
......@@ -6,7 +6,6 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Builder;
/**
* @OA\Schema(
......@@ -38,6 +37,7 @@ protected static function booted(): void
*/
protected $fillable = [
'server_id',
'mode',
];
protected $hidden = [
......@@ -46,7 +46,7 @@ protected static function booted(): void
public function users()
{
return $this->belongsToMany(user::class, 'users_games', 'game_id', 'user_id')->using(UserGame::class);
return $this->belongsToMany(User::class, 'users_games', 'game_id', 'user_id')->using(UserGame::class)->withPivot("team_id", "rank");
}
public function server(): BelongsTo
......@@ -63,8 +63,12 @@ public function start()
public function end($user_data)
{
foreach($user_data as $user) {
$this->users()->updateExistingPivot($user['id'], ["rank" => $user['rank']]);
}
$this->finished_at = Carbon::now();
$this->state = 'finished';
$this->save();
$this->push();
}
}
......@@ -14,10 +14,22 @@
* type="object",
* @OA\Property(type="integer", property="user_id"),
* @OA\Property(type="integer", property="game_id"),
* @OA\Property(type="integer", property="team_id"),
* @OA\Property(type="integer", property="rank"),
* )
* )
*/
class UserGame extends Pivot
{
use HasFactory;
}
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'team_id',
'rank',
];
}
\ No newline at end of file
<?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::table('games', function (Blueprint $table) {
$table->enum("mode", ["ffa", "team"]);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('games', function (Blueprint $table) {
$table->dropColumn("mode");
});
}
};
<?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::table('users_games', function (Blueprint $table) {
$table->integer("team_id")->nullable()->default(null);
$table->integer("rank")->nullable()->default(null);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users_games', function (Blueprint $table) {
$table->dropColumn("team_id");
$table->dropColumn("rank");
});
}
};
......@@ -102,7 +102,8 @@ public function test_expired_heartbeat()
$server->save();
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->users()->attach($user->id);
......@@ -170,7 +171,8 @@ public function test_expired_heartbeat_game_finished()
$server->save();
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->users()->attach($user->id);
......
......@@ -2,6 +2,7 @@
namespace Tests\Feature;
use App\Models\Game;
use App\Models\Server;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
......@@ -47,6 +48,7 @@ public function test_good()
$response = $this->withHeaders([
"Authorization" => "Bearer " . $token
])->postJson('/api/game', [
"mode" => "ffa",
"users" => [[
"id" => $user1->id
], [
......@@ -58,6 +60,7 @@ public function test_good()
"game" => [
"id",
"server_id",
"mode",
"created_at",
"updated_at"
]
......@@ -78,9 +81,11 @@ public function test_good()
])->putJson('/api/game/' . $id, [
"state" => "finished",
"users" => [[
"id" => $user1->id
"id" => $user1->id,
"rank" => 1
], [
"id" => $user2->id
"id" => $user2->id,
"rank" => 2
]]
]);
$response->assertStatus(200);
......@@ -115,6 +120,7 @@ public function test_user_not_connected()
$response = $this->withHeaders([
"Authorization" => "Bearer " . $token
])->postJson('/api/game', [
"mode" => "ffa",
"users" => [[
"id" => $user1->id
], [
......@@ -234,6 +240,7 @@ public function test_user_connected_to_other_server()
$response = $this->withHeaders([
"Authorization" => "Bearer " . $token
])->postJson('/api/game', [
"mode" => "ffa",
"users" => [[
"id" => $user1->id
], [
......@@ -285,6 +292,7 @@ public function test_update_game_of_other_server()
$response = $this->withHeaders([
"Authorization" => "Bearer " . $token
])->postJson('/api/game', [
"mode" => "ffa",
"users" => [[
"id" => $user1->id
], [
......@@ -347,6 +355,7 @@ public function test_update_skip_state()
$response = $this->withHeaders([
"Authorization" => "Bearer " . $token
])->postJson('/api/game', [
"mode" => "ffa",
"users" => [[
"id" => $user1->id
], [
......@@ -371,9 +380,11 @@ public function test_update_skip_state()
])->putJson('/api/game/' . $id, [
"state" => "finished",
"users" => [[
"id" => $user1->id
"id" => $user1->id,
"rank" => 1
], [
"id" => $user2->id
"id" => $user2->id,
"rank" => 2
]]
]);
$response->assertStatus(403);
......
......@@ -30,7 +30,8 @@ public function test_index()
]);
Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$token = Auth::login($user);
......@@ -44,6 +45,7 @@ public function test_index()
'*' => [
"id",
"server_id",
"mode",
"created_at",
"updated_at"
]
......@@ -81,7 +83,8 @@ public function test_show_good()
]);
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->state = 'finished';
$game->save();
......@@ -97,6 +100,7 @@ public function test_show_good()
"game" => [
"id",
"server_id",
"mode",
"server" => [
"id",
"login",
......@@ -138,7 +142,8 @@ public function test_index_users_good()
]);
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->state = 'finished';
$game->save();
......@@ -196,7 +201,8 @@ public function test_show_users_good()
]);
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->state = 'finished';
$game->save();
......
......@@ -140,7 +140,8 @@ public function test_change_pubkey()
$server->save();
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->users()->attach($user->id);
......
......@@ -125,7 +125,8 @@ public function test_index_games_good()
]);
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$response = $this->withHeaders([
......@@ -137,6 +138,7 @@ public function test_index_games_good()
'*' => [
"id",
"server_id",
"mode",
"created_at",
"updated_at"
]
......@@ -175,7 +177,8 @@ public function test_show_games_good()
]);
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->state = 'finished';
$game->save();
......@@ -188,6 +191,7 @@ public function test_show_games_good()
"game" => [
"id",
"server_id",
"mode",
"created_at",
"updated_at"
]
......
......@@ -51,7 +51,8 @@ public function test_stop()
$server->save();
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->users()->attach($user->id);
......@@ -123,7 +124,8 @@ public function test_stop_game_finished()
$server->save();
$game = Game::create([
"server_id" => $server->id
"server_id" => $server->id,
"mode" => "ffa"
]);
$game->users()->attach($user->id);
......
......@@ -112,6 +112,7 @@ public function test_index_usergame_good()
$game = Game::create([
'server_id' => $server->id,
"mode" => "ffa"
]);
$game->users()->attach($user);
......@@ -127,6 +128,7 @@ public function test_index_usergame_good()
"server_id",
"created_at",
"updated_at",
"mode",
"pivot" => [
"game_id",
"user_id"
......@@ -169,6 +171,7 @@ public function test_show_usergame_good()
$game = Game::create([
'server_id' => $server->id,
"mode" => "ffa"
]);
$game->state = 'finished';
$game->save();
......@@ -185,6 +188,7 @@ public function test_show_usergame_good()
"server_id",
"created_at",
"updated_at",
"mode",
"pivot" => [
"game_id",
"user_id"
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment