Website Summary Files Commits Branches @commit @diff @stats
path: root / scripts / app.js
blob: a8f8e70b165de5384177c928868b66feecb9d3a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var app = app || {};

const globalEvents = (() => {
    app.eventService.addEventHandler('GameStart', (color) => {
        app.socketService.disconnectHome();
        app.config.USER.COLOR = color;
        window.location.hash = '#/play';
    });

    app.eventService.addEventHandler('GameEnd', (result) => {
        if(app.Game.isRunning) {
            app.Game.reset();
            app.socketService.disconnectGame();
            window.location.hash = '#/home';
        }
    });
})();

const handleRouting = () => {
    const path = window.location.hash.substr(window.location.hash.indexOf('#') + 1);

    switch (path) {
        case '/home':
            app.htmlService.getTemplate('home').then((template) => {
                const compliedTemplate = app.htmlService.createElement(template);

                app.htmlService.clearElement('#app');
                app.htmlService.attachElement(compliedTemplate, '#app');

                app.socketService.initHome().then(() => {
                    app.userService.refreshLogin();
                    app.Home.init();
                });
            });

            break;
        case '/play':
            app.htmlService.getTemplate('game').then((template) => {
                const compliedTemplate = app.htmlService.createElement(template);

                app.htmlService.clearElement('#app');
                app.htmlService.attachElement(compliedTemplate, '#app');

                app.Game.init();
                app.Game.load(app.config.USER.COLOR);
            });

            break;
    }
};

window.location.hash = '#/home';

// Routing

window.addEventListener('hashchange', handleRouting);
window.addEventListener('load', handleRouting);