Jump to content

time based actions


Go to solution Solved by kicken,

Recommended Posts

What I am trying to do is,

 

I am building a game right now that works on user building, buildings in there "town", for each level of the building in the town it cost money and power.

 

I have done all the coding for the money and power part.

But what I am trying to do is make is so that for each level they upgrade it takes 5 minutes real time from when they click upgrade.

 

 

I am not asking anyone to write the code for me what I am asking is a pushing in the right way so I can write it myself.

 

 

Thank you for your time

 

 

Link to comment
https://forums.phpfreaks.com/topic/285290-time-based-actions/
Share on other sites

First thing that comes to mind is to create a cron task that runs at a defined time like every 1 minute and checks for some info in a table of a txt file that tells it what to post.  Or the cron task runs once and is created and schedule at some future time.  Using the first one would just mean that when an upgrade is done your script would just post some info about it in a table and when the cron task next runs it would look for any un-handled records, grab the info and do the 'post' and then mark the record as done.

Link to comment
https://forums.phpfreaks.com/topic/285290-time-based-actions/#findComment-1464854
Share on other sites

First thing that comes to mind is to create a cron task that runs at a defined time like every 1 minute and checks for some info in a table of a txt file that tells it what to post.  Or the cron task runs once and is created and schedule at some future time.  Using the first one would just mean that when an upgrade is done your script would just post some info about it in a table and when the cron task next runs it would look for any un-handled records, grab the info and do the 'post' and then mark the record as done.

 

 

I was going to do it with cron jobs at first but it wont really work for my needs,

 

 

My first plan was to,

 

When the user clicks upgrade it would save the userID, Base ID, building type and how long to upgrade is finished to a php file,

Then have a cron job run every 30 sec, when the cron job runs it will check the php file that while have a array while loop.

at the end of running the page it will delete the user save info

 

But it would not work for a number of reason,

 

1) user would not be able to see how long to upgrade is finished using this way.

2)As the building level up it takes longer to upgrade using the while it was to hard to add that in and get it to work.

3)delete never worked well.

4)the cron job wont work well if building time is not in 30sec or minutes, I.E if the build time was 1 minute and 10 sec, you would have to wait for 20 sec for the cron job to run 

Link to comment
https://forums.phpfreaks.com/topic/285290-time-based-actions/#findComment-1464902
Share on other sites

  • Solution

Depending on what you actually need to do when the time expires, you may not even need to schedule anything.

 

Essentially what you'd do is when the user clicks the upgrade button, you would record one of two things (or both)

 

1) The time they clicked the button or

2) The time the upgrade would be complete.

 

Eg:

INSERT INTO user_building (BuildingId, CompletedAt) VALUES (?, NOW() + INTERVAL 5 MINUTE)
In your code to display buildings to the user you can check the CompletedAt value and if it is < the current date, calculate the time remaining before it is completed. If the display to the user is all you need to change when the building is complete then you don't need a cron job or timed task at all.

 

If you do need to do some other tasks then you could have a cron join run every minute to search for newly completed buildings and do whatever is necessary. The timing wouldn't be perfect but would be close.

 

If you want to get even better timing what I would do is setup a daemon process you can communicate with and initiate some kind of timer within it. Sort of like your own customized cron daemon.

Link to comment
https://forums.phpfreaks.com/topic/285290-time-based-actions/#findComment-1464911
Share on other sites

I agree with Kicken. Store the time (and duration if it can vary for various tasks) and check if the current time is beyond the stored time plus the duration. If it isn't, make the user wait.

 

Here's an example (requires img_bar.php attached)

<?php
session_start();
if (isset($_GET['btnSub']) && $_GET['btnSub']=='Upgrade') {
    $t = time();
    $d = $_GET['duration'];     // seconds
} else {
    $t = $d = 0;
}
$_SESSION['upgradetime'] = $t;
$_SESSION['upgradeduration'] = $d;     // seconds
?>
<html>
<head>
<meta name="generator" content="PhpED 12.0 (Build 12010, 64bit)">
<title>Example delay</title>
<link rel="shortcut icon"  href="">
<style type="text/css">
img#bar, input#btnSub {
    visibility: hidden;
}
</style>
<script type="text/javascript" src="../jquery-1.10.2.js"></script>
<script type="text/javascript">
var upgradetime = <?php echo $_SESSION['upgradetime'] ?>;
var duration = <?php echo $_SESSION['upgradeduration']?>;
var waiting = false;

function bar() 
{
    var t = new Date();
    var val = (t.getTime()/1000)-upgradetime;
    if (val < 0) {
        $("#bar").css("visibility","hidden");
        return;
    }
    if (val > duration) {
        $("#bar").css("display","none");
        if (waiting) {
            $("#btnSub").css("visibility","visible");
        }
        return;
    }
    waiting = true;
    $("#bar").css("visibility","visible");
    $("#bar").attr("src","img_bar.php?max="+duration+"&val="+val);
}

$().ready (function() {
    bar();
    setInterval("bar()", 2000);
});

</script>
</head>
<body>
<?php
   if ($_SESSION['upgradetime'] > 0) {
?>
    <form id="fm1">
    <img src="img_bar.php" id="bar">
    <input type='submit' name='btnSub' id='btnSub' value='Continue'>
    </form>
<?php
   } else {
?>
    <p>Here goes whatever your page normally does</p>
    <form>
    <!-- duration set to 10, 20, 30secs for demo purposes -->
    Duration<br>
    <input type='radio' name='duration' value='10'> 10 secs<br>
    <input type='radio' name='duration' value='20'> 20 secs<br>
    <input type='radio' name='duration' value='30'> 30 secs<br>
    <input type="submit" name="btnSub" value="Upgrade">
    </form>
<?php
   }
?> 
</body>
</html>

img_bar.php

Link to comment
https://forums.phpfreaks.com/topic/285290-time-based-actions/#findComment-1464921
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.