Basic commands for deeptown optimizer
This commit is contained in:
parent
f4f8eec2a6
commit
6f09002720
4
main.py
4
main.py
@ -96,7 +96,7 @@ class Guild:
|
||||
|
||||
for module in module_to_load:
|
||||
# Try to load all modules by name
|
||||
if module not in self.bot.modules.keys() or module.startswith("_"):
|
||||
if module not in self.bot.modules.keys():
|
||||
# Module is not an existing module
|
||||
self.config["modules"].remove(module)
|
||||
# Write an error in log
|
||||
@ -132,7 +132,7 @@ class FoBot(discord.Client):
|
||||
|
||||
def load_modules(self):
|
||||
for module in os.listdir('modules'):
|
||||
if module[0] != "_" and module.endswith(".py"):
|
||||
if module != "__pycache__":
|
||||
imported = importlib.import_module('modules.' + module[:-3])
|
||||
self.modules.update({module[:-3]: imported.MainClass})
|
||||
|
||||
|
@ -1,69 +0,0 @@
|
||||
import datetime
|
||||
|
||||
import discord
|
||||
import traductions as tr
|
||||
import modules.deeptownOptimizer.optimizer as optimizer
|
||||
|
||||
|
||||
class MainClass:
|
||||
name = "deeptown"
|
||||
|
||||
def __init__(self, guild):
|
||||
self.guild = guild
|
||||
self.optimizer = optimizer.Optimizer()
|
||||
|
||||
async def best_place_mine(self, msg, command, args):
|
||||
if args[0] not in self.optimizer.mines["0"].keys():
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["error"]["OreNotFoundError"].format(ore=args[0]))
|
||||
return
|
||||
else:
|
||||
text = tr.tr[self.guild.config["lang"]]["modules"]["deeptown"].format(ore=args[0])
|
||||
i = 0
|
||||
for mine in self.optimizer.best_mines(args[0]):
|
||||
if i >= 10:
|
||||
break
|
||||
if mine[0] == "0":
|
||||
continue
|
||||
text += mine[0].center(3, " ")
|
||||
text += ": "
|
||||
text += str(mine[1][args[0]] * 100)
|
||||
text += "%\n"
|
||||
i += 1
|
||||
text += "```"
|
||||
await msg.channel.send(text)
|
||||
return
|
||||
return
|
||||
|
||||
async def reload_optimizer(self, msg, command, args):
|
||||
if msg.author.id not in self.guild.config["master_admins"]:
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["PermissionError"])
|
||||
return
|
||||
else:
|
||||
self.optimizer = optimizer.Optimizer()
|
||||
|
||||
async def to_make(self, msg, command, args):
|
||||
if args[0] not in self.optimizer.items.keys():
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["ItemNotFound"].format(item=args[0]))
|
||||
try:
|
||||
quantity = int(args[1])
|
||||
except ValueError:
|
||||
await msg.channel.send(tr.tr[self.guild.config["lang"]]["errors"]["NotIntError"].format(number=args[1]))
|
||||
return
|
||||
result = self.optimizer.to_make(args[0], quantity)
|
||||
time = datetime.timedelta(seconds=int(result["time"]))
|
||||
needed = ", ".join([str(quantity) + " " + name for name, quantity in result["needed"].items()])
|
||||
await msg.channel.send(
|
||||
tr.tr[self.guild.config["lang"]]["modules"]["deeptown"]["to_make"].format(time=time, quantity=quantity,
|
||||
item=args[0], needed=needed,
|
||||
value=result["value"]))
|
||||
|
||||
async def on_message(self, msg):
|
||||
if msg.content.startswith(self.guild.config["prefix"]):
|
||||
command, *args = msg.content.lstrip(self.guild.config["prefix"]).split(" ")
|
||||
if command == "best_place_mine":
|
||||
await self.best_place_mine(msg, command, args)
|
||||
elif command == "reload_optimizer":
|
||||
await self.reload_optimizer(msg, command, args)
|
||||
elif command == "to_make":
|
||||
await self.to_make(msg, command, args)
|
||||
return
|
@ -1,65 +0,0 @@
|
||||
{
|
||||
"graphite": {
|
||||
"value": 15,
|
||||
"time": 5,
|
||||
"quantity": 1,
|
||||
"required": {
|
||||
"coal": 1
|
||||
},
|
||||
"building": "crafting"
|
||||
},
|
||||
"copperBar": {
|
||||
"value": 25,
|
||||
"time": 10,
|
||||
"quantity": 1,
|
||||
"required": {
|
||||
"copper": 5
|
||||
},
|
||||
"building": "smelting"
|
||||
},
|
||||
"ironBar": {
|
||||
"value": 40,
|
||||
"time": 15,
|
||||
"quantity": 1,
|
||||
"required": {
|
||||
"iron": 5
|
||||
},
|
||||
"building": "smelting"
|
||||
},
|
||||
"amberCharger": {
|
||||
"value": 4,
|
||||
"time": 5,
|
||||
"quantity": 1,
|
||||
"required": {
|
||||
"amber": 1
|
||||
},
|
||||
"building": "crafting"
|
||||
},
|
||||
"polishedAmber": {
|
||||
"value": 70,
|
||||
"time": 30,
|
||||
"quantity": 1,
|
||||
"required": {
|
||||
"amber": 5
|
||||
},
|
||||
"bullding": "jewel"
|
||||
},
|
||||
"goldBar": {
|
||||
"value": 40,
|
||||
"time": 15,
|
||||
"quantity": 1,
|
||||
"required": {
|
||||
"gold": 5
|
||||
},
|
||||
"building": "smelting"
|
||||
},
|
||||
"copperNail": {
|
||||
"value": 7,
|
||||
"time": 20,
|
||||
"quantity": 10,
|
||||
"required": {
|
||||
"copperBar": 1
|
||||
},
|
||||
"building": "crafting"
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
{
|
||||
"0": {
|
||||
"coal": 0,
|
||||
"copper": 0,
|
||||
"iron": 0,
|
||||
"amber": 0,
|
||||
"gold": 0,
|
||||
"aluminium": 0,
|
||||
"silver": 0,
|
||||
"emerald": 0,
|
||||
"ruby": 0,
|
||||
"diamond": 0,
|
||||
"topaz": 0,
|
||||
"sapphire": 0,
|
||||
"amethyst": 0,
|
||||
"alexandrite": 0,
|
||||
"titanium": 0,
|
||||
"uranium": 0,
|
||||
"platinum": 0,
|
||||
"helium3": 0,
|
||||
"obsidian": 0
|
||||
},
|
||||
"1": {
|
||||
"coal": 1
|
||||
},
|
||||
"2": {
|
||||
"coal": 0.70,
|
||||
"copper": 0.30
|
||||
},
|
||||
"3": {
|
||||
"coal": 0.595,
|
||||
"copper": 0.2833,
|
||||
"iron": 0.0917,
|
||||
"amber": 0.025,
|
||||
"gold": 0.005
|
||||
},
|
||||
"4": {
|
||||
"coal": 0.5425,
|
||||
"copper": 0.325,
|
||||
"iron": 0.1025,
|
||||
"amber": 0.0225,
|
||||
"gold": 0.0075
|
||||
},
|
||||
"5": {
|
||||
"coal": 0.4900,
|
||||
"copper": 0.3667,
|
||||
"iron": 0.1133,
|
||||
"amber": 0.0200,
|
||||
"gold": 0.0100
|
||||
},
|
||||
"13": {
|
||||
"copper": 1
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
import copy
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
|
||||
|
||||
class Optimizer():
|
||||
def __init__(self):
|
||||
self.mines = {}
|
||||
self.items = {}
|
||||
# get mine stats
|
||||
with open(os.path.join("modules", "deeptownOptimizer", "mines.json")) as mines:
|
||||
self.mines = json.load(mines)
|
||||
# get items stats
|
||||
with open(os.path.join("modules", "deeptownOptimizer", "items.json")) as items:
|
||||
self.items = json.load(items)
|
||||
# Add zero values to mine stat
|
||||
ores = self.mines["0"].keys()
|
||||
for area, stats in self.mines.items():
|
||||
for ore in ores:
|
||||
if self.mines[area].get(ore) is None:
|
||||
self.mines[area].update({ore: 0})
|
||||
print(self.mines)
|
||||
|
||||
def best_mines(self, ore):
|
||||
if ore not in self.mines["0"].keys():
|
||||
raise ValueError("{ore} is not a correct ore.".format(ore=ore))
|
||||
ordered_mines = [(k, v) for k, v in self.mines.items()]
|
||||
ordered_mines.sort(key=lambda x: x[1][ore], reverse = True)
|
||||
return ordered_mines
|
||||
|
||||
def to_make(self, item, quantity=1):
|
||||
if item not in self.items.keys():
|
||||
raise ValueError("{item} is not a correct item.".format(item=item))
|
||||
number_of_craft = quantity/self.items[item]["quantity"]
|
||||
if number_of_craft % 1 != 0:
|
||||
number_of_craft = (number_of_craft // 1)+1
|
||||
time = self.items[item]["time"]*number_of_craft
|
||||
value = self.items[item]["value"]*number_of_craft*self.items[item]["quantity"]
|
||||
needed = {}
|
||||
for resource, quantity in self.items[item]["required"].items():
|
||||
needed.update({resource: quantity*number_of_craft})
|
||||
return {"time": time, "value": value, "needed": needed}
|
||||
|
||||
def recursive_to_make(self, item, quantity=1):
|
||||
# Compute needed time and resource for craft item
|
||||
result = self.to_make(item, quantity)
|
||||
# For each needed resource try to craft it
|
||||
for i, q in result["needed"].keys():
|
||||
try:
|
||||
temp_result = self.recursive_to_make(i, g)
|
||||
result["time"] = result["time"] + temp_result["time"]
|
||||
for
|
||||
|
||||
return result
|
@ -10,4 +10,4 @@ pycparser==2.18
|
||||
six==1.11.0
|
||||
websockets==6.0
|
||||
yarl==1.2.6
|
||||
fs
|
||||
fs
|
||||
|
@ -3,7 +3,7 @@ tr = {
|
||||
"description": "Français",
|
||||
"modules": {
|
||||
"modules": {
|
||||
"description": "Gestion des modules.",
|
||||
"description": "Permet de gérer les modules.",
|
||||
"aide": {
|
||||
"list_modules": {
|
||||
"description": "Liste tous les modules. Les modules en gras sont activés.",
|
||||
@ -75,14 +75,6 @@ tr = {
|
||||
"add_master_admin": "L'utilisateur {user} est maintenant un administrateur du bot.",
|
||||
"del_master_admin": "L'utilisateur {user} n'est plus un administrateur du bot.",
|
||||
},
|
||||
"deeptown": {
|
||||
"description": "Commandes relatives au jeu deeptown.",
|
||||
"aide":{
|
||||
|
||||
},
|
||||
"best_place_mine":"Voici les meilleurs emplacements pour le minerais {ore}\n```\n",
|
||||
"to_make":"Pour faire {quantity} {item} il faudra {time}. Il vous faudra {needed}. La valeur totale de la production est {value}.",
|
||||
},
|
||||
},
|
||||
"errors": {
|
||||
"LangNotFoundError": "La langue {lang} est introuvable, tapez {prefix}list_lang pour voir les langues "
|
||||
@ -105,10 +97,7 @@ tr = {
|
||||
"modifiables.",
|
||||
"NotEnoughParamError": "Il manque un ou plusieurs parametres à la commande.",
|
||||
"NoMentionsError": "Vous devez mentioner un utilisateur pour le rajouter à la liste des administrateurs "
|
||||
"du bot.",
|
||||
"OreNotFoundError": "{ore} n'est pas un minerais valide.",
|
||||
"NotIntError":"{number} n'est pas un nombre entier valide.",
|
||||
"ItemNotFoundError":"{item} n'extiste pas dans deeptown",
|
||||
"du bot."
|
||||
},
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user