Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feat] UI and component for realtime rollings
- Loading branch information
1 parent
6c5fd4f
commit 610028e
Showing
29 changed files
with
578 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
Oops, something went wrong.