Author Topic: Basic PHP Game Framework  (Read 3382 times)

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Basic PHP Game Framework
« on: October 26, 2006, 12:28:09 PM »
I've quickly created a simple PHP game framework for anybody who wants to try it!
It's just a small collection of libraries and classes, most of which are written by me.
Thanks for codestryke for the account.lib! (codestryke, if you don't approve, just PM me or reply here, sorry!)

I've forgotten the SQL file, and I didn't test it yet.
There's also a demo register page included, and basic header/footer templates.

Important! It uses ADODB, so you will need to have an adodb directory already on your server! You can change the path to adodb in the config.php file.

If you want to change the welcome email, which is in account.lib.php.
If you need any more support for this, just post a new topic in the PHP board, or here.



Oh yeah, about the database tables:
A table called members with at least the following columns:
id (autoincrement, key)
username (unique, varchar)
password (varchar)
email (unique, varchar)
activated (enum: "yes","no")
vcode (varchar)
There's probably more. If the table is missing something, just tell me.

A table called settings with the following columns:
name (varchar)
value (varchar)

Basically, just add global game settings in that table. Settings are automatically extracted.

Enough delaying, let's get to the files now :)

Included:
config.php
demoregister.php
foot.php
footer.php
head.php
header.php
lib.php
lib/account.lib.php
lib/basic.lib.php
lib/bbcode.lib.php
lib/setting.class.php
lib/user.class.php


Using this code you could easily extend it all to a fully-featured game, but not without some hard work first ;)

Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Basic PHP Game Framework
« Reply #1 on: October 27, 2006, 01:24:54 AM »
So how is this any different than what we already had going?

I really think it would have been a little better to contribute to the project we had already started and been working on. 

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Basic PHP Game Framework
« Reply #2 on: October 27, 2006, 02:39:10 AM »
Meh... this isn't really supposed to be made into a complete game... Just something that anybody can use to make whatever they want.

Offline Cripple

  • Level 17
  • *
  • Posts: 170
  • Reputation: +10/-4
    • View Profile
Re: Basic PHP Game Framework
« Reply #3 on: October 27, 2006, 08:43:17 AM »
zeg - nobody's hasslin ya - i think all dvd was sayin is that we (as a n00b community) have already got the groundwork in place & mostly understood. all he is sayin is we should be startin to creep beyond the lines in the mudmap now.

only my  interpretation.

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Basic PHP Game Framework
« Reply #4 on: October 27, 2006, 10:21:30 AM »
It's alright :)

I'll see if I can add anything to the community project :)

Offline dvd871

  • Level 21
  • *
  • Posts: 238
  • Reputation: +7/-0
    • View Profile
    • Dominion Siege
Re: Basic PHP Game Framework
« Reply #5 on: October 27, 2006, 01:54:00 PM »
Sorry I didn't mean to discredit your work Zeggy.

I'm not to strong on php classes so if that is something that you could explain a bit more that would be great.  I guess I just don't understand why you would use them.  I know that its object oriented and all that but whats the grand scheme of things? 

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Basic PHP Game Framework
« Reply #6 on: October 27, 2006, 03:18:03 PM »
Basically, it's re-useable, anywhere, anytime.

For example, I've got a user class in there. Now, every time you want a user's information, you can type out all the queries or something, but with my user class, all you have to do is call this for each user:
Code: [Select]
$exampleuser = new User($exampleid, $db);

//Examples:
$zeggy = new User($zeggyid, $db);
$dvd871 = new User($dvdid, $db);

This way, all information about the user is extracted automatically when you call that. There's no need for typing multiple queries and checking that the user exists for every user.


You could also write simple functions for that stuff, similar to the account.lib.php, but I like to try writing OO since I'm not so good with it :P

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Basic PHP Game Framework
« Reply #7 on: October 30, 2006, 04:41:27 PM »
Beware of OO (object orientation, ie classes) when dealing with a player account!!!! I learned this the hard way and let me pass this knowlege onto you for reasons NOT to use OO with players. First though let me say I love OO and I try to use it as much as I can anywhere I can. You just have to realize, like I did, that sometimes its best to code the old procedural way ;)

So my first game BordelloBattles I used a class to hold the player information. I would create an instance of the class and it would load up all the player variables, I could pass changed values then when I was done do something like $player->save() and whala the player account was saved. It was awesome and it was easy to use and work with.. Then the game got popular and here is what happened...

A player would open for business or launch an attack. During this process I would create an instance of the player class do the opening or attack, update the data and then save it via the class. Now both the attack and opening for business routines are VERY complex and can run for a while based on what you have in your inventory (the more you had the longer it would take to run).

So anyways Player A would open for business, while that was running on there system another player would launch an attack (very common in a multi-player game). So depending on what would end first the opening for business or the attack the second process would over-write the first and it caused a nightmare!!!!

While you have the class initialized it's holding all the variable in memory so if you have another process running against that same player account with a different class one will over write the other! That is just one example another one is when a player opens two, three or more windows and does various actions via those open windows.

It really is a nightmare to deal with. If your game is running say 10-15 ppl online at a time like mine was when I stated it was no big deal. When you start running 100+ people online at the same time it becomes a HUGE deal, so huge you won't be able to fix it without a HUGE rewrite. In the end I had to take BordelloBattles down for 7 months and completly rewrite the game from the ground up to be a true "multi-player" game. Ok truth be told I also added ADODB and Smarty at the same time, but the real reason it was dropped was cuz I used a class system for player accounts ;)

Hope this makes sense, probably won't to really new people but those that understand and have used OO before should get where I'm coming from!



Creating online addictions, one game at a time:

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Basic PHP Game Framework
« Reply #8 on: October 31, 2006, 08:20:55 AM »
But wouldn't that problem occur the same even if you used functions or just wrote the code straight out onto your page?
If it takes a while to go through in the class, it would take a while outside the class as well.

Maybe I'm not understanding your examples very well :P
Could you explain a little more?
And thanks for this detail, I'll stop using the player class if it's bad :D

Offline compuken

  • Level 10
  • *
  • Posts: 63
  • Reputation: +2/-5
    • View Profile
Re: Basic PHP Game Framework
« Reply #9 on: October 31, 2006, 01:01:23 PM »
You could create your classes with a finished variable or close function and create an action queue class to be added to, so the next action won't start till the previous action is finished. I haven't gone fully into the code base of legend of the green dragon but they have it so you can start an attack while you are online, go offline and go back online and finish attacking the player even though the other play has gone through many actions since. I think they save the state at which the player is, have him attacked, and react upon the new data that the player has since the attack. You could implemete that as well for attacking another player in a pimp game, have the player's state saved, attack him and do a win loss condition on his current state that the player is in now. EX. player A has 15 guns - gets attacked, while getting attacked by player B sells his guns - attack over, player A losses some money and if he has 15 guns (or what ever % you use for loss) take them away, if not then player B gets nothing or the money and 15 guns.

Take a look at the code base for Kiddy Kartel from php game programming book, he creates a lot of states and checks to see if they are finished before going to the next one, its pretty good examples for game coding.

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Basic PHP Game Framework
« Reply #10 on: October 31, 2006, 01:11:57 PM »
I don't have that book, for php game programming :P
I will check it out though. What you're talking about sounds interesting. It could work.
I have a sort of 'saved state' thing on my rpg, except it's pretty much a one player game, so it's not so complicated. If a person is in the middle of a battle, then leaves and comes back later, the player needs to finish the battle before they can do something else.
They just continue from where they left off.

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Basic PHP Game Framework
« Reply #11 on: November 01, 2006, 04:29:38 PM »
Here is an example....

You create an instance of the class
Player does an action that takes a while...

Some where in the code you would have $player->addGold(150) or even $player->gold +=150 to add 150 gold to that player. You would then, after the process is finished, do a $player->save();

The save player routine would look something like
UPDATE player_table SET gold = $gold WHERE playerid = $playerid


If someone where to attack that player during this time either the attack would over write the save or visa versa, it's all up to timing after that.

In procedural code where you only grab the data you need at the start of the routine and then save it after would be
$gold_won = 150

Then the save routine should be:
UPDATE player SET gold = gold + 150 WHERE playerid = $playerid

So in the procedural code you are updating the current gold amount by how much they won rather then how much they had total.... You can write a class to sync this out if you really want to go pure OO but then your adding a lot of code for something that can be easily handled by not using OO.

In the end I did have to go with the above mentioned idea where I placed a "lock" on the player account during an attack or opening for business. However even with that and the large number of players online at a given time I would still get instances of weird data overwrites. When I went to the procedural way all those problems went away!



Creating online addictions, one game at a time:

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Basic PHP Game Framework
« Reply #12 on: November 02, 2006, 03:42:22 AM »
Ah, okay. I suppose it would be fine if I did the updating myself then...
Like:
Code: [Select]
$db->execute("update `users` set `gold`=? where `id`=?", array($user->gold, $user->id));

Offline Mgccl

  • Level 7
  • *
  • Posts: 33
  • Reputation: +1/-0
    • View Profile
    • WebDevLogs
Re: Basic PHP Game Framework
« Reply #13 on: November 09, 2006, 09:59:55 PM »
sticking to... MySQL... no abstraction for me... :)

Offline toto

  • Level 12
  • *
  • Posts: 82
  • Reputation: +1/-0
    • View Profile
Re: Basic PHP Game Framework
« Reply #14 on: December 10, 2006, 03:41:59 AM »
Hi, thank you for the files. I'll try to test them later on today. I've set this page as my homepage in order to remember testing this out.

Offline Marek

  • Level 18
  • *
  • Posts: 177
  • Reputation: +7/-0
  • XHTML, CSS, JS, PHP and MySQL are my pantheon.
    • View Profile
Re: Basic PHP Game Framework
« Reply #15 on: December 12, 2006, 07:24:17 PM »

It's true that it can cause problems if more than one event happens at the same time.

BUT this can be solved using table locks or transactions. Basically you can get MySQL to lock the the rows you need while the script does its stuff. Then unlock the table when you're done.

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: Basic PHP Game Framework
« Reply #16 on: December 13, 2006, 05:32:19 AM »

It's true that it can cause problems if more than one event happens at the same time.

BUT this can be solved using table locks or transactions. Basically you can get MySQL to lock the the rows you need while the script does its stuff. Then unlock the table when you're done.

If you are using INNODB then yes you can use this option if you are using the default ISAM tables then this is exactly what you DON'T want to do! INNODB will lock the row and allow other transactions on other rows to be committed. ISAM lock the entire table, thus preventing other transactions on the entire table not to get processed. When a lock condition exists on the table mySQL will queue the information until the lock is freed. If you have a table that is constantly getting written to (player table) then this queue starts to grow exponentially and the server starts to lag and if bad enough mySQL will just drop it's connection and reset ;)

This is what I've been getting at.. It's one thing to code for 10 player's online, you'll probably never see these things happen... Get 20+ online at the same time and all these nasty little problems start creeping up ;)

Creating online addictions, one game at a time:

Offline toto

  • Level 12
  • *
  • Posts: 82
  • Reputation: +1/-0
    • View Profile
Re: Basic PHP Game Framework
« Reply #17 on: December 15, 2006, 04:48:16 PM »
Yeah, I remember getting 100+ users online on one of my games, and some of them would randomly get the maximum integer which would fit in the money cell - the code had worked fine for many months, and at that point it began doing things wrong.
That, or they were absolute cheaters...

Offline Broda

  • Level 13
  • *
  • Posts: 97
  • Reputation: +2/-0
    • View Profile
    • Nightfall Games
Re: Basic PHP Game Framework
« Reply #18 on: March 23, 2007, 01:17:10 AM »
Just an FYI

So far I've found 3 errors in account.lib

first:
around line 80, in account_logion()   
the line that reads (ignore the changes to table/field names):
$db->execute("update `tblPlayer` set `LastIP`=?, LastLogin=? where `ID`=?", array($ip, now(), $user->id));     

It was missing the second ) at the end of the line so I was getting the "unexpected ';'" error

second:
around line 130, in account_create()
the line that reads:
if($query->recordcount() != 0)

It was missing the $ in front of query so I was getting the "unexpected T_OBJECT_OPERATOR" error

third:
in account_create()
the line that reads:
$insert['JoinDate'] = now();

It told me now() was undefined so I switched it to time() and it worked fine.

Offline sokii

  • Level 14
  • *
  • Posts: 107
  • Reputation: +2/-1
  • Bored? Yea me too... :/
    • View Profile
    • Pyloth
Re: Basic PHP Game Framework
« Reply #19 on: June 10, 2007, 09:54:41 PM »
sorry for the interuption...but codestryke is your real name Nick? just asking cuz that userpic looks familiar...
Forum Designer Since: March 23, 2006
We are still looking for staff at Pyloth! Send me a PM at my forums located here.

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal