Jump to content

Recommended Posts

yes sorry a value in a database....

 

has to work out 20% of $maxenergy... and give it to $energy.

 

---------------------------------------------------------------------------

 

to help people understand what i mean... im building a game with php....

 

i have a energy bar which i need going up 20% every 5 minutes.

 

$maxenergy = 100 (but can change)

 

now i could just :

 

$energyupdate = $player->energy + 5;

$query = $db->execute("update `players` set `energy`=? where `id`=? and `energy`<`maxenergy`",

array($energyupdate, $player->id));

 

 

but because maxenergy can change +5 would be unfair on alot of players... so i want it to gain 20% of maxenergy instead.

 

 

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635543
Share on other sites

<?php
$query = "SELECT * FROM players";
$result = mysql_query($query) OR die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
  $energy = $row['energy'] + ($row['maxenergy'] * .20);
  $energy = ($energy > $row['maxenergy']) ? $row['maxenergy'] : $energy;
  $query = sprintf('UPDATE players SET energy = %d WHERE id = %d', $energy, $row['id']);
  mysql_query($query) or die(mysql_error());
}
?>

 

Math skills really come in handy for programming. =/

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635842
Share on other sites

At Darkwater that is disgustingly bloated

 

You can write your query to do update all rows

Update `players` SET energy = (energy+(maxenergy*.2))

 

Now if you don't have a way to set a max value to energy  (so E is not greater than Emax)you need to add a conditional statement

'but don't do a loop to run 5000 queries run just 1 with a condition part please

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635956
Share on other sites

You need to make sure it's not greater than the maxenergy.  Otherwise I would have done it in one query...

 

Like I said I believe you can set a MAX value for a field to take (and if greater it automatically sets to be what ever MAX is)

 

However you can do conditional like this (untested but can be debugged very easily)

 


UPDATE
`players`
if((energy+(maxenergy*.2) <= maxenergy),
	energy = energy+(maxenergy*.2),
	energy = maxenergy
)

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635967
Share on other sites

Yeah, I know cooldude, but it doesn't really matter for something like this, considering that it'll be run via cron and it'll take like, 0.3 seconds to run anyway.

 

 

When u can run 1 query instead of 1 query for each user (which in this forum would  be near 69 thousand)  I think you rather run 1 query.

 

 

69 thousand queries * 4 times an hour * 24 hours a day = 3.312 million queries a day

 

1 query * 4 times an hour * 24 hours a day = 96 queries a day

 

I leave you to make your own predictions

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635972
Share on other sites

Less queries that are written properly  are always better. 

 

A query that doesn't have a WHERE statement means the table needs  not be seeked each time (as you are doing 69k seeks)

 

The simple math being executed is a simple check and takes aggregate time to run (even at running it twice)  + because I have no conditional limiter to my query the given table is only seeked (and updated) in a single pass.

 

You can run test but I have a strong feeling I win in this scenerio

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-635987
Share on other sites

I just ran a test, and mine took about 21 seconds to run with 70000 rows.  I didn't try yours because it throws a syntax error I think and I don't feel like cleaning it up.  70000 rows is quite a lot of rows though, and if he was using cron I can't see the issue.  If you clean up your query I'll try testing it too.

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-636003
Share on other sites

well cron still is using processing on your server and if the script is using processing power (or mysql bandwidth) that is bandwidth and processing power that can't be used elsewhere.

 

Sure a user isn't sitting their watching the cron run but you could be bogging down (or crashing your server) because of the cron job

Link to comment
https://forums.phpfreaks.com/topic/123071-help-with-crons/#findComment-636010
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.