Browse Source

Add chat form that sends messages to Flask server

Tyler Hallada 6 years ago
parent
commit
71bed5ab17
5 changed files with 151 additions and 2 deletions
  1. 11 0
      chat/minecraft-chat.ini
  2. 34 0
      chat/server.py
  3. 49 0
      css/styles.css
  4. 23 2
      index.html
  5. 34 0
      js/sendChat.js

+ 11 - 0
chat/minecraft-chat.ini

@@ -0,0 +1,11 @@
1
+[uwsgi]
2
+module = wsgi:application
3
+
4
+master = true
5
+processes = 2
6
+
7
+socket = minecraft-chat.sock
8
+chmod-socket = 664
9
+vacuum = true
10
+
11
+die-on-term = true

+ 34 - 0
chat/server.py

@@ -0,0 +1,34 @@
1
+import logging
2
+import subprocess
3
+
4
+from flask import Flask, request
5
+
6
+app = Flask(__name__)
7
+
8
+
9
+@app.before_first_request
10
+def setup_logging():
11
+    app.logger.addHandler(logging.StreamHandler())
12
+    app.logger.setLevel(logging.INFO)
13
+
14
+
15
+@app.route('/chat/', methods=['GET', 'POST'])
16
+def send_chat():
17
+    if request.method == 'POST':
18
+        if request.form.get('email', None):
19
+            return 'Text was entered into honeypot!', 200
20
+        if not request.form.get('say-text', None):
21
+            return 'No message to send!', 422
22
+        if request.form.get('say-username', None):
23
+            subprocess.call(['/usr/bin/screen', '-S', 'mc-panic-shack', '-p', '0', '-X', 'stuff',
24
+                             '/say [{}]: {}\015'.format(request.form['say-username'], request.form['say-text'])])
25
+        else:
26
+            subprocess.call(['/usr/bin/screen', '-S', 'mc-panic-shack', '-p', '0', '-X', 'stuff',
27
+                             '/say {}\015'.format(request.form['say-text'])])
28
+        return 'Sending chat: ' + request.form.get('say-username', '') + ': ' + request.form['say-text']
29
+    else:
30
+        app.logger.info('Hello, world!')
31
+        return 'Hello, world!'
32
+
33
+if __name__ == "__main__":
34
+    app.run(host='0.0.0.0', port="8888")

+ 49 - 0
css/styles.css

@@ -81,3 +81,52 @@ ol li code {
81 81
   max-height: 600px;
82 82
   overflow: scroll;
83 83
 }
84
+
85
+#say-header {
86
+  margin-top: 15px;
87
+  margin-bottom: 10px;
88
+}
89
+
90
+#say-form {
91
+  border: 1px dashed darkgray;
92
+  padding: 10px;
93
+}
94
+
95
+#say-form label {
96
+  width: 100%;
97
+}
98
+
99
+#say-form, #say-send {
100
+  margin-top: 5px;
101
+}
102
+
103
+#say-username, #say-text {
104
+  margin-bottom: 5px;
105
+  margin-right: 5px;
106
+}
107
+
108
+#say-text {
109
+  width: 100%;
110
+}
111
+
112
+#say-dnf {
113
+  position: absolute;
114
+  left: -2000px;
115
+}
116
+
117
+#say-sending {
118
+  display: none;
119
+}
120
+
121
+#say-error {
122
+  color: darkred;
123
+}
124
+
125
+#say-success {
126
+  display: none;
127
+}
128
+
129
+#say-notice {
130
+  margin-top: 5px;
131
+  margin-bottom: 0;
132
+}

+ 23 - 2
index.html

@@ -85,10 +85,30 @@
85 85
 
86 86
         <div class="width-limit">
87 87
             <label>
88
-              <input type="checkbox" id="auto-scroll" checked>
89
-              Automatically scroll to bottom on log refresh
88
+                <input type="checkbox" id="auto-scroll" checked>
89
+                Automatically scroll to bottom on log refresh
90 90
             </label>
91 91
             <p class="small-text">Updated every 10 seconds.</p>
92
+            <h3 id="say-header">Send Chat to Server</h3>
93
+            <form id="say-form">
94
+              <label class="inline">
95
+                  Name:
96
+                  <br>
97
+                  <input type="text" name="say-username" id="say-username">
98
+              </label>
99
+              <label class="inline">
100
+                  Say:
101
+                  <br>
102
+                  <input type="text" name="say-text" id="say-text">
103
+              </label>
104
+              <br>
105
+              <input type="text" name="email" id="say-dnf">
106
+              <input type="submit" name="say-send" id="say-send" class="inline" value="Send Chat">
107
+              <span id="say-sending" class="inline">Sending...</span>
108
+              <span id="say-success" class="inline">Sent!</span>
109
+              <span id="say-error" class="inline"></span>
110
+              <p id="say-notice" class="small-text">It may take a minute or two before the message appears in the above server log.</p>
111
+            </form>
92 112
         </div>
93 113
 
94 114
         <script src="https://mcapi.us/scripts/minecraft.js"></script>
@@ -96,5 +116,6 @@
96 116
         <script src="js/getStatus.js"></script>
97 117
         <script src="js/getLog.js"></script>
98 118
         <script src="js/getMapUpdate.js"></script>
119
+        <script src="js/sendChat.js"></script>
99 120
     </body>
100 121
 </html>

+ 34 - 0
js/sendChat.js

@@ -0,0 +1,34 @@
1
+var form = document.getElementById('say-form');
2
+var username = document.getElementById('say-username');
3
+var text = document.getElementById('say-text');
4
+var send = document.getElementById('say-send');
5
+var sending = document.getElementById('say-sending');
6
+var success = document.getElementById('say-success');
7
+var error = document.getElementById('say-error');
8
+
9
+function sendChat(e) {
10
+  e.preventDefault();
11
+
12
+  var xhr = new XMLHttpRequest();
13
+  var formData = new FormData(form);
14
+  xhr.addEventListener('load', function (event) {
15
+    console.log(event.target.responseText);
16
+    if (event.target.status === 200) {
17
+      error.textContent = '';
18
+      text.value = '';
19
+      success.style.display = 'inline-block';
20
+    } else if (event.target.status === 422) {
21
+      error.textContent = 'You must give a message to send! (' + event.target.status + ')';
22
+      success.style.display = 'none';
23
+    } else {
24
+      error.textContent = 'Error Sending! (' + event.target.status + ')';
25
+      success.style.display = 'none';
26
+    }
27
+    sending.style.display = 'none';
28
+  });
29
+  xhr.open('POST', '/chat/');
30
+  xhr.send(formData);
31
+  sending.style.display = 'inline-block';
32
+}
33
+
34
+form.addEventListener('submit', sendChat);