Advertisement
Jewsus

ShopScript

Jul 26th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Correct
  2. var Shopcolor = "250,250,47"; //A color, which will be used by the whole command
  3. var enabled = true; // Whether shop is currently enabled or not
  4. var plr; //Making the player global, so I don't have to pass it to every function that needs it
  5.  
  6. //This is what creates the command. Sets plr to reference player who executed the command, calls shopSwitch and onCommandExecuted if it's defined
  7. var shopCommand = acmd_alias_create("shop", 0, 0, "shop", function (player, args) {
  8.     plr = player;
  9.     if (args !== undefined) {
  10.         shopSwitch(player, args);
  11.         try {
  12.             onCommandExecuted();
  13.         } catch (e) {}
  14.     }
  15. });
  16.  
  17. //Correct
  18. function trace(msg) {
  19.     tshock_msg_colour(Shopcolor, plr, String(msg));
  20. }
  21.  
  22. //Correct
  23. function findItemByName(test, callback) {
  24.     if (test.length < 3) {
  25.         return false;
  26.     }
  27.     for (var prop in itemDataQWERTY) {
  28.        
  29.         if(test.substring(0, 3) == prop.substring(0,3)) {
  30.             callback(prop);
  31.             return true;
  32.         }
  33.     }
  34. }
  35. function fail() {
  36.     trace("Failed to execute desired statement. Try again and make sure you didn't mispell any parameter.");
  37.     if(typeof onFailed == "function") {
  38.         onFailed();
  39.     }
  40. }
  41.  
  42.  
  43.  
  44. function procceedToBuy(block, name) {
  45.     for (var prop in block) {
  46.         if (block.hasOwnProperty(prop)) {
  47.             if (block[prop].Xcmd == name) {
  48.                 buy(block[prop]);
  49.                 return;
  50.             }
  51.         }
  52.     }
  53.     fail();
  54. }
  55.  
  56. function buy(item) {
  57.     if (plr.InventorySlotAvailable) {
  58.         var secAcc = seconomy_get_account(plr);
  59.         var cost = seconomy_parse_money(item.XitemPrice);
  60.         seconomy_pay_async(secAcc, seconomy_get_account(tshock_get_player("Nalah")) ? tshock_get_player("Nalah") : seconomy_world_account(), cost, item.Xcmd, function (payResult) {
  61.             if (payResult.TransferSucceeded === true) {
  62.                 for (i = 0; i < item.Xitems.length; i++) {
  63.                     tshock_exec(tshock_server(), '/give \"' + item.Xitems[i] + '\" \"' + plr.Name + '\"');
  64.                 }
  65.                 if(typeof item.XonBought == "function") {
  66.                     item.XonBought();
  67.                 }
  68.             }
  69.             else {
  70.                 try {
  71.                     onPaymentFailed();
  72.                 }
  73.                 catch (e) {
  74.                     trace("Sorry, you are too broke, you need " + item.XitemPrice + " Cookies/Cuboids to buy this. Do /bank bal to see how much do you currently have.");
  75.                 }
  76.             }
  77.         });
  78.     }
  79.     else {
  80.         trace("Your inventory is full! Make sure you have enough free space");
  81.     }
  82. }
  83.  
  84. function seeInfo(block, cmd) {
  85.     for (var prop in block) {
  86.         if (block[prop].Xcmd == cmd) {
  87.             var subblock = block[prop];
  88.             var i = 0;
  89.             for (var subprop in subblock) {
  90.                 switch (i) {
  91.                 case 0:
  92.                     trace("  price : " + String(subblock[subprop]));
  93.                     break;
  94.                 case 1:
  95.                     trace("  name  : " + String(subblock[subprop]));
  96.                     break;
  97.                 case 2:
  98.                     trace("  items : " + String(subblock[subprop]));
  99.                     break;
  100.  
  101.                 }
  102.                 i++;
  103.             }
  104.         }
  105.     }
  106. }
  107.  
  108. function alertAll() {
  109.     if(tshock_has_permission(plr, "youshouldnthavethis")) {
  110.         //Things like vanity, armor, etc...
  111.         for (var prop in itemDataQWERTY) {
  112.             trace(prop + " :");
  113.             var Property = itemDataQWERTY[prop];
  114.             //Things like item, anotheritem, etc...
  115.             for (var subprop in Property) {
  116.                 trace("  " + subprop + " : ");
  117.                 var subProperty = Property[subprop];
  118.                 //Xcmd, Xprice, etc...
  119.                 for (var subsubprop in subProperty) {
  120.                     trace("    " + subsubprop + " : " + String(subProperty[subsubprop]));
  121.                 }
  122.                 break;
  123.             }
  124.         }
  125.     }
  126.     else {
  127.         fail();
  128.     }
  129. }
  130.  
  131. function traceHelp() {
  132.     trace("Welcome to /shop help! Behold:");
  133.     trace("/shop help - print this help");
  134.     trace("/shop (type) (name) - purchase an item/package from the shop");
  135.     trace("/shop see (type) (name) - view information about the package/item");
  136.     if(tshock_has_permission(plr, "youshouldnthavethis")) {
  137.        trace("/shop ON/OFF - enable/disable the shop");
  138.        trace("/shop test - test");
  139.     }
  140. }
  141.  
  142.  
  143.  
  144. function shopSwitch(player, args) {
  145.     if (args.Count > 0) {
  146.         if (String(args[0]) == "ON" && enabled === false && tshock_has_permission(player, "youshouldnthavethis")) {
  147.             enabled = true;
  148.             trace("Shop enabled.");
  149.         } else if (enabled === false) {
  150.             trace("Sorry! The shop is currently disabled! Come back next time");
  151.             return;
  152.         } else if (!tshock_has_permission(player, "youshouldnthavethis") && args[0] == "ON") {
  153.             trace("You are not allowed to do that.");
  154.         }
  155.         switch (args[0]) {
  156.         case "OFF":
  157.             enabled = false;
  158.             trace("Shop disabled.");
  159.             break;
  160.         case "ON":
  161.             trace("Shop is already enabled");
  162.             break;
  163.         case "test":
  164.             alertAll();
  165.             break;
  166.         case "see":
  167.             if (findItemByName(args[1], function(prop) {seeInfo(itemDataQWERTY[prop], args[2]); }) !== true) {
  168.                 fail();
  169.             }
  170.             break;
  171.         case "help":
  172.             traceHelp();
  173.             break;
  174.         default:
  175.                 if (findItemByName(args[0], function(prop) {procceedToBuy(itemDataQWERTY[prop], args[1]); }) !== true) {
  176.                     trace("Thingy not found! Try /shop help or check the spreadsheet!");
  177.                 }
  178.             break;
  179.         }
  180.     }
  181.     else {
  182.         trace("Shop is currently enabled.");
  183.         trace("Welcome to the shop, use /shop help to see what you can do");
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement