Refactor add category back.

This commit is contained in:
--unset 2024-05-05 09:44:17 -04:00
parent 76424aba7f
commit 5b58af2226
4 changed files with 84 additions and 14 deletions

View File

@ -64,7 +64,9 @@ class BigBucket:
try:
response = requests.get(f"{self.base_url}/manifest.json", timeout=5)
if response.status_code in [200, 201]:
return response.json().get("mc_java_servers")
data = response.json()
del data["manifest_version"]
return data
return {}
except TimeoutError as e:
logger.error(f"Unable to get jars from remote with error {e}")
@ -126,10 +128,11 @@ class BigBucket:
logger.info("Automatic cache refresh initiated due to old cache.")
self._refresh_cache()
def get_fetch_url(self, server, version) -> str:
def get_fetch_url(self, jar, server, version) -> str:
"""
Constructs the URL for downloading a server JAR file based on the server type.
Parameters:
jar (str): The category of the JAR file to download.
server (str): Server software name (e.g., "paper").
version (str): Server version.
@ -139,21 +142,23 @@ class BigBucket:
"""
try:
# Read cache file for URL that is in a list of one item
return self._read_cache()["types"][server]["versions"][version]["url"][0]
return self._read_cache()["types"][jar][server]["versions"][version]["url"][
0
]
except Exception as e:
logger.error(f"An error occurred while constructing fetch URL: {e}")
return None
def download_jar(self, server, version, path, server_id):
def download_jar(self, jar, server, version, path, server_id):
update_thread = threading.Thread(
name=f"server_download-{server_id}-{server}-{version}",
target=self.a_download_jar,
daemon=True,
args=(server, version, path, server_id),
args=(jar, server, version, path, server_id),
)
update_thread.start()
def a_download_jar(self, server, version, path, server_id):
def a_download_jar(self, jar, server, version, path, server_id):
"""
Downloads a server JAR file and performs post-download actions including
notifying users and setting import status.
@ -166,6 +171,7 @@ class BigBucket:
notifies server users about the completion of the download.
Parameters:
- jar (str): The category of the JAR file to download.
- server (str): The type of server software (e.g., 'forge', 'paper').
- version (str): The version of the server software.
- path (str): The local filesystem path where the JAR file will be saved.
@ -183,7 +189,7 @@ class BigBucket:
# delaying download for server register to finish
time.sleep(3)
fetch_url = self.get_fetch_url(server, version)
fetch_url = self.get_fetch_url(jar, server, version)
if not fetch_url:
return False

View File

@ -572,11 +572,14 @@ class Controller:
if create_data["type"] != "forge-installer":
server_obj = self.servers.get_server_obj(new_server_id)
url = self.big_bucket.get_fetch_url(
create_data["type"], create_data["version"]
create_data["category"],
create_data["type"],
create_data["version"],
)
server_obj.executable_update_url = url
self.servers.update_server(server_obj)
self.big_bucket.download_jar(
create_data["category"],
create_data["type"],
create_data["version"],
full_jar_path,

View File

@ -134,7 +134,13 @@ new_server_schema = {
"mem_min",
"mem_max",
"server_properties_port",
"category",
],
"category": {
"title": "Jar Category",
"type": "string",
"examples": ["Mc_java_servers", "Mc_java_proxies"],
},
"properties": {
"type": {
"title": "Server JAR Type",

View File

@ -65,10 +65,10 @@
<div class="col-sm-12">
<div class="form-group">
<label for="server_type">{{ translate('serverWizard', 'serverSelect', data['lang']) }}</label>
<label for="server_jar">{{ translate('serverWizard', 'serverType', data['lang']) }}</label>
<div class="input-group">
<select required class="form-control form-control-lg select-css" id="server_type" name="type"
onchange="serverTypeChange(this)">
<select required class="form-control form-control-lg select-css" id="server_jar" name="type"
onchange="serverJarChange(this)">
<option value="">{{ translate('serverWizard', 'selectServer', data['lang']) }}</option>
{% for s in data['server_types'] %}
<option value="{{ s }}">{{ s.capitalize() }}</option>
@ -84,6 +84,16 @@
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="server_type">{{ translate('serverWizard', 'serverSelect', data['lang']) }}</label>
<select required class="form-control form-control-lg select-css" id="server_type" name="type"
onchange="serverTypeChange(this)">
<option value="">{{ translate('serverWizard', 'selectServer', data['lang']) }}</option>
</select>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label for="server_version">{{ translate('serverWizard', 'serverVersion', data['lang']) }}</label>
@ -957,8 +967,9 @@
"minecraft_java_create_data": {
"create_type": "download_jar",
"download_jar_create_data": {
"type": formDataObject.version.split("|")[0],
"version": formDataObject.version.split("|")[1],
"category": formDataObject.version.split("|")[0],
"type": formDataObject.version.split("|")[1],
"version": formDataObject.version.split("|")[2],
"mem_min": formDataObject.mem_min,
"mem_max": formDataObject.mem_max,
"server_properties_port": formDataObject.port
@ -1184,11 +1195,12 @@
function serverTypeChange(selectObj) {
// get the index of the selected option
let idx = document.getElementById('server_type').value;
let idx_list = idx.split("|");
// get the value of the selected option
let cSelect = document.getElementById("server");
let which = {};
try {
versions = Object.keys(serverTypesLists[idx]["versions"]);
versions = Object.keys(serverTypesLists[idx_list[0]][idx_list[1]]["versions"]);
} catch {
while (cSelect.options.length > 0) {
cSelect.remove(0);
@ -1230,5 +1242,48 @@
$('.version-hint').popover("hide");
}
});
function serverJarChange(selectObj) {
const type_select = document.getElementById('server_jar')
const tidx = type_select.selectedIndex;
const val = type_select.options[tidx].value;
console.log(val);
let jcSelect = {};
if (val == 'None') {
jcSelect = document.getElementById("server_type");
while (jcSelect.options.length > 0) {
jcSelect.remove(0);
}
serverTypeChange(selectObj);
return;
}
// get the index of the selected option
let jidx = selectObj.selectedIndex;
// get the value of the selected option
let jwhich = selectObj.options[jidx].value;
// use the selected option value to retrieve the list of items from the serverTypesLists array
let jcList = Object.keys(serverTypesLists[jwhich]);
// get the country select element via its known id
jcSelect = document.getElementById("server_type");
// remove the current options from the country select
while (jcSelect.options.length > 0) {
jcSelect.remove(0);
}
let jnewOption;
// create new options ordered by ascending
jcList.forEach(type => {
jnewOption = document.createElement("option");
jnewOption.value = jwhich + "|" + type; // assumes option string and value are the same
jnewOption.text = type;
// add the new option
try {
jcSelect.add(jnewOption); // this will fail in DOM browsers but is needed for IE
}
catch (e) {
jcSelect.appendChild(jnewOption);
}
})
serverTypeChange(selectObj);
}
</script>
{% end %}