/* * Verrückte Turing-Maschinen — A turing complete game * Copyright (C) 2016 Christian Fraß * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ module mod_vtm { export module mod_manifestation { export module mod_web { export module mod_round { /** * @author kcf */ function text_nachedit(text : string) : string { let regexp : RegExp = (new RegExp("\\$\{s(\\d*)\}", "g")); return text.replace(regexp, "  "); } /** * @author kcf */ export type type_round = { model : mod_model.mod_round.type_round; area : Element; intervall : lib_errormonade.type_errormonade; } ; /** * @author kcf */ function create ( model : mod_model.mod_round.type_round, area : Element ) : type_round { return { "model": model, "area": area, "intervall": (lib_errormonade.create_nothing()), }; } /** * @author kcf */ export function create_erweitert ( model : mod_model.mod_round.type_round, area : Element ) : type_manifestation { return { "kind": "web_round", "data": create(model, area) }; } /** * @author kcf */ function update_task(round : type_round) : void { document.querySelector("#task_text").innerHTML = ( text_nachedit ( mod_vtm.mod_model.mod_task.text ( mod_vtm.mod_model.mod_round.task_read(round.model) ) ) ); } /** * @author kcf */ function update_world(round : type_round) : void { let node_svg : lib_xml.type_node = mod_svg.root ( -4, -4, +4, +4, 800, 800, [mod_manifestation.view(mod_svg.mod_round.create_manifestation(round.model))] ) ; round.area.innerHTML = lib_xml.view(node_svg); } /** * @author kcf */ function update_token(round : type_round) : void { let node_svg : lib_xml.type_node = mod_svg.root ( -4, -4, +4, +4, 800, 800, [mod_manifestation.view(mod_svg.mod_round.create_manifestation(round.model))] ) ; round.area.innerHTML = lib_xml.view(node_svg); } /** * @author kcf */ function update_mode(round : type_round) : void { let status : string; switch (mod_vtm.mod_model.mod_round.mode_read(round.model)) { case mod_vtm.mod_model.mod_mode.initial: { status = lib_translate.get("model.modes.initial"); break; } case mod_vtm.mod_model.mod_mode.uncertain: { status = lib_translate.get("model.modes.uncertain"); break; } case mod_vtm.mod_model.mod_mode.wrong: { status = lib_translate.get("model.modes.wrong"); break; } case mod_vtm.mod_model.mod_mode.correct: { status = lib_translate.get("model.modes.correct"); break; } default: { let message : string = "unbehandelter Modus"; throw (new Error(message)); break; } } document.querySelector("#task_status").textContent = status; } /** * @author kcf */ function update_buttons(round : type_round) : void { let mode : mod_vtm.mod_model.mod_mode.type_mode = mod_vtm.mod_model.mod_round.mode_read(round.model); let klasse : string; switch (mode) { case mod_vtm.mod_model.mod_mode.initial: { klasse = "initial"; break; } case mod_vtm.mod_model.mod_mode.uncertain: { klasse = ( lib_errormonade.filled(round.intervall) ? "uncertain_running" : "uncertain_standing" ); break; } case mod_vtm.mod_model.mod_mode.wrong: case mod_vtm.mod_model.mod_mode.correct: { klasse = "done"; break; } default: { throw (new Error("unbehandelt!")); break; } } document.querySelector("#buttons").setAttribute("class", klasse); } /** * @author kcf */ function view(round : type_round) : void { update_task(round); update_world(round); update_token(round); update_mode(round); update_buttons(round); } /** * @author kcf */ function stop(round : type_round) : void { if (lib_errormonade.filled(round.intervall)) { clearInterval(lib_errormonade.read(round.intervall)); round.intervall = (lib_errormonade.create_nothing()); } else { let message : string = "kein Intervall gesetzt"; console.warn(message); } update_buttons(round); } /** * @author kcf */ function resume(round : type_round) : void { mod_vtm.mod_model.mod_round.resume(round.model); let mode : mod_vtm.mod_model.mod_mode.type_mode = mod_vtm.mod_model.mod_round.mode_read(round.model); if (mode <= 1) { // nothing tun } else { stop(round); } update_buttons(round); } /** * @author kcf */ function testen(round : type_round) : void { let handle : any = setInterval(() => resume(round), 500); round.intervall = (lib_errormonade.create_just(handle)); } /** * @author kcf */ function edit(round : type_round) : void { stop(round); mod_vtm.mod_model.mod_round.reset(round.model); } /** * @author kcf */ function clear(round : type_round) : void { mod_vtm.mod_model.mod_round.world_clear(round.model); mod_vtm.mod_model.mod_round.reset(round.model); } /** * @author kcf */ function bind(round : type_round) : void { let spot_ermitteln = (target : EventTarget) => { let spot : lib_errormonade.type_errormonade; let dom_tile : Element = target["closest"](".tile"); if (dom_tile == null) { spot = (lib_errormonade.create_nothing()); } else { let rel : string = dom_tile.getAttribute("rel") spot = (lib_errormonade.create_just(mod_vtm.mod_model.mod_spot.von_hash(rel))); } return spot; } ; mod_vtm.mod_model.mod_round.lauschen ( round.model, "aendrung_task", (data) => { update_task(round); } ) ; mod_vtm.mod_model.mod_round.lauschen ( round.model, "aendrung_world", (data) => { update_world(round); } ) ; mod_vtm.mod_model.mod_round.lauschen ( round.model, "aendrung_token", (data) => { update_token(round); } ) ; mod_vtm.mod_model.mod_round.lauschen ( round.model, "aendrung_mode", (data) => { update_mode(round); update_buttons(round); } ) ; // Links-Klick round.area.addEventListener ( "click", event => { event.preventDefault(); let spot_ : lib_errormonade.type_errormonade = spot_ermitteln(event.target); if (lib_errormonade.filled(spot_)) { mod_vtm.mod_model.mod_round.world_tile_wechseln(round.model, lib_errormonade.read(spot_), false); } else { console.info("-- kein Feld"); } } ) ; // Rechts-Klick round.area.addEventListener ( "contextmenu", event => { event.preventDefault(); let spot_ : lib_errormonade.type_errormonade = spot_ermitteln(event.target); if (lib_errormonade.filled(spot_)) { mod_vtm.mod_model.mod_round.world_tile_wechseln(round.model, lib_errormonade.read(spot_), true); } else { console.info("-- kein Feld"); } } ) ; // Mausrad round.area.addEventListener ( "wheel", event => { event.preventDefault(); let spot_ : lib_errormonade.type_errormonade = spot_ermitteln(event.target); if (lib_errormonade.filled(spot_)) { let inkrement : int = ((event["deltaY"] < 0) ? -1 : +1); mod_vtm.mod_model.mod_round.world_tile_rotate(round.model, lib_errormonade.read(spot_), inkrement); } else { console.info("-- kein Feld"); } } ) ; // Schritt document.querySelector("#button_step").addEventListener ( "click", event => { stop(round); resume(round); } ) ; // Testen document.querySelector("#button_test").addEventListener ( "click", event => { testen(round); } ) ; // stop document.querySelector("#button_stop").addEventListener ( "click", event => { stop(round); } ) ; // edit document.querySelector("#button_edit").addEventListener ( "click", event => { edit(round); } ) ; // Leeren document.querySelector("#button_clear").addEventListener ( "click", event => { clear(round); } ) ; } /** * @author kcf */ lib_trait.attend> ( trait_manifestation, "web_round", { "view": (manifestation) => view(manifestation.data), "bind": (manifestation) => bind(manifestation.data), } ) ; } } } }