Oké, update.
Donc @Lakh92 a raison, les dépendances c'est chiant, donc j'ai scrapé toutes les coordonnées de tous les indices et je les ai rangées dans un JSON.
Ça a l'avantage d'être super léger, et rapide à l'utilisation (en gros instantané au lieu de 1-2 secondes)
Par contre, le JSON contient un snapshot des coordonées de dofusama, donc il ne suivra pas les évolutions et corrections des indices du site.
Du coup le code :
Code
Cliquez pour révéler
Cliquez pour masquer
# -*- coding: UTF-8 -*-
import json
class ClueFinder:
def __init__(self):
with open('Clues.json', 'r') as f:
self.clues = json.load(f)
f.close()
def get_directions(self, input_coords, clue, direction, pretty=False):
formatted_clue = ''.join([i if ord(i) < 128 else '-' for i in clue]).replace('"', '-').replace("'", '-').replace(' ', '-').replace('---', '-').replace('--', '-').lower()
try:
all_clue_coords = self.clues[formatted_clue]
except Exception:
raise Exception('Clue does not exist')
if direction.lower() == 'n':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[0] == coord[0] and input_coords[1]-10 <= coord[1] <= input_coords[1]-1)], key=lambda pos: pos[1], reverse=True)
elif direction.lower() == 's':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[0] == coord[0] and input_coords[1]+1 <= coord[1] <= input_coords[1]+10)], key=lambda pos: pos[1])
elif direction.lower() == 'o':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[1] == coord[1] and input_coords[0]-10 <= coord[0] <= input_coords[0]-1)], key=lambda pos: pos[1], reverse=True)
elif direction.lower() == 'e':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[1] == coord[1] and input_coords[0]+1 <= coord[0] <= input_coords[0]+10)], key=lambda pos: pos[1])
else:
raise Exception('Direction must be "n", "s", "e", "o"')
if clue_pos:
clue_pos = clue_pos[0]
else:
raise Exception('Clue not found within 10 tile in that direction : ' + direction)
distance = max(abs(clue_pos[0]-input_coords[0]), abs(clue_pos[1]-input_coords[1]))
if pretty:
print 'Coordonnées de départ : ' + str(input_coords)
print 'Indice : ' + clue
print 'Direction : ' + direction
print 'L\'indice est à {} cases, en {}.\n'.format(distance, tuple(clue_pos))
return clue_pos, distance
CF = ClueFinder()
CF.get_directions((5, -26), 'Souche qui ne repousse pas', 'n', True)
CF.get_directions((1, -4), 'Tonneau', 's', True)
CF.get_directions((5, -26), 'Souche qui ne repousse pas', 'o')
CF.get_directions((5, -26), 'Souche qui ne repousse pas', 'e')
Donc le truc cool c'est qu'on a plus besoin de modules hyper lourds.
EDIT : Le script pour ce qui veulent l’exécuter depuis un shell
Code
Cliquez pour révéler
Cliquez pour masquer
# -*- coding: UTF-8 -*-
import json
import sys
from ast import literal_eval
class ClueFinder:
def __init__(self):
with open('Clues.json', 'r') as f:
self.clues = json.load(f)
f.close()
def get_directions(self, input_coords, clue, direction, pretty=False):
formatted_clue = ''.join([i if ord(i) < 128 else '-' for i in clue]).replace('"', '-').replace("'", '-').replace(' ', '-').replace('---', '-').replace('--', '-').lower()
try:
all_clue_coords = self.clues[formatted_clue]
except Exception:
raise Exception('Clue does not exist')
if direction.lower() == 'n':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[0] == coord[0] and input_coords[1]-10 <= coord[1] <= input_coords[1]-1)], key=lambda pos: pos[1], reverse=True)
elif direction.lower() == 's':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[0] == coord[0] and input_coords[1]+1 <= coord[1] <= input_coords[1]+10)], key=lambda pos: pos[1])
elif direction.lower() == 'o':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[1] == coord[1] and input_coords[0]-10 <= coord[0] <= input_coords[0]-1)], key=lambda pos: pos[1], reverse=True)
elif direction.lower() == 'e':
clue_pos = sorted([coord for coord in all_clue_coords if (input_coords[1] == coord[1] and input_coords[0]+1 <= coord[0] <= input_coords[0]+10)], key=lambda pos: pos[1])
else:
raise Exception('Direction must be "n", "s", "e", "o"')
if clue_pos:
clue_pos = clue_pos[0]
else:
raise Exception('Clue not found within 10 tile in that direction : ' + direction)
distance = max(abs(clue_pos[0]-input_coords[0]), abs(clue_pos[1]-input_coords[1]))
if pretty:
print 'Coordonnées de départ : ' + str(input_coords)
print 'Indice : ' + clue
print 'Direction : ' + direction
print 'L\'indice est à {} cases, en {}.\n'.format(distance, tuple(clue_pos))
return clue_pos, distance
if __name__ == "__main__":
CF = ClueFinder()
if len(sys.argv) == 4:
CF.get_directions(literal_eval(sys.argv[1]), sys.argv[2], sys.argv[3])
elif len(sys.argv) == 5:
CF.get_directions(literal_eval(sys.argv[1]), sys.argv[2], sys.argv[3], sys.argv[4] == 'y')
else:
print 'Usage : <(x,y)> <clue (- instead of spaces)> <direction> <pretty : y/n (optionnal)>'
Kenavo,
Ugdha