Skip to content

Instantly share code, notes, and snippets.

@abler98
Created November 18, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abler98/cb81ebe53eb62129403bfdc66070ca6e to your computer and use it in GitHub Desktop.
Save abler98/cb81ebe53eb62129403bfdc66070ca6e to your computer and use it in GitHub Desktop.
/**
* Бот для csgo.life
*/
console.log('Запуск бота...');
// var io = io(config.host);
var io = SOCKET;
const TYPE_HELLO = "hello";
const TYPE_BET = "bet";
const TYPE_ROLL = "roll";
const TYPE_CONFIRM = "betconfirm";
const TYPE_ERROR = "error";
const TYPE_ERROR_R = "Ошибка";
const TYPE_BALANCE = "balance";
const TYPE_CHAT = "chat";
const DELAY = 10; // Минимальная задержка перед ставкой
const RED = [1, 2, 3, 4, 5, 6, 7];
const BLACK = [8, 9, 10, 11, 12, 13, 14];
const RED_MIN = minValue(RED);
const RED_MAX = maxValue(RED);
const BLACK_MIN = minValue(BLACK);
const BLACK_MAX = maxValue(BLACK);
var playing = false; // Сбрасывается при победе
var paused = false; // Приостаналивает ставки
var round = 0; // Текущий раунд
var balance = 0; // Текущий баланс
var minAmount = 5; // Начальная ставка
var maxAmount = 5115; // Максимальная ставка
var ratio = 2; // Множитель
var minBet, maxBet; // Ограничение ставок
var lastLower, lastUpper;
var lastAmound = 0; // Последняя ставка
var lastRoll = 0; // Последнее выпавшее число
var lastRound = 0; // Последний раунд, в котором есть ставка
io.on('message', function (msg) {
handleMessage(msg);
});
function randomInteger(min, max) {
var rand = min + Math.random() * (max + 1 - min);
rand = Math.floor(rand);
return rand;
}
function randomLowerUpper() {
if (randomInteger(1, 2) == 1 && randomInteger(1, 14) <= 7) {
return BLACK;
} else if (randomInteger(1, 2) == 2 && randomInteger(1, 14) >= 8) {
return BLACK;
} else {
return RED;
}
}
function maxValue(array) {
return Math.max.apply(null, array);
}
function minValue(array) {
return Math.min.apply(null, array);
}
function emit(data, callback) {
io.emit('mes', data);
if (callback) callback(data);
}
function handleMessage(msg) {
if (msg.type == TYPE_HELLO || msg.type == TYPE_CONFIRM || msg.type == TYPE_BALANCE) {
updateBalance(msg);
}
if (msg.type == TYPE_HELLO) {
parseHelloMessage(msg);
makeBetIfNeed();
}
if (msg.type == TYPE_CONFIRM) {
console.log('Ставка принята');
lastRound = round;
lastLower = msg.bet.lower;
lastUpper = msg.bet.upper;
lastAmound = msg.bet.amount;
requestBalance();
playing = true;
}
if (msg.type == TYPE_ROLL) {
emit({ type: TYPE_CHAT, msg: 'Выпало число: ' + msg.roll, lang: 1 });
updateBalance(msg);
handleRoll(msg);
}
if (msg.type == TYPE_ERROR || msg.type == TYPE_ERROR_R) {
console.log('Ошибка: ' + msg.error);
}
}
function handleRoll(msg) {
if (isWin(msg.roll)) {
console.log('Выпало число: ' + msg.roll + ' (ПОБЕДА)');
resetPlaying();
} else {
console.log('Выпало число: ' + msg.roll);
}
lastRoll = msg.roll;
round = msg.rollid;
requestBalance();
makeBetIfNeed();
}
function isWin(won) {
return playing && (lastLower <= won && won <= lastUpper);
}
function resetPlaying() {
lastAmound = minAmount;
playing = false;
}
function parseHelloMessage(msg) {
var last = msg.rolls[msg.rolls.length - 1];
maxBet = msg.maxbet;
round = last.rollid;
lastRoll = last.roll;
}
function requestBalance() {
setTimeout(function () {
emit({ type: "balance" });
}, 1000);
}
function updateBalance(msg) {
if (msg.balance != null) {
console.log('Текущий баланс: ' + msg.balance);
balance = msg.balance;
}
}
function makeBetIfNeed() {
if (!paused && round > lastRound) {
if (playing) {
if (lastAmound >= maxAmount) {
console.log('Достигнуто максимально значение ставки (' + maxAmount + ')');
resetPlaying();
} else {
console.log('Удваиваем ставку');
makeBet(lastAmound * ratio, BLACK_MIN, BLACK_MAX);
}
} else {
if (lastRoll < BLACK_MIN) {
console.log('Начинаем игру');
makeBet(minAmount, BLACK_MIN, BLACK_MAX);
} else {
console.log('Ожидание след. раунда');
}
}
}
}
function makeBet(amount, lower, upper) {
setTimeout(function () {
if (!paused && round > lastRound && balance >= amount) {
emit({
"type": "bet",
"amount": amount,
"lower": lower,
"upper": upper,
"round": round
}, function (data) {
console.log('Ставка сделана: ' + data.amount);
// console.log(data);
});
} else if (balance < amount) {
console.log('Недостаточно средств');
resetPlaying();
}
}, getDelay());
}
function getDelay() {
return (randomInteger(1, 10) + DELAY) * randomInteger(1000, 1050);
}
function togglePause() {
paused = !paused;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment