17.10.2010, 21:37
I have looked around the forums a bit before, and noticed that no one has ever made a tutorial on how to create a chat log. I believe chat logs are an important part in a server. As they help you catch the people spamming, and or being racist etc. This is the most up to date info as of 10/17/10 (please correct me if i am wrong).
So with that, lets get started!
Well, that is the end of this tutorial. I hope you guys learned something. There may be easier ways to do this, if so please post comments below telling me how. I will gladly adjust my post to fit that specific way.
If any of this information is wrong please PM me or leave a comment containing the correct information.
So with that, lets get started!
File Creation
Lets start off by creating the "logs folder" and "chatlog.txt"We will be putting these into your "scriptfiles folder", so lets go into that folder and create a new folder. Then name it "logs", open your newly created logs folder and create a new text doccument(Win7: Right click> new > Text Doccument). Lets name this new text Doc. ChatLog (Vista: ChatLog.txt). Ok so we have completed the file creation part of this tutorial, now its time to get into the scripting.
Scripting
Now its time to get into the fun stuff, lets open up a new or existing gamemode in pawno (Or any other editor you may posses). Lets search forNow under that callback we will add the following code:pawn Code:OnPlayerText
pawn Code:new File:chatlog = fopen("/logs/ChatLog.txt",io_append),chat[128],chater[128],hour,minute,second;
GetPlayerName(playerid,chater,sizeof(chater));
gettime(hour,minute,second);
format(chat,sizeof(chat),"[%d:%d:%d]: ( %s ): %s\n",hour,minute,second,chater,text); // \n is used to make a new line
if(chatlog)
{
if(fexist("chatlog.txt"))
{
fwrite(chatlog,chat);
fclose(chatlog);
}
}
I know what you are thinking, what the heck does all this gibberish mean? don`t worry i will explain it.
This line will create a new file named chat log, as indicated bypawn Code:new File:chatlog = fopen("/logs/ChatLog.txt",io_append),chat[128],chater[128],hour,minute,second;this line will represent the opening of the chat log file as indicated herepawn Code:new File:chatlognow if you read a little further you will seepawn Code:fopen("/logs/ChatLog.txt"I believe there are only two writing parameters (correct me if i am wrong) And they are:pawn Code:io_append
Since we are making a chat log we want to use io_append. Now we are going to create a couple of variables and strings to hold our information.pawn Code:io_write - this will overwrite the whole entire file, basically it creates a new file every time its used
io_append - this will add the line after the last documented line.The next line is the line that will get the players name when they speak and store it to a variable named chater.pawn Code:chat[128],chater[128],hour,minute,second;
Parameters of GetPlayerName :pawn Code:GetPlayerName(playerid,chater,sizeof(chater))
this next part is where we get the time of day that they spoke, so we use the variables Hour, Minute, and Secondpawn Code:GetPlayerName(playerid, const name[], len);
The time that will display in the log is military time, so it is on a 24 hour basis instead of a 12 hour.pawn Code:gettime(hour,minute,second);
gettime parameters:
pawn Code:gettime(&hour,&minute,&second);
Now we format the text to display the time first, then the players name, then the message they spoke. this is where the string chat[128] comes in.
Now i know alot of people have trouble understanding the format function, so i will explain it to you.pawn Code:format(chat,sizeof(chat),"[%d:%d:%d]: ( %s ): %s\n",hour,minute,second,chater,text);
Format Parameters :
So lets break this down,pawn Code:format(output[], len, const format[], {float,_....});
Output is the string you declared up above, so in our case it is chat[128].
len is of course the length of that string, so we useto get the length.pawn Code:sizeof(chat)
const format[], is the was you would like your text to display, in our case we want the time to come first then the name then the text. So we use the percent values, %d(whole number), and %s(string).
float, this is where we place what those values are. The values are assigned left to right, so the %d`s come first, then the %s`s.
We are almost done, so lets now check if chatlog is open, then if it exists, and if it does we write the contents of chat to the file.
[pawn]if(chatlog)[pawn]
This will check is the chatlog is open.
This is checking if the file chatlog.txt existspawn Code:if(fexits(chatlog))
Now, this writes what ever the player said too the chatlog.txt file in the format we formatted in the format function (sorry for the confusion there :P)pawn Code:fwrite(chatlog,chat);
fclose(chatlog);
After that we just close all of the brackets and return a value.
Well, that is the end of this tutorial. I hope you guys learned something. There may be easier ways to do this, if so please post comments below telling me how. I will gladly adjust my post to fit that specific way.
If any of this information is wrong please PM me or leave a comment containing the correct information.