vk-bot-sdk
Version:
NodeJS Lib for VK API
228 lines (181 loc) • 6.42 kB
JavaScript
var Group = require( './Group' );
var bot = new Group( '' );
var Keyboard = require( './structures/keyboard/keyboard' );
var Button = require( './structures/keyboard/button' );
var TextButton = require( './structures/keyboard/text-button' );
var users = { }
var Fields = { BET: 'bet', PERCENT: 'percent', NONE: false }
var Game = { MIN: 'game_min', MAX: 'game_max' }
class User {
constructor( id ) {
let self = this;
self.id = id;
self.balance = 100;
self.game = { bet: 1, percent: 50, profit: 5 }
self.currentField = Fields.NONE
}
bet( type ) {
if( this.balance < this.game.bet ) {
return 'Недостаточно денег!';
}
var r = Math.floor( Math.random() * 1000000 ), range;
this.profit();
if( type == Game.MIN ) {
range = Math.floor( this.game.percent / 100 * 999999 );
if( r < range ) {
this.balance += this.profit() - this.game.bet;
return 'Вы выиграли ' + this.profit() + '!\n\nБаланс: ' + this.balance;
} else {
this.balance -= this.game.bet;
return 'Выпало число ' + r + '!\n\nБаланс: ' + this.balance;
}
} else if( type == Game.MAX ) {
range = 999999 - Math.floor( this.game.percent / 100 * 999999 );
if( r > range ) {
this.balance += this.profit() - this.game.bet;
return 'Вы выиграли ' + this.profit() + '!\n\nБаланс: ' + this.balance;
} else {
this.balance -= this.game.bet;
return 'Выпало число' + r +'!\n\nБаланс: ' + this.balance;
}
} else {
return 'Неизвестная ошибка произошла во время игры.';
}
}
profit() {
return 100 / this.game.percent * this.game.bet;
}
setPercent( v ) {
this.game.percent = Math.min(Math.max(5, v), 99);
return 'Шанс выигрыша: ' + this.game.percent + ' [!]\nСтавка: ' + this.game.bet + '\n\nВозможный выигрыш: ' + this.profit();
}
setBet( v ) {
this.game.bet = Math.min(Math.max(1, v), this.balance);
return 'Шанс выигрыша: ' + this.game.percent + '\nСтавка: ' + this.game.bet + ' [!]\n\nВозможный выигрыш: ' + this.profit();
}
static getUser( id ) {
if( !User.users[id] ) {
User.users[id] = new User( id );
}
return User.users[id];
}
}
User.users = {};
var mainKeyboard = Keyboard.keyboard([
// 1
Keyboard.textButton({
label: 'Помощь',
payload: {
command: 'help'
}
}),
// 2
[
Keyboard.textButton({
label: 'Ставка',
payload: {
command: 'bet'
},
color: Button.PRIMARY_COLOR
}),
Keyboard.textButton({
label: 'Шанс выигрыша',
payload: {
command: 'percent'
},
color: Button.PRIMARY_COLOR
})
],
// 3
[
Keyboard.textButton({
label: 'Мин',
payload: {
command: 'bet_min'
}
}),
Keyboard.textButton({
label: 'Мин',
payload: {
command: 'percent_min'
}
})
],
// 4
[
Keyboard.textButton({
label: 'Макс',
payload: {
command: 'bet_max'
}
}),
Keyboard.textButton({
label: 'Макс',
payload: {
command: 'percent_max'
}
})
],
// 5
[
Keyboard.textButton({
label: 'Удвоить',
payload: {
command: 'bet_double'
}
}),
Keyboard.textButton({
label: '50%',
payload: {
command: 'percent_middle'
}
})
],
// 6
[
Keyboard.textButton({
label: 'Меньше',
payload: {
command: 'game_min'
},
color: Button.POSITIVE_COLOR
}),
Keyboard.textButton({
label: 'Больше',
payload: {
command: 'game_max'
},
color: Button.POSITIVE_COLOR
})
]
]);
// commands
bot.onPayloadMessage( 'start', function( msg ) {
msg.addText(
`Привет! Я игровой бот wordes. Игра очень простая:
1. Укажите размер ставки и свой шанс выигрыша. Будет показан возможный (расчетный) выигрыш от вашей ставки.
2. Выбираете промежуток больше или меньше.
3. Заранее генерируется число от 0 до 999 999. Если число находится в пределах диапазона больше/меньше , который вы выбрали,вы выигрываете.
Удачи!`).addKeyboard( mainKeyboard ).send();
});
bot.onPayloadMessage( 'game_min', msg => {
var u = User.getUser( msg.user_id );
msg.addText( u.bet( Game.MIN ) ).setKeyboard( mainKeyboard ).send();
});
bot.onPayloadMessage( 'game_max', msg => {
var u = User.getUser( msg.user_id );
msg.addText( u.bet( Game.MAX ) ).setKeyboard( mainKeyboard ).send();
});
bot.onPayloadMessage( '' )
bot.onCommand( 'ставка', msg => {
var u = User.getUser( msg.user_id );
msg.addText( u.setBet( parseInt( msg.body.split( ' ' )[1] ) ) ).send();
})
bot.onCommand( 'шанс', msg => {
var u = User.getUser( msg.user_id );
msg.addText( u.setPercent( parseInt( msg.body.split( ' ' )[1] ) ) ).send();
})
bot.onMessage( msg => {
msg.addText( 'Я тебя не понимаю :с' ).send();
})
// bot.onCallBackEvent( '' );