me
/
LeekBots
Archived
1
0
Fork 0
Browse Source

Initial Commit

master
sheychen 5 years ago
commit
83e785bcb7
  1. 3
      .gitignore
  2. 798
      APILeekwars.py
  3. 139
      CommandTree.py
  4. 506
      LeekBots.py
  5. 1
      README.md

3
.gitignore vendored

@ -0,0 +1,3 @@
ai
__pycache__
LeekBots.json

798
APILeekwars.py

@ -0,0 +1,798 @@
import requests
class APIAi:
"""docstring for APIAi"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/ai"
def change_folder(self, ai_id, folder_id, token):
url = self.url + "/change-folder/"
return self.session.post(url, data={"ai_id" : ai_id, "folder_id" : folder_id, "token" : token}).json()
def delete(self, ai_id, token):
url = self.url + "/delete/"
return self.session.post(url, data={"ai_id" : ai_id, "token" : token}).json()
def get(self, ai_id, token):
url = self.url + "/get/" + str(ai_id) + "/" + token
return self.session.get(url).json()
def get_farmer_ais(self, token):
url = self.url + "/get-farmer-ais/" + token
return self.session.get(url).json()
def new(self, folder_id, v2, token):
url = self.url + "/new/"
return self.session.post(url, data={"folder_id" : folder_id, "v2" : v2, "token" : token}).json()
def rename(self, ai_id, new_name, token):
url = self.url + "/rename/"
return self.session.post(url, data={"ai_id" : ai_id, "new_name" : new_name, "token" : token}).json()
def save(self, ai_id, code, token):
url = self.url + "/save/"
return self.session.post(url, data={"ai_id" : ai_id, "code" : code, "token" : token}).json()
def test(self, ai_id, leek_id, bots, type, token):
url = self.url + "/test/"
return self.session.post(url, data={"ai_id" : ai_id, "leek_id" : leek_id, "bots" : bots, "type" : type, "token" : token}).json()
def test_new(self, data, token):
url = self.url + "/test-new/"
return self.session.post(url, data={"data" : data, "token" : token}).json()
class APIAiFolder:
"""docstring for APIAiFolder"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/ai-folder"
def change_folder(self, folder_id, dest_folder_id, token):
url = self.url + "/change-folder/"
return self.session.post(url, data={"folder_id" : folder_id, "dest_folder_id" : dest_folder_id, "token" : token}).json()
def delete(self, folder_id, token):
url = self.url + "/delete/"
return self.session.post(url, data={"folder_id" : folder_id, "token" : token}).json()
def new(self, folder_id, token):
url = self.url + "/new/"
return self.session.post(url, data={"folder_id" : folder_id, "token" : token}).json()
def rename(self, folder_id, new_name, token):
url = self.url + "/rename/"
return self.session.post(url, data={"folder_id" : folder_id, "new_name" : new_name, "token" : token}).json()
class APIChangelog:
"""docstring for APIChangelog"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/changelog"
def get(self, language):
url = self.url + "/get/" + language
return self.session.get(url).json()
def get_last(self, language):
url = self.url + "/get-last/" + language
return self.session.get(url).json()
class APIChip:
"""docstring for APIChip"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/chip"
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
def get_templates(self):
url = self.url + "/get-templates"
return self.session.get(url).json()
class APIConstant:
"""docstring for APIConstant"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/constant"
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
class APICountry:
"""docstring for APICountry"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/country"
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
class APIFarmer:
"""docstring for APIFarmer"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/farmer"
def activate(self, farmer_id, code):
url = self.url + "/activate/"
return self.session.post(url, data={"farmer_id" : farmer_id, "code" : code}).json()
def change_country(self, country_code, token):
url = self.url + "/change-country/"
return self.session.post(url, data={"country_code" : country_code, "token" : token}).json()
def change_password(self, password, new_password, token):
url = self.url + "/change-password/"
return self.session.post(url, data={"password" : password, "new_password" : new_password, "token" : token}).json()
def disconnect(self, token):
url = self.url + "/disconnect/"
return self.session.post(url, data={"token" : token}).json()
def get(self, farmer_id):
url = self.url + "/get/" + str(farmer_id)
return self.session.get(url).json()
def get_connected(self):
url = self.url + "/get-connected"
return self.session.get(url).json()
def get_from_token(self, token):
url = self.url + "/get-from-token/" + token
return self.session.get(url).json()
def login_token(self, login, password):
url = self.url + "/login-token/"
return self.session.post(url, data={"login" : login, "password" : password}).json()
def register(self, login, password, email, leek_name, godfather):
url = self.url + "/register/"
return self.session.post(url, data={"login" : login, "password" : password, "email" : email, "leek_name" : leek_name, "godfather" : godfather}).json()
def register_tournament(self, token):
url = self.url + "/register-tournament/"
return self.session.post(url, data={"token" : token}).json()
def set_avatar(self, avatar, token):
url = self.url + "/set-avatar/"
return self.session.post(url, data={"avatar" : avatar, "token" : token}).json()
def set_github(self, github, token):
url = self.url + "/set-github/"
return self.session.post(url, data={"github" : github, "token" : token}).json()
def set_in_garden(self, in_garden, token):
url = self.url + "/set-in-garden/"
return self.session.post(url, data={"in_garden" : in_garden, "token" : token}).json()
def set_website(self, website, token):
url = self.url + "/set-website/"
return self.session.post(url, data={"website" : website, "token" : token}).json()
def unregister(self, password, delete_forum_messages, token):
url = self.url + "/unregister/"
return self.session.post(url, data={"password" : password, "delete_forum_messages" : delete_forum_messages, "token" : token}).json()
def unregister_tournament(self, token):
url = self.url + "/unregister-tournament/"
return self.session.post(url, data={"token" : token}).json()
def update(self, token):
url = self.url + "/update/"
return self.session.post(url, data={"token" : token}).json()
class APIFight:
"""docstring for APIFight"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/fight"
def comment(self, fight_id, comment, token):
url = self.url + "/comment/"
return self.session.post(url, data={"fight_id" : fight_id, "comment" : comment, "token" : token}).json()
def get(self, fight_id):
url = self.url + "/get/" + str(fight_id)
return self.session.get(url).json()
def get_logs(self, fight_id, token):
url = self.url + "/get-logs/" + str(fight_id) + "/" + token
return self.session.get(url).json()
class APIFunction:
"""docstring for APIFunction"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/function"
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
def get_categories(self):
url = self.url + "/get-categories"
return self.session.get(url).json()
class APIGarden:
"""docstring for APIGarden"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/garden"
def get(self, token):
url = self.url + "/get/" + token
return self.session.get(url).json()
def get_composition_opponents(self, composition, token):
url = self.url + "/get-composition-opponents/" + str(composition) + "/" + token
return self.session.get(url).json()
def get_farmer_challenge(self, target, token):
url = self.url + "/get-farmer-challenge/" + str(target) + "/" + token
return self.session.get(url).json()
def get_farmer_opponents(self, token):
url = self.url + "/get-farmer-opponents/" + token
return self.session.get(url).json()
def get_leek_opponents(self, leek_id, token):
url = self.url + "/get-leek-opponents/" + str(leek_id) + "/" + token
return self.session.get(url).json()
def get_solo_challenge(self, leek_id, token):
url = self.url + "/get-solo-challenge/" + str(leek_id) + "/" + token
return self.session.get(url).json()
def start_farmer_challenge(self, target_id, token):
url = self.url + "/start-farmer-challenge/"
return self.session.post(url, data={"target_id" : target_id, "token" : token}).json()
def start_farmer_fight(self, target_id, token):
url = self.url + "/start-farmer-fight/"
return self.session.post(url, data={"target_id" : target_id, "token" : token}).json()
def start_solo_challenge(self, leek_id, target_id, token):
url = self.url + "/start-solo-challenge/"
return self.session.post(url, data={"leek_id" : leek_id, "target_id" : target_id, "token" : token}).json()
def start_solo_fight(self, leek_id, target_id, token):
url = self.url + "/start-solo-fight/"
return self.session.post(url, data={"leek_id" : leek_id, "target_id" : target_id, "token" : token}).json()
def start_team_fight(self, composition_id, target_id, token):
url = self.url + "/start-team-fight/"
return self.session.post(url, data={"composition_id" : composition_id, "target_id" : target_id, "token" : token}).json()
class APIHat:
"""docstring for APIHat"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/hat"
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
def get_templates(self):
url = self.url + "/get-templates"
return self.session.get(url).json()
class APILang:
"""docstring for APILang"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/lang"
def get(self, file, lang):
url = self.url + "/get/" + file + "/" + lang
return self.session.get(url).json()
class APILeek:
"""docstring for APILeek"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/leek"
def add_chip(self, leek_id, chip_id, token):
url = self.url + "/add-chip/"
return self.session.post(url, data={"leek_id" : leek_id, "chip_id" : chip_id, "token" : token}).json()
def add_weapon(self, leek_id, weapon_id, token):
url = self.url + "/add-weapon/"
return self.session.post(url, data={"leek_id" : leek_id, "weapon_id" : weapon_id, "token" : token}).json()
def create(self, name, token):
url = self.url + "/create/"
return self.session.post(url, data={"name" : name, "token" : token}).json()
def delete_register(self, leek_id, key, token):
url = self.url + "/delete-register/"
return self.session.post(url, data={"leek_id" : leek_id, "key" : key, "token" : token}).json()
def get(self, leek_id):
url = self.url + "/get/" + str(leek_id)
return self.session.get(url).json()
def get_count(self):
url = self.url + "/get-count"
return self.session.get(url).json()
def get_image(self, leek, scale):
url = self.url + "/get-image/" + str(leek) + "/" + str(scale)
return self.session.get(url).json()
def get_level_popup(self, leek_id, token):
url = self.url + "/get-level-popup/" + str(leek_id) + "/" + token
return self.session.get(url).json()
def get_next_price(self, token):
url = self.url + "/get-next-price/" + token
return self.session.get(url).json()
def get_private(self, leek_id, token):
url = self.url + "/get-private/" + str(leek_id) + "/" + token
return self.session.get(url).json()
def get_registers(self, leek_id, token):
url = self.url + "/get-registers/" + str(leek_id) + "/" + token
return self.session.get(url).json()
def register_tournament(self, leek_id, token):
url = self.url + "/register-tournament/"
return self.session.post(url, data={"leek_id" : leek_id, "token" : token}).json()
def remove_ai(self, leek_id, token):
url = self.url + "/remove-ai/"
return self.session.post(url, data={"leek_id" : leek_id, "token" : token}).json()
def remove_chip(self, chip_id, token):
url = self.url + "/remove-chip/"
return self.session.post(url, data={"chip_id" : chip_id, "token" : token}).json()
def remove_hat(self, leek_id, token):
url = self.url + "/remove-hat/"
return self.session.post(url, data={"leek_id" : leek_id, "token" : token}).json()
def remove_weapon(self, weapon_id, token):
url = self.url + "/remove-weapon/"
return self.session.post(url, data={"weapon_id" : weapon_id, "token" : token}).json()
def rename_crystals(self, leek_id, new_name, token):
url = self.url + "/rename-crystals/"
return self.session.post(url, data={"leek_id" : leek_id, "new_name" : new_name, "token" : token}).json()
def rename_habs(self, leek_id, new_name, token):
url = self.url + "/rename-habs/"
return self.session.post(url, data={"leek_id" : leek_id, "new_name" : new_name, "token" : token}).json()
def set_ai(self, leek_id, ai_id, token):
url = self.url + "/set-ai/"
return self.session.post(url, data={"leek_id" : leek_id, "ai_id" : ai_id, "token" : token}).json()
def set_hat(self, leek_id, hat_id, token):
url = self.url + "/set-hat/"
return self.session.post(url, data={"leek_id" : leek_id, "hat_id" : hat_id, "token" : token}).json()
def set_in_garden(self, leek_id, in_garden, token):
url = self.url + "/set-in-garden/"
return self.session.post(url, data={"leek_id" : leek_id, "in_garden" : in_garden, "token" : token}).json()
def set_popup_level_seen(self, leek_id, token):
url = self.url + "/set-popup-level-seen/"
return self.session.post(url, data={"leek_id" : leek_id, "token" : token}).json()
def set_register(self, leek_id, key, value, token):
url = self.url + "/set-register/"
return self.session.post(url, data={"leek_id" : leek_id, "key" : key, "value" : value, "token" : token}).json()
def spend_capital(self, leek, characteristics, token):
url = self.url + "/spend-capital/"
return self.session.post(url, data={"leek" : leek, "characteristics" : characteristics, "token" : token}).json()
def unregister_tournament(self, leek_id, token):
url = self.url + "/unregister-tournament/"
return self.session.post(url, data={"leek_id" : leek_id, "token" : token}).json()
def use_potion(self, leek_id, potion_id, token):
url = self.url + "/use-potion/"
return self.session.post(url, data={"leek_id" : leek_id, "potion_id" : potion_id, "token" : token}).json()
class APILeekWars:
"""docstring for APILeekWars"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/leek-wars"
def version(self):
url = self.url + "/version"
return self.session.get(url).json()
class APIMarket:
"""docstring for APIMarket"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/market"
def buy_crystals(self, item_id, token):
url = self.url + "/buy-crystals/"
return self.session.post(url, data={"item_id" : item_id, "token" : token}).json()
def buy_habs(self, item_id, token):
url = self.url + "/buy-habs/"
return self.session.post(url, data={"item_id" : item_id, "token" : token}).json()
def get_item_templates(self, token):
url = self.url + "/get-item-templates/" + token
return self.session.get(url).json()
def sell_habs(self, item_id, token):
url = self.url + "/sell-habs/"
return self.session.post(url, data={"item_id" : item_id, "token" : token}).json()
class APIMessage:
"""docstring for APIMessage"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/message"
def create_conversation(self, farmer_id, message, token):
url = self.url + "/create-conversation/"
return self.session.post(url, data={"farmer_id" : farmer_id, "message" : message, "token" : token}).json()
def get_latest_conversations(self, count, token):
url = self.url + "/get-latest-conversations/" + str(count) + "/" + token
return self.session.get(url).json()
def get_messages(self, conversation_id, count, page, token):
url = self.url + "/get-messages/" + str(conversation_id) + "/" + str(count) + "/" + str(page) + "/" + token
return self.session.get(url).json()
def quit_conversation(self, conversation_id, token):
url = self.url + "/quit-conversation/"
return self.session.post(url, data={"conversation_id" : conversation_id, "token" : token}).json()
def send_message(self, conversation_id, message, token):
url = self.url + "/send-message/"
return self.session.post(url, data={"conversation_id" : conversation_id, "message" : message, "token" : token}).json()
class APINotification:
"""docstring for APINotification"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/notification"
def get_latest(self, count, token):
url = self.url + "/get-latest/" + str(count) + "/" + token
return self.session.get(url).json()
def read_all(self, token):
url = self.url + "/read-all/"
return self.session.post(url, data={"token" : token}).json()
class APIPotion:
"""docstring for APIPotion"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/potion"
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
class APIRanking:
"""docstring for APIRanking"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/ranking"
def fun(self, token):
url = self.url + "/fun/" + token
return self.session.get(url).json()
def get(self, category, order, page):
url = self.url + "/get/" + category + "/" + order + "/" + str(page)
return self.session.get(url).json()
def get_farmer_rank(self, farmer_id, order):
url = self.url + "/get-farmer-rank/" + str(farmer_id) + "/" + order
return self.session.get(url).json()
def get_home_ranking(self):
url = self.url + "/get-home-ranking"
return self.session.get(url).json()
def get_leek_rank(self, leek_id, order):
url = self.url + "/get-leek-rank/" + str(leek_id) + "/" + order
return self.session.get(url).json()
def get_team_rank(self, team_id, order):
url = self.url + "/get-team-rank/" + str(team_id) + "/" + order
return self.session.get(url).json()
def search(self, query, search_leeks, search_farmers, search_teams):
url = self.url + "/search/"
return self.session.post(url, data={"query" : query, "search_leeks" : search_leeks, "search_farmers" : search_farmers, "search_teams" : search_teams}).json()
class APIService:
"""docstring for APIService"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/service"
def get_all(self, token):
url = self.url + "/get-all/" + token
return self.session.get(url).json()
class APISummon:
"""docstring for APISummon"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/summon"
def get_templates(self):
url = self.url + "/get-templates"
return self.session.get(url).json()
class APITeam:
"""docstring for APITeam"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/team"
def accept_candidacy(self, candidacy_id, token):
url = self.url + "/accept-candidacy/"
return self.session.post(url, data={"candidacy_id" : candidacy_id, "token" : token}).json()
def ban(self, farmer_id, token):
url = self.url + "/ban/"
return self.session.post(url, data={"farmer_id" : farmer_id, "token" : token}).json()
def cancel_candidacy(self, token):
url = self.url + "/cancel-candidacy/"
return self.session.post(url, data={"token" : token}).json()
def cancel_candidacy_for_team(self, team_id, token):
url = self.url + "/cancel-candidacy-for-team/"
return self.session.post(url, data={"team_id" : team_id, "token" : token}).json()
def change_description(self, description, token):
url = self.url + "/change-description/"
return self.session.post(url, data={"description" : description, "token" : token}).json()
def change_member_grade(self, member_id, new_grade, token):
url = self.url + "/change-member-grade/"
return self.session.post(url, data={"member_id" : member_id, "new_grade" : new_grade, "token" : token}).json()
def change_owner(self, new_owner, password, token):
url = self.url + "/change-owner/"
return self.session.post(url, data={"new_owner" : new_owner, "password" : password, "token" : token}).json()
def create(self, team_name, token):
url = self.url + "/create/"
return self.session.post(url, data={"team_name" : team_name, "token" : token}).json()
def create_composition(self, composition_name, token):
url = self.url + "/create-composition/"
return self.session.post(url, data={"composition_name" : composition_name, "token" : token}).json()
def delete_composition(self, composition_id, token):
url = self.url + "/delete-composition/"
return self.session.post(url, data={"composition_id" : composition_id, "token" : token}).json()
def dissolve(self, token):
url = self.url + "/dissolve/"
return self.session.post(url, data={"token" : token}).json()
def get(self, team_id):
url = self.url + "/get/" + str(team_id)
return self.session.get(url).json()
def get_connected(self, team_id, token):
url = self.url + "/get-connected/" + str(team_id) + "/" + token
return self.session.get(url).json()
def get_private(self, team_id, token):
url = self.url + "/get-private/" + str(team_id) + "/" + token
return self.session.get(url).json()
def move_leek(self, leek_id, to, token):
url = self.url + "/move-leek/"
return self.session.post(url, data={"leek_id" : leek_id, "to" : to, "token" : token}).json()
def quit(self, token):
url = self.url + "/quit/"
return self.session.post(url, data={"token" : token}).json()
def register_tournament(self, composition_id, token):
url = self.url + "/register-tournament/"
return self.session.post(url, data={"composition_id" : composition_id, "token" : token}).json()
def reject_candidacy(self, candidacy_id, token):
url = self.url + "/reject-candidacy/"
return self.session.post(url, data={"candidacy_id" : candidacy_id, "token" : token}).json()
def send_candidacy(self, team_id, token):
url = self.url + "/send-candidacy/"
return self.session.post(url, data={"team_id" : team_id, "token" : token}).json()
def set_emblem(self, team_id, emblem, token):
url = self.url + "/set-emblem/"
return self.session.post(url, data={"team_id" : team_id, "emblem" : emblem, "token" : token}).json()
def set_opened(self, opened, token):
url = self.url + "/set-opened/"
return self.session.post(url, data={"opened" : opened, "token" : token}).json()
def unregister_tournament(self, composition_id, token):
url = self.url + "/unregister-tournament/"
return self.session.post(url, data={"composition_id" : composition_id, "token" : token}).json()
class APITestLeek:
"""docstring for APITestLeek"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/test-leek"
def delete(self, id, token):
url = self.url + "/delete/"
return self.session.post(url, data={"id" : id, "token" : token}).json()
def get_all(self, token):
url = self.url + "/get-all/" + token
return self.session.get(url).json()
def new(self, name, token):
url = self.url + "/new/"
return self.session.post(url, data={"name" : name, "token" : token}).json()
def update(self, id, data, token):
url = self.url + "/update/"
return self.session.post(url, data={"id" : id, "data" : data, "token" : token}).json()
class APITestMap:
"""docstring for APITestMap"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/test-map"
def delete(self, id, token):
url = self.url + "/delete/"
return self.session.post(url, data={"id" : id, "token" : token}).json()
def get_all(self, token):
url = self.url + "/get-all/" + token
return self.session.get(url).json()
def new(self, name, token):
url = self.url + "/new/"
return self.session.post(url, data={"name" : name, "token" : token}).json()
def update(self, id, data, token):
url = self.url + "/update/"
return self.session.post(url, data={"id" : id, "data" : data, "token" : token}).json()
class APITestScenario:
"""docstring for APITestScenario"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/test-scenario"
def delete(self, id, token):
url = self.url + "/delete/"
return self.session.post(url, data={"id" : id, "token" : token}).json()
def get_all(self, token):
url = self.url + "/get-all/" + token
return self.session.get(url).json()
def new(self, name, token):
url = self.url + "/new/"
return self.session.post(url, data={"name" : name, "token" : token}).json()
def update(self, id, data, token):
url = self.url + "/update/"
return self.session.post(url, data={"id" : id, "data" : data, "token" : token}).json()
class APIToken:
"""docstring for APIToken"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/token"
def check(self, token):
url = self.url + "/check/"
return self.session.post(url, data={"token" : token}).json()
class APITournament:
"""docstring for APITournament"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/tournament"
def comment(self, tournament_id, comment, token):
url = self.url + "/comment/"
return self.session.post(url, data={"tournament_id" : tournament_id, "comment" : comment, "token" : token}).json()
def get(self, tournament_id, token):
url = self.url + "/get/" + str(tournament_id) + "/" + token
return self.session.get(url).json()
class APITrophy:
"""docstring for APITrophy"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/trophy"
def get_admin(self, lang, token, supertoken):
url = self.url + "/get-admin/" + lang + "/" + token + "/" + supertoken
return self.session.get(url).json()
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
def get_categories(self):
url = self.url + "/get-categories"
return self.session.get(url).json()
def get_farmer_trophies(self, farmer_id, lang, token):
url = self.url + "/get-farmer-trophies/" + str(farmer_id) + "/" + lang + "/" + token
return self.session.get(url).json()
def unlock(self, trophy_id, token):
url = self.url + "/unlock/"
return self.session.post(url, data={"trophy_id" : trophy_id, "token" : token}).json()
class APIWeapon:
"""docstring for APIWeapon"""
def __init__(self, session):
self.session = session
self.url = "https://leekwars.com/api/weapon"
def get_all(self):
url = self.url + "/get-all"
return self.session.get(url).json()
def get_templates(self):
url = self.url + "/get-templates"
return self.session.get(url).json()
class APILeekwars():
"""docstring for APILeekwars"""
def __init__(self):
self.session = requests.Session()
self.ai = APIAi(self.session)
self.aiFolder = APIAiFolder(self.session)
self.changelog = APIChangelog(self.session)
self.chip = APIChip(self.session)
self.constant = APIConstant(self.session)
self.country = APICountry(self.session)
self.farmer = APIFarmer(self.session)
self.fight = APIFight(self.session)
self.function = APIFunction(self.session)
self.garden = APIGarden(self.session)
self.hat = APIHat(self.session)
self.lang = APILang(self.session)
self.leek = APILeek(self.session)
self.leekWars = APILeekWars(self.session)
self.market = APIMarket(self.session)
self.message = APIMessage(self.session)
self.notification = APINotification(self.session)
self.potion = APIPotion(self.session)
self.ranking = APIRanking(self.session)
self.service = APIService(self.session)
self.summon = APISummon(self.session)
self.team = APITeam(self.session)
self.testLeek = APITestLeek(self.session)
self.testMap = APITestMap(self.session)
self.testScenario = APITestScenario(self.session)
self.token = APIToken(self.session)
self.tournament = APITournament(self.session)
self.trophy = APITrophy(self.session)
self.weapon = APIWeapon(self.session)

139
CommandTree.py

@ -0,0 +1,139 @@
#Sheychen 2017
#CC BY
import itertools
class CommandTree:
def __init__(self):
self.commands = {}
#self.options = {}
def addCommand(self, path, text, func, params):
if self.commands.get(path) is None:
self.commands[path] = {
'text': text,
'func': func,
'params': params
}
else:
print('Command {0}: Allready added'.format(path))
return self
def runCommand(self, path, args):
command = self.commands[path]
params = []
for data, param in itertools.zip_longest(args[len(path.split()):], command['params']):
if not param is None:
name = param.get('name', '')
if len(name) > 0:
name += ':'
if data is None:
if not param.get('optional', False):
print('Wrong params count in "{0}", {1} isn\'t optional.'.
format(path, name))
return
else:
ptype = param.get('type')
if type(ptype) is type:
try:
data = ptype(data)
except (TypeError, ValueError) as e:
print('Wrong type in "{0}", {1}"{2}" must be an {3}.'.format(
path, name, data, ptype.__name__))
return
if ptype == int:
pmin = param.get('min')
if type(pmin) is int:
if data < pmin:
print('Wrong value in "{0}", {1}"{2}" must be >= {3}.'.
format(path, name, data, pmin))
return
pmax = param.get('max')
if type(pmax) is int:
if data > pmax:
print('Wrong value in "{0}", {1}"{2}" must be <= {3}.'.
format(path, name, data, pmax))
return
plist = param.get('list')
if type(plist) is list:
if not data in plist:
print(
'Wrong value in "{0}", {1}"{2}" must be one of ({3}).'.
format(path, name, data, ', '.join(
str(x) for x in plist)))
return
else:
print('"{0}" only accepts {1} params.'.format(
path, len(command['params'])))
return
params.append(data)
command['func'](params)
def helpCommand(self, command, args):
print('TODO print params details')
print('{0}: {1}'.format(command, self.commands[command]['text']))
def listCommands(self, paths):
print('TODO print short params')
print(
'See "help <command>" for more details or "help <command path>" to filter commands'
)
paths.sort()
last = []
for path in paths:
current = path.split()
for i in range(0, min(len(last), len(current))):
if current[i] == last[i]:
current[i] = ' ' * len(current[i])
last = path.split()
print('{0}: {1}'.format(
' '.join(current), self.commands[path]['text'].split('\n')[0]))
def parse(self, args):
#TODO parse options
path = args
name = path.pop(0)
commands = list(self.commands.keys())
if len(args) > 0:
run = self.runCommand
if path[0] == 'help':
path.pop(0)
run = self.helpCommand
for i in range(0, len(path)):
newcommands = []
for command in commands:
splits = command.split()
if len(splits) >= i:
if splits[i] == path[i]:
newcommands.append(command)
commands = newcommands
if len(commands) <= 1:
break
if len(commands) == 0:
print('Unknown command "{0}"'.format(' '.join(path)))
print('See "{0} help"'.format(name))
elif len(commands) == 1:
run(commands[0], args)
else:
self.listCommands(commands)
else:
self.listCommands(commands)
'''
def addOption(self, key, names, options):
if self.options.get(key) is None:
self.options[key] = {'names': names, 'options': options}
else:
print('Option {0}: Allready added'.format(key))
return self
def getOption(self, key):
option = self.options.get(key)
if not option is dict:
return option.get('value')
else:
return option
'''

506
LeekBots.py

@ -0,0 +1,506 @@
#!/usr/bin/env python
#Sheychen 2017
#CC BY
import sys
import random
import os.path
import json
import APILeekwars as API
from CommandTree import CommandTree
global lwapi
lwapi = API.APILeekwars()
class Settings:
def __init__(self):
self.filePath = 'LeekBots.json'
self.settings = None
self.get()
def save(self):
with open(self.filePath, 'w') as outfile:
json.dump(self.settings, outfile)
def get(self):
if self.settings is None:
self.settings = {'farmers': {}}
if os.path.exists(
self.filePath) and os.path.getsize(self.filePath) > 0:
with open(self.filePath) as json_data_file:
js = json.load(json_data_file)
self.settings = js
return self.settings
def getFarmers(self):
return self.get()['farmers']
def addFarmer(self, id, login, password):
if not self.settings['farmers'].get(str(id)) is None:
raise ValueError('Farmer {0}: Allready added'.format(login))
self.settings['farmers'][id] = {
'login': login,
'password': password
}
self.save()
class Farmers:
def login(login, password):
r = lwapi.farmer.login_token(login, password)
if not r.get('success', False):
raise ValueError('{0}: {1}'.format(login, r.get('error', 'Fail')))
return Farmer(r)
def get():
farmers = []
logins = Settings().getFarmers()
for id in logins:
try:
farmers.append(
Farmers.login(logins[id]['login'], logins[id]['password']))
except ValueError as err:
print(format(err))
return farmers
def list(options):
mode = options[0]
if mode is None:
farmers = Settings().getFarmers()
for farmer in farmers:
print('{0}: {1}'.format(farmer, farmers[farmer]['login']))
else:
for farmer in Farmers.get():
print('{0}:'.format(farmer.name))
if mode == 'infos':
for field in ['id', 'talent', 'fights', 'habs']:
print(' {0} : {1}'.format(field, farmer.data[field]))
elif mode == 'ais':
for ai in farmer.getAis():
print(' {0} : {1}'.format(ai['name'], ai['id']))
elif mode == 'stuff':
print(' {0}'.format(', '.join(
str(item['template'])
for item in farmer.weapons + farmer.chips)))
elif mode == 'leeks':
for id in farmer.leeks:
print(' {0} : {1} ({2}, {3})'.format(
id, farmer.leeks[id]['name'],
farmer.leeks[id]['level'],
farmer.leeks[id]['talent']))
def stats(options):
print('Deprecated: use "pool stats"')
mode = options[0]
if mode == 'infos':
fields = {'talent': [], 'fights': [], 'habs': []}
for farmer in Farmers.get():
for field in fields:
fields[field].append(farmer.data[field])
print('value : min, avg, max')
for field in fields:
print('{0} : {1}, {2}, {3}'.format(field, min(
fields[field]), int(sum(fields[field]) / len(fields[field])),
max(fields[field])))
def register(options):
login = options[0]
password = options[1]
try:
farmer = Farmers.login(login, password)
Settings().addFarmer(farmer.id, login, password)
farmer.raiseError('OK') #Ugly
except ValueError as err:
print(format(err))
def buy(options):
item = options[0]
for farmer in Farmers.get():
try:
for x in (farmer.chips + farmer.weapons):
if x['template'] == item:
farmer.raiseError('Allready have one')
farmer.buy(item)
farmer.raiseError('OK') #Ugly
except ValueError as err:
print(format(err))
def sell(options):
item = options[0]
for farmer in Farmers.get():
try:
farmer.sell(item)
farmer.raiseError('OK') #Ugly
except ValueError as err:
print(format(err))
class FirstLeeks:
def fight(options):
print('Deprecated: use "pool fight"')
#NOTE: In pools use pool's ids and not name.startswith
random.seed()
farmers = Farmers.get()
random.shuffle(farmers)
for farmer in farmers:
try:
fights = options[0] if type(
options[0]) is int else farmer.fights
if fights < 1:
farmer.raiseError('No more fights')
leek = farmer.getLeek(farmer.getFirstLeekId())
for _ in range(fights):
opponents = leek.getOpponents()
if len(opponents) < 1:
leek.raiseError('Probably no more fights')
opponents = [
x['id'] for x in opponents
if options[1] == 'force'
or not x['name'].startswith('LeekBots')
]
if len(opponents) < 1:
leek.raiseError(
'Really? All your opponnents are allies')
print('https://leekwars.com/fight/{0}'.format(
leek.fight(random.choice(opponents))))
except ValueError as err:
print(format(err))
def list(options):
print('Deprecated: use "pool list"')
for farmer in Farmers.get():
try:
leek = farmer.getLeek(farmer.getFirstLeekId())
print(leek.name)
if len(options) == 1:
mode = options[0]
if mode == 'infos':
for field in ['id', 'talent', 'level']:
print(' {0} : {1}'.format(field,
leek.data[field]))
elif mode == 'stuff':
for item in leek.weapons + leek.chips:
print(' {0}'.format(item['template']))
elif mode == 'characteristics':
for field in [
'life', 'strength', 'wisdom', 'agility',
'resistance', 'science', 'magic', 'tp', 'mp',
'frequency', 'capital'
]:
print(' {0} : {1}'.format(field,
leek.data[field]))
except ValueError as err:
print(format(err))
def stats(options):
print('Deprecated: use "pool stats"')
mode = options[0]
if mode == 'infos':
fields = {'talent': [], 'level': []}
elif mode == 'characteristics':
fields = {
'life': [],
'strength': [],
'wisdom': [],
'agility': [],
'resistance': [],
'science': [],
'magic': [],
'tp': [],
'mp': [],
'frequency': [],
'capital': []
}
for farmer in Farmers.get():
try:
leek = farmer.getLeek(farmer.getFirstLeekId())
for field in fields:
fields[field].append(leek.data[field])
except ValueError as err:
print(format(err))
print('value : min, avg, max')
for field in fields:
print('{0} : {1}, {2}, {3}'.format(field, min(
fields[field]), int(sum(fields[field]) / len(fields[field])),
max(fields[field])))
def setupAI(options):
print('Deprecated: use "pool ais"')
try:
if not os.path.exists('ai'):
raise ValueError('Can\'t find "ai" folder')
ais = {}
for fileName in os.listdir('ai'):
if fileName.endswith('.leek'):
with open(os.path.join('ai', fileName), 'r') as myfile:
ais[fileName[:-5]] = myfile.read()
if (ais.get('AI') is None):
raise ValueError('Can\'t find "ai/AI.leek" file')
for farmer in Farmers.get():
try:
fais = farmer.getAis()
leek = farmer.getLeek(farmer.getFirstLeekId())
for ai in ais:
aid = None
for current in fais:
if current['name'] == ai:
aid = current['id']
break
if aid is None:
print('New ai "{0}" for {1}'.format(
ai, farmer.name))
aid = farmer.newAi(0, ai)['id']
farmer.saveAi(aid, ais[ai])
if ai == 'AI':
leek.setAi(aid)
leek.raiseError('OK') #Ugly
except ValueError as err:
print(format(err))
except ValueError as err:
print(format(err))
def tournament(options):
print('Deprecated: use "pool tournament"')
for farmer in Farmers.get():
try:
leek = farmer.getLeek(farmer.getFirstLeekId())
leek.tournament()
leek.raiseError('OK') #Ugly
except ValueError as err:
print(format(err))
def equipWeapon(options):
print('Deprecated: use "pool equip weapon"')
template = options[0]
for farmer in Farmers.get():
try:
wid = None
for weapon in farmer.weapons:
if (weapon['template'] == template):
wid = weapon['id']
if wid is None:
farmer.raiseError(
'Have any {0} available'.format(template))
leek = farmer.getLeek(farmer.getFirstLeekId())
leek.equipWeapon(wid)
leek.raiseError('OK') #Ugly
except ValueError as err:
print(format(err))
def unequipWeapon(options):
print('Deprecated: use "pool unequip weapon"')
template = options[0]
for farmer in Farmers.get():
try:
wid = None
leek = farmer.getLeek(farmer.getFirstLeekId())
for weapon in leek.weapons:
if (weapon['