Jump to content

PHP Game


Minase

Recommended Posts

hy i was wondering what's the best method in order to make some automatic updates in a php game

 

let's say that the user should gain 1gold each minute but the user is not logged in and the process is happening automatically . what will be the best method?

if it's cron jobs then what you will use if you need constant update (real time) because cron jobs every second is resource killing

 

thank you

Link to comment
Share on other sites

I think i would do something like this. Consider this logic.

 

Create a table in your mysql database for a time, say "last activity", set a time in session also $_SESSION['last_activity'] and evalute it on each page load, if it goes over 10 minutes from last activity time, then reupdate the session time, and update their coins by 10 coins. (1 coin x 10 minutes). Also reupdate their database field saying there last time..

 

when thye log in again when they dont have their session. Then evaluate the time in the database of their activity and compare is too the current time and then evaluate how many coins.

 

 

Good luck. :D

Link to comment
Share on other sites

make a field of integer type to hold a unix timestamp in the users table and store the users username in a PHP session.

 

then use something along these lines.

 

<?php
session_start();

// connect to db or require db connection file here.

$timer     = 60;
$goldToAdd = 1;

$query  = "SELECT gold, time FROM users WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "' LIMIT 1";
$result = mysql_query($query);
   while (list($gold, $time) = mysql_fetch_row($result))
   {
      $time -= time();
         while ($time >= $timer)
         {
            $gold += $goldToAdd;
            $time -= $timer;
         }
      $time += time();

      $query  = "UPDATE users SET gold = " . $gold . ", time = " . $time . " WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "' LIMIT 1";
      $result = mysql_query($query)or trigger_error('Error on line: ' . __LINE__ . ' : Gold could not be updated.');
   }
?>

 

but what if the user is not logged in (the coins will not update) and if other user attacks him then no those coins will not be calculated.. so it's unfair

 

As for that you could update a users gold before they are attacked or w.e. as the attacker will input their username.

Link to comment
Share on other sites

My Ideas

 

1. for that.... i would make a refresh page... that refreshes every minute and you have it on your computer and have leave it up 24/7 and make sure that it can't be accessed by others.

 

2. Make it so any time someone on the site refreshes a page or goes to a new one it updates all the gold based on the last time stamp... For example you go from one page to a other.. it will update everyones gold based off there last gold update timestamp

Link to comment
Share on other sites

I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in

 

? javascript updates php ?

 

No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more.

Link to comment
Share on other sites

I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in

 

? javascript updates php ?

 

No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more.

 

do u Think I wasn't being starcastic?

Link to comment
Share on other sites

andy is on the right path,

you could have someones gold stores what is in the database plus the amount automatically given to them since their account creation

i.e. have a unix timestamp of their creation, then each time someone interacts with that user they work out the amount of gold that user has

which would be

(current unix timestamp - account creation timestamp) / 60 (for minutes).

then add that to the amount in the database, which is not inclusive of any automatically given gold... and could be a negative or positive number...

so when someone stole gold or wanted to see how much gold a person had

$sql = @mysql_query("SELECT accountCreation, gold FROM users WHERE userid = xx");
$results = mysql_fetch_row($sql);

//amount of gold in users account
$gold = (time() - $results['accountCreation']) / 60 + $results['gold'];

 

either that or you can have it so each time a user logs on or a user interacts with another user, a script runs which updates the amount of gold in their account...

 

Link to comment
Share on other sites

I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in

 

? javascript updates php ?

 

No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more.

 

do u Think I wasn't being starcastic?

 

Ahh lmao, I only gave it a quick scan... Sorry ;)

Link to comment
Share on other sites

andy is on the right path,

you could have someones gold stores what is in the database plus the amount automatically given to them since their account creation

i.e. have a unix timestamp of their creation, then each time someone interacts with that user they work out the amount of gold that user has

which would be

(current unix timestamp - account creation timestamp) / 60 (for minutes).

then add that to the amount in the database, which is not inclusive of any automatically given gold... and could be a negative or positive number...

so when someone stole gold or wanted to see how much gold a person had

$sql = @mysql_query("SELECT accountCreation, gold FROM users WHERE userid = xx");
$results = mysql_fetch_row($sql);

//amount of gold in users account
$gold = (time() - $results['accountCreation']) / 60 + $results['gold'];

 

either that or you can have it so each time a user logs on or a user interacts with another user, a script runs which updates the amount of gold in their account...

 

 

You would also have to track the amount of times their gold has been updated with that one, otherwise it would add one gold per minute since account cration each time the code piece was run. Say 4 mins after account creation, they would have 10 gold rather than 4

ie, 1 + 2 + 3 + 4

rather than 1 + 1 + 1 + 1

 

Well, assuming the code runs once per min.

Link to comment
Share on other sites

noo i meant so the automatically added gold was never stored in the database...

instead only acquired gold was stored in there... and then each time the user interacted with someone or someone viewed their gold it would add the amount in teh database + (current unix timestamp - account creation timestamp) / 60

 

i.e. if they might have -400 gold in the database however since they've been a member so long they actually have 10000 gold.

 

i guess then there's issues if you want to select the person with the most gold from the database etc etc...

 

other way is to write a script which runs each time the user is interacted with... and after it puts in gold it adds the current unix timestamp to the database, so next time someone interacts with the user it adds the amount of gold accumulated since the previous interaction...?

Link to comment
Share on other sites

noo i meant so the automatically added gold was never stored in the database...

instead only acquired gold was stored in there... and then each time the user interacted with someone or someone viewed their gold it would add the amount in teh database + (current unix timestamp - account creation timestamp) / 60

 

i.e. if they might have -400 gold in the database however since they've been a member so long they actually have 10000 gold.

 

i guess then there's issues if you want to select the person with the most gold from the database etc etc...

 

other way is to write a script which runs each time the user is interacted with... and after it puts in gold it adds the current unix timestamp to the database, so next time someone interacts with the user it adds the amount of gold accumulated since the previous interaction...?

 

Also, issues with amount of gold spent, issues with checking if a user can afford an item....

 

As for your solution, thats exactly what the code I supplied does...

//edit

 

Although I should have written it to a function/class method which takes a username and updates that users gold by the required amount, easily done though.

Link to comment
Share on other sites

I would just do somethink like Andy posted. Probably just use ajax to update their gold when they are logged on, then just update their gold when they log in

 

? javascript updates php ?

 

No, AJAX uses Javascript to sent HTTP messages to PHP scripts that send back content so that javascript can update content without refreshing the whole page, theres a whole board dedicated to AJAX on PHPFreaks if you wish to learn more.

 

do u Think I wasn't being starcastic?

 

Ahh lmao, I only gave it a quick scan... Sorry ;)

 

I was just havin fun sorry about that lol

Link to comment
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.