Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeansouzak committed Oct 19, 2020
0 parents commit 025f457
Show file tree
Hide file tree
Showing 16 changed files with 517 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# '<ctrl>+<alt>+s': start, '<ctrl>+<alt>+p': pause, '<ctrl>+<alt>+q': die
#MANA_SPENT
#https://wiki.mediviastats.info/Medivia_Vocations_Introduction
#Quantos segundos para recuperar 1 de mana
#blank.png, food.png, hand.png, inside_backpack.png, trash_container.png
#RECONNECT caso caia ou deslogue o amiguinho irá tentar relogar

DRAG_TIME=0
SLEEP_ACTION=1
MEDIVIA_WINDOW=Medivia
IMAGES_PATH=C:\Users\XXX\Pictures\bot
MEDIVIA_PATH=C:\Users\XXX\medivia\Medivia_D3D.exe
RECONNECT=true

AGENT_NUMBER=3


WINDOW_1=Player One
SPELL_NAME_1=adori gran
MANA_SPENT_1=50
SECONDS_TO_ONE_MANA_1=3
MANA_TRAIN_1=false

WINDOW_2=Player Two
SPELL_NAME_2=adori vita vis
MANA_SPENT_2=180
SECONDS_TO_ONE_MANA_2=1
MANA_TRAIN_2=false

WINDOW_3=Player Three
SPELL_NAME_3=adori vita vis
MANA_SPENT_3=180
SECONDS_TO_ONE_MANA_3=3
MANA_TRAIN_3=false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
build/
dist/
main.spec
67 changes: 67 additions & 0 deletions action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import pyautogui
import keyboard
import os
import time

pyautogui.PAUSE = 1.4
pyautogui.FAILSAFE = False


class Action:

DRAG_DURATION = float(os.getenv('DRAG_TIME'))
TIME_SLEEP = float(os.getenv('SLEEP_ACTION'))

@staticmethod
def dragRuneToHand(blank, hand):
if not blank:
return False

pyautogui.moveTo(blank)
if Action.DRAG_DURATION > 0:
pyautogui.dragTo(hand, button='left', duration=Action.DRAG_DURATION)
time.sleep(Action.DRAG_DURATION)
else:
pyautogui.mouseDown(button='left')
pyautogui.moveTo(hand)
pyautogui.mouseUp(button='left')
return True

@staticmethod
def openInsideBackpack(inside_bp):
if not inside_bp:
return False

pyautogui.rightClick(inside_bp)
return True


@staticmethod
def conjureSpell(spell_name):
pyautogui.typewrite(spell_name)
keyboard.press_and_release('enter')


@staticmethod
def dragBackRune(blank, hand):
pyautogui.moveTo(hand)
if Action.DRAG_DURATION > 0:
pyautogui.dragTo(blank, button='left', duration=Action.DRAG_DURATION)
time.sleep(Action.DRAG_DURATION)
else:
pyautogui.mouseDown(button='left')
pyautogui.moveTo(blank)
pyautogui.mouseUp(button='left')

@staticmethod
def eatFood(food):
pyautogui.moveTo(food)
pyautogui.click(button='right', clicks=6, interval=0.30)

@staticmethod
def logout():
keyboard.press_and_release('ctrl+q')

@staticmethod
def login():
keyboard.press_and_release('enter')
213 changes: 213 additions & 0 deletions agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
import os
import datetime as dt
from action import Action
from item import Item
import pywinauto
import pyautogui
import time
pyautogui.FAILSAFE = False


class Agent:

STOPPED = 0
RUNNING = 1
WAITING = 2
FINISHED = 3
hand = None
food = None
inside_backpack = None
last_blank_pos = None
#if client delay and rune not back to bp, try to remove from hand
last_located_hand = None
trash_container = None

def __init__(self, index, game_window):
self.index = str(index)
self.game_window = game_window
if self.isValidConfig():
self.char_name = os.getenv('WINDOW_'+self.index)
self.spell = os.getenv('SPELL_NAME_'+self.index)
self.mana_spent = float(os.getenv('MANA_SPENT_'+self.index))
self.seconds_to_one_mana = float(os.getenv('SECONDS_TO_ONE_MANA_'+self.index))
self.mana_train = os.getenv('MANA_TRAIN_'+self.index)
self.time_to_spell = float(self.seconds_to_one_mana * self.mana_spent)
self.last_running_time = dt.datetime.now()
self.state = self.STOPPED
self.has_blank = True
self.total_mana_spent = 0
self.total_rune_made = 0
self.reconnect = os.getenv('RECONNECT')
print([self.char_name, self.spell, self.mana_spent, self.mana_train, self.seconds_to_one_mana,
self.last_running_time, self.state, self.reconnect])
else:
pyautogui.alert('Burrão! \nConfigurações icorretas ['+str(index)+'].\nRefaça e comece novamente por favor', 'Atenção')
self.game_window.kill()


def isValidConfig(self):

if not isinstance(self.game_window, pywinauto.Application):
return False

if not self.index:
return False

if not (os.getenv('WINDOW_'+self.index) or os.getenv('SPELL_NAME_'+self.index)
or os.getenv('MANA_SPENT_'+self.index) or os.getenv('SECONDS_TO_ONE_MANA_'+self.index)
or os.getenv('MANA_TRAIN_'+self.index)):
return False

if not (self.representsInt(os.getenv('MANA_SPENT_'+self.index))
or self.representsInt(os.getenv('SECONDS_TO_ONE_MANA_'+self.index))):
return False


return True


def isValidItems(self):
try:
if not self.getBlank():
self.sendAlert('suas BLANKS')
return False
if not self.getHand():
self.sendAlert('a MÃO vazia')
return False
if not self.getFood():
self.sendAlert('suas FOODS')
return False
if not self.getTrashContainer():
self.sendAlert('seu container de RETENÇÃO')
return False

return True
except FileNotFoundError:
pyautogui.alert('Existem arquivos de imagem faltante / nome errado, refaça e reinicie a configuração', 'Atenção')
return False

def sendAlert(self, obj):
pyautogui.alert('Não consegui encontrar '+obj+' , refaça e reinicie a configuração', 'Atenção')

def representsInt(self, s):
try:
int(s)
return True
except ValueError:
return False

def getHand(self):
return pyautogui.locateOnScreen(Item.image_path + '/hand.png', grayscale=True)

def getFood(self):
return pyautogui.locateOnScreen(Item.image_path + '/food.png', grayscale=True)

def getInsideBackpack(self):
return pyautogui.locateOnScreen(Item.image_path + '/inside_backpack.png', grayscale=True)

def getTrashContainer(self):
return pyautogui.locateOnScreen(Item.image_path + '/trash_container.png', grayscale=True)

def getMediviaLoginScreen(self):
return pyautogui.locateOnScreen(Item.image_path + '/medivia.png', grayscale=True)


def getBlank(self):
blank = pyautogui.locateOnScreen(Item.image_path + '/blank.png', grayscale=True)
#to back rune
self.last_blank_pos = blank
return blank

def focus(self):
#app_dialog = self.game_window.top_window()
#app_dialog.wrapper_object().set_focus()
#pyautogui.moveTo(x,y)
#pywinauto.application.Application().connect(process=self.game_window.process).top_window().set_focus()
#time.sleep(0.5)
app_dialog = self.game_window.top_window()
app_dialog.set_focus()


def run(self):
self.state = self.RUNNING
if self.canSpell():
self.focus()
self.throws()

def die(self):
self.game_window.kill()


def canSpell(self):
now = dt.datetime.now()
spentTime = (now-self.last_running_time).total_seconds()
return self.state != self.FINISHED and spentTime >= self.time_to_spell

def wasDisconneted(self):
if self.reconnect == 'true' and self.state != self.FINISHED:
loginScreen = self.getMediviaLoginScreen()
if loginScreen:
print('Char '+self.char_name+' foi desconectado por algum motivo, reconectando...')
Action.login()
time.sleep(2)
return True
return False

def throws(self):
self.last_running_time = dt.datetime.now()
if self.mana_train == 'true':
Action.conjureSpell(os.getenv('SPELL_NAME_'+self.index))
self.total_mana_spent += self.mana_spent
food = self.getFood()
if not food and self.wasDisconneted() == False:
print('Char '+self.char_name+' sem food e conectado, deslogando')
self.state = self.FINISHED
Action.logout()
self.game_window.kill()
return 0
else:
Action.eatFood(self.getFood())
self.state = self.WAITING
print(self.char_name+': Trabalho feito, aguardando mana')
else:
hand = self.getHand()
if not hand:
#If rune stack on hand
Action.dragBackRune(self.getTrashContainer(), self.last_located_hand)
else:
self.last_located_hand = hand

# try to drag blank to hand
if not Action.dragRuneToHand(self.getBlank(), hand):
self.has_blank = False
# if blank not found try open inside blank backpack
if not Action.openInsideBackpack(self.getInsideBackpack()):
if self.wasDisconneted() == False:
print('Char '+self.char_name+' sem blank e conectado, deslogando')
# logout if not found inside blank backpack
self.state = self.FINISHED
Action.logout()
self.game_window.kill()
return 0
else:
#if found blank backpack dragToHand again
Action.dragRuneToHand(self.getBlank(), hand)
self.has_blank = True

Action.conjureSpell(os.getenv('SPELL_NAME_'+self.index))
Action.dragBackRune(self.last_blank_pos, hand)
self.total_rune_made += 1
self.total_mana_spent += self.mana_spent
food = self.getFood()
if not food and self.wasDisconneted() == False:
print('Char '+self.char_name+' sem food e conectado, deslogando')
self.state = self.FINISHED
Action.logout()
self.game_window.kill()
return 0
else:
Action.eatFood(self.getFood())
self.state = self.WAITING
print(self.char_name+': Trabalho feito, aguardando mana')


Binary file added computer.ico
Binary file not shown.
Binary file added image_name_examples/blank.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_name_examples/food.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_name_examples/hand.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_name_examples/inside_backpack.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_name_examples/medivia.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image_name_examples/trash_container.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import os
import pyautogui
pyautogui.FAILSAFE = False


class Item:

image_path = os.getenv('IMAGES_PATH')
Loading

0 comments on commit 025f457

Please sign in to comment.