summaryrefslogtreecommitdiffstats
path: root/nfsw/static/io.js
blob: aee14388555063b2e2bd68ad7414ea8690dad339 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
document.addEventListener('DOMContentLoaded', function() {
    /**
     * Handling for sending commands to server.
     */
    window.qip = false;  // query in prograss flag.
    function query(q) {
        if (window.qip || window.iip) {
            return window.setTimeout(query, 10, q);
        }
        window.qip = true;

        var qipoff = function() {
            window.qip = false;
            ioconsole.autoScroll = true;
        };
        var spit = function(response) {
            var gdg = 'Oops! getting gobbledygook from server';

            var r;
            try {
                r = JSON.parse(response);
            } catch (e) {
                return barfslow(gdg, 'error', qipoff);
            }

            if (typeof r !== 'object') {
                return barf(gdg);
            }

            if (!('ans' in r)) {
                return barfslow(
                    'No answer from server!',
                    'error',
                    qipoff
                );
            }

            if ('logout' in r && r.logout) {
                return window.location.href = '/';
            }

            if ('reset' in r && r.reset) {
                return window.location.href = '/epilogue';
            }

            return barfslow(r.ans, 'concierge', qipoff);
        };

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/io/query', true);

        xhr.onreadystatechange = function() {
            if (this.readyState != 4) {
                return;
            }

            if (this.status == 200) {
                return spit(this.responseText);
            } else {
                return barfslow(
                    '💥', 'error', qipoff
                );
            }
        };
        xhr.send(q);
    }

    window.iip = false;
    function intro() {
        if (window.qip) {
            return window.setTimeout(intro, 10);
        }
        window.iip = true;

        var iipoff = function() {
            window.iip = false;
            ioconsole.autoScroll = true;
        };
        var spit = function(response, status) {
            var r;
            try {
                r = JSON.parse(response);
            } catch (e) {
                return barfslow('💥God is busy sodomizing the waitress.' +
                                '\nTry refreshing the page.', 'error', iipoff);
            }

            if (!('intro' in r)) {
                return barfslow(
                    '💥Too bad Dr. Gonad Dick is ' +
                    '\ntoo busy whipping his eggplant.' +
                    '\nTry refreshing the page.',
                    'error',
                    iipoff
                );
            }

            if (status == 200) {
                barfslow(r.intro, 'concierge', iipoff);
            } else {
                barfslow(r.intro, 'error', iipoff);
            }
        };

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/io/intro', true);

        xhr.onreadystatechange = function() {
            if (this.readyState != 4) {
                return;
            }

            if ([200, 500].indexOf(this.status) >= 0) {
                return spit(this.responseText, this.status);
            } else {
                return barfslow(
                    '💥God just fucked a skunk.' +
                    '\nTry refreshing the page.',
                    'error',
                    iipoff
                );
            }
        };
        xhr.send();
    }

    /**
     * Handling for console.
     */
    function barf(txt, type, p) {
        if (txt.length < 1) {
            return '';
        }
        barfblank();

        p = document.createElement('pre');
        p.className = type;

        p.append(txt);

        ioconsole.appendChild(p);

        return '';
    }
    function barfblank() {
        p = document.createElement('pre');
        p.className = 'blank';

        p.innerHTML = '\n';

        ioconsole.appendChild(p);
    }
    function barfslow(txt, type, cb, p) {
        if (!txt || txt.length < 1) {
            if (cb)
                cb();

            return '';
        }

        if (!p)
            barfblank();
        if (!p) {
            p = document.createElement('pre');
            p.className = type;

            ioconsole.appendChild(p);
        }
        p.append(txt.substring(0, 1));

        if (ioconsole.autoScroll) {
            p.scrollIntoView(false);
        }


        window.setTimeout(
            barfslow, 0,
            txt.substring(1, txt.length),
            type,
            cb,
            p
        );

        return '';
    }
    var ioconsole = document.getElementsByClassName('console')[0];
    ioconsole.onscroll = function(e) {
        if (!window.qip && !window.iip) {
            return;
        }

        if (e.target.scrollTop < e.target.lastScrollTop) {
            e.target.autoScroll = false;
        }

        e.target.lastScrollTop = e.target.scrollTop;
    };
    ioconsole.lastScrollTop = ioconsole.scrollTop;
    ioconsole.autoScroll = true;


    /**
     * Handling for the prompt.
     */
    function submit(e) {
        e.preventDefault();

        var input = e.target.querySelector('input');

        var cmd = input.value;
        if (cmd.length < 1)
            return;

        /**
         * Reset prompt.
         */
        input.value = '';

        /**
         * Send command to server.
         */
        query(cmd);
    }
    var form = document.querySelector('form');
    form.onsubmit = submit;

    intro();
});