Sort server types, I suffered a lot while doing this

This commit is contained in:
luukas 2021-06-02 21:17:50 +03:00
parent 11a004ea74
commit 5a5cd65d4f
2 changed files with 24 additions and 1 deletions

View File

@ -71,6 +71,29 @@ class ServerJars:
data = self._read_cache() data = self._read_cache()
return data.get('servers') return data.get('servers')
def get_serverjar_data_sorted(self):
data = self.get_serverjar_data()
def str_to_int(x, counter=0):
try:
return ord(x[0]) + str_to_int(x[1:], counter + 1) + len(x)
except IndexError:
return 0
def to_int(x):
try:
return int(x)
except ValueError:
temp = x.split('-')
return to_int(temp[0]) + str_to_int(temp[1]) / 100000
sort_key_fn = lambda x: [to_int(y) for y in x.split('.')]
for key in data.keys():
data[key] = sorted(data[key], key=sort_key_fn)
return data
def _check_api_alive(self): def _check_api_alive(self):
logger.info("Checking serverjars.com API status") logger.info("Checking serverjars.com API status")

View File

@ -63,7 +63,7 @@ class ServerHandler(BaseHandler):
if page == "step1": if page == "step1":
page_data['server_types'] = server_jar_obj.get_serverjar_data() page_data['server_types'] = server_jar_obj.get_serverjar_data_sorted()
template = "server/wizard.html" template = "server/wizard.html"
self.render( self.render(