vtm/quelldatein/manifestation/web/partie.ts

494 lines
13 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/>.
*/
2018-03-26 14:22:20 +02:00
module mod_vtm
2017-11-09 18:42:09 +01:00
{
2018-03-26 14:22:20 +02:00
export module mod_manifestation
2017-11-09 21:57:35 +01:00
{
2018-03-26 14:22:20 +02:00
export module mod_web
2017-11-09 18:42:09 +01:00
{
2018-03-26 14:22:20 +02:00
2018-03-28 13:59:29 +02:00
export module mod_round
2017-11-09 21:57:35 +01:00
{
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function text_nachedit(text : string) : string
2017-11-09 21:57:35 +01:00
{
2018-03-26 14:22:20 +02:00
let regexp : RegExp = (new RegExp("\\$\{s(\\d*)\}", "g"));
return text.replace(regexp, "<span class=\"symbol_$1\">&nbsp;&nbsp;</span>");
2017-11-09 21:57:35 +01:00
}
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export type type_round =
2017-11-09 21:57:35 +01:00
{
2018-03-28 13:59:29 +02:00
model : mod_model.mod_round.type_round;
area : Element;
intervall : lib_errormonade.type_errormonade<any>;
2017-11-09 21:57:35 +01:00
}
2018-03-26 14:22:20 +02:00
;
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function create
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
model : mod_model.mod_round.type_round,
area : Element
2018-03-26 14:22:20 +02:00
)
2018-03-28 13:59:29 +02:00
: type_round
2017-11-09 18:42:09 +01:00
{
2018-03-26 14:22:20 +02:00
return {
2018-03-28 13:59:29 +02:00
"model": model,
"area": area,
"intervall": (lib_errormonade.create_nothing<any>()),
2018-03-26 14:22:20 +02:00
};
2017-11-09 18:42:09 +01:00
}
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create_erweitert
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
model : mod_model.mod_round.type_round,
area : Element
2018-03-26 14:22:20 +02:00
)
2018-03-28 13:59:29 +02:00
: type_manifestation<mod_model.mod_round.type_round>
2017-11-09 18:42:09 +01:00
{
2018-03-26 14:22:20 +02:00
return {
2018-03-28 13:59:29 +02:00
"kind": "web_round",
"data": create(model, area)
2018-03-26 14:22:20 +02:00
};
2017-11-09 18:42:09 +01:00
}
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function update_task(round : type_round) : void
2017-11-09 18:42:09 +01:00
{
2018-03-28 13:59:29 +02:00
document.querySelector("#task_text").innerHTML = (
text_nachedit
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_task.text
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.task_read(round.model)
2018-03-26 14:22:20 +02:00
)
)
);
2017-11-09 18:42:09 +01:00
}
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function update_world(round : type_round) : void
2018-03-20 13:30:00 +01:00
{
2018-03-28 13:59:29 +02:00
let node_svg : lib_xml.type_node = mod_svg.root
2018-03-26 14:22:20 +02:00
(
-4, -4,
+4, +4,
800, 800,
2018-03-28 13:59:29 +02:00
[mod_manifestation.view(mod_svg.mod_round.create_manifestation(round.model))]
2018-03-26 14:22:20 +02:00
)
;
2018-03-28 13:59:29 +02:00
round.area.innerHTML = lib_xml.view(node_svg);
2018-03-20 13:30:00 +01:00
}
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function update_token(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
let node_svg : lib_xml.type_node = mod_svg.root
2018-03-26 14:22:20 +02:00
(
-4, -4,
+4, +4,
800, 800,
2018-03-28 13:59:29 +02:00
[mod_manifestation.view(mod_svg.mod_round.create_manifestation(round.model))]
2018-03-26 14:22:20 +02:00
)
;
2018-03-28 13:59:29 +02:00
round.area.innerHTML = lib_xml.view(node_svg);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function update_mode(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
let status : string;
2018-03-28 13:59:29 +02:00
switch (mod_vtm.mod_model.mod_round.mode_read(round.model))
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
case mod_vtm.mod_model.mod_mode.initial:
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
status = lib_translate.get("model.modes.initial");
2018-03-26 14:22:20 +02:00
break;
}
2018-03-28 13:59:29 +02:00
case mod_vtm.mod_model.mod_mode.uncertain:
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
status = lib_translate.get("model.modes.uncertain");
2018-03-26 14:22:20 +02:00
break;
}
2018-03-28 13:59:29 +02:00
case mod_vtm.mod_model.mod_mode.wrong:
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
status = lib_translate.get("model.modes.wrong");
2018-03-26 14:22:20 +02:00
break;
}
2018-03-28 13:59:29 +02:00
case mod_vtm.mod_model.mod_mode.correct:
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
status = lib_translate.get("model.modes.correct");
2018-03-26 14:22:20 +02:00
break;
}
default:
{
2018-03-28 13:59:29 +02:00
let message : string = "unbehandelter Modus";
throw (new Error(message));
2018-03-26 14:22:20 +02:00
break;
}
}
2018-03-28 13:59:29 +02:00
document.querySelector("#task_status").textContent = status;
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function update_buttons(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
let mode : mod_vtm.mod_model.mod_mode.type_mode = mod_vtm.mod_model.mod_round.mode_read(round.model);
2018-03-26 14:22:20 +02:00
let klasse : string;
2018-03-28 13:59:29 +02:00
switch (mode)
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
case mod_vtm.mod_model.mod_mode.initial:
2018-03-26 14:22:20 +02:00
{
klasse = "initial";
break;
}
2018-03-28 13:59:29 +02:00
case mod_vtm.mod_model.mod_mode.uncertain:
2018-03-26 14:22:20 +02:00
{
klasse = (
2018-03-28 13:59:29 +02:00
lib_errormonade.filled<any>(round.intervall)
? "uncertain_running"
: "uncertain_standing"
2018-03-26 14:22:20 +02:00
);
break;
}
2018-03-28 13:59:29 +02:00
case mod_vtm.mod_model.mod_mode.wrong:
case mod_vtm.mod_model.mod_mode.correct:
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
klasse = "done";
2018-03-26 14:22:20 +02:00
break;
}
2018-03-26 22:32:10 +02:00
default:
{
throw (new Error("unbehandelt!"));
break;
}
2018-03-26 14:22:20 +02:00
}
2018-03-28 13:59:29 +02:00
document.querySelector("#buttons").setAttribute("class", klasse);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function view(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
update_task(round);
update_world(round);
update_token(round);
update_mode(round);
update_buttons(round);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function stop(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled(round.intervall))
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
clearInterval(lib_errormonade.read(round.intervall));
round.intervall = (lib_errormonade.create_nothing<any>());
2018-03-26 14:22:20 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
let message : string = "kein Intervall gesetzt";
console.warn(message);
2018-03-26 14:22:20 +02:00
}
2018-03-28 13:59:29 +02:00
update_buttons(round);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function resume(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
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)
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 14:22:20 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
stop(round);
2018-03-26 14:22:20 +02:00
}
2018-03-28 13:59:29 +02:00
update_buttons(round);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function testen(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
let handle : any = setInterval(() => resume(round), 500);
round.intervall = (lib_errormonade.create_just<any>(handle));
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function edit(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
stop(round);
mod_vtm.mod_model.mod_round.reset(round.model);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function clear(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.world_clear(round.model);
mod_vtm.mod_model.mod_round.reset(round.model);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function bind(round : type_round) : void
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
let spot_ermitteln = (target : EventTarget) =>
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
let spot : lib_errormonade.type_errormonade<mod_vtm.mod_model.mod_spot.type_spot>;
let dom_tile : Element = target["closest"](".tile");
if (dom_tile == null)
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
spot = (lib_errormonade.create_nothing<mod_vtm.mod_model.mod_spot.type_spot>());
2018-03-26 14:22:20 +02:00
}
else
{
2018-03-28 13:59:29 +02:00
let rel : string = dom_tile.getAttribute("rel")
spot = (lib_errormonade.create_just<mod_vtm.mod_model.mod_spot.type_spot>(mod_vtm.mod_model.mod_spot.von_hash(rel)));
2018-03-26 14:22:20 +02:00
}
2018-03-28 13:59:29 +02:00
return spot;
2018-03-26 14:22:20 +02:00
}
;
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
round.model,
"aendrung_task",
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
update_task(round);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
round.model,
"aendrung_world",
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
update_world(round);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
round.model,
"aendrung_token",
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
update_token(round);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-28 13:59:29 +02:00
round.model,
"aendrung_mode",
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
update_mode(round);
update_buttons(round);
2018-03-26 14:22:20 +02:00
}
)
;
// Links-Klick
2018-03-28 13:59:29 +02:00
round.area.addEventListener
2018-03-26 14:22:20 +02:00
(
"click",
event =>
{
event.preventDefault();
2018-03-28 13:59:29 +02:00
let spot_ : lib_errormonade.type_errormonade<mod_vtm.mod_model.mod_spot.type_spot> = spot_ermitteln(event.target);
if (lib_errormonade.filled(spot_))
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.world_tile_wechseln(round.model, lib_errormonade.read(spot_), false);
2018-03-26 14:22:20 +02:00
}
else
{
console.info("-- kein Feld");
}
}
)
;
// Rechts-Klick
2018-03-28 13:59:29 +02:00
round.area.addEventListener
2018-03-26 14:22:20 +02:00
(
"contextmenu",
event =>
{
event.preventDefault();
2018-03-28 13:59:29 +02:00
let spot_ : lib_errormonade.type_errormonade<mod_vtm.mod_model.mod_spot.type_spot> = spot_ermitteln(event.target);
if (lib_errormonade.filled(spot_))
2018-03-26 14:22:20 +02:00
{
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.world_tile_wechseln(round.model, lib_errormonade.read(spot_), true);
2018-03-26 14:22:20 +02:00
}
else
{
console.info("-- kein Feld");
}
}
)
;
// Mausrad
2018-03-28 13:59:29 +02:00
round.area.addEventListener
2018-03-26 14:22:20 +02:00
(
"wheel",
event =>
{
event.preventDefault();
2018-03-28 13:59:29 +02:00
let spot_ : lib_errormonade.type_errormonade<mod_vtm.mod_model.mod_spot.type_spot> = spot_ermitteln(event.target);
if (lib_errormonade.filled(spot_))
2018-03-26 14:22:20 +02:00
{
let inkrement : int = ((event["deltaY"] < 0) ? -1 : +1);
2018-03-28 13:59:29 +02:00
mod_vtm.mod_model.mod_round.world_tile_rotate(round.model, lib_errormonade.read(spot_), inkrement);
2018-03-26 14:22:20 +02:00
}
else
{
console.info("-- kein Feld");
}
}
)
;
// Schritt
2018-03-28 13:59:29 +02:00
document.querySelector("#button_step").addEventListener
2018-03-26 14:22:20 +02:00
(
"click",
event =>
{
2018-03-28 13:59:29 +02:00
stop(round);
resume(round);
2018-03-26 14:22:20 +02:00
}
)
;
// Testen
2018-03-28 13:59:29 +02:00
document.querySelector("#button_test").addEventListener
2018-03-26 14:22:20 +02:00
(
"click",
event =>
{
2018-03-28 13:59:29 +02:00
testen(round);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-28 13:59:29 +02:00
// stop
document.querySelector("#button_stop").addEventListener
2018-03-26 14:22:20 +02:00
(
"click",
event =>
{
2018-03-28 13:59:29 +02:00
stop(round);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-28 13:59:29 +02:00
// edit
document.querySelector("#button_edit").addEventListener
2018-03-26 14:22:20 +02:00
(
"click",
event =>
{
2018-03-28 13:59:29 +02:00
edit(round);
2018-03-26 14:22:20 +02:00
}
)
;
// Leeren
2018-03-28 13:59:29 +02:00
document.querySelector("#button_clear").addEventListener
2018-03-26 14:22:20 +02:00
(
"click",
event =>
{
2018-03-28 13:59:29 +02:00
clear(round);
2018-03-26 14:22:20 +02:00
}
)
;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
lib_trait.attend<signature_manifestation<mod_vtm.mod_model.mod_round.type_round, void>>
2018-03-28 12:09:27 +02:00
(
2018-03-28 13:59:29 +02:00
trait_manifestation,
"web_round",
2018-03-28 12:09:27 +02:00
{
2018-03-28 13:59:29 +02:00
"view": (manifestation) => view(manifestation.data),
"bind": (manifestation) => bind(manifestation.data),
2018-03-28 12:09:27 +02:00
}
)
2018-03-26 14:22:20 +02:00
;
2018-03-26 00:41:10 +02:00
}
2018-03-26 14:22:20 +02:00
}
2017-11-09 18:42:09 +01:00
}
}