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 10-12-2008, 11:51 PM
devilsknite1 devilsknite1 is offline
C:
devilsknite1's Avatar
Join Date: Jul 2006
Location: Florida, USA
Posts: 269
devilsknite1 has a spectacular aura about
Send a message via AIM to devilsknite1 Send a message via MSN to devilsknite1 Send a message via Yahoo to devilsknite1
Simple Safe

Intended for a class. Just a quick one I threw together, and some player wanted it. So... Here it is

PHP Code:
function onActionServerSideafiacc )
{
  switch( 
afi )
  {
    case 
"allowPlayer":
    {
      
temp.findPlayeracc );
      if ( 
player.account == "Your account here" )
      {
        
temp.i.clientr.canWithdraw 1;
        
temp.i.chat "I can now withdraw from the safe!";
      }
     break;
    }
    case 
"disallowPlayer":
    {
      
temp.findPlayeracc );
      if ( 
player.account == "" )
      {
        
temp.i.chat "I'm not allowed to withdraw from the safe anymore!";
        
temp.i.clientr.canWithdraw 0;
      }
     break;
    }
  }
}

function 
onCreated()
{
  
setImg"" ); // Your safe image
  
setShape13232 );
}

function 
onPlayerChats()
{
  
temp.tokens player.chat.tokenize();
  if ( 
player.chat.starts":allow" ) )
  {
    
triggerServer"gui"name"allowPlayer"temp.tokens] );
    
player.chat "You allowed:" SPC temp.tokensSPC "to withdraw from the safe!";
  }
  if ( 
player.chat.starts":disallow" ) )
  {
    
triggerServer"gui"name"disallowPlayer"temp.tokens] );
    
player.chat "You have disallowed:" SPC temp.tokensSPC "to withdraw from the safe!";
  }
  if ( 
player.chat.starts":donate" ) )
  {
    
this.moneyAdd temp.tokens];
    if ( 
clientr.item.Money >= this.moneyAdd )
    {
      if ( 
this.moneyAdd )
      {
        
this.safeMoney += this.moneyAdd;
        
player.rupees -= this.moneyAdd;
        
onShowMoney();
        
saveLog2"logs/safeDonate.txt""Player:" SPC player.account SPC "donated:" SPC temp.tokensSPC "at:" SPC player.level.name "!" );
      }
        else
      {
        
player.chat "You can't donate a negative amount!";
      }
    }
      else
    {
      
player.chat "You don't have enough money to donate this amount!";
    }
  }
  if ( 
player.chat.starts":withdraw" ) )
  {
    
this.takeMoney temp.tokens];
    if ( 
this.safeMoney >= this.takeMoney )
    {
      if ( 
this.takeMoney )
      {
        if ( 
clientr.canWithdraw == )
        {
          
player.rupees += this.takeMoney;
          
this.safeMoney -= this.takeMoney;
          
saveLog2"logs/safeWithdraw.txt""Player:" SPC player.account SPC "withdrew" SPC temp.tokensSPC "at:" SPC player.level.name );
          
onShowMoney();
        }
          else
        {
          
player.chat "You're not allowed to withdraw from the safe!";
        }
      }
        else
      {
        
player.chat "You can't withdraw a negative amount!";
      }
    }
      else
    {
      
player.chat "There isn't that much money in the safe!";
    }
  }
}

function 
onShowMoney()
{
  
showText1x.5"Arial""bc""There is $" this.safeMoney SPC "in the safe!" );

Feel free to point out mistakes.

Last edited by devilsknite1; 10-13-2008 at 03:57 AM..
Reply With Quote
  #2  
Old 10-13-2008, 01:07 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Consecutive if statements without using else if is improper programming. Although it works fine in GScript, if you tried programming like that in other languages, the compiler would report errors.

Also, to help prevent possible glitching, you should take the absolute value of the donated amount and then run your checks from there. I see that you do have a > 0 check, but you don't have any else statements in the code which makes it not-so user friendly when it comes to the script reporting proper syntax for players that use the donate command improperly.

For the withdraw bit, you should create an array where you can simply add accounts to the array granting permission to withdrawing from the safe. For convenience, adding and removing of accounts into the array should be able to be handled from client to prevent the inconvenience of loading the script and editing it each time you wish to give or take the right to withdraw from the safe from someone.

For the actual donating, there should be a verifying option that tells the user how much they are going to donate and asks them if they wish to donate that amount. This is to prevent people from donating an amount that they didn't want to. Perhaps you should also do the same for the withdrawing bit to prevent someone from withdrawing too much or so.

And there should also be a cooldown system for withdrawing to prevent someone from glitching it to where they take money from the chest without the chest actually losing money. The same should be done for donating to prevent adding money to the chest while not removing the money from the player.


Just some things I noticed at a quick glance.

Last edited by Gambet; 10-13-2008 at 01:44 AM..
Reply With Quote
  #3  
Old 10-13-2008, 02:29 AM
napo_p2p napo_p2p is offline
oh snaps
napo_p2p's Avatar
Join Date: Sep 2003
Location: Pismo Beach, California
Posts: 2,118
napo_p2p has a spectacular aura aboutnapo_p2p has a spectacular aura about
Send a message via AIM to napo_p2p Send a message via MSN to napo_p2p
Quote:
Originally Posted by Gambet View Post
Also, to help prevent possible glitching, you should take the absolute value of the donated amount and then run your checks from there.
Probably also using int() so people don't deposit/withdraw fractions of currency.
__________________
Scito hoc super omnia.
Haec vita est tua una sola.
Dum vita superest, utere maxime quoque puncto, momento, et hora quae habes.
Tempus neminem non manet.
Noli manere tempus.
Carpe Diem

Seize the Day.
Reply With Quote
  #4  
Old 10-13-2008, 03:27 AM
devilsknite1 devilsknite1 is offline
C:
devilsknite1's Avatar
Join Date: Jul 2006
Location: Florida, USA
Posts: 269
devilsknite1 has a spectacular aura about
Send a message via AIM to devilsknite1 Send a message via MSN to devilsknite1 Send a message via Yahoo to devilsknite1
Thanks Gambet. Last time I did the cool down system, I had to change it to clientside for some odd reason. Which enabled more glitching. Or memory editing for that matter. I'll update it in a second.
Reply With Quote
  #5  
Old 10-13-2008, 03:27 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
You should be using the && (AND) operator to join conditional statements rather than having a repeated nested if statement without an else.
PHP Code:
//BAD
if (this.safeMoney >= this.takeMoney) {
  if (
this.takeMoney 0) {
    if (
player.account == "PERSONS ACCOUNT HERE") {
      
//BLAH
    
}
  }
}
//GOOD
if (this.safeMoney >= this.takeMoney && this.takeMoney && player.account == "BLAH") {
  
//BLAH
}
//GOOD TOO
if (this.safeMoney >= this.takeMoney &&
    
this.takeMoney &&
    
player.account == "BLAH")
{
  
//BLAH

Quote:
Originally Posted by Gambet View Post
Consecutive if statements without using else if is improper programming.
Consecutive nested if statements are to be avoided without a good reason (can't think of one).
__________________
Reply With Quote
  #6  
Old 10-13-2008, 03:39 AM
devilsknite1 devilsknite1 is offline
C:
devilsknite1's Avatar
Join Date: Jul 2006
Location: Florida, USA
Posts: 269
devilsknite1 has a spectacular aura about
Send a message via AIM to devilsknite1 Send a message via MSN to devilsknite1 Send a message via Yahoo to devilsknite1
I should, but this way, I can do what Gambet said (else commands) and make it a lot easier.
Reply With Quote
  #7  
Old 10-13-2008, 04:51 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
You don't need to set any types of flags on the player to give them rights to withdraw, especially a clientr. flag.

Currently, the way you have it, if you have clientr.canWithdraw, then you'll be allowed to withdraw from ANY chest, which is a very bad thing.

Record the string of accounts in the script itself and read it via lindexof or (player.account in array)
Reply With Quote
  #8  
Old 10-13-2008, 07:47 AM
Mark Sir Link Mark Sir Link is offline
Kevin Azite
Mark Sir Link's Avatar
Join Date: Sep 2005
Posts: 1,489
Mark Sir Link is just really niceMark Sir Link is just really nice
Send a message via AIM to Mark Sir Link
Quote:
Originally Posted by Gambet View Post
Consecutive if statements without using else if is improper programming. Although it works fine in GScript, if you tried programming like that in other languages, the compiler would report errors.
That's news to me.
Reply With Quote
  #9  
Old 10-13-2008, 07:50 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Mark Sir Link View Post
That's news to me.
MSVC doesn't report errors for that, or warnings.
__________________
Reply With Quote
  #10  
Old 10-13-2008, 08:04 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
By errors I mean errors in result.

PHP Code:
class Testing
{
 public static 
void main(String[] args)
  {
   
int foo 20bar 0;

   if (
foo 15bar 100;
   if (
foo 0bar 50;

   
System.out.println(bar);
  }

Quote:
Originally Posted by Result
50

Compared to:

PHP Code:
class Testing
{
 public static 
void main(String[] args)
  {
   
int foo 20bar 0;

   if (
foo 15bar 100;
   else if (
foo 0bar 50;

   
System.out.println(bar);
  }

Quote:
Originally Posted by Result
100

I can see where the confusion came from, I should have chosen my words more carefully. My apologies for that, but just know that I'm referring to errors in calculations produced by the compiler, not errors that would keep the code from compiling (of which would yield warnings).

When I think about it, I've never tried the above code in GScript, so I have no clue what results would be produced with the GScript compiler so it might work the same, in which case the first bit of the statement that I made would be false. Regardless, the rest holds its value. Whether GScript bypasses your need to use an else statement in the situation or not, you should always use else if when writing statements checking for different values of the same variable.


Quote:
Originally Posted by Inverness View Post
Consecutive nested if statements are to be avoided without a good reason (can't think of one).
Well, combining all of your checks into one statement limits how specific the information that you return to the user can be. If you nest if statements, then you can add an else check to each if statement returning error messages to the user that specifies exactly what they did wrong, rather than a general message coming from a large check that tells the compiler that the user violated one of the checks in the statement.

In the case of the safe script in the first post, if devilsknite un-nests his if statements as you told him to, then he can't report error messages back to the player if they, say, try to withdraw money from the safe but are not in the array of allowed accounts, since every check would be in one if statement and if you violate any of the checks, then the same message would be sent back to the player regardless of which one you violated. In this case, nested if statements would be better in that you can communicate information back to the player more efficiently in the event that they violate separate parts of the checks.

Pseudocode (for the withdrawing bit):
PHP Code:
  if player's account in allowed array
  {
    if withdraw amount > 0
     {
       //Do withdraw stuff
     } else tell player they must withdraw a positive number that'
s greater than 0
  
} else tell player they're not allowed to withdraw money 

Just an example of why you would nest if statements.

Last edited by Gambet; 10-13-2008 at 09:01 AM..
Reply With Quote
  #11  
Old 10-13-2008, 02:14 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Gambet View Post
When I think about it, I've never tried the above code in GScript, so I have no clue what results would be produced with the GScript compiler so it might work the same, in which case the first bit of the statement that I made would be false. Regardless, the rest holds its value. Whether GScript bypasses your need to use an else statement in the situation or not, you should always use else if when writing statements checking for different values of the same variable.
GScript lets a lot of things slide (to my annoyance) but that is certainly not one of them.

I'm quite familiar with the advantages of nested if statements. My case only applies if there is no else or leading or following statements around the nested if statement.
__________________
Reply With Quote
  #12  
Old 10-13-2008, 03:02 PM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by Inverness View Post
Consecutive nested if statements are to be avoided without a good reason (can't think of one).

Quote:
Originally Posted by Inverness View Post
I'm quite familiar with the advantages of nested if statements.


I'm not the only one that should have better worded their original statements.



Reply With Quote
  #13  
Old 10-13-2008, 04:12 PM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Gambet View Post
I'm not the only one that should have better worded their original statements.





My initial post is referring to nested if statements that don't have an else or other intermediate statement. Like seen in the OP's original script.

And I am available if you need someone to talk to about programming. I will attempt to convince you to learn C++ and Python
__________________
Reply With Quote
  #14  
Old 10-13-2008, 09:13 PM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Inverness View Post
You should be using the && (AND) operator to join conditional statements rather than having a repeated nested if statement without an else.
Although it makes no difference whatsoever during runtime, this just causes obfuscation and should be AVOIDED.

edit: please review.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #15  
Old 10-14-2008, 04:11 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Tolnaftate2004 View Post
Although it makes no difference whatsoever during runtime, this just causes obfuscation and should be AVOIDED.

edit: please review.
That is implied.

Another thing that causes obfuscation would be the lack of spaces between operators, it looks like that was just done so he could better make his point.
__________________
Reply With Quote
  #16  
Old 10-16-2008, 05:05 AM
Kristi Kristi is offline
Bowie's Deciple
Kristi's Avatar
Join Date: Dec 2003
Location: Boston, MA
Posts: 748
Kristi has a spectacular aura aboutKristi has a spectacular aura about
Send a message via AIM to Kristi Send a message via MSN to Kristi
Quote:
Originally Posted by Gambet View Post
Consecutive if statements without using else if is improper programming. Although it works fine in GScript, if you tried programming like that in other languages, the compiler would report errors.
Um, what? There is nothing wrong with consecutive if statements if the situation calls for it. It should never error, unless you are referring to a lack of end if/end/fi/whatever (which is a minority of languages)

This is valid php code that is necessary to use two consecutive ifs to do what it needs to do (granted it is pointless)
PHP Code:
if ($i 3) {
  
$text .= "This number is less then 3. ";
}
if (
$i 1) {
  
$text .= "This number is greater then 1. ";
}
print 
$text
Quote:
Originally Posted by Gambet View Post
Also, to help prevent possible glitching, you should take the absolute value of the donated amount and then run your checks from there. I see that you do have a > 0 check, but you don't have any else statements in the code which makes it not-so user friendly when it comes to the script reporting proper syntax for players that use the donate command improperly.
Um, why? Absolute value? He should just give an error message for anything less then 0 and that is it.

Quote:
Originally Posted by Inverness View Post
I'm quite familiar with the advantages of nested if statements. My case only applies if there is no else or leading or following statements around the nested if statement.
Your case is still wrong (as PFA already pointed out). You don't need an else to justify nested if statements. You should nest if statements if they don't follow the same logical flow. It makes no difference to the compiler so why not make it more readable?
PHP Code:
// This rules
if (player.chat == "let us do this") {
  if(
player.30 && player.40) {
       
//stuff
   
}
}

// This sucks
if (player.chat == "let us do this" && player.40 && player.30) {
  
//stuff

__________________
Reply With Quote
  #17  
Old 10-16-2008, 05:15 AM
Gambet Gambet is offline
Registered User
Join Date: Oct 2003
Posts: 2,712
Gambet is on a distinguished road
Quote:
Originally Posted by Kristi View Post
Um, what? There is nothing wrong with consecutive if statements if the situation calls for it. It should never error, unless you are referring to a lack of end if/end/fi/whatever (which is a minority of languages)

This is valid php code that is necessary to use two consecutive ifs to do what it needs to do (granted it is pointless)
I elaborated on what I meant and gave examples using Java in the previous page. No point on reiterating my explanation, I'm just going to assume that you didn't look at it?

Quote:
Originally Posted by Kristi View Post
Um, why? Absolute value? He should just give an error message for anything less then 0 and that is it.
It's a mode of preference, so there shouldn't be an argument because both ways could work without tampering efficiency. Without the absolute value, the script would reject negative values, so if you simply converted the negative values to positive values, then you'd save yourself having to check for only positive numbers.

If the script prompted the user to verify the amount that they are donating/withdrawing, then this wouldn't be a problem, since they could simply reject to donate/withdraw the amount if they entered, say, -5, and the script read it as 5.

Since there should be a verification bit, it would save you a check in the end since you wouldn't have to do if (whatever >= 0), since whatever will always be greater than or equal to 0.


In the end, it depends on how you want to do it.
Reply With Quote
  #18  
Old 10-16-2008, 07:10 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
I would not use absolute value myself. If the person enters -5 they should be told they're not doing it correctly.
Quote:
Originally Posted by Kristi View Post
Um, what? There is nothing wrong with consecutive if statements if the situation calls for it.
I believe I clarified that with consecutive nested if statements.
Quote:
Originally Posted by Kristi View Post
Your case is still wrong (as PFA already pointed out). You don't need an else to justify nested if statements. You should nest if statements if they don't follow the same logical flow. It makes no difference to the compiler so why not make it more readable?
I already said that was implied. Separating unrelated conditions is not something I think about because I just do it naturally.
__________________
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 11:39 PM.


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