Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-21-2009, 03:45 PM
fragman85 fragman85 is offline
Banned
Join Date: Mar 2009
Location: Switzerland
Posts: 261
fragman85 is on a distinguished road
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

Last edited by fragman85; 03-21-2009 at 09:36 PM..
Reply With Quote
  #2  
Old 03-21-2009, 04:57 PM
Deas_Voice Deas_Voice is offline
Deas
Deas_Voice's Avatar
Join Date: Jun 2007
Location: Sweden
Posts: 2,264
Deas_Voice is a jewel in the roughDeas_Voice is a jewel in the rough
Send a message via AIM to Deas_Voice Send a message via MSN to Deas_Voice Send a message via Yahoo to Deas_Voice
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.
__________________
.
WTF is real life, and where do I Download it?
There is no Real Life, just AFK!
since 2003~
I Support~
ღAeonღ | ღTestbedღ | ღDelteriaღ

if you are going to rep me, don't be an idiot, leave your name!
I got nothing but love for you
Reply With Quote
  #3  
Old 03-21-2009, 05:19 PM
Codein Codein is offline
jwd
Codein's Avatar
Join Date: Oct 2005
Location: Greater Manchester
Posts: 2,423
Codein has a spectacular aura aboutCodein has a spectacular aura about
Send a message via AIM to Codein Send a message via MSN to Codein
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 View Post
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.
Reply With Quote
  #4  
Old 03-21-2009, 05:21 PM
salesman salesman is offline
Finger lickin' good.
salesman's Avatar
Join Date: Nov 2008
Location: Colorado
Posts: 1,865
salesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud ofsalesman has much to be proud of
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.
Reply With Quote
  #5  
Old 03-21-2009, 05:51 PM
TSAdmin TSAdmin is offline
Forum Moderator
TSAdmin's Avatar
Join Date: Aug 2006
Location: Australia
Posts: 1,980
TSAdmin has much to be proud ofTSAdmin has much to be proud ofTSAdmin has much to be proud ofTSAdmin has much to be proud ofTSAdmin has much to be proud ofTSAdmin has much to be proud of
Quote:
Originally Posted by salesman View Post
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.
__________________
TSAdmin (Forum Moderator)
Welcome to the Official GraalOnline Forums! Where sharing an opinion may be seen as a declaration of war!
------------------------
· User Agreement · Code of Conduct · Forum Rules ·
· Graal Support · Administrative Contacts ·
Reply With Quote
  #6  
Old 03-21-2009, 09:32 PM
fragman85 fragman85 is offline
Banned
Join Date: Mar 2009
Location: Switzerland
Posts: 261
fragman85 is on a distinguished road
Quote:
Originally Posted by Codein View Post
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"); 
Reply With Quote
  #7  
Old 03-23-2009, 12:27 PM
[email protected] sid.gottlieb@googlemail.com is offline
Banned
Join Date: Mar 2008
Posts: 861
sid.gottlieb@googlemail.com will become famous soon enough
You should generally try a cache- you don't want to be modifying a text document every five seconds.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 04:12 PM.


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