07.05.2014, 09:20
(
Последний раз редактировалось RajatPawar; 09.05.2014 в 14:47.
)
List of contents -
__________________________________________________ _
1) Creating table for storing chat
We need about three fields for storing the total information about each chat line.
The information we need is - (along with the datatype and max length)
The table looks like -
2) Storing the chat from OnPlayerText
There are two ways to handle this situation.
You need to now use the callback to send a query EACH time. We use a static variable..
In the above query using NOW() and wherever further it is used, please consider this -
That's it!
Your chat should successfully log in the table.
3) Emptying the table every X days.
Even though MySQL handles large sites like ********, sometimes, I like to keep the amount of rows limited with a periodic deletion of the log every 15 - 30 days. This can be done using a feature of MySQL called 'events'.
Events are run every X time as specified and do something that we want it to. Usually, the event_scheduler -which needs to be ON for events to run, is not ON, so you need to enable it. Here are some topics that will help you to turn on the event scheduler to ON, on your server, whatever it is.
(http://forum.logzilla.net/?topic=71.0) (-- Make sure your event scheduler is on by running the tests given there)
You can run the query needed to start the event through PHPMYADMIN or through your gamemode, as you wish. (I'd rather run it ONCE on the phpMyAdmin)
That will start an event named 'DeleteOldLogs' which will run EVERY 3 days, and delete all chats which are more than 5 days old. It checks the 5 days thing using the `Time` column we made in the table. The LOW_PRIORITY clause is there to make sure we don't delete while someone is reading from the table at that time. Cool, right?
4) Automatically backing up the database
We have all been there. Some update ruins the database, and we need to rollback on the data. You need not worry. You can set an automatic backup mechanism to backup a table.
You can use various methods to do that. You can use CRON plus mysqldump, or use a PHP script (which is usually not recommended due to security issues) or there are some actual total scripts which do that for you, waiting to be installed.
You can also use the EVENT feature to backup the table. The only drawback/change is that the exported file is in the .tsv format which may make it difficult for you.
Here's a small snippet you might like to use for this method - View topic
For using CRON or other methods, view - 5) Showing the chat on a webpage
You can use a combination of PHP, jQuery (AJAX) and MySQL to show a kind of LIVE chat between a webpage and the server. We can pull the data from the table and show it.
http://tutorialzine.com/2010/10/ajax...hat-php-mysql/
This is a tutorial on a web chat room. If you understand enough, you can easily modify it to get the data from the table (making the job easy, you don't even have to store anything!) and displaying it without refreshing, somewhat like an IRC. Since it's given there, it would be a waste of time for me to explain this anymore!
6) Notes
NOTE: Use the escape function provided to avoid SQL injection by smart asses.
NOTE: You can find the my.ini file under 'MySQL' for WAMP.
NOTE: Thanks to Calgon - you should add an anti-spam to avoid rows of the same text being stored in the table.
Thank you, I hope you enjoyed this or it helped you.
Please point out ANY mistakes.
__________________________________________________ _
- Creating table for storing chat
- Storing the chat from OnPlayerText
- Emptying the table every X days
- Automatically backing up the database
- Showing the chat on a webpage
1) Creating table for storing chat
We need about three fields for storing the total information about each chat line.
The information we need is - (along with the datatype and max length)
- Username [ VARCHAR(24) ]
- Text [VARCHAR(144)] or [TEXT]
- Time [DATETIME]
The table looks like -
2) Storing the chat from OnPlayerText
There are two ways to handle this situation.
- Storing about 50 - 60 chats in a variable, then sending a query to add them ALL.
- Send a query to update with EACH text.
You need to now use the callback to send a query EACH time. We use a static variable..
pawn Код:
public OnPlayerText(playerid, text[])
{
static pQuery[168 + 50], pName[MAX_PLAYERNAME];
GetPlayerName(playerid, pName, MAX_PLAYER_NAME);
format(pQuery, sizeof(pQuery), "INSERT INTO "#TABLE_NAME"(Username, Time, Text) VALUES('%s', NOW(), '%s')", pName, text);
// The NOW() function inserts the current datetime into the table.
mysql_function_query(connectionHandle, pQuery, false, "", "");
// No need to store any cache data, nor any need for processing under a callback.
return 1;
}
Quote:
Use of the datetime field is up for discussion. I've always preferred regular Unix timestamps. In any case you should use UTC_TIMESTAMP() rather than NOW(). The latter one gets the local server time, which can lead to inconsistencies if migrating the server to another country (e.g. from Europe to the US) or even if trying to display data in the user's local time.
|
Your chat should successfully log in the table.
3) Emptying the table every X days.
Even though MySQL handles large sites like ********, sometimes, I like to keep the amount of rows limited with a periodic deletion of the log every 15 - 30 days. This can be done using a feature of MySQL called 'events'.
Events are run every X time as specified and do something that we want it to. Usually, the event_scheduler -which needs to be ON for events to run, is not ON, so you need to enable it. Here are some topics that will help you to turn on the event scheduler to ON, on your server, whatever it is.
(http://forum.logzilla.net/?topic=71.0) (-- Make sure your event scheduler is on by running the tests given there)
You can run the query needed to start the event through PHPMYADMIN or through your gamemode, as you wish. (I'd rather run it ONCE on the phpMyAdmin)
pawn Код:
CREATE EVENT `DeleteOldLogs` ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 DAY DO
DELETE LOW_PRIORITY FROM my_database.table_name WHERE `Time` < DATE_SUB(NOW(), INTERVAL 5 DAY)
4) Automatically backing up the database
We have all been there. Some update ruins the database, and we need to rollback on the data. You need not worry. You can set an automatic backup mechanism to backup a table.
You can use various methods to do that. You can use CRON plus mysqldump, or use a PHP script (which is usually not recommended due to security issues) or there are some actual total scripts which do that for you, waiting to be installed.
You can also use the EVENT feature to backup the table. The only drawback/change is that the exported file is in the .tsv format which may make it difficult for you.
Here's a small snippet you might like to use for this method - View topic
For using CRON or other methods, view - 5) Showing the chat on a webpage
You can use a combination of PHP, jQuery (AJAX) and MySQL to show a kind of LIVE chat between a webpage and the server. We can pull the data from the table and show it.
http://tutorialzine.com/2010/10/ajax...hat-php-mysql/
This is a tutorial on a web chat room. If you understand enough, you can easily modify it to get the data from the table (making the job easy, you don't even have to store anything!) and displaying it without refreshing, somewhat like an IRC. Since it's given there, it would be a waste of time for me to explain this anymore!
6) Notes
NOTE: Use the escape function provided to avoid SQL injection by smart asses.
NOTE: You can find the my.ini file under 'MySQL' for WAMP.
NOTE: Thanks to Calgon - you should add an anti-spam to avoid rows of the same text being stored in the table.
Thank you, I hope you enjoyed this or it helped you.
Please point out ANY mistakes.