function list_all_games() { let req = {action: "get_all_game_headers"}; let tbody = document.getElementById("game_manager_table"); tbody.innerHTML = ""; request(req).then(resp => { let games = JSON.parse(resp); for (let i = 0; i < games.length; i++) { let g = games[i]; let row = document.createElement("tr"); let chkbox = document.createElement("input"); chkbox.type = "checkbox"; chkbox.name = "game_chkbox"; chkbox.game = g; let tdChkBox = document.createElement("td"); tdChkBox.appendChild(chkbox); tdChkBox.classList.add("checkbox"); let tdGameName = create_table_cell(g["name"]); let tdGameDescription = create_table_cell(g["description"]); let tdOwner = create_table_cell(g["owner"]); row.append(tdChkBox, tdGameName, tdGameDescription, tdOwner); tbody.appendChild(row); let edit_group_action = () => { create_edit_game(g); }; tdGameName.addEventListener("click", edit_group_action); tdGameDescription.addEventListener("click", edit_group_action); tdOwner.addEventListener("click", edit_group_action); } }); } var EDITED_GAME = null; function create_edit_game(game = null) { EDITED_GAME = game; let updating = game !== null; let nameF = document.getElementById("game_name"); let descriptionF = document.getElementById("game_description"); let submit_btn = document.getElementById("game_editor_submit_btn"); let ownerF = document.getElementById("game_owner"); let contributorsF = document.getElementById("game_contributors"); let gameFileF = document.getElementById("game_file"); let download_challenges_btn = document.getElementById("download_challenges_btn"); let show_game_file_upload_btn = document.getElementById("show_game_file_upload"); let cancel_game_file_upload_btn = document.getElementById("cancel_game_file_upload"); let edit_challenges_btn = document.getElementById("edit_challenges_btn"); let groupF = document.getElementById("game_groups"); let time_limitedChk = document.getElementById("time_limited"); let time_limitF = document.getElementById("time_limit"); let repeatableChk = document.getElementById("repeatable"); if (!updating) { // creating a new game nameF.value = ""; descriptionF.value = ""; submit_btn.value = "Létrehozás" ownerF.value = USERDATA["nickname"]; ownerF.readOnly = true; contributorsF.value = ""; groupF.value = ""; time_limitedChk.checked = false; repeatableChk.checked = true; // hide additional controls hide("additional_controls"); } else { // editing an existing one nameF.value = game["name"]; descriptionF.value = game["description"]; submit_btn.value = "Mentés" ownerF.value = game["owner"]; ownerF.readOnly = false; contributorsF.value = game["contributors"].join(", "); groupF.value = game["groups"].join(", "); let props = game["properties"]; let time_limit = Math.max(Number(props["time_limit"]), 0); time_limitF.value = seconds_to_time(time_limit); time_limitedChk.checked = time_limit > 0; handle_time_limit_chkbox(); repeatableChk.checked = props["repeatable"]; // show additional controls show("additional_controls"); } gameFileF.value = ""; let game_file_present = updating && game["game_file_present"]; if (game_file_present) { show(download_challenges_btn); show(edit_challenges_btn); edit_challenges_btn.onclick = () => { edit_challenges(game); }; } else { hide(download_challenges_btn); hide(edit_challenges_btn); } show_hide_gamefile_upload(false); submit_btn.onclick = () => { let game_name = document.getElementById("game_name").value.trim(); if (updating && time_limitedChk.checked && time_limitF.validity.patternMismatch) { alert("Hibás időformátum!") return; } if (game_name !== "") { let reqData = { name: game_name, description: descriptionF.value.trim(), owner: updating ? ownerF.value.trim() : USERDATA["nickname"], contributors: contributorsF.value.trim(), groups: groupF.value.trim(), properties: { time_limit: updating ? (time_limitedChk.checked ? time_to_seconds(time_limitF.value) : 0) : 0, repeatable: updating ? repeatableChk.checked : false } }; if (updating) { reqData["_id"] = game["_id"]; } let req = { action: updating ? "update_game" : "create_game", data: JSON.stringify(reqData) }; let uploading_game_file = gameFileF.files.length > 0; if (uploading_game_file) { // append game file if selected req["game_file"] = gameFileF.files[0]; } request(req).then(resp => { if (uploading_game_file) { let file_upload_status = JSON.parse(resp); alert(`Importálás eredménye:\n${file_upload_status["n"]} kérdés, ${file_upload_status["encoding"]} szövegkódolással`); } list_all_games(); }); hide("game_editor_window"); } }; show("game_editor_window"); } function show_hide_gamefile_upload(en) { if (en) { // hide("download_challenges_btn"); hide("show_game_file_upload"); show("game_file"); show("cancel_game_file_upload"); } else { // show("download_challenges_btn"); show("show_game_file_upload"); hide("game_file"); hide("cancel_game_file_upload"); document.getElementById("game_file").value = ""; } } function download_challenges() { let action = "export_game_file_csv"; let gameid = EDITED_GAME["_id"]; window.open(`interface.php?action=${action}&gameid=${gameid}`, "_blank"); } function get_selected_games() { let selected_chkboxes = document.getElementsByName("game_chkbox"); let selected_games = []; selected_chkboxes.forEach((chkbox) => { if (chkbox.checked) { selected_games.push(chkbox.game); } }); return selected_games; } function delete_games() { let games = get_selected_games(); if (games.length === 0) { return; } let game_names = []; let game_ids = []; games.forEach((g) => { game_names.push(g["name"]); game_ids.push(g["_id"]); }); let msg = "Biztosan törölni kívánja a következő játéko(ka)t?\n\n" + game_names.join(", ") + "\n\n" + "A törlés nem vonható vissza!"; if (confirm(msg)) { let req = {action: "delete_games", ids: game_ids.join(",")}; request(req).then(resp => { list_all_games(); }); } } function handle_time_limit_chkbox() { let time_limitedChk = document.getElementById("time_limited"); let time_limitF = document.getElementById("time_limit"); time_limitF.disabled = !time_limitedChk.checked; } function edit_challenges(game) { let req = {action: "get_challenges", gameid: game["_id"]}; request(req).then(resp => { console.log(JSON.parse(resp)); }); } // function hint_all_groups(target_element_id) { // const hintbox_insert_fn = (record) => { // let targetF = document.getElementById(target_element_id); // let groups = explode_sanitize_string_list(targetF.value); // groups.pop(); // groups.push(record); // targetF.value = groups.join(", "); // close_hintbox(true); // } // // let req = { // action: "search_groups", // } // // open_hintbox_at(target_element_id, req, print_group_name, hintbox_insert_fn); // }