Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
game-article.js 5.78 KiB
Newer Older
peter_rabbit's avatar
peter_rabbit committed
"use strict";

Pierre Jarriges's avatar
Pierre Jarriges committed
const { images_url } = require("../../../../../admin-frontend/src/constants");
const { data_url } = require("../../../../constants");
peter_rabbit's avatar
peter_rabbit committed
const ImageCarousel = require("../../../generic-components/image-carousel");
peter_rabbit's avatar
peter_rabbit committed
const { getArticleBody } = require("../../../lib/article-utils");
const { fetch_json_or_error_text } = require("../../../lib/fetch");
const { MentaloEngine } = require("mentalo-engine");
peter_rabbit's avatar
peter_rabbit committed

class GameArticle {
    constructor(props) {
        this.props = props;
        this.parse_body();
    }

    parse_body() {
        let body = getArticleBody(this.props.body);
        const play_btn_regex = /\[PLAY_BUTTON\s\{.+\}\]/g;
        const found_play_buttons = body.match(play_btn_regex);
        if (found_play_buttons) {
            this.build_play_button(JSON.parse(found_play_buttons[0].replace(/[\[\]PLAY_BUTTON\s]/g, "")));
            body = body.replace(play_btn_regex, "");
        }
        this.body = body;
    }

    build_play_button(button_data) {
        this.render_play_button = {
            tag: "button",
            class: "play-button",
Pierre Jarriges's avatar
Pierre Jarriges committed
            contents: t("Jouer"),
            onclick: this.handle_click_play.bind(this, button_data.filename, button_data.engine)
        };
    }

    load_and_run_mentalo_game(filename, button_element) {
        const button_text = button_element.innerHTML;
        button_element.innerHTML = "Loading ...";
        button_element.style.pointerEvents = "none";

        fetch_json_or_error_text(`${data_url}/${filename}`)
            .then(game_data => {
                const container = document.createElement("div");
                container.style.position = "fixed";
                container.style.top = 0;
                container.style.left = 0;
                container.style.right = 0;
                container.style.bottom = 0;
                container.style.zIndex = 10;
                container.style.display = "flex";
                container.style.justifyContent = "center";
                container.style.alignItems = "center";

                container.id = "kuadrado-tmp-game-player-container";
                document.body.appendChild(container);
                document.body.style.overflow = "hidden";

                const engine = new MentaloEngine({
                    game_data,
                    fullscreen: true,
                    frame_rate: 30,
                    container,
                    on_quit_game: () => {
                        container.remove();
                        document.body.style.overflow = "visible";
                    }
                });

                engine.init();
                engine.run_game();
            })
            .catch(err => console.log(err))
            .finally(() => {
                button_element.innerHTML = button_text;
                button_element.style.pointerEvents = "unset";
            });
    handle_click_play(filename, engine, e) {
        switch (engine) {
            case "mentalo":
                this.load_and_run_mentalo_game(filename, e.target);
                break;
            default:
                console.log("Error, unkown engine")
peter_rabbit's avatar
peter_rabbit committed
    }

    render() {
peter_rabbit's avatar
peter_rabbit committed
        const {
            title,
            subtitle,
            images,
Pierre Jarriges's avatar
Pierre Jarriges committed
            details,
peter_rabbit's avatar
peter_rabbit committed
        } = this.props;
peter_rabbit's avatar
peter_rabbit committed
        return {
            tag: "article",
            typeof: "VideoGame",
            additionalType: "Article",
peter_rabbit's avatar
peter_rabbit committed
            class: "game-article",
            contents: [
                {
                    tag: "h2",
                    property: "name",
peter_rabbit's avatar
peter_rabbit committed
                    class: "game-title",
                    contents: title,
                },
peter_rabbit's avatar
peter_rabbit committed
                {
                    tag: "div",
                    class: "game-banner",
                    contents: [
Pierre Jarriges's avatar
Pierre Jarriges committed
                        { tag: "img", class: "pixelated", src: `${images_url}/${images[0]}` },
peter_rabbit's avatar
peter_rabbit committed
                    ],
                },
peter_rabbit's avatar
peter_rabbit committed
                {
                    tag: "h3",
                    class: "game-subtitle",
                    contents: subtitle,
                    property: "alternativeHeadline",
peter_rabbit's avatar
peter_rabbit committed
                },
                {
                    tag: "div",
                    class: "game-description",
                    property: "description",
                    contents: [{ tag: "p", style_rules: { margin: 0 }, contents: this.body }]
                        .concat(this.render_play_button
                            ? [this.render_play_button]
                            : []),
peter_rabbit's avatar
peter_rabbit committed
                },
Pierre Jarriges's avatar
Pierre Jarriges committed
                new ImageCarousel({ images: images.map(img => `${images_url}/${img}`) }).render(),
                details.length > 0 && {
peter_rabbit's avatar
peter_rabbit committed
                    tag: "div",
Pierre Jarriges's avatar
Pierre Jarriges committed
                    class: "article-details",
peter_rabbit's avatar
peter_rabbit committed
                    contents: [
                        {
                            tag: "h2",
Pierre Jarriges's avatar
Pierre Jarriges committed
                            contents: "Details",
peter_rabbit's avatar
peter_rabbit committed
                        },
                        {
Pierre Jarriges's avatar
Pierre Jarriges committed
                            tag: "ul",
                            class: "details-list",
                            contents: details.map(detail => {
                                return {
                                    tag: "li",
                                    class: "detail",
                                    contents: [
                                        { tag: "label", contents: detail.label },
                                        {
                                            tag: "div",
Pierre Jarriges's avatar
Pierre Jarriges committed
                                            class: "detail-value",
Pierre Jarriges's avatar
Pierre Jarriges committed
                                            contents: detail.value
                                        },
                                    ],
                                };
                            }),
peter_rabbit's avatar
peter_rabbit committed
                        },
                    ],
                },
            ],
        };
    }
}

module.exports = GameArticle;