Resources per Hour, Best page for query?

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Resources per Hour, Best page for query?

Post by vitinho444 »

Hey guys!!

Im near completion of my browser game and im making the resources system.
I already have a working time function that works 200% xD

So now i want to deliver some resources that are stored in the db to the player village. I know the query and all but what page should i use to store the query?

Like i want it to be 1hour precisely but if i put it on inde.php and you are on the other, does it works anyway?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Resources per Hour, Best page for query?

Post by Jackolantern »

What do you mean by "Like i want it to be 1hour precisely but if i put it on inde.php and you are on the other, does it works anyway"? I am not familiar with the layout of your game. What pages would be an option to put the resource update code on?

Also, unless you use crons, it won't be exactly precise. Until you get probably several thousand players, you can't guarantee something will run on the second you want it to using the "on-view" crons-alternative technique. While you have a small-to-moderate sized community, sometimes minutes will pass without something even loading the index.php page. But the beauty of the "on-view" method is that if the players aren't loading the page, and no scripts are being run, they can't know that your action didn't occur. As soon as they try to go check, the act of them going to check will cause the action to occur.
The indelible lord of tl;dr
User avatar
UnknownUser
Posts: 51
Joined: Tue Sep 08, 2009 2:54 pm

Re: Resources per Hour, Best page for query?

Post by UnknownUser »

use batch time set with windows scheduled task and set the time and in the batch tell it to visit the site, to load the script done :P
a real cool idea i just came up with is try to make use of facebook,twitter, etc

you can do this use the facebook api every time you get a feed update or something or a twitter post - > run script to load resource
that way the work gets done with the help of social network, or try to connect it to something that is always time consistent

search google there is a lot of easy ways to do this another way is to use an api from like a img host and every time some one uploads
a pic it runs the script but need to do a restriction to the function so it wont run for every img that would cause some problems

there is an infinte ways to make this work or you can make an real time checker for the game that don't rely on other ppl to function.

or just use crons :P

sorry for the long txt lol

edit:
Like i want it to be 1hour precisely but if i put it on inde.php and you are on the other, does it works anyway?
just use a constant or something that connects globally with all pages with the function so that it wont Matter on what page
you are on all of them send a load, but don't forget to check if it has all ready been loaded recently
When a programming language is created that allows programmers to program in simple English, it will be discovered that programmers cannot speak English. ~Author Unknown
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Resources per Hour, Best page for query?

Post by vitinho444 »

@Jacko
Well i mean that if i put it on a page where the village info is displayed, and if the player is not seeing that page on the 1 hour momment then would the script do his job?

I heared about cron before but i dont know how to setup, and i cant use a constant because the resources player gets its not the same for every player...
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Resources per Hour, Best page for query?

Post by Winawer »

vitinho444 wrote: Well i mean that if i put it on a page where the village info is displayed, and if the player is not seeing that page on the 1 hour momment then would the script do his job?
If nobody sees the results, the script doesn't have to run. In other words, include the script on every page it's needed instead of just putting it on one page.

Also, your term "1 hour moment" sounds confusing. You should be checking for 1 hour or more since last tick.

Something like

Code: Select all

while( hasTicked() ) {
    runTicker();
}

function hasTicked() {
    $query = "SELECT lasttick FROM ticker";
    $result = mysql_query( $query );
    $row = mysql_fetch_assoc( $result );
    if( time() > ( $row['lasttick'] + 3600 ) ) {
        return true;
    }
    return false;
}

function runTicker() {
    $query = "UPDATE ticker SET tickerstate = " . STATE_RUNNING;
    mysql_query( $query );
    if( mysql_affected_rows() > 0 ) {
        updateResources();
        foo();
        bar();
        $query = "UPDATE ticker SET lasttick = lasttick + 3600, tickerstate = " . STATE_IDLE;
        mysql_query( $query );
    }
} 
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Resources per Hour, Best page for query?

Post by vitinho444 »

well for time i got a the time(); function

i use:

Code: Select all

$current_time = time();
		$last_time = $village['lasttime']; //this is the current time since the other last time + the interval (1hour). It works nice :D
		if($current_time > $last_time)
		{
			//time passed
		}
		else
		{
			$time_left = $last_time - $current_time;
			//time has not passed
		}
So i will put it in every page then xD
Thanks
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Resources per Hour, Best page for query?

Post by Chris »

Another method, rather than having to put this on every page would be to run a never ending script.

Code: Select all

while( true ) // infinite loop
{
    set_time_limit(99999); // make sure the script doesn't stop

    $current_time = time();
    $villagesQuery = mysql_query("SELECT `id` FROM `villages` WHERE `lasttime` >= $current_time");
    while( $village = mysql_fetch_assoc($villagesQuery) )
    {
        // do something
        // eg. resource_id, 1 = iron 
        mysql_query("UPDATE `village_resources` SET `total` = `total`+100 WHERE `village_id` = {$village['id']} AND `resource_id` = 1"); // add 100 iron to village resources

        mysql_query("UPDATE `villages` SET `lasttime` = '". (time() + 3600) /* update lasttime to one hour in the future */  ."'");
    }

    sleep(1); // wait one second before looping again
} 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Resources per Hour, Best page for query?

Post by vitinho444 »

wow its a nice system, but sorry to ask :D where do i put the script so it ran everytime.

And another thing.. im storing the ammount of resources villages get each hour in their database field so its not the same for everyone so how can the script before adding the resources can identify how much to add?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: Resources per Hour, Best page for query?

Post by Chris »

You have several options. You have to save it as its own script. Call it updateLoop.php or something that's easy to remember.

Then you can either visit it with a browser by simply going to http://localhost/updateLoop.php (notice the page will keep loading forever). Or you could run it from the command line of your server.

I don't know what operating system you use or whatnot. If you use windows and wampserver try this:
  • click Windows button -> Run (winkey+r) Or go to C:\Windows\system32\cmd.exe
  • type in "cmd" (without the quotations of course)
  • type in "C:\wamp\bin\php5.3.9\php.exe C:\wamp\www\path to script\updateLoop.php"
Your script will now be running, forever until you cancel it or close the command prompt.

Linux would be something like as follows, depending on your distribution.
  • > php /var/www/path to script/updateLoop.php
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Resources per Hour, Best page for query?

Post by vitinho444 »

well, i think its very nice but doesn't it f*ck the bandwidth if the page is always loading :S

Im thinking making a php script right, but then include it on every page, the problem is.. if there's no players playing.. it won't work :lol:
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Post Reply

Return to “Coding”