Welcome Guest, Not a member yet? Register   Sign In
How to setup an automatic function?
#1

[eluser]Corey Freeman[/eluser]
For my game, the player's stamina needs to replenish itself automatically once every 6 hours. I thought setting up a cron job would work, but it won't run the controller that way. So I'm not sure how to go about doing this.

Here's the code I have for the stamina update:

Code:
<?php

class Stamina extends Controller {

    function Stamina()
    {
        parent::Controller();    
    }
    
    function index()
    {
         $query = $this->db->get('players');
         foreach($query->result() as $row):
         $max = $row->max_stamina;
         $data = array('stamina' => $max);
         $this->db->where('id', $row->id);
         $this->db->update('players', $data);
         endforeach;
    }
}

This is really important so any help is majorly appreciated!
#2

[eluser]richthegeek[/eluser]
A cron job would work fine - you are just doing it wrong.

You need the cron job to either run a direct call to the `mysql` program or use the php executor to run a script.

Two versions;
Code:
mysql --user=USERNAME --password=PASSWORD < echo 'USE database_name; UPDATE `players` SET `stamina` = 100'
Code:
php /path/to/script/update_stamina.php

The first one is easier, but less secure as anyone inspecting the cron list will see the username and password.

The second one is a bit more complex, as you need to secure "update_stamina.php" against web-side executions, which can be done by just placing it outside the webroot and possibly checking the execution path (/bin/sh probably). Update_stamina.php would be a simple linear script (non CI) which just connects and runs the query.

A final option if you are lazy is to run the systems web browser (lynx, www-browser, OR a graphical one such as firefox, remembering to `export DISPLAY=:0.0` first) and point it to the correct URL, checking the IP and UA-string for security.
#3

[eluser]Corey Freeman[/eluser]
Thanks. I have two questions:

How do I know what that "path to script" is, exactly?

How do I use a variable to set the stamina? Not all players have the same maximum threshold, so could I run a check and then the update function?
#4

[eluser]Boris Strahija[/eluser]
You can also run a CI controller/method via wget. Something like this:
Code:
wget http://someurl.com/my_controller/my_method
#5

[eluser]richthegeek[/eluser]
Ah, didn't consider wget - a much more elegant solution for just grabbing a page automatically and executing through the CI stack. Would still have to secure via IP/UA checking of course.

"path to script" is wherever you put the script. If you are unsure, type "pwd" to get the current working directory (ie, where you are such as /home/corey/httpdocs/)

For the maximum stamina, I assume you have two columns in your table along the lines of "current_stamina" and "max_stamina". If so, simply do:
Code:
UPDATE `players` SET `current_stamina` = `max_stamina`

From purely a game theory viewpoint, it'd probably be better to increase the stamina steadily over time rather than maxing it at a set point, so as to avoid people doing massively expensive things just before the tickover and suffering no penalty for it. Something like this every 216 seconds would replenish from 0% to 100% over six hours:
Code:
UPDATE `players` SET `current_stamina` = `current_stamina` + (`max_stamina` / 100);
UPDATE `players` SET `current_stamina` = `max_stamina` WHERE `current_stamina` > `max_stamina`;
#6

[eluser]KingSkippus[/eluser]
[quote author="Corey Freeman" date="1279503091"]For my game, the player's stamina needs to replenish itself automatically once every 6 hours. I thought setting up a cron job would work, but it won't run the controller that way. So I'm not sure how to go about doing this.

Here's the code I have for the stamina update:

<snip!>

This is really important so any help is majorly appreciated![/quote]

I suggest richthegeek's second method outlined above. Create a script that looks something like this:

Code:
&lt;?php // refresh.php: Refresh stamina script
$db_user = 'update_username';  // A user who has UPDATE privileges to the database
$db_pass = 'update_password';  // Password of the user above.

$dbh = mysql_connect('localhost', $db_user, $db_pass);
if ($dbh) {
    mysql_select_db($db_name);

    // This query updates all of the players and sets the stamina to max_stamina.
    $sql = 'UPDATE `players` SET `stamina` = `max_stamina`';

    if (mysql_query($sql, $dbh) !== true) {
        echo "Unable to update stamina. Check SQL syntax.\n";
    }
}
else {
    echo "Unable to connect to database server. Check user name and password.";
}

mysql_close($dbh);

?&gt;

Then use sudo crontab -e and add the following line:

Code:
# Run stamina refresh script every day at 2:00am
* 2 * * * /usr/bin/php /path/to/refresh.php

To secure all of this stuff, you need to lock down the file so that other users can't read it. Assuming that you used sudo crontab -e (instead of just crontab -e), you can do so with the following commands in the directory in which refresh.php is stored:

Code:
sudo chown root:root refresh.php
sudo chmod 500 refresh.php

If you want to make it even more secure, create a database user account that has only update privileges on that one database or even that one table, and use that user account name and password in the script instead of an administrative account or one that has higher or unneeded privileges. That way, if by some weird chance the script is compromised, you won't have given away the keys to the kingdom, just to that one table.
#7

[eluser]KingSkippus[/eluser]
By the way, an alternative method of doing this that would use CodeIgniter and bypass crontabs and creating other scripts would be to have a helper function that does the following:

- Check a database config table to see when the last time the stamina was refreshed.
- If it was more than a day ago, run the above UPDATE query on the table and write the current time to the config table.

Then add a call to that helper function in the constructor of all Controllers. If it has been less than a day, nothing will happen. If it has been more than a day, it will run the update query before doing everything else.
#8

[eluser]Phil Sturgeon[/eluser]
[quote author="Corey Freeman" date="1279503091"]For my game, the player's stamina needs to replenish itself automatically once every 6 hours. I thought setting up a cron job would work, but it won't run the controller that way. So I'm not sure how to go about doing this.[/quote]

Google search for "CodeIgniter command line" :-p

Wiki: CI on the command line
#9

[eluser]Corey Freeman[/eluser]
[quote author="Phil Sturgeon" date="1279548225"][quote author="Corey Freeman" date="1279503091"]For my game, the player's stamina needs to replenish itself automatically once every 6 hours. I thought setting up a cron job would work, but it won't run the controller that way. So I'm not sure how to go about doing this.[/quote]

Google search for "CodeIgniter command line" :-p

Wiki: CI on the command line[/quote]

Thanks. I'm not even sure what a command line is so that never occurred to me.
#10

[eluser]bretticus[/eluser]
Using the CodeIgniter "stack" for a simple database update via cron is way overkill. Just take KingSkippus's suggestion and work on it until you get it working. Steer clear from the wget method. I suppose it's convenient but having to write code to prevent any user/bot in the whole wide world from running your script on a whim is also overkill. Yes, you'd have security by obscurity (or you could use PHP to deny access--or even apache), but it's just bad practice to allow access to code that shouldn't be accessed via the Web (just doesn't make any sense.) Seriously, the path to follow here is the one that KingSkippus has demonstrated. Everyone else is suggesting things that might be easier for you to grasp with your understanding of CodeIgniter I suppose.

No offense, but you are building a game and do not know what a command line is or how to specify a file path? Sorry, just seems like you might need to get a basic understanding of a basic concept or two before jumping into more advanced projects. Might save you form making a few mistakes along the way. I am impressed that you are doing what you are doing despite that lack.




Theme © iAndrew 2016 - Forum software by © MyBB