diff options
-rw-r--r-- | advtrains_line_automation/line_editor.lua | 89 | ||||
-rw-r--r-- | advtrains_line_automation/line_functions.lua | 58 |
2 files changed, 139 insertions, 8 deletions
diff --git a/advtrains_line_automation/line_editor.lua b/advtrains_line_automation/line_editor.lua index c24e2e6..aabd783 100644 --- a/advtrains_line_automation/line_editor.lua +++ b/advtrains_line_automation/line_editor.lua @@ -16,10 +16,13 @@ local color_red = core.get_color_escape_sequence("#ff0000") local color_green = core.get_color_escape_sequence("#00ff00") local cancel_linevar = assert(advtrains.lines.cancel_linevar) +local get_last_passages = assert(advtrains.lines.get_last_passages) local get_line_description = assert(advtrains.lines.get_line_description) local linevar_decompose = assert(advtrains.lines.linevar_decompose) local try_get_linevar_def = assert(advtrains.lines.try_get_linevar_def) +local show_last_passages_formspec -- forward declaration + local function check_rights(pinfo, owner) if pinfo.role == "new" or pinfo.role == "none" then return false @@ -100,7 +103,8 @@ local function get_formspec(custom_state) return "" end - local selection_index = custom_state.selection_index + local selection_index_raw = custom_state.selection_index + local selection_index = selection_index_raw or 1 local formspec = { ch_core.formspec_header({formspec_version = 6, size = {20, 16}, auto_background = true}), "label[0.5,0.6;Editor variant linek]".. @@ -126,7 +130,7 @@ local function get_formspec(custom_state) table.insert(formspec, "vypnutá") end end - if selection_index ~= nil then + if selection_index_raw ~= nil then table.insert(formspec, ";"..selection_index.."]") else table.insert(formspec, ";]") @@ -135,7 +139,7 @@ local function get_formspec(custom_state) table.insert(formspec, "button[14.5,0.3;3.5,0.75;create;nová varianta...]") end local has_rights_to_open_variant = - pinfo.role == "admin" or (selection_index or 1) == 1 or + pinfo.role == "admin" or selection_index == 1 or pinfo.player_name == custom_state.linevars[selection_index - 1].owner if has_rights_to_open_variant then @@ -155,6 +159,10 @@ local function get_formspec(custom_state) if pinfo.role ~= "new" then if has_rights_to_open_variant then + if selection_index > 1 then + table.insert(formspec, "button[5,15;4.5,0.75;last_passages;posledních 10 jízd...]".. + "tooltip[last_passages;Zobrazí přehled časů posledních 10 jízd na dané variantě linky.]") + end table.insert(formspec, "button[10,15;4.5,0.75;save;".. ifthenelse(custom_state.compiled_linevar == nil, "ověřit změny\npřed uložením]", "uložit změny]")) end @@ -649,6 +657,14 @@ local function formspec_callback(custom_state, player, formname, fields) ch_core.systemovy_kanal(pinfo.player_name, "Mazání selhalo: "..(errmsg or "Neznámá chyba.")) end end + elseif fields.last_passages then + local selected_linevar_def = try_get_linevar_def(custom_state.linevars[(custom_state.selection_index or 1) - 1].name) + if selected_linevar_def ~= nil then + print("DEBUG: selected_linevar_def: "..dump2(selected_linevar_def)) + assert(selected_linevar_def.name) + show_last_passages_formspec(player, selected_linevar_def, assert(selected_linevar_def.name)) + return + end end if update_formspec then @@ -656,21 +672,80 @@ local function formspec_callback(custom_state, player, formname, fields) end end -local function show_formspec(player) +local function show_editor_formspec(player, linevar_to_select) if player == nil then return false end local custom_state = { player_name = assert(player:get_player_name()), evl_scroll = 0, } - custom_state_refresh_linevars(custom_state) - custom_state_set_selection_index(custom_state, 1) + if not custom_state_refresh_linevars(custom_state, linevar_to_select) then + custom_state_set_selection_index(custom_state, 1) + end ch_core.show_formspec(player, "advtrains_line_automation:editor_linek", get_formspec(custom_state), formspec_callback, custom_state, {}) end +local function lp_formspec_callback(custom_state, player, formname, fields) + if fields.back then + show_editor_formspec(player, custom_state.selected_linevar) + end +end + +show_last_passages_formspec = function(player, linevar_def, selected_linevar) + local formspec = { + "formspec_version[6]".. + "size[20,10]".. + "label[0.5,0.6;Poslední jízdy na variantě linky ", + F(assert(linevar_def.name)), + "]".. + "tablecolumns[text;text;text,width=5;text,width=5;text,width=5;text,width=5;text,width=5;text,width=5;text,width=5;text,width=5;text,width=5;text,width=5]", + "table[0.5,1.25;19,8;jizdy;KÓD,DOPRAVNA,1.j.,2.j.,3.j.,4.j.,5.j.,6.j.,7.j.,8.j.,9.j.,10.j." + } + local passages, stops = get_last_passages(linevar_def) + local max_time = {} + if passages ~= nil then + for i = 1, 10 do + if passages[i] == nil then + passages[i] = {} + end + end + for i = 1, #stops do -- i = index zastávky + table.insert(formspec, ","..F(stops[i][1])..","..F(stops[i][2])) + for j = 1, 10 do -- j = index jízdy + local time = passages[j][i] + if time ~= nil then + table.insert(formspec, ","..time) + if max_time[j] == nil or max_time[j] < time then + max_time[j] = time + end + else + table.insert(formspec, ",-") + end + end + end + table.insert(formspec, ",,DOBA JÍZDY:") + for i = 1, 10 do + if max_time[i] ~= nil then + table.insert(formspec, ",_"..(max_time[i] - passages[i][1]).."_") + else + table.insert(formspec, ",-") + end + end + end + table.insert(formspec, ";]".. + "button[17.75,0.3;1.75,0.75;back;zpět]".. + "tooltip[jizdy;Časové údaje jsou v sekundách železničního času.]") + formspec = table.concat(formspec) + local custom_state = { + player_name = player:get_player_name(), + selected_linevar = selected_linevar, + } + ch_core.show_formspec(player, "advtrains_line_automation:posledni_jizdy", formspec, lp_formspec_callback, custom_state, {}) +end + def = { -- params = "", description = "Otevře editor variant linek", privs = {ch_registered_player = true}, - func = function(player_name, param) show_formspec(minetest.get_player_by_name(player_name)) end, + func = function(player_name, param) show_editor_formspec(minetest.get_player_by_name(player_name)) end, } core.register_chatcommand("linky", def) diff --git a/advtrains_line_automation/line_functions.lua b/advtrains_line_automation/line_functions.lua index b25182d..ba1b360 100644 --- a/advtrains_line_automation/line_functions.lua +++ b/advtrains_line_automation/line_functions.lua @@ -29,6 +29,12 @@ local last_passages = {--[[ } ]]} +local diakritika_na_velka = { + ["á"] = "Á", ["ä"] = "Ä", ["č"] = "Č", ["ď"] = "Ď", ["é"] = "É", ["Ě"] = "Ě", ["Í"] = "Í", ["ĺ"] = "Ĺ", ["ľ"] = "Ľ", + ["ň"] = "Ň", ["ó"] = "Ó", ["ô"] = "Ô", ["ŕ"] = "Ŕ", ["ř"] = "Ř", ["š"] = "Š", ["ť"] = "Ť", ["ú"] = "Ú", ["ů"] = "Ů", + ["ý"] = "Ý", ["ž"] = "Ž", +} + local debug_print_i = 0 -- LOCAL funkce: @@ -49,6 +55,24 @@ local function get_station_name(stn) end end +local function na_velka_pismena(s) + local l = #s + local i = 1 + local res = "" + local c + while i <= l do + c = diakritika_na_velka[s:sub(i, i + 1)] + if c then + res = res .. c + i = i + 2 + else + res = res .. s:sub(i, i) + i = i + 1 + end + end + return string.upper(res) +end + --[[ -- Vrací index následujícího výskytu 'stn' v seznamu zastávek podle linevar_def. -- Vrací i skryté zastávky, ale ne vypnuté. @@ -96,7 +120,13 @@ local function line_start(train, stn, departure_rwtime) ls.linevar_dep = departure_rwtime ls.linevar_last_dep = departure_rwtime ls.linevar_last_stn = stn - train.text_outside = al.get_line_description(linevar_def, {line_number = true, last_stop = true, last_stop_prefix = "", train_name = true}) + train.text_outside = al.get_line_description(linevar_def, { + line_number = true, + last_stop = true, + last_stop_prefix = "", + last_stop_uppercase = true, + train_name = true, + }) -- print("DEBUG: line_start(): "..dump2({train_id = train.id, line_status = ls})) return true end @@ -255,6 +285,7 @@ end first_stop = bool or nil, -- zahrnout do popisu název výchozí zastávky? nil => false last_stop = bool or nil, -- zahrnout do popisu název cílové zastávky? nil => true last_stop_prefix = string or nil, -- text před název cílové zastávky; nil => "⇒ " + last_stop_uppercase = bool or nil, -- je-li true, název cílové zastávky se před uvedením převede na velká písmena train_name = bool or nil, -- zahrnout do popisu jméno vlaku, je-li k dispozici; nil => false train_name_prefix = string or nil, -- text před jméno vlaku; nil => "\n" } @@ -281,6 +312,9 @@ function al.get_line_description(linevar_def, options) local terminus_index, terminus_data = al.get_terminus(linevar_def, 1, false) if terminus_index ~= nil then s3 = get_station_name(terminus_data.stn) + if options.last_stop_uppercase then + s3 = na_velka_pismena(s3) + end end s3 = (options.last_stop_prefix or "⇒ ")..s3 else @@ -691,6 +725,28 @@ function al.linevar_decompose(linevar) return parts[1], parts[2] or "", parts[3] or "" end +--[[ + Vrací: + a) pokud linevar existuje a má průjezdy: + passages, stops: + {{[1] = rwtime, ...}...}, {"kód", "název"}...} + b) jinak: + nil, nil +]] +function al.get_last_passages(linevar_def) + local lp = last_passages[linevar_def.name] + if linevar_def ~= nil and lp ~= nil and lp[1] ~= nil then + local passages, stops = {}, {} + for i, stop in ipairs(linevar_def.stops) do + stops[i] = {stop.stn, get_station_name(stop.stn)} + end + for i = 1, #lp do + passages[i] = table.copy(lp[i]) + end + return passages, stops + end +end + --[[ DEBUG: local debug_print = {} function debug_print.print() |