简体   繁体   中英

Save php function in mysql

It has been my late-childhood dream to create a game, and now as I actually know how I thought I should fulfill my dream and started working on a little game project in my spare time. It's basically a combat type of game where you have up to 3 units, as well as your opponent, and you take turns ( since it's http, you know that feel ) to attack each other and cast spells and stuff. The issue I came across with is with abilities and how to store them. Basically if I were to store abilities in an array it would look something like

$abilities = array(
    0 => array(
        'name' => 'Fire ball',
        'desc' => 'Hurls a fire ball at your enemy, dealing X damage.'
        'effect' => function($data){
            $data['caster']->damage($data['target'], $data['caster']->magicPower);
        }
    ),
    1 => array(...
);

But if I were to store abilities this way, every time I needed to fetch information about a single ability I would need to load the whole array and it's probably going to get pretty big in time, so that would be a tremendous waste of memory. So I jumped to my other option, to save the abilities in a mysql table, however I'm having issues with the effect part. How can I save a function into a mysql field and be able to run it on demand?

Or if you can suggest another way to save the abilities, I may have missed.

To answer your question related to storing arrays into database like MySQL I would like you to serialize the Array as String. The normal direct serialization not going to work because they don't deal with closure.

You need to use classes like super_closure which can serialize the methods and convert them into string. Read more here

https://github.com/jeremeamia/super_closure

$helloWorld = new SerializableClosure(function($data){
            $data['caster']->damage($data['target'], $data['caster']->magicPower);
        });
$serializedFunc = serialize($helloWorld);

Now you can create array like this:

$abilities = array(
    0 => array(
        'name' => 'Fire ball',
        'desc' => 'Hurls a fire ball at your enemy, dealing X damage.'
        'effect' => $serializedFunc
    ));

This Array can now be saved directly, serialized or encoded to JSON.

I would recommend you to look at Redis or Memcache for caching query results and don't use MySQL to store functions.

You could have tree tables

spell

  • id
  • name
  • description

spell_effect

  • id
  • name
  • serversidescript

spell_effect_binder

  • spell_id
  • spell_effect_id

This would make sure, that your logic is in php files, where ever you would like them to be located, but all the meta of the spells, effects and how they bind together in the database. Meaning you will only load the function/script of the ones in need. Plus, giving you the possibility to append multiple effects to one spell.

//Firedamage.php
public function calculateEffects($level,$caster,$target) {

    $extraDamage = 5*$level;
    $randDamage = rand(10,50);

    $caster->damage( $target, ($randDamage+$extraDamage) );
}

Spell_effect entry

  • id = 1
  • name = 'firedamage'
  • serversidescript = 'Firedamage.php'

spell

  • id = 1
  • name = 'Fireball'
  • description = 'Hurls a fireball at your foe'

spell_effect_binder

  • spell_id = 1
  • spell_effect_id = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM