ResourcesLib
With lib we can manage any resources in bot.
- balance (in USD, BTC or any other)
- any game resources: gold, woods, stone, etc
- etc, any float values
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
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.
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"
res.value()
res.set(amount)
for example:
Libs.ResourcesLib.userRes("wood").set(10);
res.add(amount)
res.have(amount)
- if res value equal amount or more return trueres.remove(amount)
- if have it res.removeAnyway(amount) - take away amount anyway.// telegramid - it is telegram id for another user
let res = Libs.ResourcesLib.anotherUserRes("money", telegramid);
Bot.sendMessage("Cur your money: " + res.value());
// 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());
let res = Libs.ResourcesLib.userRes("gold");
// telegramid - it is telegram id for another user
let anotherRes = Libs.ResourcesLib.anotherUserRes("gold", telegramid);
res.takeFromAnother(anotherRes, amount);
res.transferTo(anotherRes, amount)
res.takeFromAnotherAnyway(anotherRes, amount)
res.transferToAnyway(anotherRes, amount)
For example "gold" for "wood":
res.exchangeTo(anotherRes, { remove_amount: 10, add_amount:23 } )
Resource can have growth.
For example simple growth:
add 5 every 10 secs to res
let health = Libs.ResourcesLib.userRes("health");
health.set(1);
health.growth.add({value: 5, interval:10 });
Interval - it is value in seconds. Value is added every interval
//Max value: 100
let secs_in_hour = 1 * 60 * 60;
health.growth.add({
value: 5,
interval: secs_in_hour,
max: 100
});
//Min value: -20
let secs_in_30hours = 1 * 60 * 60 * 30;
health.growth.add({
value: -5, // just add negative value
interval: secs_in_30hours,
min: -20
});
health.growth.add(
{value: 5,
interval: secs_in_30hours,
max_iterations_count: 3
});
For example add 15% every month for 100 USD
let usd = Libs.ResourcesLib.userRes("usd");
usd.set(100);
let secs_in_month = 60 * 60 * 24 * 31;
usd.growth.addPercent({
value: 15,
interval: secs_in_month
});
For example add 0.8% every day for 0.5 BTC with reinvest
let btc = Libs.ResourcesLib.userRes("BTC");
btc.set(0.5);
let secs_in_day = 1 * 60 * 60 * 24;
usd.growth.addCompoundInterest({
value: 0.8,
interval: secs_in_day
});
You can get initial res value by:
res.baseValue()
res.growth.info()
- get info for current growthres.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.progress()
- current progress for next iteration res.growth.willCompletedAfter()
- will completed iteration after this time in secondsFor example we have:
- bank deposit 100$ with yearly growth 10%
- and simple wallet - 500$
Every year we add bank growth to wallet.
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
});
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);
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.
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
//..
Last modified 1yr ago