Snooble Posted March 16, 2008 Share Posted March 16, 2008 Hey, I'm working on a battle script to use... I have all the stats in variables pulled from a mysql db... but i want to reveal the fight one turn at a time... the easiest way of doing this? Im guessing is to while(hp > 0) create an array with each turn in it turns['0'] etc... but how can i reveal them one by one to the user? javascript? Thanks ever so much Sam Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/ Share on other sites More sharing options...
Snooble Posted March 17, 2008 Author Share Posted March 17, 2008 is there anyone that can help me? Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494133 Share on other sites More sharing options...
lemmin Posted March 17, 2008 Share Posted March 17, 2008 Could you be more specific as to what you are trying to do? If you are trying to create turn based combat (single player?) then you can just continually post a form to itself for every attack. Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494196 Share on other sites More sharing options...
Snooble Posted March 17, 2008 Author Share Posted March 17, 2008 ok, i want to create a battle that the user can view... $name = user1 $hp = 10 $min = 1 $max = 3 $name2 = user2 $hp2 = 40 $min = 5 $max = 7 Every turn user one will deal between 1 and 3 damage (eg. rand(1, 3)) my script needs to output: Your weapon done rand(1,3) damage to $name2 then it should output name2's attack: $name2's weapon did rand(5,7) damage to you it needs to continue the loop untill someones hp goes beneath zero. i need to output each turn eg(user does random damage) one at a time with a 3 sec delay between posting the info the the user. hope that clears it up...? thanks in advance lemmin Sam Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494220 Share on other sites More sharing options...
lemmin Posted March 17, 2008 Share Posted March 17, 2008 So the user doesn't actually have to do anything for each turn? I guess you just keep loading the same page. That page checks a db for whose turn it is and last damage done, probably. You will probably have to refresh the page a couple times without the turn having changed since you don't have any event handlers in PHP. Do you get what I am trying to say? Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494223 Share on other sites More sharing options...
discomatt Posted March 17, 2008 Share Posted March 17, 2008 You will have to use javascript / flash / refersh the page every x seconds. HTTP requests do not stream. Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494234 Share on other sites More sharing options...
Snooble Posted March 17, 2008 Author Share Posted March 17, 2008 ok, so where would i be looking in javascript? to refresh every so many seconds... see i was thinking that the fight would all be done in one process... and each turn put into an array... user1() user2() but how can i get javascript to output each line of the array and how do i put each turn into an array? thanks (sorry for all the questions) Sam Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494238 Share on other sites More sharing options...
revraz Posted March 17, 2008 Share Posted March 17, 2008 Can't you just display all the damage at once or do you want to pause a little after each round? Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494239 Share on other sites More sharing options...
Snooble Posted March 17, 2008 Author Share Posted March 17, 2008 i want to pause... so (for the user) it seems it's turn based... every turn i can output the damage done and the hp left.... Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494250 Share on other sites More sharing options...
revraz Posted March 17, 2008 Share Posted March 17, 2008 What about using SLEEP in your while loop? http://www.php.net/manual/en/function.sleep.php Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494260 Share on other sites More sharing options...
Snooble Posted March 17, 2008 Author Share Posted March 17, 2008 that would be useful.. if the user was to press back on the page what would happen? I need the script to continue until it finishes... otherwise there would be alot of errors... ?? Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494273 Share on other sites More sharing options...
lemmin Posted March 17, 2008 Share Posted March 17, 2008 as revraz suggested, the easiest solution would be to do all of the turns in one process on the server and print out to the users the results of the different turns. Quote but how can i get javascript to output each line of the array and how do i put each turn into an array? The set_timeout("function", x) function will wait x milliseconds and then execute the function (I think. I can't seem to find the documentation for this. Someone correct me if I am wrong.). Your function would be something like top.location="page.php"; so that it keeps refreshing at certain intervals. Or you can send a header with Refresh: 3 in your php Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494290 Share on other sites More sharing options...
Snooble Posted March 17, 2008 Author Share Posted March 17, 2008 The thing is, the reason i'd say javascript is because i dont mind the content to be on the page to start with (in the source)... and then javascript (if im right) can output the information without refreshing the page?... but (if you dont know javascript) could you help me work out a while($hp > 0){ put information into an array? } just how would i increment the array... so each line equals a number in the array. thanks people Sam Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494316 Share on other sites More sharing options...
lemmin Posted March 17, 2008 Share Posted March 17, 2008 You can do something like this to instantiate your array in javascript. (The \0 is so that the extra comma at the end doesn't give a syntax error, but there are other ways around this.) echo "<script language=\"Javascript\">var nums = new Array ("; while ($row = mysql_fetch_array($qry)) echo $row['value'] . ","; echo "\0);</script>"; Quote Link to comment https://forums.phpfreaks.com/topic/96447-a-battle-script/#findComment-494344 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.