Junior Member
|
02-18-2018
, 04:13
Re: how to make a bonus every 24 hours?
|
#3
|
Quote:
Originally Posted by Totenfluch
Steps:
1) Create a Database (SQL or MySQL)
2) Create a Table (Primary key: SteamId, timestamp)
3) On Command -> check if an entry exists withing the last 24h (use date functions of the database)
4) If not -> Send the reward and update the timestamp of the last reward. If exists -> no reward
|
php
Quote:
pragma semicolon 1
#include <sourcemod>
#include <shop>
#pragma tabsize 0
#include<sdktools>
#include<sdkhooks>
new Handle:g_hConVar_iDelayTime;
new String:g_sAuth[66][32];
new g_iLastTime[66];
new g_iDelay;
new HandleB_hndl;
public Plugin:myinfo =
{
name = "[Shop] Bonus",
author = "Delires",
description = "Adds shop bonus.",
version = "1.0",
}
public OnPluginStart()
{
g_hConVar_iDelayTime = CreateConVar("sm_shop_bonus_delaytime", "25", "after how many seconds the player can get the following bonus", 0, false, 0.0, false, 0.0);
AutoExecConfig(true, "/shop_bonus", "shop");
/
RegConsoleCmd("playerbonus", Command_Bonus, "", 0);
}
public OnMapStart()
{
g_iDelay = GetConVarInt(g_hConVar_iDelayTime);
if(DB_hndl != INVALID_HANDLE) CloseHandle(DB_hndl); /
decl String:szError[255];
DB_hndl = SQLite_UseDatabase("shop_bonus", szError, sizeof(szError));
if(DB_hndl == INVALID_HANDLE)
{
SetFailState("[shop_bouns] Unable to connect to database (%s)", szError);
return;
}
SQL_LockDatabase(DB_hndl); /
// создаем таблицу
SQL_FastQuery(DB_hndl, "CREATE TABLE IF NOT EXISTS `shop_bonus` (`auth` TEXT(32), `time` INTEGER);");
SQL_UnlockDatabase(DB_hndl);
}
public OnClientPutInServer(iClient)
{
if (!IsFakeClient(iClient) && !IsClientSourceTV(iClient))
{
GetClientAuthString(iClient, g_sAuth[iClient], 31, true);
g_iLastTime[iClient] = 0;
}
return;
}
public Action:Command_Bonus(iClient, iArgs)
{
TryGiveBonus(iClient);
return Action:0;
}
TryGiveBonus(iClient)
{
new iCurrentTime = GetTime({0,0});
if (g_iLastTime[iClient] > iCurrentTime)
{
PrintToConsole(iClient, "please, do not spam!");
PrintToChat(iClient, "1");
return 0;
}
g_iLastTime[iClient] = iCurrentTime + 2;
decl String:sQuery[256];
Format(sQuery, 255, "SELECT `time` FROM `shop_bonus` WHERE `auth` = '%s'", g_sAuth[iClient]);
SQL_TQuery(DB_hndl, SQL_OnTryGiveBonus, sQuery, iClient, DBPriority:1);
return 0;
}
public SQL_OnTryGiveBonus(Handle:hOwner, Handle:hQuery, String:sError[], any:iClient)
{
if (hQuery)
{
decl String:sQuery[256];
if (SQL_FetchRow(hQuery))
{
new zero1 = 0;
new zero2 = 0;
new iCurrentTime = GetTime({0,0});
new iExpiriedTime = SQL_FetchInt(hQuery, zero1, zero2);
if (iExpiriedTime < iCurrentTime)
{
iCurrentTime = g_iDelay + iCurrentTime;
Format(sQuery, 255, "UPDATE `shop_bonus` SET `time` = %d WHERE `auth` = '%s'", iCurrentTime, g_sAuth[iClient]);
SQL_TQuery(DB_hndl, SQL_OnEmptyCallback, sQuery, any:0, DBPriority:1);
GiveBonus(iClient);
}
else
{
decl String:sExpiriedTime[64];
decl String:sCurrentTime[64];
FormatTime(sExpiriedTime, 63, "%d/%m/%Y - %H:%M", iExpiriedTime);
FormatTime(sCurrentTime, 63, "%d/%m/%Y - %H:%M", iCurrentTime);
PrintToChat(iClient, "\x01You will be able to get the bonus no sooner \x04%d\x03, \x01now \x04%s", sExpiriedTime, sCurrentTime);
ClientCommand(iClient, "play buttons/button11.wav");
}
}
else
{
Format(sQuery, 255, "INSERT INTO `shop_bonus` (`auth`, `time`) VALUES ('%s', %d)", g_sAuth[iClient], GetTime({0,0}) + g_iDelay);
SQL_TQuery(DB_hndl, SQL_OnEmptyCallback, sQuery, any:0, DBPriority:1);
GiveBonus(iClient);
}
}
return 0;
}
public SQL_OnEmptyCallback(Handle:hOwner, Handle:hQuery, String:sError[], any:data)
{
return 0;
}
GiveBonus(iClient)
{
new iBonus = GetRandomInt(200, 5000);
PrintToChat(iClient, "\x04You received a daily bonus: \x03%d loans", iBonus);
Shop_GiveClientCredits(iClient, iBonus);
}
|
does not work,please help me to correct mistakes.
Last edited by Domenator; 02-18-2018 at 09:05.
|
|