Central Scoreboard with Flash and ASP - The database
(Page 2 of 4 )
First thing to do is create a blank database; I called mine database.mdb to keep things simple. In the database you need to create a table to hold the scores, surprising enough I called mine: scores.
The structure should be:
Player (text field, default length)
Score (number field)
Let’s also add some default players so that there are already results on the scoreboard when visitors start playing. I have done it 3 letter arcade style throughout the article:
CJK – 10000
SAM – 20000
XER – 30000
Once you have entered the values you can close the database, we won’t need to open it again.
The scoreboard file
Now open up your text editor and create a file called scoreboard.asp. This will extract the top 3 results from the database along with their scores so that the flash file can read the top scoring players and compile them into a scoreboard.
Define a connection to the database and open up a recordset:
<%
conn = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=” + Server.MapPath(“database.mdb”)
set rs=Server.CreateObject("ADODB.recordset")
rs.Source = "SELECT * FROM scores ORDER BY score DESC"
rs.ActiveConnection = conn
rs.Open()
Now we need to write out the variables of the top 3 players:
Number=1
Do until number = 4
response.write("player" & number & "=" & (rs.Fields.Item("player").value)) & vbCrLf
response.write("score" & number & "=" & (rs.Fields.Item("score").value)) & vbCrLf
number = number + 1
rs.MoveNext
loop
Then finally close the recordset
rs.Close()
Set rs = Nothing
You can then upload these files and open them in your browser (or skip past the uploading if you’re working on your local machine). It should produce:
player1=XER score1=30000 player2=SAM score2=20000 player3=CJK score3=10000
Adding player’s scores
Next we need a file that will add player’s scores when they are sent. We also need a system to stop users adding scores to the database without actually playing the game. Bring up a new file in your text editor and save it as addscore.asp. This file will be sent variables from the flash movie:
Player – this will be the player’s name
Score – the player’s score
Passcode – this is a variable that makes sure the user is not just accessing the page using their browse
In this file we open up the connection as normal but change the SQL:
<%
If Request.Form("passcode") = "XHjf5" Then
Dim playername
playername = "XXX"
If Request.Form("player") <> "" Then
playername = Request.Form("player")
End If
playername = (Left(playername, 3))
Dim conn
conn = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" + Server.MapPath("database.mdb")
set rs=Server.CreateObject("ADODB.recordset")
rs.Source = "INSERT INTO scores (player, score) VALUES ('" + Request.Form("player") + "', " + Request.Form("score") + ")"
rs.ActiveConnection = conn
rs.Open()
rs.Close()
Set rs = Nothing
End If
Response.Write(“Score Recorded. Close Window”)
%>
conn
= "Driver={Microsoft Access Driver (*.mdb)}; DBQ=” + Server.MapPath(“database.mdb”)
set rs=Server.CreateObject("ADODB.recordset")
rs.Source = "SELECT * FROM scores ORDER BY score DESC"
rs.ActiveConnection = conn
rs.Open()
Now we need to write out the variables of the top 3 players:
Number
=1
Do until number = 4
response.write("player" & number & "=" & (rs.Fields.Item("player").value)) & vbCrLf
response.write("score" & number & "=" & (rs.Fields.Item("score").value)) & vbCrLf
number = number + 1
rs.MoveNext
loop
Then finally close the recordset.
rs
.Close()
Set rs = Nothing
You can then upload these files and open them in your browser (or skip past the uploading if you’re working on your local machine). It should produce:
player1
=XER score1=30000 player2=SAM score2=20000 player3=CJK score3=10000
Next: Adding player’s scores >>
More ASP Articles
More By Chris Worfolk