Skip to content

Commit

Permalink
Rename the privot table as game_player as Laravel suggests.
Browse files Browse the repository at this point in the history
Fix response not a JSON if no resource find.
Update README.
  • Loading branch information
furic committed Dec 8, 2020
1 parent e80760d commit ee0f4a0
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 81 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
- [Web Console](#web-console)
- [Redeem Code Parameters](#redeem-code-parameters)
- [Redeem Validator API](#redeem-validator-api)
- [Unity Client Repo](#unity-client-repo)
- [Games Table](#games-table)
- [Players Table](#players-table)
- [GamePlayer Privot Table](#gameplayer-privot-table)
- [API URLs](#api-urls)
- [TODO](#todo)
- [License](#license)

Expand All @@ -43,16 +43,15 @@ Find the `providers` array and add our service provider.

## Configuration

To create table for redeem codes in database run:
To create table for game essentials in database run:
```bash
$ php artisan migrate
```

## Usage

### Game Parameters
### Games Table

The games table structure:
```
| Name | Type | Not Null |
|-----------------|----------|----------|
Expand All @@ -70,9 +69,8 @@ The games table structure:
- Android Version: The latest version number in Android, used for force update in client.
- tvOS Version: The latest version number in tvOS, used for force update in client.

### Player Parameters
### Players Table

The players table structure:
```
| Name | Type | Not Null |
|---------------|----------|----------|
Expand All @@ -94,49 +92,51 @@ The players table structure:
- Name: The name of the player input in game/app. (Optional)
- IP: The IP of the player when being first seen.

### Player Parameters
### GamePlayer Privot Table

Finally, the player-games table structure:
```
| Name | Type | Not Null |
|------------|----------|----------|
| id | integer | ✓ |
| player_id | integer | ✓ |
| game_id | integer | ✓ |
| player_id | integer | ✓ |
| channel | integer | ✓ |
| version | integer | ✓ |
| is_hack | tinyint | ✓ |
| created_at | datetime | |
| updated_at | datetime | |
```

- Player ID: The ID of a player.
- Game ID: The ID of a the game that player launched.
- Player ID: The ID of a player.
- Channel: The channel of the player getting into the game. Mostly used for Android players, e.g. Sumsung Store, Huawei App Gallery, etc. (Optional)
- Version: The current game version that the player device running.
- Version: The current game version that the player device is running.
- Is Hacked: A boolean to mark if the player ever performed any hack in game, for analytics and limiting functions in client.

### API
### API URLs

GET `<server url>/games/{id}`
Returns a JSON data from a given game ID, for debug usage only.

GET `<server url>/games/{id}/versions`
Returns a JSON data containing the versions from a given game ID, for client checking the latest game version and perform force-update.

GET `<server url>/games/{id}/players`
Returns a JSON array of all players from a given game ID, for debug usage only.

GET `<server url>/players/{id}`
Returns a JSON data from a given player ID, for debug usage only.

GET `<server url>/players/name/{name}`
Returns a JSON data from a given player name, for debug usage only.

POST `<server url>/players`
Creates a player with given POST data.
Creates a JSON data of a player with given POST data. If the player is already exists with the given UDID, Facebook ID, or game servies IDs, it update the player data instead.

PUT `<server url>/players/{id}`
Updates a player with a given player id and POST data.
Updates a JSON data of a player with a given player id.

GET `<server url>/games/{game-id}/players`
GET `<server url>/players/{id}/games`
Returns a JSON array of all players from a given game ID, for debug usage only.

## TODO
Expand All @@ -147,4 +147,4 @@ Returns a JSON array of all players from a given game ID, for debug usage only.

## License

laravel-redeem-codes is licensed under a [MIT License](https://github.com/furic/laravel-game-essentials/blob/main/LICENSE).
laravel-game-essentials is licensed under a [MIT License](https://github.com/furic/laravel-game-essentials/blob/main/LICENSE).
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class CreatePlayerGamesTable extends Migration
*/
public function up()
{
Schema::create('player_games', function (Blueprint $table) {
Schema::create('game_player', function (Blueprint $table) {
$table->increments('id');

$table->integer('player_id')->unsigned();
$table->integer('game_id')->unsigned();
$table->integer('player_id')->unsigned();
$table->tinyInteger('channel')->unsigned()->default('0');
$table->smallInteger('version')->unsigned()->default('100');
$table->boolean('is_hack')->default(false);

$table->timestamps();

$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
$table->foreign('game_id')->references('id')->on('games')->onDelete('cascade');
$table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
});
}

Expand Down
6 changes: 2 additions & 4 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
use Illuminate\Support\Facades\Route;
use Furic\GameEssentials\Http\Controllers\GameController;
use Furic\GameEssentials\Http\Controllers\PlayerController;
use Furic\GameEssentials\Http\Controllers\PlayerGameController;

// Games
Route::get('games/{id}', [GameController::class, 'show'])->name('games.show');
Route::get('games/{id}/versions', [GameController::class, 'showVersions'])->name('games.versions');
Route::get('games/{id}/players', [GameController::class, 'showPlayers'])->name('games.players');

// Players
Route::get('players/{id}', [PlayerController::class, 'show'])->name('players.show');
Route::get('players/name/{name}', [PlayerController::class, 'showWithName'])->name('players.show-with-name');
Route::post('players', [PlayerController::class, 'create'])->name('players.create');
Route::put('players/{id}', [PlayerController::class, 'update'])->name('players.update');

// Player Games
Route::get('games/{id}/players', [PlayerGameController::class, 'showPlayers'])->name('games.players');
Route::get('players/{id}/games', [PlayerController::class, 'showGames'])->name('players.games');
44 changes: 41 additions & 3 deletions src/Http/Controllers/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ class GameController extends Controller
*/
public function show($id)
{
return response(Game::findOrFail($id), 200);
try {
return response(Game::findOrFail($id), 200);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response([
'error' => 'No game found.'
], 400);
}
}

/**
Expand All @@ -28,8 +34,40 @@ public function show($id)
*/
public function showVersions($id) // Depricated
{
$game = Game::findOrFail($id);
return response(['ios' => $game->version_ios, 'android' => $game->version_android, 'tvos' => $game->version_tvos], 200);
try {
$game = Game::findOrFail($id);
return response([
'ios' => $game->version_ios,
'android' => $game->version_android,
'tvos' => $game->version_tvos
], 200);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response([
'error' => 'No game found.'
], 400);
}
}

/**
* Display a listing of the player resource by a given game ID.
*
* @param Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function showPlayers(Request $request, $id)
{
try {
if ($request->limit <= 0) {
return response(Game::findOrFail($id)->players, 200);
} else {
return response(Game::findOrFail($id)->players->take($request->limit), 200);
}
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response([
'error' => 'No game found.'
], 400);
}
}

}
67 changes: 58 additions & 9 deletions src/Http/Controllers/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Furic\GameEssentials\Http\Controllers;

use Furic\GameEssentials\Models\Player;
use Furic\GameEssentials\Models\PlayerGame;
use Furic\GameEssentials\Models\GamePlayer;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

Expand All @@ -18,7 +18,13 @@ class PlayerController extends Controller
*/
public function show($id)
{
return response(Player::findOrFail($id), 200);
try {
return response(Player::findOrFail($id), 200);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response([
'error' => 'No player found.'
], 400);
}
}

/**
Expand All @@ -30,7 +36,11 @@ public function show($id)
public function showWithName($name)
{
$player = Player::findByName($name);
return response($player, 200);
if ($player != NULL)
return response($player, 200);
return response([
'error' => 'No player found.'
], 400);
}

/**
Expand Down Expand Up @@ -62,15 +72,15 @@ public function create(Request $request)
$player = Player::create($data);
}

// Create playerGame if not exists
// Update/create GamePlayer entry if not exists
if ($request->has('game_id')) {
$playerGame = PlayerGame::findByPlayerGame($player->id, $request->game_id);
$playerGame = GamePlayer::findByGamePlayer($request->game_id, $player->id);
if ($playerGame) {
$playerGame->update($request->all());
} else {
$data = $request->all();
$data['player_id'] = $player->id;
PlayerGame::create($data);
GamePlayer::create($data);
}
}

Expand All @@ -86,9 +96,48 @@ public function create(Request $request)
*/
public function update(Request $request, $id)
{
$player = Player::findOrFail($id);
$player->update($request->all());
return response($player, 200);
try {
$player = Player::findOrFail($id);
$player->update($request->all());
// Update/create GamePlayer entry
if ($request->has('game_id')) {
$playerGame = GamePlayer::findByPlayerGame($request->game_id, $id);
if ($playerGame) {
$playerGame->update($request->all());
} else {
$data = $request->all();
$data['player_id'] = $player->id;
PlayerGame::create($data);
}
}
return response($player, 200);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response([
'error' => 'No player found.'
], 400);
}
}

/**
* Display a listing of the game resource by a given player ID.
*
* @param Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function showGames(Request $request, $id)
{
try {
if ($request->limit <= 0) {
return response(Player::findOrFail($id)->games, 200);
} else {
return response(Player::findOrFail($id)->games->take($request->limit), 200);
}
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return response([
'error' => 'No player found.'
], 400);
}
}

}
30 changes: 0 additions & 30 deletions src/Http/Controllers/PlayerGameController.php

This file was deleted.

5 changes: 5 additions & 0 deletions src/Models/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ class Game extends Model

protected $fillable = ['name', 'version_ios', 'version_android', 'version_tvos'];

public function players()
{
return $this->belongsToMany('Furic\GameEssentials\Models\Player');
}

}
16 changes: 4 additions & 12 deletions src/Models/PlayerGame.php → src/Models/GamePlayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,16 @@

use Illuminate\Database\Eloquent\Model;

class PlayerGame extends Model
class GamePlayer extends Model
{

protected $fillable = ['player_id', 'game_id', 'is_online', 'channel', 'version', 'is_hack'];
protected $table = 'game_player';

public function player()
{
return $this->belongsTo('App\Player');
}
protected $fillable = ['player_id', 'game_id', 'is_online', 'channel', 'version', 'is_hack'];

public static function findByPlayerGame($playerId, $gameId)
public static function findByGamePlayer($gameId, $playerId)
{
return SELF::where('player_id', $playerId)->where('game_id', $gameId)->first();
}

public static function getPlayers($gameId)
{
return SELF::where('game_id', $gameId);
}

}
Loading

0 comments on commit ee0f4a0

Please sign in to comment.