vtm/quelldatein/manifestation/web/partie.ts

470 lines
11 KiB
TypeScript
Raw Normal View History

2017-11-09 18:42:09 +01:00
/*
* Verrückte Turing-Maschinen A turing complete game
* Copyright (C) 2016 Christian Fraß <vidofnir@folksprak.org>
*
* 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 <https://www.gnu.org/licenses/>.
*/
module mod_vtm_manifestation
{
2017-11-09 21:57:35 +01:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
function text_nachbearbeiten(text : string) : string
{
let regexp : RegExp = (new RegExp("\\$\{s(\\d*)\}", "g"));
2017-11-10 00:05:15 +01:00
return text.replace(regexp, "<span class=\"symbol_$1\">&nbsp;&nbsp;</span>");
2017-11-09 21:57:35 +01:00
}
2017-11-09 18:42:09 +01:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-26 00:41:10 +02:00
export type typ_web_partie =
2017-11-09 18:42:09 +01:00
{
2018-03-26 00:41:10 +02:00
aufbau : mod_vtm_aufbau.typ_partie;
bereich : Element;
intervall : schnittstelle_fehlermonade<any>;
}
;
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_erstellen
(
aufbau : mod_vtm_aufbau.typ_partie,
bereich : Element
)
: typ_web_partie
{
return {
"aufbau": aufbau,
"bereich": bereich,
"intervall": (new klasse_nichts<any>()),
};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
export function web_partie_erstellen_erweitert
(
aufbau : mod_vtm_aufbau.typ_partie,
bereich : Element
)
: typ_manifestation<mod_vtm_aufbau.typ_partie>
{
return {
"art": "web_partie",
"angaben": web_partie_erstellen(aufbau, bereich)
};
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_erneuern_aufgabe(web_partie : typ_web_partie) : void
{
document.querySelector("#aufgabe_text").innerHTML = (
text_nachbearbeiten
(
mod_vtm_aufbau.aufgabe_text
(
mod_vtm_aufbau.partie_aufgabe_lesen(web_partie.aufbau)
)
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_erneuern_welt(web_partie : typ_web_partie) : void
{
let knoten_svg : mod_vtm_helfer.typ_xmlknoten = mod_vtm_manifestation.svg_wurzel
(
-4, -4,
+4, +4,
800, 800,
[manifestation_darstellen(svg_partie_erstellen_manifestation(web_partie.aufbau))]
)
;
web_partie.bereich.innerHTML = mod_vtm_helfer.xmlknoten_darstellen(knoten_svg);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_erneuern_figur(web_partie : typ_web_partie) : void
{
let knoten_svg : mod_vtm_helfer.typ_xmlknoten = mod_vtm_manifestation.svg_wurzel
(
-4, -4,
+4, +4,
800, 800,
[manifestation_darstellen(svg_partie_erstellen_manifestation(web_partie.aufbau))]
)
;
web_partie.bereich.innerHTML = mod_vtm_helfer.xmlknoten_darstellen(knoten_svg);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_erneuern_modus(web_partie : typ_web_partie) : void
{
let status : string;
switch (mod_vtm_aufbau.partie_modus_lesen(web_partie.aufbau))
2017-11-09 18:42:09 +01:00
{
2018-03-26 00:41:10 +02:00
case mod_vtm_aufbau.modus_initial:
{
status = "Maschine aufbauen";
break;
}
case mod_vtm_aufbau.modus_ungewiss:
{
status = "wird geprüft …";
break;
}
case mod_vtm_aufbau.modus_fehlerhaft:
{
status = "fehlerhaft :/";
break;
}
case mod_vtm_aufbau.modus_korrekt:
{
status = "anscheinend korrekt :)";
break;
}
default:
{
let meldung : string = "unbehandelter Modus";
throw (new Error(meldung));
break;
}
2017-11-09 18:42:09 +01:00
}
2018-03-26 00:41:10 +02:00
document.querySelector("#aufgabe_status").textContent = status;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_erneuern_knoepfe(web_partie : typ_web_partie) : void
{
let modus : mod_vtm_aufbau.typ_modus = mod_vtm_aufbau.partie_modus_lesen(web_partie.aufbau);
let klasse : string;
switch (modus)
2017-11-09 21:57:35 +01:00
{
2018-03-26 00:41:10 +02:00
case mod_vtm_aufbau.modus_initial:
{
klasse = "initial";
break;
}
case mod_vtm_aufbau.modus_ungewiss:
{
klasse = (
web_partie.intervall.ist_schlicht()
? "ungewiss_laufend"
: "ungewiss_stehend"
);
break;
}
case mod_vtm_aufbau.modus_fehlerhaft:
case mod_vtm_aufbau.modus_korrekt:
{
klasse = "fertig";
break;
}
2017-11-09 21:57:35 +01:00
}
2018-03-26 00:41:10 +02:00
document.querySelector("#knoepfe").setAttribute("class", klasse);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_darstellen(web_partie : typ_web_partie) : void
{
web_partie_erneuern_aufgabe(web_partie);
web_partie_erneuern_welt(web_partie);
web_partie_erneuern_figur(web_partie);
web_partie_erneuern_modus(web_partie);
web_partie_erneuern_knoepfe(web_partie);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_anhalten(web_partie : typ_web_partie) : void
{
if (web_partie.intervall.ist_schlicht())
2017-11-09 21:57:35 +01:00
{
2018-03-26 00:41:10 +02:00
clearInterval(web_partie.intervall.lesen());
web_partie.intervall = (new klasse_nichts<any>());
2017-11-09 21:57:35 +01:00
}
2018-03-26 00:41:10 +02:00
else
2017-11-09 21:57:35 +01:00
{
2018-03-26 00:41:10 +02:00
let meldung : string = "kein Intervall gesetzt";
console.warn(meldung);
2017-11-09 21:57:35 +01:00
}
2018-03-26 00:41:10 +02:00
web_partie_erneuern_knoepfe(web_partie);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_fortfahren(web_partie : typ_web_partie) : void
{
mod_vtm_aufbau.partie_fortfahren(web_partie.aufbau);
let modus : mod_vtm_aufbau.typ_modus = mod_vtm_aufbau.partie_modus_lesen(web_partie.aufbau);
if (modus <= 1)
2017-11-09 21:57:35 +01:00
{
2018-03-26 00:41:10 +02:00
// nichts tun
2017-11-09 21:57:35 +01:00
}
2018-03-26 00:41:10 +02:00
else
2017-11-09 21:57:35 +01:00
{
2018-03-26 00:41:10 +02:00
web_partie_anhalten(web_partie);
2017-11-09 21:57:35 +01:00
}
2018-03-26 00:41:10 +02:00
web_partie_erneuern_knoepfe(web_partie);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_testen(web_partie : typ_web_partie) : void
{
let handle : any = setInterval(() => web_partie_fortfahren(web_partie), 500);
web_partie.intervall = (new klasse_schlicht<any>(handle));
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_bearbeiten(web_partie : typ_web_partie) : void
{
web_partie_anhalten(web_partie);
mod_vtm_aufbau.partie_zuruecksetzen(web_partie.aufbau);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_leeren(web_partie : typ_web_partie) : void
{
mod_vtm_aufbau.partie_welt_leeren(web_partie.aufbau);
mod_vtm_aufbau.partie_zuruecksetzen(web_partie.aufbau);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
function web_partie_binden(web_partie : typ_web_partie) : void
{
let stelle_ermitteln = (target : EventTarget) =>
2017-11-09 18:42:09 +01:00
{
2018-03-26 00:41:10 +02:00
let stelle : schnittstelle_fehlermonade<mod_vtm_aufbau.typ_stelle>;
let dom_feld : Element = target["closest"](".feld");
if (dom_feld == null)
2017-11-10 19:00:25 +01:00
{
2018-03-26 00:41:10 +02:00
stelle = (new klasse_nichts<mod_vtm_aufbau.typ_stelle>());
2017-11-10 19:00:25 +01:00
}
2018-03-26 00:41:10 +02:00
else
{
let rel : string = dom_feld.getAttribute("rel")
stelle = (new klasse_schlicht<mod_vtm_aufbau.typ_stelle>(mod_vtm_aufbau.stelle_von_hash(rel)));
}
return stelle;
2017-11-09 21:57:35 +01:00
}
2018-03-26 00:41:10 +02:00
;
mod_vtm_aufbau.partie_lauschen
(
web_partie.aufbau,
"aenderung_aufgabe",
(angaben) =>
2017-11-10 19:00:25 +01:00
{
2018-03-26 00:41:10 +02:00
web_partie_erneuern_aufgabe(web_partie);
2017-11-10 19:00:25 +01:00
}
2018-03-26 00:41:10 +02:00
)
;
mod_vtm_aufbau.partie_lauschen
(
web_partie.aufbau,
"aenderung_welt",
(angaben) =>
2017-11-10 19:00:25 +01:00
{
2018-03-26 00:41:10 +02:00
web_partie_erneuern_welt(web_partie);
2017-11-10 19:00:25 +01:00
}
2018-03-26 00:41:10 +02:00
)
;
mod_vtm_aufbau.partie_lauschen
(
web_partie.aufbau,
"aenderung_figur",
(angaben) =>
2017-11-10 19:00:25 +01:00
{
2018-03-26 00:41:10 +02:00
web_partie_erneuern_figur(web_partie);
2017-11-10 19:00:25 +01:00
}
2018-03-26 00:41:10 +02:00
)
;
mod_vtm_aufbau.partie_lauschen
(
web_partie.aufbau,
"aenderung_modus",
(angaben) =>
2017-11-10 19:00:25 +01:00
{
2018-03-26 00:41:10 +02:00
web_partie_erneuern_modus(web_partie);
web_partie_erneuern_knoepfe(web_partie);
2017-11-10 19:00:25 +01:00
}
2018-03-26 00:41:10 +02:00
)
;
// Links-Klick
web_partie.bereich.addEventListener
(
"click",
event =>
2017-11-09 21:57:35 +01:00
{
2018-03-26 00:41:10 +02:00
event.preventDefault();
let stelle_ : schnittstelle_fehlermonade<mod_vtm_aufbau.typ_stelle> = stelle_ermitteln(event.target);
if (stelle_.ist_schlicht())
2017-11-09 21:57:35 +01:00
{
2018-03-26 00:41:10 +02:00
mod_vtm_aufbau.partie_welt_feld_wechseln(web_partie.aufbau, stelle_.lesen(), false);
2017-11-09 21:57:35 +01:00
}
else
{
2018-03-26 00:41:10 +02:00
console.info("-- kein Feld");
2017-11-09 21:57:35 +01:00
}
}
2018-03-26 00:41:10 +02:00
)
;
// Rechts-Klick
web_partie.bereich.addEventListener
(
"contextmenu",
event =>
{
event.preventDefault();
let stelle_ : schnittstelle_fehlermonade<mod_vtm_aufbau.typ_stelle> = stelle_ermitteln(event.target);
if (stelle_.ist_schlicht())
2017-11-09 18:42:09 +01:00
{
2018-03-26 00:41:10 +02:00
mod_vtm_aufbau.partie_welt_feld_wechseln(web_partie.aufbau, stelle_.lesen(), true);
2017-11-09 18:42:09 +01:00
}
2018-03-26 00:41:10 +02:00
else
2017-11-09 18:42:09 +01:00
{
2018-03-26 00:41:10 +02:00
console.info("-- kein Feld");
2017-11-09 18:42:09 +01:00
}
2018-03-26 00:41:10 +02:00
}
)
;
// Mausrad
web_partie.bereich.addEventListener
(
"wheel",
event =>
{
event.preventDefault();
let stelle_ : schnittstelle_fehlermonade<mod_vtm_aufbau.typ_stelle> = stelle_ermitteln(event.target);
if (stelle_.ist_schlicht())
2017-11-09 18:42:09 +01:00
{
2018-03-26 00:41:10 +02:00
let inkrement : int = ((event["deltaY"] < 0) ? -1 : +1);
mod_vtm_aufbau.partie_welt_feld_drehen(web_partie.aufbau, stelle_.lesen(), inkrement);
2017-11-09 18:42:09 +01:00
}
2018-03-26 00:41:10 +02:00
else
2018-03-20 13:30:00 +01:00
{
2018-03-26 00:41:10 +02:00
console.info("-- kein Feld");
2018-03-20 13:30:00 +01:00
}
2018-03-26 00:41:10 +02:00
}
)
;
// Schritt
document.querySelector("#knopf_schritt").addEventListener
(
"click",
event =>
{
web_partie_anhalten(web_partie);
web_partie_fortfahren(web_partie);
}
)
;
// Testen
document.querySelector("#knopf_testen").addEventListener
(
"click",
event =>
{
web_partie_testen(web_partie);
}
)
;
// Anhalten
document.querySelector("#knopf_anhalten").addEventListener
(
"click",
event =>
{
web_partie_anhalten(web_partie);
}
)
;
// Bearbeiten
document.querySelector("#knopf_bearbeiten").addEventListener
(
"click",
event =>
{
web_partie_bearbeiten(web_partie);
}
)
;
// Leeren
document.querySelector("#knopf_leeren").addEventListener
(
"click",
event =>
{
web_partie_leeren(web_partie);
}
)
;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
implementierung_manifestation["web_partie"] =
{
"darstellen": (manifestation) => web_partie_darstellen(manifestation.angaben),
"binden": (manifestation) => web_partie_binden(manifestation.angaben),
2017-11-09 18:42:09 +01:00
}
2018-03-26 00:41:10 +02:00
;
2017-11-09 18:42:09 +01:00
}