Add buy option and formating
This commit is contained in:
parent
04519ae217
commit
c4abd79970
|
@ -27,9 +27,9 @@ class CommandTree:
|
||||||
name += ':'
|
name += ':'
|
||||||
if data is None:
|
if data is None:
|
||||||
if not param.get('optional', False):
|
if not param.get('optional', False):
|
||||||
print('Wrong params count in "{0}", {1} isn\'t optional.'.
|
raise ValueError(
|
||||||
|
'Wrong params count in "{0}", {1} isn\'t optional.'.
|
||||||
format(path, name))
|
format(path, name))
|
||||||
return False
|
|
||||||
else:
|
else:
|
||||||
pdef = param.get('default')
|
pdef = param.get('default')
|
||||||
if not pdef is None:
|
if not pdef is None:
|
||||||
|
@ -40,41 +40,39 @@ class CommandTree:
|
||||||
try:
|
try:
|
||||||
data = ptype(data)
|
data = ptype(data)
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
print('Wrong type in "{0}", {1}"{2}" must be an {3}.'.
|
raise ValueError(
|
||||||
|
'Wrong type in "{0}", {1}"{2}" must be an {3}.'.
|
||||||
format(path, name, data, ptype.__name__))
|
format(path, name, data, ptype.__name__))
|
||||||
return False
|
|
||||||
if ptype == int:
|
if ptype == int:
|
||||||
pmin = param.get('min')
|
pmin = param.get('min')
|
||||||
if type(pmin) is int:
|
if type(pmin) is int:
|
||||||
if data < pmin:
|
if data < pmin:
|
||||||
print(
|
raise ValueError(
|
||||||
'Wrong value in "{0}", {1}"{2}" must be >= {3}.'.
|
'Wrong value in "{0}", {1}"{2}" must be >= {3}.'.
|
||||||
format(path, name, data, pmin))
|
format(path, name, data, pmin))
|
||||||
return False
|
|
||||||
pmax = param.get('max')
|
pmax = param.get('max')
|
||||||
if type(pmax) is int:
|
if type(pmax) is int:
|
||||||
if data > pmax:
|
if data > pmax:
|
||||||
print(
|
raise ValueError(
|
||||||
'Wrong value in "{0}", {1}"{2}" must be <= {3}.'.
|
'Wrong value in "{0}", {1}"{2}" must be <= {3}.'.
|
||||||
format(path, name, data, pmax))
|
format(path, name, data, pmax))
|
||||||
return False
|
|
||||||
plist = param.get('list')
|
plist = param.get('list')
|
||||||
if type(plist) is list:
|
if type(plist) is list:
|
||||||
if not data in plist:
|
if not data in plist:
|
||||||
print(
|
raise ValueError(
|
||||||
'Wrong value in "{0}", {1}"{2}" must be one of ({3}).'.
|
'Wrong value in "{0}", {1}"{2}" must be one of ({3}).'.
|
||||||
format(path, name, data, ', '.join(
|
format(path, name, data, ', '.join(
|
||||||
str(x) for x in plist)))
|
str(x) for x in plist)))
|
||||||
return False
|
|
||||||
else:
|
else:
|
||||||
print('"{0}" doesn\'t accepts so many params.'.format(path))
|
raise ValueError(
|
||||||
return False
|
'"{0}" doesn\'t accepts so many params.'.format(path))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def runCommand(self, path, args, options):
|
def runCommand(self, path, args, options):
|
||||||
command = self.commands[path]
|
command = self.commands[path]
|
||||||
params = []
|
params = []
|
||||||
for data, param in itertools.zip_longest(args[len(path.split()):], command['params']):
|
for data, param in itertools.zip_longest(args[len(path.split()):],
|
||||||
|
command['params']):
|
||||||
data = CommandTree.checkOption(data, param, path)
|
data = CommandTree.checkOption(data, param, path)
|
||||||
if data is False:
|
if data is False:
|
||||||
return
|
return
|
||||||
|
@ -109,9 +107,15 @@ class CommandTree:
|
||||||
option = path.pop(0)[1:]
|
option = path.pop(0)[1:]
|
||||||
for key in self.options:
|
for key in self.options:
|
||||||
if option in self.options[key]['names']:
|
if option in self.options[key]['names']:
|
||||||
|
try:
|
||||||
|
value = True
|
||||||
|
if not self.options[key]['check'].get('type') == bool:
|
||||||
|
value = path.pop(0)
|
||||||
data = CommandTree.checkOption(
|
data = CommandTree.checkOption(
|
||||||
path.pop(0), self.options[key]['check'], option)
|
value, self.options[key]['check'],
|
||||||
if data is False:
|
option)
|
||||||
|
except ValueError as err:
|
||||||
|
print(format(err))
|
||||||
return
|
return
|
||||||
options[key] = data
|
options[key] = data
|
||||||
option = None
|
option = None
|
||||||
|
@ -119,11 +123,12 @@ class CommandTree:
|
||||||
print('Unknown option "{0}"'.format(option))
|
print('Unknown option "{0}"'.format(option))
|
||||||
return
|
return
|
||||||
for key in self.options:
|
for key in self.options:
|
||||||
data = CommandTree.checkOption(
|
try:
|
||||||
|
options[key] = CommandTree.checkOption(
|
||||||
options.get(key), self.options[key]['check'], key)
|
options.get(key), self.options[key]['check'], key)
|
||||||
if data is False:
|
except ValueError as err:
|
||||||
|
print(format(err))
|
||||||
return
|
return
|
||||||
options[key] = data
|
|
||||||
commands = list(self.commands.keys())
|
commands = list(self.commands.keys())
|
||||||
if len(path) > 0:
|
if len(path) > 0:
|
||||||
run = self.runCommand
|
run = self.runCommand
|
||||||
|
|
92
LeekBots.py
92
LeekBots.py
|
@ -211,8 +211,9 @@ class Farmers:
|
||||||
fields[field].append(farmer.data[field])
|
fields[field].append(farmer.data[field])
|
||||||
print('value : min, avg, max')
|
print('value : min, avg, max')
|
||||||
for field in fields:
|
for field in fields:
|
||||||
print('{0} : {1}, {2}, {3}'.format(field, min(
|
print('{0} : {1}, {2}, {3}'.format(
|
||||||
fields[field]), int(sum(fields[field]) / len(fields[field])),
|
field, min(fields[field]),
|
||||||
|
int(sum(fields[field]) / len(fields[field])),
|
||||||
max(fields[field])))
|
max(fields[field])))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -264,8 +265,7 @@ class Farmers:
|
||||||
try:
|
try:
|
||||||
random.seed()
|
random.seed()
|
||||||
farmer = Farmers.farmer(Settings(options), Farmers.parse(options))
|
farmer = Farmers.farmer(Settings(options), Farmers.parse(options))
|
||||||
for _ in range(params[0]
|
for _ in range(params[0] if type(params[0]) is int else 20):
|
||||||
if type(params[0]) is int else 20):
|
|
||||||
opponents = farmer.getOpponents()
|
opponents = farmer.getOpponents()
|
||||||
if len(opponents) < 1:
|
if len(opponents) < 1:
|
||||||
farmer.raiseError('Probably, no more farmer fights')
|
farmer.raiseError('Probably, no more farmer fights')
|
||||||
|
@ -279,13 +279,10 @@ class Farmers:
|
||||||
else:
|
else:
|
||||||
optalent = 0
|
optalent = 0
|
||||||
for x in opponents:
|
for x in opponents:
|
||||||
if (
|
if (options['selection-mode'] == 'worst'
|
||||||
options['selection-mode'] == 'worst'
|
and optalent > x['talent']) or (
|
||||||
and optalent > x['talent']
|
|
||||||
) or (
|
|
||||||
not options['selection-mode'] == 'worst'
|
not options['selection-mode'] == 'worst'
|
||||||
and optalent < x['talent']
|
and optalent < x['talent']):
|
||||||
):
|
|
||||||
opid = x['id']
|
opid = x['id']
|
||||||
optalent = x['talent']
|
optalent = x['talent']
|
||||||
|
|
||||||
|
@ -321,7 +318,8 @@ class Farmers:
|
||||||
while haveTooMuch:
|
while haveTooMuch:
|
||||||
print('With ' + str(itemsCount) + ' items')
|
print('With ' + str(itemsCount) + ' items')
|
||||||
haveTooMuch = False
|
haveTooMuch = False
|
||||||
for combination in itertools.combinations_with_replacement(buys, itemsCount):
|
for combination in itertools.combinations_with_replacement(
|
||||||
|
buys, itemsCount):
|
||||||
currentHabs = sum(buys[op] for op in combination)
|
currentHabs = sum(buys[op] for op in combination)
|
||||||
if currentHabs == targetHabs:
|
if currentHabs == targetHabs:
|
||||||
print(combination)
|
print(combination)
|
||||||
|
@ -484,8 +482,9 @@ class Pool:
|
||||||
print(format(err))
|
print(format(err))
|
||||||
print('value : min, avg, max')
|
print('value : min, avg, max')
|
||||||
for field in fields:
|
for field in fields:
|
||||||
print('{0} : {1}, {2}, {3}'.format(field, min(
|
print('{0} : {1}, {2}, {3}'.format(
|
||||||
fields[field]), int(sum(fields[field]) / len(fields[field])),
|
field, min(fields[field]),
|
||||||
|
int(sum(fields[field]) / len(fields[field])),
|
||||||
max(fields[field])))
|
max(fields[field])))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -514,9 +513,9 @@ class Pool:
|
||||||
print(format(err))
|
print(format(err))
|
||||||
print('item : min, avg, max')
|
print('item : min, avg, max')
|
||||||
for field in items:
|
for field in items:
|
||||||
print('{0} : {1}, {2}, {3}'.format(Items.getName(field), min(
|
print('{0} : {1}, {2}, {3}'.format(
|
||||||
items[field]), int(sum(items[field]) / len(items[field])),
|
Items.getName(field), min(items[field]),
|
||||||
max(items[field])))
|
int(sum(items[field]) / len(items[field])), max(items[field])))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fight(params, options):
|
def fight(params, options):
|
||||||
|
@ -528,7 +527,8 @@ class Pool:
|
||||||
force = (params[1] == 'force')
|
force = (params[1] == 'force')
|
||||||
for leek in leeks:
|
for leek in leeks:
|
||||||
try:
|
try:
|
||||||
fights = params[0] if type(params[0]) is int else leek.farmer.fights
|
fights = params[0] if type(
|
||||||
|
params[0]) is int else leek.farmer.fights
|
||||||
if fights < 1:
|
if fights < 1:
|
||||||
leek.raiseError('No more fights')
|
leek.raiseError('No more fights')
|
||||||
|
|
||||||
|
@ -556,13 +556,10 @@ class Pool:
|
||||||
for x in opponents:
|
for x in opponents:
|
||||||
if force or not x['id'] in leeksids:
|
if force or not x['id'] in leeksids:
|
||||||
score = x['level'] * x['talent']
|
score = x['level'] * x['talent']
|
||||||
if (
|
if (options['selection-mode'] == 'worst'
|
||||||
options['selection-mode'] == 'worst'
|
|
||||||
and score < opscore
|
and score < opscore
|
||||||
) or (
|
) or (not options['selection-mode'] ==
|
||||||
not options['selection-mode'] == 'worst'
|
'worst' and score > opscore):
|
||||||
and score > opscore
|
|
||||||
):
|
|
||||||
opid = x['id']
|
opid = x['id']
|
||||||
opscore = score
|
opscore = score
|
||||||
if opid is None:
|
if opid is None:
|
||||||
|
@ -645,8 +642,13 @@ class Pool:
|
||||||
wid = weapon['id']
|
wid = weapon['id']
|
||||||
|
|
||||||
if wid is None:
|
if wid is None:
|
||||||
|
if options['buy']:
|
||||||
|
print(leek.farmer.name + ' buy')
|
||||||
|
wid = leek.farmer.buy(template)
|
||||||
|
else:
|
||||||
leek.farmer.raiseError(
|
leek.farmer.raiseError(
|
||||||
'Have any {0} available'.format(template))
|
'Have any {0} available. (use --buy)'.format(
|
||||||
|
template))
|
||||||
|
|
||||||
leek.equipWeapon(wid)
|
leek.equipWeapon(wid)
|
||||||
leek.raiseError('OK') #Ugly
|
leek.raiseError('OK') #Ugly
|
||||||
|
@ -689,8 +691,13 @@ class Pool:
|
||||||
wid = chip['id']
|
wid = chip['id']
|
||||||
|
|
||||||
if wid is None:
|
if wid is None:
|
||||||
|
if options['buy']:
|
||||||
|
print(leek.farmer.name + ' buy')
|
||||||
|
wid = leek.farmer.buy(template)
|
||||||
|
else:
|
||||||
leek.farmer.raiseError(
|
leek.farmer.raiseError(
|
||||||
'Have any {0} available'.format(template))
|
'Have any {0} available. (use --buy)'.format(
|
||||||
|
template))
|
||||||
|
|
||||||
leek.equipChip(wid)
|
leek.equipChip(wid)
|
||||||
leek.raiseError('OK') #Ugly
|
leek.raiseError('OK') #Ugly
|
||||||
|
@ -733,8 +740,13 @@ class Pool:
|
||||||
pid = potion['id']
|
pid = potion['id']
|
||||||
|
|
||||||
if pid is None:
|
if pid is None:
|
||||||
|
if options['buy']:
|
||||||
|
print(leek.farmer.name + ' buy')
|
||||||
|
pid = leek.farmer.buy(template)
|
||||||
|
else:
|
||||||
leek.farmer.raiseError(
|
leek.farmer.raiseError(
|
||||||
'Have any {0} available'.format(template))
|
'Have any {0} available. (use --buy)'.format(
|
||||||
|
template))
|
||||||
|
|
||||||
leek.usePotion(pid)
|
leek.usePotion(pid)
|
||||||
leek.raiseError('OK') #Ugly
|
leek.raiseError('OK') #Ugly
|
||||||
|
@ -890,10 +902,8 @@ class Pool:
|
||||||
if (
|
if (
|
||||||
options['selection-mode'] == 'worst'
|
options['selection-mode'] == 'worst'
|
||||||
and optalent > x['talent']
|
and optalent > x['talent']
|
||||||
) or (
|
) or (not options['selection-mode'] == 'worst'
|
||||||
not options['selection-mode'] == 'worst'
|
and optalent < x['talent']):
|
||||||
and optalent < x['talent']
|
|
||||||
):
|
|
||||||
opid = x['id']
|
opid = x['id']
|
||||||
optalent = x['talent']
|
optalent = x['talent']
|
||||||
|
|
||||||
|
@ -998,13 +1008,15 @@ class Farmer:
|
||||||
return req
|
return req
|
||||||
|
|
||||||
def buy(self, item):
|
def buy(self, item):
|
||||||
self.checkRequest(lwapi.market.buy_habs(item, self.token))
|
return self.checkRequest(lwapi.market.buy_habs(item,
|
||||||
|
self.token))['item']
|
||||||
|
|
||||||
def sell(self, item):
|
def sell(self, item):
|
||||||
self.checkRequest(lwapi.market.sell_habs(item, self.token))
|
self.checkRequest(lwapi.market.sell_habs(item, self.token))
|
||||||
|
|
||||||
def market(self):
|
def market(self):
|
||||||
return self.checkRequest(lwapi.market.get_item_templates(self.token))['items']
|
return self.checkRequest(lwapi.market.get_item_templates(
|
||||||
|
self.token))['items']
|
||||||
|
|
||||||
def getLeek(self, leek):
|
def getLeek(self, leek):
|
||||||
return Leek(
|
return Leek(
|
||||||
|
@ -1025,7 +1037,8 @@ class Farmer:
|
||||||
def saveAi(self, ai, script):
|
def saveAi(self, ai, script):
|
||||||
ai = self.checkRequest(lwapi.ai.save(ai, script, self.token))['result']
|
ai = self.checkRequest(lwapi.ai.save(ai, script, self.token))['result']
|
||||||
if len(ai[0]) > 3:
|
if len(ai[0]) > 3:
|
||||||
self.raiseError('Script error ('+', '.join(str(x) for x in ai[0][3:])+')')
|
self.raiseError('Script error (' + ', '.join(
|
||||||
|
str(x) for x in ai[0][3:]) + ')')
|
||||||
|
|
||||||
def joinTeam(self, team):
|
def joinTeam(self, team):
|
||||||
self.checkRequest(lwapi.team.send_candidacy(team, self.token))
|
self.checkRequest(lwapi.team.send_candidacy(team, self.token))
|
||||||
|
@ -1056,19 +1069,22 @@ class Farmer:
|
||||||
lwapi.team.delete_composition(composition, self.token))
|
lwapi.team.delete_composition(composition, self.token))
|
||||||
|
|
||||||
def tournamentTeamComposition(self, composition):
|
def tournamentTeamComposition(self, composition):
|
||||||
self.checkRequest(lwapi.team.register_tournament(composition, self.token))
|
self.checkRequest(
|
||||||
|
lwapi.team.register_tournament(composition, self.token))
|
||||||
|
|
||||||
def tournament(self):
|
def tournament(self):
|
||||||
self.checkRequest(lwapi.farmer.register_tournament(self.token))
|
self.checkRequest(lwapi.farmer.register_tournament(self.token))
|
||||||
|
|
||||||
def fight(self, target):
|
def fight(self, target):
|
||||||
return self.checkRequest(lwapi.garden.start_farmer_fight(target, self.token))
|
return self.checkRequest(
|
||||||
|
lwapi.garden.start_farmer_fight(target, self.token))
|
||||||
|
|
||||||
def avatar(self, avatar):
|
def avatar(self, avatar):
|
||||||
return self.checkRequest(lwapi.farmer.set_avatar(avatar, self.token))
|
return self.checkRequest(lwapi.farmer.set_avatar(avatar, self.token))
|
||||||
|
|
||||||
def teamEmblem(self, team, emblem):
|
def teamEmblem(self, team, emblem):
|
||||||
return self.checkRequest(lwapi.team.set_emblem(team, emblem, self.token))
|
return self.checkRequest(
|
||||||
|
lwapi.team.set_emblem(team, emblem, self.token))
|
||||||
|
|
||||||
def createLeek(self, name):
|
def createLeek(self, name):
|
||||||
return self.checkRequest(lwapi.leek.create(name, self.token))
|
return self.checkRequest(lwapi.leek.create(name, self.token))
|
||||||
|
@ -1127,7 +1143,8 @@ class Leek:
|
||||||
self.checkRequest(lwapi.leek.remove_chip(wid, self.farmer.token))
|
self.checkRequest(lwapi.leek.remove_chip(wid, self.farmer.token))
|
||||||
|
|
||||||
def usePotion(self, pid):
|
def usePotion(self, pid):
|
||||||
self.checkRequest(lwapi.leek.use_potion(self.id, pid, self.farmer.token))
|
self.checkRequest(
|
||||||
|
lwapi.leek.use_potion(self.id, pid, self.farmer.token))
|
||||||
|
|
||||||
def characteristics(self, bonuses):
|
def characteristics(self, bonuses):
|
||||||
self.checkRequest(
|
self.checkRequest(
|
||||||
|
@ -1152,6 +1169,7 @@ if __name__ == "__main__":
|
||||||
.addOption('farmer', ['f', '-farmer'], {'name': 'farmer (id)', 'optional': True})\
|
.addOption('farmer', ['f', '-farmer'], {'name': 'farmer (id)', 'optional': True})\
|
||||||
.addOption('selection-mode', ['sm', '-selection-mode'], {'name': 'selection mode', 'optional': True, 'list': ['random', 'best', 'worst'], 'default': 'worst'})\
|
.addOption('selection-mode', ['sm', '-selection-mode'], {'name': 'selection mode', 'optional': True, 'list': ['random', 'best', 'worst'], 'default': 'worst'})\
|
||||||
.addOption('sleep', ['s', '-sleep'], {'name': 'sleep time', 'optional': True, 'type': int, 'min': 0, 'default': 1})\
|
.addOption('sleep', ['s', '-sleep'], {'name': 'sleep time', 'optional': True, 'type': int, 'min': 0, 'default': 1})\
|
||||||
|
.addOption('buy', ['b', '-buy'], {'name': 'should buy', 'optional': True, 'type': bool, 'default': False})\
|
||||||
.addOption('path', ['-path'], {'name': 'config path', 'optional': True, 'default': '*/LeekBots.json'})\
|
.addOption('path', ['-path'], {'name': 'config path', 'optional': True, 'default': '*/LeekBots.json'})\
|
||||||
.addCommand('farmers list', 'list all farmers', Farmers.list, [{'name': 'mode', 'optional': True, 'list': [None, 'infos', 'ais', 'stuff', 'leeks']}])\
|
.addCommand('farmers list', 'list all farmers', Farmers.list, [{'name': 'mode', 'optional': True, 'list': [None, 'infos', 'ais', 'stuff', 'leeks']}])\
|
||||||
.addCommand('farmers stats', 'stats of all farmers', Farmers.stats, [{'name': 'mode', 'list': ['infos']}])\
|
.addCommand('farmers stats', 'stats of all farmers', Farmers.stats, [{'name': 'mode', 'list': ['infos']}])\
|
||||||
|
|
Reference in New Issue