vtm/source/manifestation/store/game.ts

173 lines
4.7 KiB
TypeScript
Raw Normal View History

/*
* Verrückte Turing-Maschinen A turing complete game
2018-03-29 22:00:42 +02:00
* Copyright (C) 2016-2018 kcf <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
{
2018-03-26 14:22:20 +02:00
export module mod_manifestation
{
2018-03-26 14:22:20 +02:00
2018-03-28 13:59:29 +02:00
export module mod_store
2018-03-26 14:22:20 +02:00
{
2018-03-29 01:13:39 +02:00
export module mod_game
2018-03-26 00:41:10 +02: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 =
{
2018-03-29 01:13:39 +02:00
model : mod_vtm.mod_model.mod_game.type_game;
}
2018-03-26 14:22:20 +02:00
;
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create
2018-03-26 14:22:20 +02:00
(
2018-03-29 01:13:39 +02:00
model : mod_vtm.mod_model.mod_game.type_game
2018-03-26 14:22:20 +02:00
)
2018-03-29 01:13:39 +02:00
: type_game
{
2018-03-26 14:22:20 +02:00
return {
2018-03-28 13:59:29 +02:00
"model": model
2018-03-26 14:22:20 +02: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_vtm.mod_model.mod_game.type_game
2018-03-26 14:22:20 +02:00
)
2018-03-29 01:13:39 +02:00
: type_manifestation<mod_vtm.mod_model.mod_game.type_game>
2018-03-26 14:22:20 +02:00
{
return {
2018-03-29 01:13:39 +02:00
"kind": "store_game",
2018-03-28 13:59:29 +02:00
"data": create(model)
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
{
}
/**
* @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 22:00:42 +02:00
mod_vtm.mod_model.mod_game.listen
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
// console.info("change_task", data);
let task : mod_vtm.mod_model.mod_task.type_task = mod_vtm.mod_model.mod_game.task_read(game.model);
2018-03-28 13:59:29 +02:00
let id : string = mod_vtm.mod_model.mod_task.id(task);
2018-03-26 14:22:20 +02:00
let key : string = ("vtm_" + id);
//
if (key in localStorage)
{
let item : string = localStorage.getItem(key);
2018-03-28 13:59:29 +02:00
let world_ : any = JSON.parse(item);
let world : mod_vtm.mod_model.mod_world.type_world = mod_vtm.mod_model.mod_world.import_(world_);
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.world_set(game.model, world, false);
2018-03-26 14:22:20 +02:00
}
else
{
2018-03-29 01:13:39 +02:00
mod_vtm.mod_model.mod_game.world_clear(game.model);
2018-03-28 13:59:29 +02:00
// nothing tun
2018-03-26 14:22:20 +02:00
}
}
)
;
2018-03-29 22:00:42 +02:00
mod_vtm.mod_model.mod_game.listen
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
let task : mod_vtm.mod_model.mod_task.type_task = mod_vtm.mod_model.mod_game.task_read(game.model);
2018-03-28 13:59:29 +02:00
let id : string = mod_vtm.mod_model.mod_task.id(task);
2018-03-26 14:22:20 +02:00
let key : string = ("vtm_" + id);
//
2018-03-29 01:13:39 +02:00
let world : mod_vtm.mod_model.mod_world.type_world = mod_vtm.mod_model.mod_game.world_read(game.model);
2018-03-28 13:59:29 +02:00
let world_ : any = mod_vtm.mod_model.mod_world.export_(world);
let item : string = JSON.stringify(world_);
2018-03-26 14:22:20 +02:00
localStorage.setItem(key, item);
}
)
;
2018-03-29 22:00:42 +02:00
mod_vtm.mod_model.mod_game.listen
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
// console.info("change_token", data);
2018-03-26 14:22:20 +02:00
}
)
;
2018-03-29 22:00:42 +02:00
mod_vtm.mod_model.mod_game.listen
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
// console.info("change_mode", data);
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
"store_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
}
}
}