Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Script: Shop system (https://forums.graalonline.com/forums/showthread.php?t=68274)

Skyld 08-21-2006 11:11 PM

Script: Shop system
 
Once again in the interest of education, here's my shop system. It's broken into several parts, so make sure to read the instructions carefully!

Now, of course you can modify the script to make it match your own setup, however, here's how it works in my configuration. It also works using gralats by default; you'll have to change it in order to use custom money.

You'll need to update the shop weapon so that it adds the item to the player matching your own system. Also, you'll want to make sure that the seteffect fade doesn't interfere with your daynight, and finally, note that the weapon has a "Debit" option. This was for my setup on Rudora so that you could pay straight from your bank account. Either match it to your own bank system or just rip it out.

The system will depend upon three database NPCs, two classes and one weapon. Start by making the Database NPCs;
  • DB_Items; this is where we will store information about the items which you will sell
  • DB_Shops; this is where we will store the names of the shops, funds received from shop sales, etc
  • DB_Prices; this is where we will store the prices of items

You'll need to populate the NPCs with some data. Let's start with DB_Items; put the following script in DB_Items:
PHP Code:

function onCreated()
{
  
/*
    Format:
    this.item_id = {Name, Internalname, Image, ActionWeapon, Canbedropped};
   */
 
  
this.item_100 = {"Block""block""block.png""-No Item"1};


Next, in DB_Prices, put the following:
PHP Code:

function onCreated()
{
  
// Format:
  // this.item_id = price
  
  
this.item_100 3// 3 gralats in this case


Next, you'll need to set up the levels with the classes. It shouldn't really matter what you name the classes, but for this example, we'll use 'shopcontrol' and 'shopitem'.

Each shop level requires one shopcontrol class. This puts the level's base name into a level variable, and displays the shop's funds and name. Put this somewhere nice-looking. Just do something like this:
PHP Code:

this.join("shopcontrol"); 

Now, for each item that you want to sell in the shop, place a shopitem. You must specify additional data for shopitem classes, such as the item you want to sell. Here's an example:
PHP Code:

this.item 100// this is item ID 100 from DB_Items
this.customimage false// use the image set in DB_Items
this.join("shopitem"); 

Finally, add the shop interface weapon into a weapon called "-Shop", and make sure that it is added to any players using the shop. Hell, you could just add it to all players on logon.

That's about it, really. Once you upload the shop, it should work. You should also be able to specify a name for the shop inside DB_Shops' flag list once it's uploaded.

Enjoy.

Shop item class:
PHP Code:

function onCreated()
{
  
onTimeout();
}

function 
onTimeout()
{
  
this.okay 0;
  
  if (
this.item == NULL)
  {
    
temp.data "-unavailable-";
  }
  
  if (
DB_Items.("item_" this.item) == NULL)
  {
    
temp.data "-unavailable-";
  }
  
  if (
DB_Prices.("item_" this.item) == NULL)
  {
    
temp.data "-unavailable-";
  }
  
  
this.itemdata DB_Items.("item_" this.item);
  
this.price DB_Prices.("item_" this.item);

  
this.setShape(13232);

  if (
temp.data == "-unavailable-")
  {
    
this.setimg(""); // <- image for unavailable item here
  
}
    else
  {
    if (
this.customimage != TRUE ||
        
this.customimage == 0)
    {
      
this.setimg(this.itemdata[2]);
    }
  }

  if (
temp.data == NULL)
  {
    
temp.data this.itemdata[0];
    
this.okay 1;
  }
  
  
showimg(1"@Tahoma@bc@" temp.datathis.1this.0.5);
  
changeimgzoom(10.5);
  
  
setTimer(2);
}

function 
onActionleftmouse()
{
  if (
this.okay == 1)
  {
    
player.triggerclient("-Shop""purchaseItem"this.itemthis.itemdatathis.price);
  }


Shop control class:
PHP Code:

function onCreated()
{
  
level.creditor extractfilebase(this.level.name);
  
  
onTimeout();
}

function 
onTimeout()
{
  
level.shopname = (!(DB_Shops.(level.creditor "_name")) ? 
                    
"Unnamed Shop" DB_Shops.(level.creditor "_name"));
  
  
showimg(1format("@Tahoma@bc@%s"level.shopname), 1.51);
  
changeimgvis(12);
  
changeimgzoom(10.8);
  
  
showimg(2format("@Tahoma@bc@Shop Funds: %ig"DB_Shops.(level.creditor "_funds")), 1.5y);
  
changeimgvis(22);
  
changeimgzoom(20.5);

  
setTimer(2);


-Shop weapon:
PHP Code:

function onActionserverside()
{
  if (
params[0] == "purchaseItem")
  {
    if (
params[1] == NULL)
    {
      
player.say2("An error occured. 1" params[1]);
      return;
    }
    
    if (
DB_Prices.("item_" params[1]) == NULL)
    {
      
player.say2("An error occured. 2");
      return;
    }
    
    
temp.price DB_Prices.("item_" params[1]);
    
temp.item params[1];
    
temp.creditor = (!level.creditor "unknown" level.creditor);
    
    if (
params[2] == false)
    {
      if (
player.rupees >= temp.price)
      {
        
player.rupees -= temp.price
        
// ADD THE ITEM TO THE PLAYER HERE
        
DB_Shops.(temp.creditor "_funds") += temp.price;
      
        
player.say2("Thank you.");
      }
        else
      {
        
player.say2("You do not have enough money!");
      }
    }
      else
    {
      if (
player.bankBalance() >= temp.price)
      {     
        
temp.res player.bankDebit(temp.pricetemp.creditor);   
        if (
temp.res == 0)
        { 
          
// ADD THE ITEM TO THE PLAYER HERE
          
DB_Shops.(temp.creditor "_funds") += temp.price;
      
          
player.say2(format("Thank you.\nDebited: %dg.\nNew bank balance: %dg."temp.priceplayer.bankBalance()));
        }
          else
        {
          
player.say2(format("Bank debit failed!\n%s."temp.res));
        }
      }
        else
      {
        
player.say2("You do not have enough money!");
      }
    }
  }
}

//#CLIENTSIDE

function onPlayerEnters()
{
  
client.menus.remove("shop");
  
hideimgs(200201);
  
  
Shop_Purchase.visible false;
  
Shop_Cancel.visible false;
  
Shop_Debit.visible false;
}

function 
onActionclientside(temp.actionnametemp.itemidtemp.itemdatatemp.itemprice)
{
  switch (
temp.actionname)
  {
    case 
"purchaseItem":
    {
      if (
"shop" in client.menus)
      {
        return;
      }
    
      
client.menus.add("shop");
      
this.sellitem temp.itemid;
      
      for (
temp.0temp.<= 0.7temp.+= 0.1)
      {
        
seteffect(000temp.i);
        
sleep(0.05);
      }
      
      
showimg(200"@Tahoma@cb@Purchase " temp.itemdata[0], screenwidth 2, (screenheight 2) - 150);
      
changeimgvis(2005);
      
      
showimg(201"@Tahoma@cb@Price: " temp.itemprice "g"screenwidth 2, (screenheight 2) - 120);
      
changeimgzoom(2010.6);
      
changeimgvis(2015);
      
      new 
GuiCheckBoxCtrl(Shop_Debit)
      {
        
profile "GuiBlueCheckBoxProfile";
        
        
position = {(screenwidth 2) - 64, (screenheight 2) - 110};
        
extent = {13616};
        
        
visible true;
        
        
text "Debit from bank account";
      }
      
      new 
GuiButtonCtrl(Shop_Purchase)
      {
        
profile "GuiGreenButtonProfile";
      
        
position = {(screenwidth 2) - 72, (screenheight 2) - 90};
        
extent = {6424};
        
        
visible true;
      
        
text "Purchase";
        
        
thiso.catchEvent(this"onAction""onPurchase");
      }
      
      new 
GuiButtonCtrl(Shop_Cancel)
      {
        
profile "GuiBlueButtonProfile";
      
        
position = {(screenwidth 2) + 12, (screenheight 2) - 90};
        
extent = {6424};
        
        
visible true;
      
        
text "Cancel";
        
        
thiso.catchEvent(this"onAction""onCancelPurchase");
      }
      
      break;
    }
  
    default:
    {
      echo(
"Invalid shop data received!");
    }
  }
}

function 
onPurchase()
{
  
triggeraction(00"serverside""-Rudora-Shop""purchaseItem"this.sellitemShop_Debit.checked);
  
onCancelPurchase();
}

function 
onCancelPurchase()
{
  if (!(
"shop" in client.menus))
  {
    return;
  }
  
  
hideimgs(200201);
  
  
Shop_Purchase.destroy();
  
Shop_Cancel.destroy();
  
Shop_Debit.destroy();
  
  for (
temp.0.7temp.>= 0temp.-= 0.1)
  {
    
seteffect(000temp.i);
    
sleep(0.05);
  }
 
  
client.menus.remove("shop");



Angel_Light 08-24-2006 01:25 AM

Sexy ;o

coreys 08-24-2006 02:11 AM

such a simple script to come from you, skyld! ;)

helpful though, way to go ;o

KuJi 08-24-2006 02:16 AM

Nifty..

Angel_Light 10-09-2006 02:51 AM

What do you mean by ActionWeapon? in DB_Items? And what exactly do I put in DB_Shops?

Inverness 10-12-2006 12:20 AM

ActionWeapon is most likely the actual Weapon the item uses to execute stuff.
I myself prefer to make certain public functions that are always executed in the weapon by the Mudlib.
(mud_onPickUp(), mud_onEquip(), mud_onActivate(), mud_onUnEquip(), mud_onDrop())

It works nicely and Oblivion is a good source of ideas.

Angel_Light 10-17-2006 02:27 AM

I'm entirely sure but okay, still having problems with it though.

sssssssssss 11-07-2009 11:23 PM

Resurrecting an old post. This is a good resource for some of us still, but Angel_Light's old question is mine.

So ActionWeapon is the -Shop weapon?
and how do you set up DB_Shops?

cbk1994 11-07-2009 11:50 PM

Quote:

Originally Posted by sssssssssss (Post 1537156)
Resurrecting an old post. This is a good resource for some of us still, but Angel_Light's old question is mine.

So ActionWeapon is the -Shop weapon?

The "ActionWeapon" looks like it's the weapon that goes along with the item, e.g. "Swords/Super Sword" might be the weapon name for a "supersword" item.
Quote:

and how do you set up DB_Shops?
It looks like it's all there, do you mean how do you add the database NPC itself? Just click Scripts > NPCs (or the NPC button if using the graphical RC), then Add (name it DB_Shops, and put it in an existing level).

sssssssssss 11-08-2009 02:35 AM

Well on here it just says make a DB called DB_Shops, and add a shop name in the flags.
doesnt tell what format, var to use if any, how to name the shops, nothing that i saw.

the block.png sample item isnt even showing up for me either, and i followed all the directions.

LoneAngelIbesu 11-08-2009 03:18 AM

Quote:

Originally Posted by sssssssssss (Post 1537241)
Well on here it just says make a DB called DB_Shops, and add a shop name in the flags.
doesnt tell what format, var to use if any, how to name the shops, nothing that i saw.

It does, but you have to actually read the code.

PHP Code:

  level.shopname = (!(DB_Shops.(level.creditor "_name")) ?  
                    
"Unnamed Shop" DB_Shops.(level.creditor "_name")); 

In DB_Shops, the format for naming shops is this.mylevel_name = "My Shop Name".

sssssssssss 11-08-2009 05:16 AM

in the flags i have

this.armageddon-shop1.nw_name = "SHOP 1"
also tried w/o .nw in the level name, w/o _name although its obviously required.

its not showing up.

can't buy anything either. Was also wondering, how would you add it to add player.bombs instead of a weapon? is typing that in where the ActionWeapon is sufficient?

LoneAngelIbesu 11-08-2009 07:04 PM

Quote:

Originally Posted by sssssssssss (Post 1537267)
in the flags i have

this.armageddon-shop1.nw_name = "SHOP 1"
also tried w/o .nw in the level name, w/o _name although its obviously required.

its not showing up.

Then you're doing something else wrong.

Quote:

Originally Posted by sssssssssss (Post 1537267)
can't buy anything either. Was also wondering, how would you add it to add player.bombs instead of a weapon? is typing that in where the ActionWeapon is sufficient?

You have to script adding the item yourself.

sssssssssss 11-09-2009 07:12 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1537338)
Then you're doing something else wrong.

Did everything that it said to do.

sssssssssss 11-13-2009 02:38 AM

Quote:

Originally Posted by LoneAngelIbesu (Post 1537251)
In DB_Shops, the format for naming shops is this.mylevel_name = "My Shop Name".

Just to clarify, its actually just
PHP Code:

mylevel_name "My Shop Name" 

no this.var at all, and no .nw either.

So for all to come, add DB_Shops, and put that in it for each shop.

Ex:
PHP Code:

armageddon-shop1_name="Lystra Shop" 

for the level armageddon-shop1.nw


All times are GMT +2. The time now is 06:31 AM.

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