AutismAlex

uPurchase uScript2

May 9th, 2021 (edited)
1,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.61 KB | None | 0 0
  1. translations = {
  2.     "TARGET_NOT_FOUND": "Use this command whilst looking at a door/vehicle",
  3.     "NOT_FOR_SALE": "This {0} is not for sale",
  4.     "USELL_USAGE": "Correct Usage: /uSell <amount>",
  5.     "THIS_COSTS": "This {0} costs: {1} experience.",
  6.     "SET_PURCHASABLE": "{0} set to purchasable! Cost: {1}",
  7.     "CANT_PURCHASE": "You don't have enough experience.",
  8.     "DOOR_PURCHASED": "Door purchased:  for {0} experience!\nSalvage and replace the door to let your group open it!",
  9.     "VEHICLE_PURCHASED": "Vehicle purchased for {0} experience!\nLeave and rejoin the server to be able to enter the car!",
  10.     "UI_PRICE": "Price: {0} Experience",
  11.     "UI_TIP": "Use /uPurchase to purchase."
  12. };
  13.  
  14. event onLoad(){
  15.     database.nonQuery("CREATE TABLE IF NOT EXISTS uPurchase_Doors(
  16.    id INT PRIMARY KEY,
  17.    price INT NOT NULL DEFAULT 0
  18.    );");
  19.     database.nonQuery("CREATE TABLE IF NOT EXISTS uPurchase_Vehicles(
  20.    id INT PRIMARY KEY,
  21.    price INT NOT NULL DEFAULT 0
  22.    );");
  23. }
  24.  
  25. function isValidTarget(target){
  26.     if(target == null or !["barricade","vehicle"].contains(target.type)) return false;
  27.     if(target.type == "barricade" and target.door == null) return false;
  28.     return true;
  29. }
  30.  
  31. function translate(player, ID, params){
  32.     if(params == null) params = [];
  33.     while(params.count != 4) params.add("");
  34.     player.message(translations[ID].format(params[0],params[1],params[2],params[3]));
  35. }
  36.  
  37. command uPurchase(){
  38.     permission = "upurchase";
  39.     allowedCaller = "player";
  40.     execute(){
  41.         target = player.look.getBarricade() != null ? player.look.getBarricade() :  player.look.getVehicle();
  42.         if(!isValidTarget(target))
  43.             return translate(player, "TARGET_NOT_FOUND");
  44.  
  45.         targetName = target.type == "vehicle" ? "Vehicle" : "Door";
  46.         if(target.owner != null)
  47.             return translate(player, "NOT_FOR_SALE", [targetName.toLower()]);
  48.  
  49.         DBcheck = database.firstRow("SELECT * FROM uPurchase_{0}s WHERE id = @p0;".format(targetName), target.instanceId);
  50.         if(DBcheck.count == 0)
  51.             return translate(player, "NOT_FOR_SALE", [targetName.toLower()]);
  52.         if(player.experience < DBcheck[1].toNumber())
  53.             return translate(player, "CANT_PURCHASE");
  54.  
  55.         player.experience-= DBcheck[1].toNumber();
  56.         translate(player, targetName.toUpper() + "_PURCHASED", [DBcheck[1]]);
  57.         database.nonQuery("DELETE FROM uPurchase_{0}s WHERE id = @p0;".format(targetName), target.instanceId);
  58.         target.owner = player.id;
  59.         target.group = player.steamGroup;
  60.     }
  61. }
  62.  
  63. command uPrice(){
  64.     permission = "upurchase";
  65.     allowedCaller = "player";
  66.     execute(){
  67.         target = player.look.getBarricade() != null ? player.look.getBarricade() : player.look.getVehicle();
  68.         if(!isValidTarget(target))
  69.             return translate(player, "TARGET_NOT_FOUND");
  70.  
  71.         targetName = target.type == "vehicle" ? "Vehicle" : "Door";
  72.  
  73.         if(target.owner != null)
  74.             return translate(player, "NOT_FOR_SALE", [targetName.toLower()]);
  75.  
  76.         DBcheck = database.firstRow("SELECT * FROM uPurchase_{0}s WHERE id = @p0;".format(targetName), target.instanceId);
  77.         if(DBcheck.count == 0)
  78.             return translate(player, "NOT_FOR_SALE", [targetName.toLower()]);
  79.  
  80.         translate(player, "THIS_COSTS", [targetName.toLower(), DBcheck[1]]);
  81.     }
  82. }
  83.  
  84. command uSell(amount){
  85.     permission = "upurchaseadmin";
  86.     allowedCaller = "player";
  87.     execute(){
  88.         target = player.look.getBarricade() != null ? player.look.getBarricade() :  player.look.getVehicle();
  89.         if(!isValidTarget(target))
  90.             return translate(player, "TARGET_NOT_FOUND");
  91.         if(amount == null)
  92.             return translate(player, "USELL_USAGE");
  93.        
  94.         targetName = target.type == "vehicle" ? "Vehicle" : "Door";
  95.         database.nonQuery("DELETE FROM uPurchase_{0}s WHERE id = @p0;".format(targetName), target.instanceId);
  96.         database.nonQuery("INSERT INTO uPurchase_{0}s (id, price) VALUES (@p0, @p1);".format(targetName), target.instanceId, amount);
  97.         target.owner = "0";
  98.         target.group = "0";
  99.         if(target.type == "vehicle") target.isLocked = true;
  100.         translate(player, "SET_PURCHASABLE", [targetName, amount]);
  101.     }
  102. }
  103.  
  104. event onPlayerGestured(player, gesture){
  105.     if(!["PUNCH_LEFT","PUNCH_RIGHT"].contains(gesture)) return;
  106.  
  107.     target = player.look.getBarricade() != null ? player.look.getBarricade() : player.look.getVehicle();
  108.     if(!isValidTarget(target) or target.owner != null)
  109.         return effectManager.clearUIById(7802,player.id);
  110.  
  111.     targetName = target.type == "vehicle" ? "Vehicle" : "Door";
  112.     DBcheck = database.firstRow("SELECT * FROM uPurchase_{0}s WHERE id = @p0;".format(targetName), target.instanceId);
  113.     if(DBcheck.count == 0) return;
  114.  
  115.     name = targetName == "Door" ? targetName : target.name;
  116.     effectManager.sendUI(7802, 7802, player.id, name, translations["UI_PRICE"].format(DBcheck[1]), translations["UI_TIP"]);
  117. }
Add Comment
Please, Sign In to add comment