Javascript timer with php. Help Please. :D.

votter

New Member
Messages
34
Reaction score
0
Points
0
<?php
session_start
();
if(!
session_is_registered(myusername
)){
header("location:index.html"
);
}
require_once(
'db.inc.php'
);

// function for generating errors
function errors($error){
#2
if (!empty($error
))
{
#3
$i = 0
;
while (
$i < count($error)){
#4
echo "<p><span>".$error[$i]."</span></p>\n"
;
$i ++;}
#2
}
#3 // close if empty errors
}
#4 // close function



$username = $_SESSION['myusername'
];
#COMMENT -> You need to put this ^ in, otherwise the $username in the mysql query below is blank.


// You had SELECT ALL `members`... instead of SELECT * FROM `members`...
$sql = "SELECT * FROM `members` WHERE `username` = '$username' LIMIT 1"
;
$res = mysql_query($sql, $dbc
);
$user = mysql_fetch_array($res
);


require_once(
'st.inc.php'
);

require_once(
'Army.inc.php'
);

require_once(
'stats.inc.php'
);



srand ((double) microtime( )*1000000
);
$find = rand(1,3
);
echo
"You found " . $find . " logs<br>"
;


$Wood = $Resources['Wood'
];
$addWood = $find
;
$newWood = $Wood + $find
;

if (!
$error
) {

mysql_query("UPDATE `Resources` SET `Wood` = '$newWood' WHERE `userid` = '$user[id]' LIMIT 1"
)
or die(
mysql_error
());
}
{
errors($error
);
}

?>

<html>
Wood: <?php echo $Resources['Wood'];
?>
</html>

<SCRIPT>
function refreshPeriodic() {
// Reload the page every 30 seconds
location.reload();
timerID = setTimeout("refreshPeriodic()", 31000);
}
timerID = setTimeout("refreshPeriodic()", 31000);
</SCRIPT>


<html>
<head>
<script language="JavaScript">
var i=30;
function countDown()
{
if(i > 0)
{
document.forms[0].elements[0].value=i;
i = i-1;
var c = window.setTimeout("countDown()", 1000);
}
}

</script>
</head>
<body onLoad="countDown()">
<form>
<input type="text" name="counter" size="3">
</form>
</body>
</html>
Is what I have, I am trying to get it so when the timer reaches 0, you get your wood. It works, but I am having the problem, because if u refresh screen, u automatically get the wood. Anybody know how I can fix this? Tyvm for the help.
biggrin.gif
.
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
You could try using AJAX instead of refreshing the page. Or have another timer that counts up and passes the time to the PHP when the page is refreshed, then the PHP code would only give the wood if the second timer was at thirty (Meaning that the user had been on that page for 30 seconds.)

Does that make sense?
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
<?php
session_start
();
if(!
session_is_registered(myusername
)){
header("location:index.html"
);
}
require_once(
'db.inc.php'
);

// function for generating errors
function errors($error){
#2
if (!empty($error
))
{
#3
$i = 0
;
while (
$i < count($error)){
#4
echo "<p><span>".$error[$i]."</span></p>\n"
;
$i ++;}
#2
}
#3 // close if empty errors
}
#4 // close function



$username = $_SESSION['myusername'
];
#COMMENT -> You need to put this ^ in, otherwise the $username in the mysql query below is blank.


// You had SELECT ALL `members`... instead of SELECT * FROM `members`...
$sql = "SELECT * FROM `members` WHERE `username` = '$username' LIMIT 1"
;
$res = mysql_query($sql, $dbc
);
$user = mysql_fetch_array($res
);


require_once(
'st.inc.php'
);

require_once(
'Army.inc.php'
);

require_once(
'stats.inc.php'
);



srand ((double) microtime( )*1000000
);
$find = rand(1,3
);
echo
"You found " . $find . " logs<br>"
;


$Wood = $Resources['Wood'
];
$addWood = $find
;
$newWood = $Wood + $find
;

if (!
$error
) {

mysql_query("UPDATE `Resources` SET `Wood` = '$newWood' WHERE `userid` = '$user[id]' LIMIT 1"
)
or die(
mysql_error
());
}
{
errors($error
);
}

?>

<html>
Wood: <?php echo $Resources['Wood'];
?>
</html>

<SCRIPT>
function refreshPeriodic() {
// Reload the page every 30 seconds
location.reload();
timerID = setTimeout("refreshPeriodic()", 31000);
}
timerID = setTimeout("refreshPeriodic()", 31000);
</SCRIPT>


<html>
<head>
<script language="JavaScript">
var i=30;
function countDown()
{
if(i > 0)
{
document.forms[0].elements[0].value=i;
i = i-1;
var c = window.setTimeout("countDown()", 1000);
}
}

</script>
</head>
<body onLoad="countDown()">
<form>
<input type="text" name="counter" size="3">
</form>
</body>
</html>
Is what I have, I am trying to get it so when the timer reaches 0, you get your wood. It works, but I am having the problem, because if u refresh screen, u automatically get the wood. Anybody know how I can fix this? Tyvm for the help.
biggrin.gif
.

better way to using ajax that code no refresh whole page.

Refresh some part of data
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
I don't think that's his problem, I think he means that the user could refresh the page manually to receive the wood immediatly...
How about you calculate the time at which the user should get his wood, and store that in your database? The next step would be calculating the time difference in your php, passing it to your JavaScript and refresh once that time has passed.
 

votter

New Member
Messages
34
Reaction score
0
Points
0
Yes marshian, That is what I mean, I am trying to do a countdown like www.syrnia.com does, doesn't have to be coded close to the same, but that is kinda how I want it, and how what I do what you said? And I did hear Ajax is what you need, but don't know if that is true? And if I did use Ajax, how would I do so?
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
Let us say you've got a page called "checkwood.php" that outputs the amount of wood a certain player has and a page called "checknextwood.php" which outputs how long a certain player has to wait to receive wood again.
In your html you could have something like
HTML:
You have <span id="numwood"></span>. Time till next wood: <span id="woodtime"></span>
Then you could, using Ajax, request the contents of "checkwood.php" and show it in element "numwood", and do the same for "woodtime". (And refresh the values (not the whole page) after the time has passed.)

Here's an introduction to Ajax: http://www.w3schools.com/Ajax/Default.Asp
If you need more help or just don't understand my ratteling, this is the place to ask stuff! =)
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Why not use a cronjob for this(that is, having a script which automatically executes every so often)? If it's because you only want the wood to increase while this page is open, then what's to stop people from just leaving the page open 24/7? Otherwise, I would recommend increasing the time between executions and the resource gain proportionately and use a cronjob. So, for example, you could have it execute every 30 minutes and have the gain be a random value from 60 to 180 instead of 1 to 3, and of course it would apply to every player.

But if you wanna stick with your current method, then I agree with marshian's solution. Although I would just have 1 script which outputs both the current amount and the time remaining separated by a string so you could split() it to get the current values.
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
The way he's currently using only gives the user wood if he's logged in, if you'ld use a cron for that, you must increase the time too, which results in users coming online once an hour just to get their wood.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Yeah, but what I'm saying is that people will just end up staying logged in 24/7 and keeping the page open if he does it like that. So what's the point? He'll just end up with people complaining that it's unfair because they can't keep their computer on all the time like other players. Might as well make it a cron in that case.

And 30 mins was just an example. Even 5 minutes is alright, but 30 seconds would just be a bit too much if he has a lot of players.
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
You could determine wether they can get new stuff based on their last click too... For example, you only get wood every 30 seconds if you've done something in the last 2 minutes... But then you get auto-typer/clickers, so it's still unfair...
Use a cron to do the same results in more auto-typer/clickers, again, unfair...
Just give everyone the same at the same time (logged in or not) and you get people complaining they're always playing the game, but they can't be better then the others, who just come online once a day...

To sum it up: face it, it'll never be fair, and they'll always complain, what ever you do :(
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
If the game is balanced, the crons alone shouldn't be the main determining factor. For example, it looks like there's attacking(there's an army script), so people who play more often will get better pickings and be able to defend themselves better. And if you can steal resources through attacking, then the people who let them pile up all day long will get them stolen by those who are on more often.
 

votter

New Member
Messages
34
Reaction score
0
Points
0
Oh, I know how to balance it out. I just need to get this code to work still. xD.
 
Top