Skip to content

Commit

Permalink
[Feat] UI and component for realtime rollings
Browse files Browse the repository at this point in the history
  • Loading branch information
mariovalney committed Sep 27, 2020
1 parent 6c5fd4f commit 610028e
Show file tree
Hide file tree
Showing 29 changed files with 578 additions and 84 deletions.
10 changes: 10 additions & 0 deletions app/Http/Controllers/GuildController.php
Expand Up @@ -43,6 +43,16 @@ public function variables(Guild $guild)
return $this->render('screens.guilds.variables', $guild);
}

/**
* The Guild Rollings
*
* @return view()
*/
public function rollings(Guild $guild)
{
return $this->render('screens.guilds.rollings', $guild);
}

/**
* Render the guild page checking guild belongs to user
*
Expand Down
127 changes: 127 additions & 0 deletions app/Http/Livewire/RollingForm.php
@@ -0,0 +1,127 @@
<?php

namespace App\Http\Livewire;

use Auth;
use App\Models\Dice;
use App\Models\Guild;
use Livewire\Component;

class RollingForm extends Component
{
/**
* The session key
*/
const SESSION_KEY = 'rolling-form-current-dicesssssss';

/**
* The current guild
* @var integer
*/
public $guild;

/**
* Dices to be rolled
* @var array of Dice
*/
public $dices = [];

/**
* The variables
* @var array
*/
public $data = [];

/**
* Render the component
*
* @return view()
*/
public function render()
{
$this->data = $this->dicesFromSession();

return view('livewire.rolling-form.index');
}

/**
* Add a dice
*
* @param string $id
* @return void
*/
public function addDice($sides)
{
$dice = new Dice();
$dice->sides = $sides;

$dices = (array) session()->get(self::SESSION_KEY);
$dices[] = $dice;

session()->put(self::SESSION_KEY, $dices);
}

/**
* Update a dice by ID
*
* @param string $id
* @return void
*/
public function updateDice($id, $prop, $value)
{
$data = (array) session()->get(self::SESSION_KEY);

$dices = [];
foreach ($data as $dice) {
if ($dice->id === $id) {
$dice->$prop = $value;
}

$dices[] = $dice;
}

session()->put(self::SESSION_KEY, $dices);
}

/**
* Remove dice by ID
*
* @param string $id
* @return void
*/
public function removeDice($id)
{
$data = (array) session()->get(self::SESSION_KEY);

$dices = [];
foreach ($data as $dice) {
if ($dice->id === $id) {
continue;
}

$dices[] = $dice;
}

session()->put(self::SESSION_KEY, $dices);
}

/**
* Retrieve dices from session or at leat one
*
* @return array
*/
public function dicesFromSession()
{
$dices = session()->get(self::SESSION_KEY);
if ( empty( $dices ) ) {
return [];
}

$data = [];
foreach ($dices as $dice) {
$data[] = $dice->toArray();
}

return $data;
}
}
4 changes: 2 additions & 2 deletions app/Http/Livewire/VariablesCrud.php
Expand Up @@ -24,7 +24,7 @@ class VariablesCrud extends Component

/**
* The current guild
* @var mixed
* @var integer
*/
public $guild;

Expand Down Expand Up @@ -74,7 +74,7 @@ public function render()
'guild_id' => $this->guild,
])->get();

return view('livewire.variables.index');
return view('livewire.variables-crud.index');
}

/**
Expand Down
45 changes: 45 additions & 0 deletions app/Models/Dice.php
@@ -0,0 +1,45 @@
<?php

namespace App\Models;

use App\Support\SimpleModel as Model;

class Dice extends Model
{
/**
* Attributes
*
* @var array
*/
protected $fillable = [
'id',
'count',
'sides',
];

/**
* Attributes
*
* @var array
*/
protected $attributes = [
'count' => 1,
'sides' => 20,
];

/**
* Create the field filling it with array of attributes.
*
* @param array $attributes
*
* @return $this
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);

if (empty($this->attributes['id'])) {
$this->attributes['id'] = uniqid();
}
}
}
112 changes: 112 additions & 0 deletions app/Support/SimpleModel.php
@@ -0,0 +1,112 @@
<?php

namespace App\Support;

use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Database\Eloquent\Concerns\GuardsAttributes;
use Illuminate\Database\Eloquent\JsonEncodingException;
use Illuminate\Support\Str;

class SimpleModel implements Arrayable, Jsonable
{
use GuardsAttributes;

/**
* The defaults attributes.
*
* @var array
*/
protected $attributes = [];

/**
* Create the field filling it with array of attributes.
*
* @param array $attributes
*
* @return $this
*/
public function __construct(array $attributes = [])
{
foreach ($this->fillableFromArray($attributes) as $key => $value) {
if (! $this->isFillable($key)) {
continue;
}

if ($this->isGuarded($key)) {
continue;
}

$this->attributes[ $key ] = $value;
}

return $this;
}

/**
* Magic method to get attributes
*
* @param string $name
* @return mixed
*/
public function __get(string $name)
{
$value = $this->attributes[ $name ] ?? null;

$accessor = 'get' . Str::studly($name) . 'Attribute';
if (method_exists($this, $accessor)) {
return $this->{$accessor}($value);
}

return $value;
}

/**
* Magic method to set attributes
*
* @param string $name
* @param mixed $value
* @return void
*/
public function __set(string $name, $value)
{
$mutator = 'set' . Str::studly($name) . 'Attribute';
if (method_exists($this, $mutator)) {
$value = $this->{$mutator}($value);
}

$this->attributes[ $name ] = $value;

return $this;
}

/**
* Convert the model instance to JSON.
*
* @param int $options
* @return string
*
* @throws \Illuminate\Database\Eloquent\JsonEncodingException
*/
public function toJson($options = 0)
{
$json = json_encode($this->toArray(), $options);

if (JSON_ERROR_NONE !== json_last_error()) {
throw JsonEncodingException::forModel($this, json_last_error_msg());
}

return $json;
}

/**
* Convert the model instance to an array.
*
* @return array
*/
public function toArray()
{
return $this->attributes;
}
}
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -28,6 +28,7 @@
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.14.0",
"jquery-mask-plugin": "^1.14.16",
"laravel-mix-imagemin": "git+https://github.com/mariovalney/laravel-mix-imagemin.git"
}
}
7 changes: 7 additions & 0 deletions resources/js/app.js
@@ -1,9 +1,16 @@
import 'bootstrap';
import '@fortawesome/fontawesome-free/js/all.js';
import 'jquery-mask-plugin';

// jQuery
window.jQuery = require('jquery');

// Lodash
window._ = require('lodash');

// Axios
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

// Masks
require('./masks');

0 comments on commit 610028e

Please sign in to comment.