Open uaktags opened 11 years ago
Could be something worth looking into.
But, if I may, an amendment to the suggested syntax:
$player = $this->app->getSingleton('player');
// with no arguments, assume current one
$nand = $player->findPlayer();
// or by default, the model would be populated with the current player's data already
$nand = $player;
// other players
$uaktags = $player->findPlayer(/** id or name **/ $uaktags_id);
// stats can still be brainstormed on.
// it would most likely be a dependent model all-together.
sprintf('nand is %s experience points above uaktas', ($nand->stats->get('xp') - $uaktags->stats->get('xp')));
<?php
namespace ezRPG\Models;
class Player extends \ezRPG\Model
{
/**
* Perform compatibility check
*
* @param object $app
*/
public function __construct(\ezRPG\Interfaces\App $app)
{
parent::__construct($app);
}
public function findPlayer($id = '')
{
if ( $id == '' )
{
$id = $this->app->getSingleton('session')->loggedIn();
if ($id === false)
return false;
else
$player = $this->app->getSingleton('session')->get('playerid');
} else {
$player = $id;
}
$dbh = $this->app->getSingleton('pdo')->getHandle();
$config = $this->app->getConfig('db');
$sth = $dbh->prepare('
SELECT
id
FROM ' . $config["prefix"] . 'players
WHERE
id = :id OR
username = :id
LIMIT 1
;');
$sth->bindParam(':id', $player);
$sth->execute();
$result = $sth->fetch(\PDO::FETCH_OBJ);
return $result->id;
}
}
namespace ezRPG\Models;
class Stats extends Player
{
/**
* Perform compatibility check
*
* @param object $app
*/
public function __construct(\ezRPG\Interfaces\App $app)
{
parent::__construct($app);
}
public function get($id = $this->app->getSingleton('player')->findPlayer())
{
throw new \Exception($id, 1); //Debugging to show whatever $id equals
}
}
$playerID = $this->app->getSingleton('session')->get('playerid');
$this->view->name = 'home';
$this->view->set('player', $this->app->getSingleton('auth')->getPlayer($playerID));
$player = $this->app->getSingleton('player');
$uaktags = $player;
$this->view->set('test', $uaktags->stats->get());
Unfortunately $uaktags->stats returns a undefined property ezRPG\Models\Player::$stats. Have to look into this, but this is the basic logic so far with the proposed syntax.
Amend player:
public $stats;
And there's two ways to link these two, either within the player class, or on instance. I suggest the latter: player:
public function __construct($app) {
parent::__constrcut($app)
$this->stats = $app->getSingleton('stats');
}
Player.php Model
<?php
namespace ezRPG\Models;
class Player extends \ezRPG\Model
{
public $stats;
public $playerID;
/**
* Perform compatibility check
*
* @param object $app
*/
public function __construct(\ezRPG\Interfaces\App $app)
{
parent::__construct($app);
$this->playerID = $this->findPlayer();
$this->stats = $app->getSingleton('stats');
}
public function findPlayer($id = '')
{
if ( $id == '' )
{
$id = $this->app->getSingleton('session')->loggedIn();
if ($id === false)
throw new \Exception('No ID found', 1);
else
$player = $this->app->getSingleton('session')->get('playerid');
} else {
$player = $id;
}
$dbh = $this->app->getSingleton('pdo')->getHandle();
$config = $this->app->getConfig('db');
$sth = $dbh->prepare('
SELECT
id
FROM ' . $config["prefix"] . 'players
WHERE
id = :id OR
username = :id
LIMIT 1
;');
$sth->bindParam(':id', $player);
$sth->execute();
$result = $sth->fetch(\PDO::FETCH_OBJ);
return $result->id;
}
}
Stats.php Model
<?php
namespace ezRPG\Models;
class Stats extends Player
{
/**
* Perform compatibility check
*
* @param object $app
*/
public function __construct(\ezRPG\Interfaces\App $app)
{
$playerID = parent::$playerID;
}
public function get($id = '')
{
throw new \Exception($this->findPlayer(), 1); //Debugging to show whatever $id equals
}
}
While using the same code from the provided index.php from earlier, we now get an error of having an undeclared procedure called "getSingleton", also $playerID isn't passing through to Stats with $this->playerID, $playerID, or parent::$playerID.
Why do we need it to be a singleton?
Need is such a strong word, simply just going with the base usage of the framework. The base implementation was to always use Singleton for a simple object such as this. I understand you want to do away with using the global scope having so much added to it as Singletons would do, but another alternative hasn't been shown yet.
Models aren't really signletons currently.
A singleton is identified by two things:
Well currently, if im reading the code correctly, singletons and models are basically one and the same in our framework, except singletons are being added to the global scope where as getmodel is just that one object itself accessible in the current state. Getsingleton calls the getmodel and its adding it to the global this->app
Alterbatively from what ive read, and he's leaning more to is depencency injectiong during the construct of a particular module which will remove the global vars. again thats if im understanding everything correctly.
This model will need to be rewritten to conform to the new Model class.
Basic Idea
This example shows the proposed usage, of what the Player Model can/should provide.
If this suggestion is unnecessary or an alternative is available, provide feedback and/or change status to "wontfix"