Advertisement
JuSTaR

[CS:GO] ShopAPI

Jun 6th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.78 KB | None | 0 0
  1. #include <clientprefs>
  2.  
  3. public Plugin myinfo =
  4. {
  5.     name = "Shop API",
  6.     author = "JuSTaR",
  7.     description = "Shop API",
  8.     version = "1.0",
  9.     url = "https://www.fxp.co.il/"
  10. }
  11.  
  12. Handle DataCookies; // used to handle cookies Database
  13. ConVar GambleChance; // cvar used to set the gamble wining precent
  14. int Points[MAXPLAYERS]; // store players points
  15.  
  16. public void OnPluginStart()
  17. {
  18.     DataCookies = RegClientCookie("ShopAPI", "ShopAPI Cookies", CookieAccess_Private);
  19.     GambleChance = CreateConVar("gamble_chance", "50", "Sets gamble winning chance");
  20.  
  21.     RegConsoleCmd("p", CheckPoints);
  22.     RegConsoleCmd("points", CheckPoints);
  23.     RegConsoleCmd("gamble", OnGamble);
  24.     RegAdminCmd("give_points", OnGivePoints, ADMFLAG_RCON);
  25.     RegAdminCmd("take_points", OnTakePoints, ADMFLAG_RCON);
  26.     RegAdminCmd("set_points", OnSetPoints, ADMFLAG_RCON);
  27.  
  28.     LoadTranslations("common.phrases.txt"); // FindTarget uses it
  29. }
  30.  
  31. public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
  32. {
  33.    CreateNative("Native_GivePoints", Native_Give_Points);
  34.    CreateNative("Native_TakePoints", Native_Take_Points);
  35.    CreateNative("Native_GetPoints", Native_Get_Points);
  36.    CreateNative("Native_SetPoints", Native_Set_Points);
  37.  
  38.    return APLRes_Success;
  39. }
  40.  
  41. public void OnClientPutInServer(int client)
  42. {
  43.     if (AreClientCookiesCached(client))
  44.     {
  45.         char sPoints[12];
  46.         GetClientCookie(client, DataCookies, sPoints, sizeof(sPoints));
  47.         Points[client] = StringToInt(sPoints);
  48.     }
  49. }
  50.  
  51. public void OnClientDisconnect(int client)
  52. {
  53.     SavePoints(client);
  54. }
  55.  
  56. public int Native_Set_Points(Handle:plugin, int numParams)
  57. {
  58.     int target = GetNativeCell(1);
  59.     int PtsToSet = GetNativeCell(2);
  60.  
  61.     GivePoints(target, PtsToSet);
  62. }
  63.  
  64. public int Native_Get_Points(Handle:plugin, int numParams)
  65. {
  66.     int target = GetNativeCell(1);
  67.  
  68.     if (IsClientConnected(target))
  69.         return Points[target];
  70.     else
  71.         LogError("[ShopAPI] (Get_Points) Unable to find target [id:%d].", target);
  72.  
  73.     return -1;
  74. }
  75.  
  76. public int Native_Give_Points(Handle:plugin, int numParams)
  77. {
  78.     int target = GetNativeCell(1);
  79.     int PtsToGive = GetNativeCell(2);
  80.  
  81.     GivePoints(target, PtsToGive);
  82. }
  83.  
  84. public int Native_Take_Points(Handle:plugin, int numParamse)
  85. {
  86.     int target = GetNativeCell(1);
  87.     int PtsToTake = GetNativeCell(2);
  88.  
  89.     TakePoints(target, PtsToTake);
  90. }
  91.  
  92. public void SetPoints(int target, int PtsToSet)
  93. {
  94.     if (IsClientConnected(target))
  95.     {
  96.         Points[target] = PtsToSet;
  97.         SavePoints(target);
  98.     }
  99.     else
  100.         LogError("[ShopAPI] (Set_Points) Unable to find target [id:%d].", target);
  101. }
  102.  
  103. public void GivePoints(int target, int PtsToGive)
  104. {
  105.     if (IsClientConnected(target))
  106.     {
  107.         Points[target] += PtsToGive;
  108.         SavePoints(target);
  109.     }
  110.     else
  111.         LogError("[ShopAPI] (Give_Points) Unable to find target [id:%d].", target);
  112. }
  113.  
  114. public void TakePoints(int target, int PtsToTake)
  115. {
  116.     if (IsClientConnected(target))
  117.     {
  118.         if (Points[target] - PtsToTake < 0)
  119.             Points[target] = 0;
  120.         else
  121.             Points[target] -= PtsToTake;
  122.            
  123.         SavePoints(target);
  124.     }
  125.     else
  126.         LogError("[ShopAPI] (Take_Points) Unable to find target [id:%d].", target);
  127. }
  128.  
  129. public Action CheckPoints(int client, int args)
  130. {
  131.     char arg[32];
  132.    
  133.     if (GetCmdArg(1, arg, sizeof(arg)) > 0)
  134.     {
  135.         int target = FindTarget(0, arg);
  136.  
  137.         if (target == -1)
  138.         {
  139.             PrintToChat(client,"Invalid player name.");
  140.             return Plugin_Handled;
  141.         }
  142.         else
  143.         {
  144.             PrintToChat(client,"%N has %d Points.", target, Points[target]);
  145.             return Plugin_Handled;
  146.         }
  147.     }
  148.  
  149.     PrintToChat(client,"You have %d Points.", Points[client]);
  150.  
  151.     return Plugin_Handled;
  152. }
  153.  
  154. public Action OnGamble(int client, int args)
  155. {
  156.     char arg[32];
  157.     GetCmdArg(1, arg, sizeof(arg));
  158.     int GambledPoints = StringToInt(arg);
  159.     bool AllIn = false;
  160.  
  161.     if (StrEqual(arg, "AllIn", false)) // if allin
  162.     {
  163.         GambledPoints = Points[client];
  164.         AllIn = true;
  165.     }
  166.    
  167.     if (GambledPoints == 0) // if arg is not a number
  168.     {
  169.         PrintToChat(client,"Invalid points number.");
  170.         return Plugin_Handled;
  171.     }
  172.     else if (Points[client] == 0 || Points[client] < GambledPoints) // not enough points
  173.     {
  174.         PrintToChat(client,"You don't have enough points.");
  175.         return Plugin_Handled;
  176.     }
  177.  
  178.     if (GetRandomInt(1, 100) >= 100-GetConVarInt(GambleChance)) // won gamble
  179.     {
  180.         if (AllIn)
  181.         {
  182.             PrintToChatAll("%N has gambled AllIn (%d Points) and Won!", client, GambledPoints);
  183.             GivePoints(client, GambledPoints);
  184.             AllIn = false;
  185.         }
  186.         else
  187.         {
  188.             PrintToChatAll("%N has gambled and Won %d Points!", client, GambledPoints);
  189.             GivePoints(client, GambledPoints);
  190.         }
  191.     }
  192.     else // lose gamble
  193.     {
  194.         if (AllIn)
  195.         {
  196.             PrintToChatAll("%N has gambled AllIn (%d Points) and Lost!", client, GambledPoints);
  197.             TakePoints(client, GambledPoints);
  198.             AllIn = false;
  199.         }
  200.         else
  201.         {
  202.             PrintToChatAll("%N has gambled %d Points and Lost.", client, GambledPoints);
  203.             TakePoints(client, GambledPoints);
  204.         }
  205.     }
  206.  
  207.     return Plugin_Handled;
  208. }
  209.  
  210. public Action OnSetPoints(int client, int args)
  211. {
  212.     char a1[32];
  213.     char a2[32];
  214.     GetCmdArg(1, a1, sizeof(a1));
  215.     GetCmdArg(2, a2, sizeof(a2));
  216.  
  217.     int target = FindTarget(0, a1);
  218.     int PtsToSet;
  219.  
  220.     if (target == -1)
  221.     {
  222.         PrintToChat(client,"Invalid player name.");
  223.         return Plugin_Handled;
  224.     }
  225.     if (StringToInt(a2) <= 0)
  226.     {
  227.         PrintToChat(client,"Invalid points number.");
  228.         return Plugin_Handled;
  229.     }
  230.  
  231.     PtsToSet = StringToInt(a2);
  232.     PrintToChatAll("%N has set %N %d Points!", client, target, PtsToSet);
  233.     SetPoints(target, PtsToSet);
  234.  
  235.     return Plugin_Handled;
  236. }
  237.  
  238. public Action OnGivePoints(int client, int args)
  239. {
  240.     char a1[32];
  241.     char a2[32];
  242.     GetCmdArg(1, a1, sizeof(a1));
  243.     GetCmdArg(2, a2, sizeof(a2));
  244.  
  245.     int target = FindTarget(0, a1);
  246.     int PtsToGive;
  247.  
  248.     if (target == -1)
  249.     {
  250.         PrintToChat(client,"Invalid player name.");
  251.         return Plugin_Handled;
  252.     }
  253.     if (StringToInt(a2) <= 0)
  254.     {
  255.         PrintToChat(client,"Invalid points number.");
  256.         return Plugin_Handled;
  257.     }
  258.  
  259.     PtsToGive = StringToInt(a2);
  260.     PrintToChatAll("%N has gave %N %d Points!", client, target, PtsToGive);
  261.     GivePoints(target, PtsToGive);
  262.  
  263.     return Plugin_Handled;
  264. }
  265.  
  266. public Action OnTakePoints(int client, int args)
  267. {
  268.     char a1[32];
  269.     char a2[32];
  270.     GetCmdArg(1, a1, sizeof(a1));
  271.     GetCmdArg(2, a2, sizeof(a2));
  272.  
  273.     int target = FindTarget(0, a1);
  274.     int PtsToTake;
  275.  
  276.     if (target == -1)
  277.     {
  278.         PrintToChat(client,"Invalid player name.");
  279.         return Plugin_Handled;
  280.     }
  281.     if (StringToInt(a2) <= 0)
  282.     {
  283.         PrintToChat(client,"Invalid points number.");
  284.         return Plugin_Handled;
  285.     }
  286.  
  287.     PtsToTake = StringToInt(a2);
  288.     PrintToChatAll("%N has took %d Points from %N!", client, PtsToTake, target);
  289.     TakePoints(target, PtsToTake);
  290.  
  291.     return Plugin_Handled;
  292. }
  293.  
  294. public void SavePoints(int client)
  295. {
  296.     if (AreClientCookiesCached(client))
  297.     {
  298.         char sPoints[12];
  299.         IntToString(Points[client], sPoints, sizeof(sPoints));
  300.         SetClientCookie(client, DataCookies, sPoints);
  301.     }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement