ezrpg-legacy / ezrpg-2.0.X-discontinued-

http://ezrpgproject.net
Other
6 stars 0 forks source link

Create a Player-centric model to handle all Player-DB #1

Open uaktags opened 11 years ago

uaktags commented 11 years ago

Basic Idea

$player = $this->app->getSingleton('player');

$curPlayer = $player->getCurrentPlayer; //getCurrentPlayer should check the session to find out who is the current player and return an array from the Players DB.

$curPlayer['id']
$curPlayer['email']

$playerXP = $player->getStats($curPlayer['id'], 'xp') //Returns the integer of XP
$playerHP = $player->getStats($curPlayer['id'], 'hp') //Returns the integer of HP
$playerStats = $player->getStats($curPlayer['id']) //Returns an array of all stats 

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"

ferdis commented 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')));
uaktags commented 11 years ago

Player.php Model


<?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; 
    }
}

Stats.php Model

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
    }
}

Inside the index.php controller

$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());

Issues

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.

ferdis commented 11 years ago

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');
}
uaktags commented 11 years ago

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
    }
}

Issues

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.

ghost commented 11 years ago

Why do we need it to be a singleton?

uaktags commented 11 years ago

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.

ferdis commented 11 years ago

Models aren't really signletons currently.

A singleton is identified by two things:

  1. Favors composition over inheritance, a.k.a. not extended(able), and,
  2. a staitic getInstance() method running the class.

see http://en.wikipedia.org/wiki/Singleton_pattern

uaktags commented 11 years ago

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.

ferdis commented 11 years ago

This model will need to be rewritten to conform to the new Model class.