Renegade Public Forums
C&C: Renegade --> Dying since 2003™, resurrected in 2024!
Home » Renegade Discussions » Mod Forum » database type script
database type script [message #231091] Sat, 18 November 2006 19:53 Go to next message
Stallion is currently offline  Stallion
Messages: 222
Registered: April 2006
Karma: 0
Recruit
I'm currently working on a script that requires the bot to be able to write player names into it's created file then be able to access those names on it's own to add a count to that name when that players name comes up again. I can have it write the file and can have it add a count to a name but how can I have the bot do this? I'm pretty certain it's just a matter of a simple command or short statement but I don't know php very well and am new at database type of programming.

Once I get the data accessing for the bot properly set there will be several types of commands I will be able to make and with any luck some of them may get used on n00bstories.


Level edit is my play ground

[Updated on: Sat, 18 November 2006 19:55]

Report message to a moderator

Re: database type script [message #231116 is a reply to message #231091] Sun, 19 November 2006 03:59 Go to previous messageGo to next message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
I assume your talking about phprenbot here?



Anyway, assuming your using PHP with a MySQL database, and ignoring the connection to the database code.

Code to increment a counter each time a name is seen.. code is case-insensitive.

$playerNameQuery = mysql_query ( "SELECT * FROM players WHERE LOWER(name) = \"" . lc($playerName) . "\"" );

if ( mysql_num_rows ( $playerNameQuery ) > 0 )
{
  $playerData = mysql_fetch_assoc ( $playerNameQuery );
  mysql_query ( "UPDATE players SET timesSeen = (timeSeen+1) WHERE name = \"" . $playerData['name'] . "\"" );
}
else
{
  mysql_query ( "INSERT INTO players ( name, timeSeen ) VALUES ( \"" . lc($playerName) . "\", 1, )" );
}


http://steamsignature.com/card/1/76561197975867233.png
Re: database type script [message #231135 is a reply to message #231091] Sun, 19 November 2006 07:04 Go to previous messageGo to next message
Stallion is currently offline  Stallion
Messages: 222
Registered: April 2006
Karma: 0
Recruit
I don't use mysql. Sad
I'm using a .txt file to load my script into mirc on someone elses php bot.

Could you possibly show me without mysql or perhaps with an array instead? (remember I need only to be able to have the bot log several names and a number next to each name increasing each time the name shows up under a certain condition such as beacon planted and reference them.)


Level edit is my play ground

[Updated on: Sun, 19 November 2006 07:09]

Report message to a moderator

Re: database type script [message #231150 is a reply to message #231091] Sun, 19 November 2006 08:59 Go to previous message
danpaul88 is currently offline  danpaul88
Messages: 5795
Registered: June 2004
Location: England
Karma: 0
General (5 Stars)
Ah, well in that case your in luck, PHP's arrays are MUCH easier to work with than many other languages, since you don't need to specify a fixed size for them.


Reading from a file every time you want to do something is a waste of CPU time, its better to store it in an array, and put it into a file for permanent storage when the script shuts down, or every x minutes as a backup.


First thing you need is a system for incrementing the name. I will leave it to you to setup when that is done, but this is the code to do it, assuming $playerName contains the players username, and the array is called $playerCountArray. Again, all code is case-insensitive.

if ( array_key_exists( strtolower($playerName), $playerCountArray )
{
  $playerCountArray[strtolower($playerName)]++;
}
else
{
  $playerCountArray[strtolower($playerName)] = 1;
}


Basically this looks through the array to see if they are already in there. If they are it simply increments their value, otherwise it creates them and sets their value to one. In PHP the syntax for creating an array key and modifying it is the same.


Next you need to be able to dump to a file when the bot closes, or however often you want to save the array. This is a quick file dump method for the array. We set the variable to "" first because PHP does not discard variables when it's finished with them, so we need to ensure it is empty before we start. The .= operator is the append operator.

$fileContents = "";
foreach ( $playerCountArray as $player => $count )
{
  $fileContents .= $player . " = " . $count . "\n";
}
$fileHandle = fopen ( "logfile.txt", "w" );
fwrite ( $fileHandle, $fileContents );
fclose ( $fileHandle );


The resulting file will be called logfile.txt and will look like this;

player1 = 5
player2 = 8
player3 = 3




Finally, you need a way to read the values back from the file when the bot is restarted.

$fileContentsArray = explode ( "\n", file_get_contents ( "logfile.txt" ) );

foreach ( $fileContentsArray as $line )
{
  list( $player, $count ) = explode ( " = ", $line );
  $playerCountArray[$player] = $count;
}



Hope that helps you out.


http://steamsignature.com/card/1/76561197975867233.png

[Updated on: Sun, 19 November 2006 09:00]

Report message to a moderator

Previous Topic: My new map issues
Next Topic: More animation help
Goto Forum:
  


Current Time: Tue Nov 12 04:28:21 MST 2024

Total time taken to generate the page: 0.00773 seconds