Jump to content
xisto Community
Supa Comix

Browser Game Specific Non N00b Question

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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 https://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.

Share this post


Link to post
Share on other sites

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 https://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.

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

Share this post


Link to post
Share on other sites

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...)

Share this post


Link to post
Share on other sites

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 shotAnother 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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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:

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!

Share this post


Link to post
Share on other sites

Remember what I said ...

PHP script that does only database work (or otherwise invisible work) in an infinite loop

... 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.
Edited by dserban (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

it's worth to buy a hosting with enough supporting features

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.

Share this post


Link to post
Share on other sites

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.


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!]

Share this post


Link to post
Share on other sites

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

 

http://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

if ( ( $io = fsockopen( "yahoo.com;, 80, $errno, $errstr, 5 ) ) !== false ){	$send  = "GET / HTTP/1.1\r\n";	$send .= "Host: yahoo.com;;	$send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";	$send .= "Referer: http://forums.xisto.com/no_longer_exists/;;	$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: yahoo.com;; 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.