PDA

View Full Version : LeaveMessage()


fragman85
03-21-2009, 03:45 PM
This is my first post in the Code Gallery, I am looking for good critique to improve my coding/styling etc.

If you like my code, feel free to use/edit it.

Also I think this is quite a good example for using DataBase NPCs :o

So I made this 4 functions:

LeaveMessage(Account, MSG, DataBase)

Will create/add a Message to a Database as an array (named as the Account of the Receiver.)

CheckMessage(Account, DataBase)

Will return how many Messages are saved for an Account in a DataBase.

GetMessage(Account, Database, ID)

Will return a Message. If ID is not provided, the first one.

DeleteMessage(Account, Database, ID)

Will delete a Message, if ID is not provided the first one.

function LeaveMessage(Account, MSG, DataBase) {
if (Account && MSG && DataBase) {
// Edited:
findNPC(DataBase).this.(@ Account).add(MSG);
return true;
}
else {
echo("LeaveMessage(Account, MSG, DataBase)!");
return false;
}
}
function CheckMessage(Account, DataBase) {
if (Account && DataBase) {
return findNPC(DataBase).(@ Account).size();
}
else {
echo("CheckMessage(Account, Database!");
return false;
}
}
function GetMessage(Account, DataBase, ID) {
if (Account && DataBase) {
if (ID > 0) {
return findNPC(DataBase).(@ Account)[ID - 1];
}
else {
return findNPC(DataBase).(@ Account)[0];
}
}
else {
echo("GetMessage(Account, DataBase, ID)!");
return false;
}
}
function DeleteMessage(Account, DataBase, ID) {
if (Account && DataBase) {
if (ID > 0) {
findNPC(DataBase).(@ Account).delete(ID - 1);
return true;
}
else {
findNPC(DataBase).(@ Account).delete(0);
return true;
}
}
else {
echo("DeleteMessage(Account, DataBase, ID)!");
return false;
}
}

As I said above, I'm looking for critique ;D

Deas_Voice
03-21-2009, 04:57 PM
could you give an example how to use it?
oh and doing "if (Account && MSG && DataBase) {" isn't very efficient IMO but i could be wrong.

Codein
03-21-2009, 05:19 PM
You've used "with()" to access DB NPC data where as in the rest of the script, you're not. Stick to one or the other. I prefer using this:

NPCNAME.variable = true

Rather than "with()".

could you give an example how to use it?

Yeah, I agree and also a brief explanation of what problem it's designed to tackle and what not.


oh and doing "if (Account && MSG && DataBase) {" isn't very efficient IMO but i could be wrong.

I think what he's done there is quite logical.

salesman
03-21-2009, 05:21 PM
I just skimmed over it, but if this is for a mail system or something similar, it'd be best to use textfiles. There was a mail system on Era not too long ago that used a DBNPC and it became so full that it caused extreme lag throughout the server everytime it was accessed. Nobody could figure out what was causing it until I think trying to open the DB manually crashed the server completely.

Not positivie, but I'm pretty sure that's what happened.

TSAdmin
03-21-2009, 05:51 PM
I just skimmed over it, but if this is for a mail system or something similar, it'd be best to use textfiles. There was a mail system on Era not too long ago that used a DBNPC and it became so full that it caused extreme lag throughout the server everytime it was accessed. Nobody could figure out what was causing it until I think trying to open the DB manually crashed the server completely.

Not positivie, but I'm pretty sure that's what happened.

Yeah, that's what happened. I had disabled the script, but I think since his return to Era, Hachi is now using text files as you have just mentioned here and it's taken a lot of stress off the server.

fragman85
03-21-2009, 09:32 PM
You've used "with()" to access DB NPC data where as in the rest of the script, you're not. Stick to one or the other. I prefer using this:

NPCNAME.variable = true

Rather than "with()".



No idea why I used with() above, lol.


And yes, it can be used for a Mail-System, I didn't know that it's better to save it into text files :o, well now I do, =P.

Example:


// Leaving a Message for somebody.
LeaveMessage("Fragman85", "Hey dude, PM me the list of the 10 richest people, please...", "DB_MailSystem");
// Checking if somebody got Messages and how many
CheckMessage("Fragman85", "DB_MailSystem");
// Would return 1.
// Get a Message, since there's only one, there's no need for "ID".
GetMessage("Fragman85", "DB_MailSystem");
// Would return "Hey dude, PM me the list of...
// Delete the Message, since there's only one there's no need for "ID".
DeleteMessage("Fragman85", "DB_MailSystem");

03-23-2009, 12:27 PM
You should generally try a cache- you don't want to be modifying a text document every five seconds.