It is bad practice to use Res for counting. Use simple Integer or Float types for it and setProp!
ResourcesLib is good for using it with . If you don't have growth it is possible you don't need use this lib.
Resource can be
balance (in USD, BTC or any other)
any game resources: gold, woods, stone, etc
etc, any float values
User's resource
let res = Libs.ResourcesLib.userRes("money");
Bot.sendMessage("Cur your money: " + res.value());
Res name is case sensitive. The resources “money”, “Money” and “MONEY” do not match. These are 3 separate resources.
One user can have same chats with bot.
For example: private and group chat.
But anywhere he have simular resources
Chat's resource
let res = Libs.ResourcesLib.chatRes("money");
Bot.sendMessage("Cur your money: " + res.value());
One user can have same chats with bot.
For example: private and group chat.
But he have diffent resources for each chats.
Methots for user's and chat resources
All methods can be for user's or chat's resources.
// get res
let res = Libs.ResourcesLib.userRes("money");
res.name - current res name. For example:
Libs.ResourcesLib.chatRes("BTC").name // is "BTC"
Basic functions
Current res amount
res.value()
Set amount for this res
res.set(amount)
for example: Libs.ResourcesLib.userRes("wood").set(10);
Add amount for this res
res.add(amount)
Res have such amount?
res.have(amount)- if res value equal amount or more return true
Take away amount from resource
res.remove(amount) - if have it res.removeAnyway(amount) - take away amount anyway.
Access to another resources
Access to another user's resources
// telegramid - it is telegram id for another user
let res = Libs.ResourcesLib.anotherUserRes("money", telegramid);
Bot.sendMessage("Cur your money: " + res.value());
Access to another chat's resources
// another chat's resources
// chatid - it is telegram id for another chat
let res = Libs.ResourcesLib.anotherChatRes("money", chatid);
Bot.sendMessage("Cur your money: " + res.value());
Resource transfering
let res = Libs.ResourcesLib.userRes("gold");
// telegramid - it is telegram id for another user
let anotherRes = Libs.ResourcesLib.anotherUserRes("gold", telegramid);
let usd = Libs.ResourcesLib.userRes("usd");
usd.resetGrowth();
Other methods for res.growth:
res.growth.info() - get info for current growth
res.growth.title() - get title. For example "add 5 once at 15 secs"
res.growth.isEnabled() - return true if is enabled
res.growth.stop() - stop growth
res.growth.resume() - resume growth after stop
res.growth.remove() - remove growth
res.growth.progress() - current progress for next iteration
res.growth.willCompletedAfter() - will completed iteration after this time in seconds
How to add growth to another resources?
For example we have:
bank deposit 100$ with yearly growth 10%
and simple wallet - 500$
Every year we add bank growth to wallet.
Init: on /start command (or any other command)
let wallet = Libs.ResourcesLib.userRes("wallet");
wallet.set(500);
let bankDeposit = Libs.ResourcesLib.userRes("deposit");
bankDeposit.set(100);
let secs_in_year = 1 * 60 * 60 * 24 * 365;
bankDeposit.growth.addPercent({
value: 10,
interval: secs_in_year
});
On/wallet command or etc
Or user can run it manually in anytime.
let wallet = Libs.ResourcesLib.userRes("wallet");
let bankDeposit = Libs.ResourcesLib.userRes("deposit");
// it is initial res value
let baseValue = bankDeposit.baseValue();
// total income by percent
let delta = bankDeposit.value() - baseValue;
// add all income to wallet
wallet.add(delta);
// and remove it from bank deposit
bankDeposit.set(baseValue);
How to
Q: How to give to referrer 5% of referral user deposit?
Q: How to give a bonus to all users every day?
For example add 10 to user's balance every day
Command /start
let balance = Libs.ResourcesLib.userRes("balance");
balance.set(0);
Bot.run( {
command: "/addBonus",
run_after: 1*60*60*24, // add bonus after 1 day
} )
Command /addBonus
if(request){
// user can not run this command manually
Bot.sendMessage("Restricted!")
return
}
let balance = Libs.ResourcesLib.userRes("balance");
balance.add(10);
// and repeat this command again after 1 day
Bot.run( {
command: "/addBonus",
run_after: 1*60*60*24, // after one day
} )
Bot.sendMessage("Bonus for you: 10")
Command /addBonus will be executed for each user. It spend 1 iteration every day for each user.
For example, for 100 user - it will be 100 iterations per day.
Q: How to make time limit bonus?
For example, user can get bonus once at 5 hours.
Command /bonus
let bonusCooldown = Libs.ResourcesLib.userRes("bonusCooldown");
// cooldown in seconds
let totalCooldown = 60*60*5; // 60*60*5 seconds = 5 hours
function resetCooldown(){
bonusCooldown.set(totalCooldown);
}
function setupCooldown(){
if(bonusCooldown.growth.isEnabled()){
// already setupped
return
}
bonusCooldown.growth.add({
value: -1, // just add negative value
interval: 1, // -1 once at 1 sec
min: 0
});
}
setupCooldown();
if(bonusCooldown.value() > 0){
Bot.sendMessage("Please wait: " + bonusCooldown.value() + " secs" );
return
}
// can give bonus now
resetCooldown(); // need to reset cooldown
Bot.sendMessage("You have bonus now");
// your other code here
//..
We can run this command every 1 year. It is possible for example, with