Skip to content

Instantly share code, notes, and snippets.

@cmosguy
Created April 15, 2016 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmosguy/f9b01a39a9fb60f1b8e2e03409980b7c to your computer and use it in GitHub Desktop.
Save cmosguy/f9b01a39a9fb60f1b8e2e03409980b7c to your computer and use it in GitHub Desktop.
<?php namespace SquareKings;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;
//use Laracasts\Commander\Events\EventGenerator;
use SquareKings\Games\GameStatusInterface;
use SquareKings\Events\SquareHasBeenActivated;
class Square extends Model implements GameStatusInterface
{
//use EventGenerator;
protected $fillable = [
'game_id',
'name',
'user_id',
'status',
'open',
'uuid',
'private',
'custom_message',
'total_dollars',
'max_invites',
'max_players',
'option'
];
public static $rules = [
'name' => 'required|alpha_dash|unique:squares',
'game_id' => 'required'
];
public static function boot()
{
parent::boot();
static::creating(function ($square) {
$square->uuid = $square->generateNewId();
$square->slug = Str::slug($square->name);
\Log::info("Square class: creating Square uuid:" . $square->uuid . "for ID: " . $square->name . " SLUG: " . $square->slug);
});
}
public function users()
{
return $this->belongsToMany('SquareKings\User');
}
public function game()
{
return $this->belongsTo('SquareKings\Game');
}
public function owner()
{
return $this->belongsTo('SquareKings\User', 'user_id');
}
public function squarerows()
{
return $this->hasMany('SquareKings\Squarerow');
}
public function invitations()
{
return $this->hasMany('SquareKings\Invitation');
}
public function generateNewId()
{
return Uuid::uuid4();
}
public static function activate(
$uuid,
$slug,
$name,
$private,
$open,
$status,
$game_id,
$custom_msg,
$total_dollars,
$max_invites,
$max_players,
$manage_money,
$option
) {
$square = static::where('uuid', $uuid);
$square->update(compact('uuid', 'slug', 'name', 'private', 'open', 'status', 'game_id', 'custom_msg',
'total_dollars', 'max_invites', 'max_players', 'manage_money', 'option'));
$instance = static::firstOrNew(compact('uuid', 'slug', 'name', 'private', 'open', 'status', 'game_id',
'custom_msg', 'total_dollars', 'max_invites', 'max_players', 'manage_money', 'option'));
event(new SquareHasBeenActivated($instance));
return $instance;
}
public function getGameUuidBySquareUuid($uuid)
{
$gameUuid = static::where('uuid', $uuid)->first()->game()->first()->uuid;
return $gameUuid;
}
public function getSquareByUuid($uuid)
{
return static::where('uuid', $uuid)->first();
}
public function scopeSquareStatus($query, $status)
{
return $query->whereStatus($status);
}
//you can debug the last query with: DB::getQueryLog()
//to see the raw output
public function scopeGameStatusLiveOrNotStarted($query, $user_uuid)
{
return $query
->join('games', 'squares.game_id', '=', 'games.id')
->join('users', 'squares.user_id', '=', 'users.id')
->where(function ($query) use ($user_uuid) {
$query->where('users.uuid', '=', $user_uuid)
->orWhere('squares.user_id', '=', $user_uuid);
})
->where(function ($query) {
$query->where('games.game_status', '=', self::GAME_STATUS_NOT_STARTED)
->orWhere('games.game_status', '=', self::GAME_STATUS_LIVE);
})
->select('squares.*');
}
public function delete()
{
//delete all the associated invitations to this square
$this->invitations()->delete();
$this->squarerows()->delete();
return parent::delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment