Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   LeaveMessage() (https://forums.graalonline.com/forums/showthread.php?t=84771)

fragman85 03-21-2009 03:45 PM

LeaveMessage()
 
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.

PHP Code:

function LeaveMessage(AccountMSGDataBase) {
  if (
Account && MSG && DataBase) {
    
// Edited:
      
findNPC(DataBase).this.(@ Account).add(MSG);
      return 
true;
  }
  else {
    echo(
"LeaveMessage(Account, MSG, DataBase)!");
    return 
false;
  }
}
function 
CheckMessage(AccountDataBase) {
  if (
Account && DataBase) {
    return 
findNPC(DataBase).(@ Account).size();
  }
  else {
    echo(
"CheckMessage(Account, Database!");
    return 
false;
  }
}
function 
GetMessage(AccountDataBaseID) {
  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(AccountDataBaseID) {
  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:

PHP Code:

NPCNAME.variable true 

Rather than "with()".

Quote:

Originally Posted by Deas_Voice (Post 1476349)
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.

Quote:

Originally Posted by Deas_Voice
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

Quote:

Originally Posted by salesman (Post 1476361)
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

Quote:

Originally Posted by Codein (Post 1476359)
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:

PHP Code:

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:

PHP Code:

// 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"); 


[email protected] 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.


All times are GMT +2. The time now is 10:26 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.