[thelist] Refreshing a page when a certain condition occurs (ASP using Javascript)

Greg Burnison gburniso at scratchpad.ca
Sun Jun 23 20:01:01 CDT 2002


--
[ Picked text/plain from multipart/alternative ]
I'm trying to create a simple game in asp using javascript.  The basic idea
is an 8x8 world, people are put into the world at a random position, then
they can move around.  If one player moves over top of another they get a
point and the other player is 'killed', they then have the option to 'play
again'.  Where my question comes is I'm trying to get the board to refresh
only when somebody has moved.  Currently I'm refreshing the board (by
calling my display.asp every second), but I'd like not to have to do that.

What I'm thinking is that if I create a check value (by adding the x & y
coordinates of all the players) when a player moves.  Then every second
that check value is compared to the current value of all the coordinates
from the database.  If the values are different then I will refresh the
page, otherwise I won't.

Example: two players
Player 1: 3,5
Player 2: 6,1
The check value is now (3+5+6+1)=15.
If player 1 moves to 3,6 the current value will be (3+6+6+1)=16, thus the
display should refresh.

Here is the relevant section of my display.asp:


<!DOCTYPE html public "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ LANGUAGE=JAVASCRIPT %>
<HTML>
<HEAD>
<TITLE>Display</TITLE>
<%
function checkTotal() {
currentTotal=parseInt(0);
rs=Server.CreateObject("ADODB.Recordset");
rs.Open("SELECT * FROM World",
                         "DRIVER={Microsoft Access Driver
(*.mdb)};DBQ=C:\\inetpub\\wwwroot\\games.mdb");
         if (!(rs.EOF)) { // This loop calculates the current x&y total
from the database
                 while(!(rs.EOF)) {
                         currentTotal+=parseInt(rs.fields.item("X"))+parseInt(rs.fields.item("Y"));
                         rs.Move(1);
                 }
         }
rs.Close();

         if (currentTotal!=Session("sum")) { // Compares currentTotal
(calculated above)
                 Response.redirect("display.asp"); // to the Session
variable if they are not
                                         //the same someone has moved, so
refresh the display
         }
}
%>
<SCRIPT type="text/javascript">
<!--
function checkMoved() {
         <%checkTotal()%>
   setTimeout("checkMoved();",1000);
}
// -->
</SCRIPT>
</HEAD>
<BODY onload="checkMoved();">
<%=out%> // outputs my game board
</BODY>
</HTML>


Any thoughts or suggestions would be greatly appreciated.

--Greg
--




More information about the thelist mailing list