Loading...


bookmark - Browser Game Specific Non N00b Question

Browser Game Specific Non N00b Question

 
 Discussion by Supa Comix with 17 Replies.
 Last Update: August 12, 2007, 10:33 pm
 
bookmark - Browser Game Specific Non N00b Question  
Quickly Post to Browser Game Specific Non N00b Question  w/o signup Share Info about Browser Game Specific Non N00b Question  using Facebook, Twitter etc. email your friend about Browser Game Specific Non N00b Question Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Right me and my mate have begun planning to make a browser game (omg yes another guy wanting to do it). I know php, java, ajax, html, vb, C++ etc to a certain level like amateur. but thats not the point.

I have been thinking about a lot of things an apart from the sheer hugeness of the size of such a project i'd like to know how the game functions in the background. With games such as ogame you have things happening in the background, fleets moving, attacks happening etc. I'd like to know how i'd get into sorting all that out. How i'd get attacks to function at the right time, how fleets get to their destination. How buildings finish, basically how the databases update themselves without anyone actually being online. Like the running whilst everyones asleep. That's the one problem i've got with such a project how i'd get it to work in the background... i've looked through other tutorials and scanned the forums nothing seems to be what i'd like to know.

Does anyone know?

   Thu Aug 9, 2007    Reply         

I've never wrote anything similar, but from what I know, you need to have a running timeline for your game. And everything will have an endtime, at which it should have been done, like in your case "get attacks to function at the right time, how fleets get to their destination. How buildings finish". Then you can have cron or configure crontab http://en.wikipedia.org/wiki/Crontab to call a php script every minute. The smallest is minute for cron. You can look for other similar scheduler which can have smaller timestep, but that also means you can't use standard linux web server. So whenever you script run, it checks for the endtime, then take action on those which is expired then decide on what to do next.

Just a suggestion. Maybe someone who had really wrote one could drop a bit of tips here also.

   Fri Aug 10, 2007    Reply         

QUOTE (faulty)

I've never wrote anything similar, but from what I know, you need to have a running timeline for your game. And everything will have an endtime, at which it should have been done, like in your case "get attacks to function at the right time, how fleets get to their destination. How buildings finish". Then you can have cron or configure crontab http://en.wikipedia.org/wiki/Crontab to call a php script every minute. The smallest is minute for cron. You can look for other similar scheduler which can have smaller timestep, but that also means you can't use standard linux web server. So whenever you script run, it checks for the endtime, then take action on those which is expired then decide on what to do next.

Just a suggestion. Maybe someone who had really wrote one could drop a bit of tips here also.
Link: view Post: 108902


Acutally what Lee said were quite right. I've done some cron that run in the background on certain time from the web server. The cron job are just scheduler that will execute those php codes to the time you specified. You can schedule a game engine to run at the background and update some specified data in the database. Eg. A soccer match engine can be program to run when the cron job starts and end 90mins later. every minute will run some kind of script that update the database. So even if there's anyone online, they can see the what happen without you (Administrator) clicking for it to execute.

But I think that's not the only way to do it. Maybe you can google and try finding some better and effective solutions out there.

Cheers

   Fri Aug 10, 2007    Reply         


Right i did have a quick shufty around google as well as on the crontab stuff and i found out that my webhoster does not support cron functions. Is there any other way that people know? Preferably free ones...

The server is apache (dunno if that helps or not...)

   Sat Aug 11, 2007    Reply         

Cron is part of linux. Thus all linux server will have cron built in. It's just the matter whether they let you have access to it or not. Those are usually left for premium services. If it's a windows server, then you're out of luck. Apache can be windows based or linux based.

For some server, you can request the hosting company to help you install the cron job, maybe you can give it a shot

Another way can i think of is to run an update script whenever someone view the page. The script will check the current time and compare with it's last run time, and update the events properly. That way, if only when ppl is looking at your page, then it's data is updated. The trick here is just comparing the time, and applying the changes depending on time difference.

There's few thing you need to take note. If you game is complex, then it might take a while to update the events. If it happens that no one logon for the past few days, then it will take a long time to first show the page. There's a duration limit to which your script can run. Normally it's 30sec, to prevent problematic script from running indefinitely, taking up all the cpu cycle along.

Let see if anyone else came up with a better idea

   Sat Aug 11, 2007    Reply         

I think PHP function fsockopen() is the answer to your question, check out this article:

http://robert.accettura.com/archives/2006/...ssing-with-php/

and I'll be back with an architectural explanation of what I think your solution will be.

   Sat Aug 11, 2007    Reply         


From what I know about the architecture of Apache / PHP and from what I understand from your description, it sounds like you need to use an asynchronous call to a PHP script that does only database work (or otherwise invisible work) in an infinite loop, with a "sleep 500 miliseconds" statement or whatever frequency you want for your housekeeping activities.

Apache spawns a new process in the operating system for every PHP script you invoke via browser.

The whole thing works on the basis of the request-response paradigm, but for your purposes, you need to separate the request part from the response part.

So you have this infinite loop housekeeping script handy and you invoke it with fsockopen once (and only once!!!).

fsockopen does the request part, after which you simply omit capturing the response and close your browser window - voila, you have your housekeeping process running in the background.

If you invoke it several times, you might have several instances of this script running at the same time, killing your server and stepping on each other's toes, crossing wires etc.

   Sat Aug 11, 2007    Reply         

Yes, that sounds good, but another thing is that most server restrict access to fsockopen(), because with the right coding, you can do virtually anything with it. Super powerful.

   Sat Aug 11, 2007    Reply         

Thanks people. I had a look at this Asynchronous Processing With PHP thing and i found that i didn't fully understand it. How would i set it up to, for example, do:

CODE

print " - This is a test!";


At one minute intervals?

What pages would i need to construct etc? Sorry for all the questions but im really having trouble getting my head around this!

   Sat Aug 11, 2007    Reply         

Remember what I said ...

QUOTE (dserban)

PHP script that does only database work (or otherwise invisible work) in an infinite loop
Link: view Post: 108973

... emphasis on "invisible" ...
The PHP script I was talking about has no way of communicating with any browser, so the idea that it would print something does not make any logical sense.
It is a PHP script that does not take any input and as well does not produce any output. It's a script that is supposed to run in the background and update database tables in real time based on what online players happen to be doing as they interact with your system.
The browser that is connected to your game system might actually run a combination of JavaScript and AJAX to provide interactivity between the online actions of the player and the ever-changing situation in the database tables, which is being continuously worked on by the background script.

   Sat Aug 11, 2007    Reply         

Asynchronous Processing is actually running the code not synchronous with the activity from the users. It also means running in background with crontab or as what dserban mentioned. It's having your event update scripts in one page, then have something call it every minute. If you can't get crontab, then if your server allows fsockopen, you can call back to your page, and have to run by itself, either indefinitely or, call itself back, with a special limited pattern, so it keep running in the background. Another method is using AJAX, to call the script from the user side from time to time, even though the user is not refreshing the page. For all this, you need some kind of method to keep track of the running thread, so if there's already a thread doing the job, the current one can skip and return right away, to save processing.

Seriously, if you really need to do such a complex game, it's worth to buy a hosting with enough supporting features. If you game is not that complex, as not requiring too much processing, then you can try the event update on every page viewing method i mentioned earlier.

It's also good to look into other's works. There's a few opensource online game. Try sourceforge.net

   Sat Aug 11, 2007    Reply         

QUOTE (faulty)

it's worth to buy a hosting with enough supporting features
Link: view Post: 108977

If you can't find a hosting platform for your game, I would recommend - at least in a first stage - running the game on your own PC at home and giving your gaming buddies access to it. The only thing you need to do is open a port or two for direct access from the Internet.
The ports you probably need to open are 80 for Apache and 3306 in extreme cases.
Even with those ports open, you still have the option of controlling which IP addresses (your gaming buddies' PCs) can connect to your online game, even by using the brain-dead built-in firewall that comes bundled with Windows XP SP2.

   Sat Aug 11, 2007    Reply         

QUOTE (dserban)

Remember what I said ...

... emphasis on "invisible" ...
The PHP script I was talking about has no way of communicating with any browser, so the idea that it would print something does not make any logical sense.
It is a PHP script that does not take any input and as well does not produce any output. It's a script that is supposed to run in the background and update database tables in real time based on what online players happen to be doing as they interact with your system.
The browser that is connected to your game system might actually run a combination of JavaScript and AJAX to provide interactivity between the online actions of the player and the ever-changing situation in the database tables, which is being continuously worked on by the background script.
Link: view Post: 108976


Yeah it needs to be invisible and it will just update database/tables in the background. I just need to know how i'd go about setting that up. What pages i need etc. Where i'd need to put the code into update the tables etc. How would i go about getting to start, how i'd get it to do that infinite loop every 500 milliseconds... As soon as i know that i'll be happy :P

[thanks for the help ur giving me btw... most appreciated!]

   Sat Aug 11, 2007    Reply         

There is a good hands-on example on the fsockopen official documentation page at:

http://www.php.net/function.fsockopen

Look a 10th of the way down at the example code posted by:
jbr at ya-right dot com
There is a sequence of code that looks like this

CODE

if ( ( $io = fsockopen( "www.yahoo.com", 80, $errno, $errstr, 5 ) ) !== false )
{
$send = "GET / HTTP/1.1\r\n";
$send .= "Host: www.yahoo.com\r\n";
$send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
$send .= "Referer: http://www.yahoo.com/\r\n";
$send .= "Accept: text/xml,application/xml,application/xhtml+xml,";
$send .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
$send .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$send .= "Accept-Language: en-us, en;q=0.50\r\n";
$send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$send .= "Connection: Close\r\n\r\n";

fputs ( $io, $send );

... this is the request part (fputs). But then he goes on to use fgets, which is the response part. That you don't need to do ... in fact it's a bad idea to attempt it.
Now instead of $send .= "Host: www.yahoo.com\r\n"; you would obviously have $send .= "Host: link-to-PHP-invisible-housekeeping-script\r\n";
Your PHP invisible housekeeping script either reacts to the actions of the online gamers which are reflected in the database as table entries, or otherwise generates random events based on rules you preset - like a Pacman that keeps popping out of random places at random intervals trying to eat you alive if you're not careful. That you need to program yourself, and you need to define how those two things interact by defining the data model correctly.

   Sat Aug 11, 2007    Reply         

Ah right ace. Thanks alot. I will give that a go very shortly. Just one last tiny question:

$send .= "Referer: http://www.yahoo.com/\r\n";

I would replace the http://www.yahoo.com with my own website address i assume?

   Sat Aug 11, 2007    Reply         

Ummmmmmmm ... no.

Actually you only need something like this:

CODE

$send = "GET / HTTP/1.1\r\n";
$send .= "Host: http://gameserver/housekeeping.php\r\n";
$send .= "Connection: Close\r\n\r\n";

But after taking a better look at the code I gave you, I realize now that you also need to replace fsockopen( "www.yahoo.com" ... ) with fsockopen( "<IP-address-of-your-game-server>" ... )

   Sat Aug 11, 2007    Reply         

QUOTE (dserban)

Ummmmmmmm ... no.

Actually you only need something like this:

CODE

$send = "GET / HTTP/1.1\r\n";
$send .= "Host: http://gameserver/housekeeping.php\r\n";
$send .= "Connection: Close\r\n\r\n";

But after taking a better look at the code I gave you, I realize now that you also need to replace fsockopen( "www.yahoo.com" ... ) with fsockopen( "<IP-address-of-your-game-server>" ... )
Link: view Post: 108997


right and i'd need to use the above code when someone logs in?

   Sun Aug 12, 2007    Reply         

No.
The idea is that you need to kick-start this house keeping process and have only one instance of it running at any point in time.
I can imagine that you would have an administration / superuser section for your game, where you pull all kinds of levers to control the game.
The above code would be part of a page in this administration section called "Kick-start housekeeping process".
The link to this page would be active if the process is not already running, and would be greyed out or missing altogether if it is.
Or something to that effect, it's really up to your imagination, personal preference and talent for designing systems.

   Sun Aug 12, 2007    Reply         

Quickly Post to Browser Game Specific Non N00b Question  w/o signup Share Info about Browser Game Specific Non N00b Question  using Facebook, Twitter etc. email your friend about Browser Game Specific Non N00b Question Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

More Linux Questions [mandriva]

Ok, well after my last post about a month or so, i have another n00b queries in Linux. My last problem of selecting a good linux distro for a beginner was solved. I opted for Ubuntu, but since i didnt have much free time during college and exams i used to pawn upon friends computers that had linux i ...more

   05-Dec-2005    Reply         

Liquid Webtoy A Fluid Similation...

Need to relax with your children after a hard day at the office? Need something fairly simple, because all day long your mind has been wrapped around the complex? Why not try Liquid Webtoy ver2.4 from Dan-ball and relax! This i ...more

   17-May-2007    Reply         

How I Use Ie Vs. How I Use Firefox ...

This posting is best viewed in Firefox. *** IE *** First, let me begin by saying that I'm not scared of using Internet Explorer. I have done a couple of things to it to turn it into a well-performing and relatively secure application. 1. I downloaded ToolbarCop at ...more

   05-Aug-2007    Reply         

Can Someone Please Lend A Hand? Php need direction   Can Someone Please Lend A Hand? Php need direction (4) (2) Easily Making A Game   Easily Making A Game