Author Topic: Easiest/Quickest way to convert a string into workable code  (Read 1039 times)

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Easiest/Quickest way to convert a string into workable code
« on: September 22, 2010, 10:07:21 PM »
Right now i'm wanting to store all of the various formulas for spells and such in a database so that they can be added without additional coding(and yes i realize that putting them within the code would make it run faster) since they'll be in there they'll be stored of course as a string. And as such i cannot think of any other way than to have to do a load of string explosion and regexing to get the string into a workable formula. If this is the only possible way of going about it then i'll just have to pretty much scrap that idea and go about just hard coding all of the formulas into a php file.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #1 on: September 23, 2010, 03:38:08 AM »
eval();
But this is semi evil function. It won't let you take advantage of precached tokenized code (eAccelerator).

Why don't you just dynamicaly create a .php file after each version/forumla change? All advantages of both hardcodring and db driven coding and none of their disadvantages (well, maybe except an additional php file to be included).

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #2 on: September 23, 2010, 07:06:12 AM »
It's nonsense, there's always coding - storing in DB would only mean MORE copy&paste, not less ...
As far as I know eval prevents any caching & optimizations

Also your way prevents calling other formula... unless you perform some formula analysis and fetch more formulas from DB

I see no benefit of this approach, only downsides

Just make a file or folder with some structure (file per formula, file per formula class or something) and just put a formulas' names (some simple variant, PHP function name's specifics compatible) into DB
Then make formula's name = function's name

Or you can do some OO thing if (you like that better, I do) in similar fashion - formula's name = classname (heavier;cleaner, possibly more flexible) or = methodname

That way you'll also have all formulas in one place (file or folder), but allowing optimization, calling anything, highlighted syntax etc. Plus it doesn't have to be just one inline formula, it can be fully-fledged algorithm for calculation of the effect this way
You won't see it in a nice way in pMA anyway
« Last Edit: September 23, 2010, 07:26:38 AM by Nox »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline BaRRaKID

  • Level 5
  • *
  • Posts: 16
  • Reputation: +1/-0
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #3 on: September 23, 2010, 07:20:47 AM »
I don't know if this helps, but why not just store the formula variables instead of the whole formula?
If your formula is for example x = y(2y-z^2) you just need to store the value of y and z and change them as needed.
You can take if further and assume that all your formulas only have two variables and store them in a variables table with 3 fields (FormulaId, ValueOfY, ValueOfZ).

Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #4 on: September 23, 2010, 07:25:55 AM »
But 1337794m3r wants to store whole formula, you have to get that from somewhere... I believe actual values will be stored elsewhere and that it's not an issue
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Easiest/Quickest way to convert a string into workable code
« Reply #5 on: September 23, 2010, 10:13:50 AM »
In one of our game we store the formulas in the database and eval() them during the "combat". Since it is only during the combat sequences the whole argument of non tolkenized code hasn't made a bit of difference in server performance. I know it's slower and I'm not arguing that point but the slower execution isn't hurting the speed of the server.

The biggest advantage I've found with going this route is *I* don't have to be the one writing the forumlas. The formulas can be easily updated / created via the admin panel by our staff based on game feedback.

Creating online addictions, one game at a time:

Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #6 on: September 24, 2010, 04:01:31 AM »
In one of our game we store the formulas in the database and eval() them during the "combat". Since it is only during the combat sequences the whole argument of non tolkenized code hasn't made a bit of difference in server performance. I know it's slower and I'm not arguing that point but the slower execution isn't hurting the speed of the server.

The biggest advantage I've found with going this route is *I* don't have to be the one writing the forumlas. The formulas can be easily updated / created via the admin panel by our staff based on game feedback.



that is pretty much the main reason i was going to do it so that i didn't have to manually do it myself. Also the idea about having a load of php files lying around doesn't sit extremely well for me. And when two players do go into battle their formulas will already be there prefetched and ready to have the variables which are taken and used within the formula from their respective information.


It's nonsense, there's always coding - storing in DB would only mean MORE copy&paste, not less ...
As far as I know eval prevents any caching & optimizations

Also your way prevents calling other formula... unless you perform some formula analysis and fetch more formulas from DB

I see no benefit of this approach, only downsides

And these are for custom things like special skills. The basic formulas such as the standard attacks and such will already be hardcoded into it. The idea of having loads of files to include would create a rather large clutter, also there will be some skills which of course would still have their own function calls. The idea though that it's stupid to put all of the formulas into a database doesn't make a great deal of sense to me since when there's a new skill about to be made, the tool i've created thus far lets someone look over all of the other formulas to see if they want to use one of those for it and thus makes it less copy-paste.

I might end up trying what chris said and then Nox later talked about later on to just see how it sits with me but at the moment i'm probably just going to end up eval()ing the formulas. And the thing i was of course speaking of was simply just naming the functions via their procedural value as far as teh formula goes something like the hex value to eliminate probability of repeat functions.

The biggest problem i saw with having each one holding their own php file was that it'd end up being a few thousand files(down the road from now) and managing them seems like it'd be more of a headache than the database solution.

I've already put most of the things in the database like the items are in there and thusly only their smallest values that are possible are stored and i cannot imagine storing that information in some file rather than a database.
« Last Edit: September 24, 2010, 04:19:23 AM by 133794m3r »

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #7 on: September 24, 2010, 06:03:34 AM »
Self rewriting code (.php file) is an old trick from 8bit era, it was used frequently, especially for interpreted languages. Sure, it sounds fishy, but definitely less fishy than eval(), and definitely much more optimization friendly/performance wise. It's just as if you hardcoded it, no real disadvantages, it's just that the computer write the code and not you, that's the only difference.
You won't need "thousands" of files, just 1. Well, maybe 2-3, but no more. You don't write one formula per file, you make multiple functions per file, just like regular coding, you call the function and it returns you the result of a given formula.

Let's say you have a variable deciding on damage. If you make it a self rewritable function you can call it 10000 times in a loop, it will be a pure precached and tokenized by eAccelerator PHP code. If you do it via eval() that fetch data from DB you would be making 10000 database queries... For intensively used formulas the difference could be life or death for the server (think about all these poor dwarves that live inside CPU and carry bits on their shoulders!).

Quote
I've already put most of the things in the database like the items are in there and thusly only their smallest values that are possible are stored and i cannot imagine storing that information in some file rather than a database.
You don't store them in file, you store them in DB. When you click "recompile" button in your admin panel the whole file is created anew using data from database. All editing is done in DB, everything is stored in DB, standard DB backup tools will store your whole game. It just adds another step, the compilation of the project which has to be done after any change.
« Last Edit: September 24, 2010, 06:17:24 AM by Chris »

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #8 on: September 24, 2010, 06:51:54 AM »
Personally, I'm paranoid enough that I don't think I could ever give over enough control like this. I kind of see the value here but I would probably create some sort of parsing engine instead of storing and then using eval() on arbitrary code.

Just one little exploit in the system and you've basically handed over your system to a hacker.

But, if you create a basic sort of language that could be interpreted by some static code you have, you're, essentially, pre-defining the available options that this code can do. Unless I'm missing the point and have the requirements for this confused.

Here's an example:

Using eval():
Players / Moderators want a way to add a field to user accounts showing their ePeen score. So, the moderator writes up something like:

// Actual strength is a meter, but younger players just tend to be more prone to GIFT.
$player->epeen = pow($numCities + $numArmies, 2) * (100 - -($player->age - 50));

Or, you store something like the following:

<add playerAttribute = "epeen">
    <formula><![CDATA[pow($numCities + $numArmies, 2) * (100 - -($player->age - 50))]]></formula>
</add>

Then, your script looks for the function that handles the addition of player attributes. That function parses the formula and is able to determine a result. You've achieved the same result, but you never opened up a potentially critical vulnerability in your system.

Of course, this would be inefficient as written and you'd probably want an application caching layer on this kind of function so you needn't perform the parsing again each time. And it would definitely take you longer than just running eval(). But, at the same time, it offers the same functionality without the loss of security.

*edit - Oops... forgot I was writing in a CDATA tag and forgot to close it there. See how easy it is to code a mistake! lol
« Last Edit: September 24, 2010, 06:54:21 AM by JGadrow »
Idiocy - Never underestimate the power of stupid people in large groups.


Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #9 on: September 24, 2010, 08:39:13 AM »
i never thought of it as a self compiling file. And i might actually end up doing that with the items and such. Since i believe a multilevel array inside of a PHP file might be faster than having to query the database to find the information. I'll still be querying it for things like "when player x got item y" and putting that data in there so that i can have a better track over the players and try to make sure that no one's doing anything bad.

And that idea definately does sound extremely interesting and now all i have to do is sit down and write up a script which'll make these files for me. Since i could still use the formulas that i do have and those tools for the actual creation and then just do a 'creation'  like thing when new ones are done.

This is one reason i love having an active internet connection and also finding this site since i've been able to hear about things that i never would've thought of and also rarely provide some insight/help to others when what i do know comes up.

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #10 on: September 24, 2010, 09:51:19 AM »
Just a little something you want to be sure you deal with on what Chris is talking about with self-rewriting PHP files....

Do not do the actual file replace until the entire file has been written. Unless you have the ini setup to avoid this, your php code stops running if a user browses away before they receive the result. Don't forget it's also possible for power outages to suddenly happen in the middle of script execution.

Here's the 3 essential operations for this:
1) Open file for writing
2) Add code
3) Close file

But what happens if lightning strikes a transformer and your server suddenly loses power in the middle of writing the new code? This probably depends on your underlying OS and filesystem. However, it's entirely possible to end up with a corrupted file which could cause a hard failure or, even worse, can silently work but do so in a corrupted manner because the full logic you were expecting isn't applied.

However, you can define a process that doesn't care about the underlying system components quite easily:
1) Create a temporary copy of file.
2) Perform code alterations.
3) Save the temporary file.
4) Rename the original file (to avoid naming conflict).
5) Rename the temporary file to the original file name.
6) Clean up the original file (archive, delete, whatever you normally do).

Using these steps, you've guaranteed that a failure at any point is a quick and easy fix.
Idiocy - Never underestimate the power of stupid people in large groups.


Offline 133794m3r

  • Level 22
  • *
  • Posts: 265
  • Reputation: +2/-0
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #11 on: September 24, 2010, 10:03:36 AM »
Just a little something you want to be sure you deal with on what Chris is talking about with self-rewriting PHP files....

Do not do the actual file replace until the entire file has been written. Unless you have the ini setup to avoid this, your php code stops running if a user browses away before they receive the result. Don't forget it's also possible for power outages to suddenly happen in the middle of script execution.

Here's the 3 essential operations for this:
1) Open file for writing
2) Add code
3) Close file

But what happens if lightning strikes a transformer and your server suddenly loses power in the middle of writing the new code? This probably depends on your underlying OS and filesystem. However, it's entirely possible to end up with a corrupted file which could cause a hard failure or, even worse, can silently work but do so in a corrupted manner because the full logic you were expecting isn't applied.

However, you can define a process that doesn't care about the underlying system components quite easily:
1) Create a temporary copy of file.
2) Perform code alterations.
3) Save the temporary file.
4) Rename the original file (to avoid naming conflict).
5) Rename the temporary file to the original file name.
6) Clean up the original file (archive, delete, whatever you normally do).

Using these steps, you've guaranteed that a failure at any point is a quick and easy fix.

I was planning on integrating it into SVN or Git and then creating a new revision/branch with the new file to go into. Along with copying of the file before hand. The actual self-writing file thing will only be done by me once every content patch or expansion pack(basically during maintenance) and i'll be sure to have enough backups. I've already lost all of my code once(hdd failed and then my flash drive stopped working) so now i keep a copy on my hard drive with daily backups to my external, a copy to my micro-sd card i keep in my phone(compressed with 7zip and password protected) this way i currently have three things that have to fail all at once before i'll actually have to be in the crapper so to speak.

I don't trust my computer, the power grid, or the code i even write myself. I always backup before trying anything that's remotely dangerous.

Also thanks to everyone in here thus far for showing me a way to speed up the code whilst still helping me to keep the development side of things simpler.

Offline JGadrow

  • Level 35
  • **
  • Posts: 1,133
  • Reputation: +23/-2
    • View Profile
Re: Easiest/Quickest way to convert a string into workable code
« Reply #12 on: September 24, 2010, 01:07:00 PM »
Awesome that you're taking precautions. I included it because there could be others who come across this post later who aren't as aware of failures such as this. If it was something obvious it wouldn't have been worth commenting on. But, some people (especially when starting out) forget that sometimes errors happen that are not related to programming bugs or software issues.

And I could have easily seen someone doing this and corrupting themselves beyond repair. ;)
Idiocy - Never underestimate the power of stupid people in large groups.


 


SimplePortal 2.3.3 © 2008-2010, SimplePortal