doof92
05-05-2007, 01:31 PM
Thought for my first ever post on this forum i'd post a guide I wrote many months ago for ASP. Hope some find it useful :)
Script Code
'This is a little stage by stage guide to explain how a script to make a
'procedure occur on every record in a database could be written.
'The following code is actually actively used by my site, as at the click of a button, it
'can automatically update the player age on Battrick by one year. I've edited it slightly so it will
'decrease the column fatigue by 1 and increase wealth by 50.
This first line isn't easy to explain, and I'm not sure it's needed, so just ignore it, but leave it in.
Response.Buffer=True
'Here, it simply sets up the required variables to do the updates
Dim Conn
Dim rs
Dim sSQL
Dim lngRecordNo
'This is another complex line to explain, but is vital in connecting to Access. You MUST have this here.
Set Conn = Server.CreateObject("ADODB.Connection")
'This line is very important, as it informs the code of what type of database you will use. I've set it up here
'so that it connects to Access, but can be changed for MySql or whatever. This code here has been tested and works, all
'you must do to change the database filename is edit the very last bit, but leave in the ./ as it tells the code that
'the database is in the same folder as this file.
conn.Open ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.MapPath("./game.mdb"))
'This is the Recordset, which keeps details of data in each record in the database. This is like previously, vital aswell
'and simply sets up the variable ready to be used.
Set rs = Server.CreateObject("ADODB.Recordset")
'Here is the SQL query that queries the database. Here is just a simple query which the result is every single record,
'but could be made more complex, by adding a WHERE into it. The * means that it selects every
single column in the table, rather than just a few.
sSQL = "SELECT * FROM player"
'This sets up the database ready to be updated. Not always needed either.
rs.CursorType = 2
rs.LockType = 3
'Opened the database and table and queries it.
rs.Open sSQL, Conn
'Moves to the first record.
rs.movefirst
'This simply says keep on doing the following procedure until there are no more records.
Do Until rs.EOF
'These lines are the codes and formulas that will be performed. I'll explain each line individually.
'This line performs this formula: New Fatigue = Old Fatigue - 1
rs.Fields("fatigue") = Rs("fatigue") - 1
'This line performs this formula: New Wealth = Old Wealth + 50
rs.Fields("wealth") = Rs("wealth") + 50
'This code makes the updates happen and update the database with the new changes
rs.Update
'This line isn't needed 100%, but if your dealing with many many records, this clears the memory to make it quicker.
Response.clear()
'This line moves onto the next record for the procedure to occur on.
rs.MoveNext
'Remember further up when I talked about Do Until rs.EOF???Well every DO must be ended with a LOOP,
'and this is what the word below does
loop
'This simply closes the Recordset and then sets the 2 variables to nothing
'after all the records have had this procedure done to them
rs.Close
Set rs = Nothing
Set Conn = Nothing
And there we go, you've just learn how a very basic game engine would be set up.
The middle bit with the formulas can be made more in depth, but the main page set-up is exactly like this.
' hope you've found this useful
More guides will follow in the future
Script Code
'This is a little stage by stage guide to explain how a script to make a
'procedure occur on every record in a database could be written.
'The following code is actually actively used by my site, as at the click of a button, it
'can automatically update the player age on Battrick by one year. I've edited it slightly so it will
'decrease the column fatigue by 1 and increase wealth by 50.
This first line isn't easy to explain, and I'm not sure it's needed, so just ignore it, but leave it in.
Response.Buffer=True
'Here, it simply sets up the required variables to do the updates
Dim Conn
Dim rs
Dim sSQL
Dim lngRecordNo
'This is another complex line to explain, but is vital in connecting to Access. You MUST have this here.
Set Conn = Server.CreateObject("ADODB.Connection")
'This line is very important, as it informs the code of what type of database you will use. I've set it up here
'so that it connects to Access, but can be changed for MySql or whatever. This code here has been tested and works, all
'you must do to change the database filename is edit the very last bit, but leave in the ./ as it tells the code that
'the database is in the same folder as this file.
conn.Open ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & server.MapPath("./game.mdb"))
'This is the Recordset, which keeps details of data in each record in the database. This is like previously, vital aswell
'and simply sets up the variable ready to be used.
Set rs = Server.CreateObject("ADODB.Recordset")
'Here is the SQL query that queries the database. Here is just a simple query which the result is every single record,
'but could be made more complex, by adding a WHERE into it. The * means that it selects every
single column in the table, rather than just a few.
sSQL = "SELECT * FROM player"
'This sets up the database ready to be updated. Not always needed either.
rs.CursorType = 2
rs.LockType = 3
'Opened the database and table and queries it.
rs.Open sSQL, Conn
'Moves to the first record.
rs.movefirst
'This simply says keep on doing the following procedure until there are no more records.
Do Until rs.EOF
'These lines are the codes and formulas that will be performed. I'll explain each line individually.
'This line performs this formula: New Fatigue = Old Fatigue - 1
rs.Fields("fatigue") = Rs("fatigue") - 1
'This line performs this formula: New Wealth = Old Wealth + 50
rs.Fields("wealth") = Rs("wealth") + 50
'This code makes the updates happen and update the database with the new changes
rs.Update
'This line isn't needed 100%, but if your dealing with many many records, this clears the memory to make it quicker.
Response.clear()
'This line moves onto the next record for the procedure to occur on.
rs.MoveNext
'Remember further up when I talked about Do Until rs.EOF???Well every DO must be ended with a LOOP,
'and this is what the word below does
loop
'This simply closes the Recordset and then sets the 2 variables to nothing
'after all the records have had this procedure done to them
rs.Close
Set rs = Nothing
Set Conn = Nothing
And there we go, you've just learn how a very basic game engine would be set up.
The middle bit with the formulas can be made more in depth, but the main page set-up is exactly like this.
' hope you've found this useful
More guides will follow in the future