1

I am building a game based on PHP and MySQL. There is a player-vs-player mode but I figured I should add a player-vs-computer mode as well, since players may not be online at all times, especially in the beginning.

It is a combat type of game where players have up to two characters and can fight each other, taking turns.

I decided to save the computer's units on the filesystem instead of in a database. There are different monster camps which players may attack. If multiple players attack the same camp, separate instances are being created.

However what I'm having my doubts about is whether I should save the full unit information in the instance or just the properties that can change in the course of battle. Let me give an example.

Here's the unit class

class Unit {
    // Generic
    protected $id = null;
    public function getID(){ return $this->id; }

    public $owner = null;
    public $name;

    // Vitality
    public $HP          = 100;
    public $HPmax       = 100;
    public $energy      = 100;
    public $NRGmax      = 100;

    // Physical
    public $strength    = 0;
    public $endurance   = 0;

    // Psychical
    public $knowledge   = 0;
    public $wisdom      = 0;

   // ...
}

And here's how the file structure looks like

/PvC/prototypes/
/PvC/clones/

in prototypes are saved the preset settings of individual units, while in clones are saved the instances that the players are fighting against currently. How it works at the moment is in clones I save serialize($computer->units) which inserts the complete full information of the unit. My concern is that some properties, such as name, owner and others, will never change during the battle, so rewriting those each time is pretty pointless. And the instance file is getting rewritten pretty often I'd say, with almost every single request during battle.

In order to avoid that continuous overwriting I could serialize only the properties that can change during battle and then load the unit from the prototype as is, then overwrite that with the properties from the clone. However the problem with that is that I'm loading two files instead of one, then loading a bunch of data from the first file that I won't use, and is going to get overwritten.

I know this is not really something that would matter, as file size will rarely exceed 10kb. What are your thoughts on this?

1
  • 1
    Sounds weird to do this with files. I would look at a different solution, preferrably something that's is good at handling data without the need to fully define a table structure. Some NoSQL database that acceppts key/value storage. Maybe redis would work for you. Jul 19, 2014 at 17:11

1 Answer 1

1

I think it is better to put units data into the database and not into files. I would not recommend serializing the objects. Just create a MySQL table with fields like hp, strength, endurance etc. Then you can update only the fields that changed during the given round of the battle. If you don't plan to use any object-relational mapper (like Propel or Doctrine), you can have an array to track the fields that changed. Example code:

class Unit {
    private $id;

    private $hp;  //sorry, you will have to make it private
    ...

    private $changedFields = array();

    public function setHp($value)
    {
        $this->changedFields['hp'] = true;
        $this->hp = $value;
    }

    //i'm using active record pattern here, which may not be a good idea; this is just an example
    public function save()
    {
        if (empty($this->changedFields)) {
            return; //nothing to save
        }

        //build the MySQL query
        $dataToWrite = array();
        foreach($this->changedFields as $name => $temp) {
            $dataToWrite[] = "$name={$this->$name}"; //TODO: escape
        }
        mysql_query("UPDATE units SET " . implode(',', $dataToWrite) . " WHERE id={$this->id}");
    }
}

Then just call save() on every object of the Unit class.

It would be better to use an entity manager that keeps track of changes and writes data to the database in a more optimized query.

1
  • I would generally concur with the first statement. Serializing data is great for passing it between processes but not necessarily what you want for persistent storage of your game state. Feb 20, 2020 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.