Website Summary Files Commits Branches @commit @diff @stats
path: root / scripts / core / home.js
blob: df46b87dd9b1f1d3ab99eba2c2359889c1f35722 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var app = app || {};

const supportedCommands = {
    '-play': {
        invoke: () => {
            app.socketService.initGame().finally(() => {
                app.socketService.send('game', 'Login', app.userService.user);
            });
        },
        help: 'Initiate matchmaking to find an opponent and play the game.',
    },
    '-help': {
        invoke: () => {
            Object.keys(supportedCommands).forEach(key => {
                const message = `[${key}]: ${supportedCommands[key].help}`;
                const user = 'PUSG';

                const helpMessageNode = app.htmlService.createElement(app.htmlService
                    .getElementFromTemplate('message', {user, message}));

                app.htmlService.attachElement(helpMessageNode, '.history-messages');
            });
        },
        help: 'The currently invoked command - prints help for all commands.',
    },
    '-pew': {
        invoke: () => {
            app.Game.config.sounds.pew.play();
        },
        help: 'Does a pew!',
    }
};

class Home {
    constructor() {
        this.commands = supportedCommands;
        this.cachedMessages = [];
    }

    isCommand(command) {
        return this.commands.hasOwnProperty(command);
    }

    dispatch(command) {
        this.commands[command].invoke();
    }

    initEvents() {
        app.eventService.addEventHandler('LoggedIn', () => {
            app.htmlService.clearElement('.history-messages');
        });

        app.eventService.addEventHandler('OnLoggedIn', (users) => {
            users.forEach(user => {
                if(!app.htmlService.getElement(`#${user}`)) {
                    const userNode = app.htmlService
                        .getElementFromTemplate('user', {
                            user: user,
                        });

                    app.htmlService.attachElement(app.htmlService.createElement(userNode), '.participants');
                }
            });
        });

        app.eventService.addEventHandler('OnLoggedOut', (users) => {
            users.forEach(user => {
                app.htmlService.getElement(`#${user}`).remove();
            });
        });

        app.eventService.addEventHandler('OnReceiveMessage', ([user, message]) => {
            const messageNode = app.htmlService
                .getElementFromTemplate('message', {
                    user: user,
                    message: message
                });

            this.cachedMessages.push(messageNode);

            app.htmlService.attachElement(app.htmlService.createElement(messageNode)
                , '.history-messages');
        });
    }

    init() {
        app.htmlService.addEventHandler('#main-chat-input', 'keyup',  (e) => {
            if (e.key === 'Enter' || e.keyCode === 13) {
                const message = e.target.value;

                if(!app.userService.isLoggedIn()) {
                    app.userService.login();
                }

                if(app.userService.isLoggedIn() && message) {
                    e.target.value = "";

                    if(this.isCommand(message)) {
                        this.dispatch(message);
                    } else {
                        app.socketService.send('home', 'SendMessage', app.userService.user, message);
                    }
                }
            }
        });

        if(app.userService.isLoggedIn()) {
            app.htmlService.clearElement('.history-messages');
            this.cachedMessages.forEach(cachedMessage => {
                app.htmlService.attachElement(app.htmlService.createElement(cachedMessage)
                    , '.history-messages');
            });
        }

        this.initEvents();
    }
}

app.Home = new Home();