vtm/source/manifestation/web/game.ts

499 lines
13 KiB
TypeScript
Raw Normal View History

2017-11-09 18:42:09 +01:00
/*
* Verrückte Turing-Maschinen A turing complete game
2018-03-29 01:13:39 +02:00
* Copyright (C) 2016-2018 Christian Fraß <vidofnir@folksprak.org>
2017-11-09 18:42:09 +01:00
*
* 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-29 01:13:39 +02:00
export module mod_game
2017-11-09 21:57:35 +01:00
{
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function text_postprocess(text : string) : string
2017-11-09 21:57:35 +01:00
{
2018-03-29 01:13:39 +02:00
let vars : {[name : string] : string} = {};
for (let i : int = 0; i <= 3; i += 1)
vars["s" + i.toFixed(0)] = ("<span class=\"symbol_" + i.toFixed(0) + "\">&nbsp;&nbsp;</span>");
return lib_string.stance(text, vars);
2017-11-09 21:57:35 +01:00
}
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
export type type_game =
2017-11-09 21:57:35 +01:00
{
2018-03-29 01:13:39 +02:00
model : mod_model.mod_game.type_game;
2018-03-28 13:59:29 +02:00
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-29 01:13:39 +02:00
model : mod_model.mod_game.type_game,
2018-03-28 13:59:29 +02:00
area : Element
2018-03-26 14:22:20 +02:00
)
2018-03-29 01:13:39 +02:00
: type_game
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-29 01:13:39 +02:00
export function create_extended
2018-03-26 14:22:20 +02:00
(
2018-03-29 01:13:39 +02:00
model : mod_model.mod_game.type_game,
2018-03-28 13:59:29 +02:00
area : Element
2018-03-26 14:22:20 +02:00
)
2018-03-29 01:13:39 +02:00
: type_manifestation<mod_model.mod_game.type_game>
2017-11-09 18:42:09 +01:00
{
2018-03-29 01:13:39 +02:00
return (
lib_call.wrap
(
"web_game",
create(model, area)
)
);
2017-11-09 18:42:09 +01:00
}
2018-03-26 14:22:20 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function update_task(game : type_game) : void
2017-11-09 18:42:09 +01:00
{
2018-03-28 13:59:29 +02:00
document.querySelector("#task_text").innerHTML = (
2018-03-29 01:13:39 +02:00
text_postprocess
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-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.task_read(game.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-29 01:13:39 +02:00
function update_world(game : type_game) : void
2018-03-20 13:30:00 +01:00
{
2018-03-29 01:13:39 +02:00
let node_svg : lib_xml.type_node = lib_svg.root
2018-03-26 14:22:20 +02:00
(
-4, -4,
+4, +4,
800, 800,
2018-03-29 01:13:39 +02:00
[mod_manifestation.view(mod_svg.mod_game.create_extended(game.model))]
2018-03-26 14:22:20 +02:00
)
;
2018-03-29 01:13:39 +02:00
game.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-29 01:13:39 +02:00
function update_token(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
let node_svg : lib_xml.type_node = lib_svg.root
2018-03-26 14:22:20 +02:00
(
-4, -4,
+4, +4,
800, 800,
2018-03-29 01:13:39 +02:00
[mod_manifestation.view(mod_svg.mod_game.create_extended(game.model))]
2018-03-26 14:22:20 +02:00
)
;
2018-03-29 01:13:39 +02:00
game.area.innerHTML = lib_xml.view(node_svg);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function update_mode(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
let status : string;
2018-03-29 01:13:39 +02:00
switch (mod_vtm.mod_model.mod_game.mode_read(game.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-29 01:13:39 +02:00
function update_buttons(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
let mode : mod_vtm.mod_model.mod_mode.type_mode = mod_vtm.mod_model.mod_game.mode_read(game.model);
let class_ : 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
{
2018-03-29 01:13:39 +02:00
class_ = "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-29 01:13:39 +02:00
class_ = (
lib_errormonade.filled<any>(game.intervall)
2018-03-28 13:59:29 +02:00
? "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-29 01:13:39 +02:00
class_ = "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-29 01:13:39 +02:00
document.querySelector("#buttons").setAttribute("class", class_);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function view(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
update_task(game);
update_world(game);
update_token(game);
update_mode(game);
update_buttons(game);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function stop(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
if (lib_errormonade.filled(game.intervall))
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
clearInterval(lib_errormonade.read(game.intervall));
game.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-29 01:13:39 +02:00
update_buttons(game);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function resume(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.resume(game.model);
let mode : mod_vtm.mod_model.mod_mode.type_mode = mod_vtm.mod_model.mod_game.mode_read(game.model);
2018-03-28 13:59:29 +02:00
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-29 01:13:39 +02:00
stop(game);
2018-03-26 14:22:20 +02:00
}
2018-03-29 01:13:39 +02:00
update_buttons(game);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function testen(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
let handle : any = setInterval(() => resume(game), 500);
game.intervall = (lib_errormonade.create_just<any>(handle));
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function edit(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
stop(game);
mod_vtm.mod_model.mod_game.reset(game.model);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function clear(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.world_clear(game.model);
mod_vtm.mod_model.mod_game.reset(game.model);
2018-03-26 14:22:20 +02:00
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
function bind(game : type_game) : void
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
let spot_determine = (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")
2018-03-29 01:13:39 +02:00
spot = (lib_errormonade.create_just<mod_vtm.mod_model.mod_spot.type_spot>(mod_vtm.mod_model.mod_spot.from_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-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-29 01:13:39 +02:00
game.model,
"change_task",
2018-03-28 13:59:29 +02:00
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
update_task(game);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-29 01:13:39 +02:00
game.model,
"change_world",
2018-03-28 13:59:29 +02:00
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
update_world(game);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-29 01:13:39 +02:00
game.model,
"change_token",
2018-03-28 13:59:29 +02:00
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
update_token(game);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.lauschen
2018-03-26 14:22:20 +02:00
(
2018-03-29 01:13:39 +02:00
game.model,
"change_mode",
2018-03-28 13:59:29 +02:00
(data) =>
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
update_mode(game);
update_buttons(game);
2018-03-26 14:22:20 +02:00
}
)
;
// Links-Klick
2018-03-29 01:13:39 +02:00
game.area.addEventListener
2018-03-26 14:22:20 +02:00
(
"click",
event =>
{
event.preventDefault();
2018-03-29 01:13:39 +02:00
let spot_ : lib_errormonade.type_errormonade<mod_vtm.mod_model.mod_spot.type_spot> = spot_determine(event.target);
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled(spot_))
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.world_tile_wechseln(game.model, lib_errormonade.read(spot_), false);
2018-03-26 14:22:20 +02:00
}
else
{
console.info("-- kein Feld");
}
}
)
;
// Rechts-Klick
2018-03-29 01:13:39 +02:00
game.area.addEventListener
2018-03-26 14:22:20 +02:00
(
"contextmenu",
event =>
{
event.preventDefault();
2018-03-29 01:13:39 +02:00
let spot_ : lib_errormonade.type_errormonade<mod_vtm.mod_model.mod_spot.type_spot> = spot_determine(event.target);
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled(spot_))
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.world_tile_wechseln(game.model, lib_errormonade.read(spot_), true);
2018-03-26 14:22:20 +02:00
}
else
{
console.info("-- kein Feld");
}
}
)
;
// Mausrad
2018-03-29 01:13:39 +02:00
game.area.addEventListener
2018-03-26 14:22:20 +02:00
(
"wheel",
event =>
{
event.preventDefault();
2018-03-29 01:13:39 +02:00
let spot_ : lib_errormonade.type_errormonade<mod_vtm.mod_model.mod_spot.type_spot> = spot_determine(event.target);
2018-03-28 13:59:29 +02:00
if (lib_errormonade.filled(spot_))
2018-03-26 14:22:20 +02:00
{
let inkrement : int = ((event["deltaY"] < 0) ? -1 : +1);
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.world_tile_rotate(game.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-29 01:13:39 +02:00
stop(game);
resume(game);
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-29 01:13:39 +02:00
testen(game);
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-29 01:13:39 +02:00
stop(game);
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-29 01:13:39 +02:00
edit(game);
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-29 01:13:39 +02:00
clear(game);
2018-03-26 14:22:20 +02:00
}
)
;
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-29 01:13:39 +02:00
lib_trait.attend<signature_manifestation<mod_vtm.mod_model.mod_game.type_game, void>>
2018-03-28 12:09:27 +02:00
(
2018-03-28 13:59:29 +02:00
trait_manifestation,
2018-03-29 01:13:39 +02:00
"web_game",
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
}
}