Central Scoreboard with Flash and ASP - Adding player’s scores
(Page 3 of 4 )
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. <A href="””javascript:window.close()”">Close Window</A>”)
The “if” around the whole script checks to make sure the variable has sent; if you’re using this on the web make sure you change the variable to something else. It’s important that your scoreboard is protected for adding scores although there is no need to project the script that displays results.
All the script essentially does is insert the values into the database. If no player name is given the script uses XXX. This is because players will need to enter their names and some players won’t bother. There is also a function in the script to stop players having any more than 3 letters giving it an old fashioned arcade feel.
The one problem that could arise in this script is players who enter 1 or 2 character long names. Personally I don’t mind this although if you want to insist on all players having 3 letters you could filter out these players and use the default using a script such as:
Playername
= (Left(playername, 3))
Playercheck1 = (Left(playername, 1))
Playercheck2 = (Left(playername, 2))
If playername = playername1 Then
‘ this player only entered one letter
Playername = “XXX”
ElseIf playername = playername2 Then
‘ this player only entered two letters
Playername = “XXX”
End If
Ok, you can save and close that file now. We are now done for scripting.
Next: The Flash file >>
More ASP Articles
More By Chris Worfolk