Last active
June 25, 2018 22:13
-
-
Save greenflame/a0a10b64e70a6be8870155429b174643 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name CW3 Shalteor's bot | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match https://web.telegram.org/ | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const cw3ChatName = 'Chat Wars 3'; | |
// js API | |
async function sleep(time) { | |
return new Promise((res) => { | |
setTimeout(() => { | |
res(); | |
}, time); | |
}); | |
} | |
function randomInt(from, to) { | |
return Math.floor(from + Math.random() * (to - from)); | |
} | |
// tg API | |
function getChats() { | |
} | |
function getCurrentChat() { | |
return $('span.tg_head_peer_title').text(); | |
} | |
function selectChat(name) { | |
$(`a.im_dialog div.im_dialog_peer span:contains(${name})`).mousedown() | |
} | |
function sendMessage(message) { | |
$('div.composer_rich_textarea').text(message); | |
$('span.im_submit_send_label').mousedown() | |
} | |
function getLastMessage() { | |
return $('div.im_message_text:visible:last').text(); | |
} | |
// cw API | |
async function switchToCw() { | |
let currentChatName = getCurrentChat(); | |
if (currentChatName !== cw3ChatName) { | |
selectChat(cw3ChatName); | |
await sleep(5000); | |
} | |
} | |
async function hero() { | |
sendMessage('🏅Герой'); | |
await sleep(5000); | |
let message = getLastMessage(); | |
return { | |
energy: extractEnergy(message) | |
}; | |
} | |
function forest() { | |
sendMessage(':evergreen_tree:Лес'); | |
} | |
function go() { | |
sendMessage('/go'); | |
} | |
// helpers | |
function extractEnergy(hero) { | |
var regexp = /Выносливость: (\d+)\/(\d+)/g; | |
var match = regexp.exec(hero); | |
if (match == null) { | |
return undefined; | |
} | |
return { | |
current: match[1], | |
max: match[2] | |
}; | |
} | |
// strategy API | |
async function simpleForestStrategy() { | |
console.log('simple forest strategy is running'); | |
while(true) { | |
let state = await hero(); | |
if (state.energy && state.energy.current >= 3) { | |
forest(); | |
} | |
await sleep(randomInt(3 * 60 * 1000, 5 * 60 * 1000)); | |
} | |
} | |
async function simpleCaravanWatcher() { | |
console.log('simple caravan watcher is running'); | |
while(true) { | |
let message = getLastMessage(); | |
if (message.includes('/go')) { | |
go(); | |
} | |
await sleep(10 * 1000); | |
} | |
} | |
function run() { | |
simpleCaravanWatcher(); | |
simpleForestStrategy(); | |
} | |
window.b = { | |
getChats: getChats, | |
getCurrentChat: getCurrentChat, | |
selectChat: selectChat, | |
sendMessage: sendMessage, | |
getLastMessage: getLastMessage, | |
switchToCw: switchToCw, | |
hero: hero, | |
forest: forest, | |
go: go, | |
simpleForestStrategy: simpleForestStrategy, | |
simpleCaravanWatcher: simpleCaravanWatcher, | |
run: run | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment