Skip to content

Instantly share code, notes, and snippets.

@gkbrk
Created August 1, 2019 00:17
Show Gist options
  • Save gkbrk/008b3a0e63da314460029104b72c0c68 to your computer and use it in GitHub Desktop.
Save gkbrk/008b3a0e63da314460029104b72c0c68 to your computer and use it in GitHub Desktop.
Game bot for Titan Conquest
#!/usr/bin/env python3
import requests
import re
from bs4 import BeautifulSoup
UA = "Mozilla/5.0 (X11; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0"
USERNAME = "usernameHere"
PASSWORD = "passwordHere"
ROOT = "https://titanconquest.com/"
WORKER = f"{ROOT}worker.php"
class TitanConquest:
def __init__(self):
self.s = requests.session()
self.s.headers.update(
{
"User-Agent": UA,
"Origin": "https://titanconquest.com",
"Referer": "https://titanconquest.org/",
"X-Requested-With": "XMLHttpRequest",
}
)
def login(self, username, password):
self._worker_action_post("login", username=username, password=password)
def get_chat(self):
return self.s.get(WORKER, params={"go": "getchat", "room": "global"})
def get_stats(self):
html = self._worker_action_get("getstats").text
levelMatch = re.search("XP to (.*?): (.*?)<", html)
return {
"level": int(levelMatch.group(1)) - 1,
"xpleft": int(levelMatch.group(2).replace(',', '')),
"money": int(
re.search(
'glimmer.png"></div><div class="chip-label">(.*?)</div></div></a>',
html,
)
.group(1)
.replace(",", "")
),
}
def change_subregion(self, subregion_id):
self._worker_action_post("changesubregion", set=subregion_id)
def patrol(self):
html = self.s.get(f"{ROOT}patrol.php").text
soup = BeautifulSoup(html, features="html5lib")
for el in soup.find_all("a", class_="initBattle"):
yield el["data-enemyguid"]
def battle(self, guid, **kwargs):
params = {"guid": guid}
params.update(kwargs)
t = self.s.post(f"{ROOT}battle.php", data=params).text
return "postBattleInfo" in t
def _worker_action_post(self, action, **kwargs):
params = {"go": action}
params.update(kwargs)
return self.s.post(WORKER, params=params)
def _worker_action_get(self, action, **kwargs):
params = {"go": action}
params.update(kwargs)
return self.s.get(WORKER, params=params)
def main():
tc = TitanConquest()
tc.login(USERNAME, PASSWORD)
while True:
tc.change_subregion(133)
for g in tc.patrol():
if g in (None, ''): break
print(f'Starting battle with {g}')
tc.battle(g, init="1")
while True:
print(tc.get_stats())
if tc.battle(g) == True:
print('Battle ended')
tc.change_subregion(133)
break
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment