1 | "use strict"; |
||
2 | |||
3 | 1 | const api = require('./api'); |
|
4 | |||
5 | 1 | let returnText = (connecting, disconnecting, timeStr, id, tex, me) => { |
|
6 | |||
7 | let text; |
||
8 | |||
9 | 4 | if (connecting) { |
|
10 | 1 | text = "(" + timeStr + ") " + id + tex; |
|
11 | 3 | } else if (disconnecting) { |
|
12 | 1 | text = "(" + timeStr + ") <b>" + id + tex + "</b>"; |
|
13 | } else { |
||
14 | 2 | text = "(" + timeStr + ") <b>" + id + " said</b>: " + tex + "<br>"; |
|
15 | } |
||
16 | |||
17 | 4 | return text; |
|
18 | }; |
||
19 | |||
20 | 1 | let checkProtocol = async (event, protocol, state) => { |
|
21 | let msg, parsedMsg, time, timeStr, connecting, disconnecting, id, text, me; |
||
22 | |||
23 | // consol.log(state); |
||
24 | |||
25 | 4 | if (protocol == "json") { |
|
26 | 3 | msg = JSON.parse(event.data); |
|
27 | 3 | parsedMsg = JSON.parse(msg.data); |
|
28 | 3 | time = new Date(parsedMsg.date); |
|
29 | 3 | timeStr = time.toLocaleTimeString(); |
|
30 | 3 | connecting = parsedMsg.connect; |
|
31 | 3 | disconnecting = parsedMsg.disconnect; |
|
32 | 3 | id = parsedMsg.id; |
|
33 | 3 | me = parsedMsg.me; |
|
34 | 3 | text = await checkText(parsedMsg.text, state, id); |
|
35 | } else { |
||
36 | 1 | msg = JSON.parse(event.data); |
|
37 | 1 | time = new Date(msg.date); |
|
38 | 1 | timeStr = time.toLocaleTimeString(); |
|
39 | 1 | connecting = msg.connect; |
|
40 | 1 | disconnecting = msg.disconnect; |
|
41 | 1 | id = msg.id; |
|
42 | 1 | me = msg.me; |
|
43 | 1 | text = await checkText(msg.text, state, id); |
|
44 | } |
||
45 | |||
46 | 4 | text = returnText(connecting, disconnecting, timeStr, id, text, me); |
|
47 | |||
48 | 4 | return text; |
|
49 | }; |
||
50 | |||
51 | 2 | let checkText = async (text, insert = false, id = false) => { |
|
52 | 4 | let rePost = new RegExp(/\[post\](\d+)\[\/post\]/g); |
|
53 | 4 | let reUser = new RegExp(/\[user\](\d+)\[\/user\]/g); |
|
54 | 4 | let matchPost = rePost.exec(text); |
|
55 | 4 | let matchUser = reUser.exec(text); |
|
56 | |||
57 | 4 | if (matchUser !== null) { |
|
58 | while (matchUser !== null) { |
||
59 | let getInformation = await api.getUserInformation(parseInt(matchUser[1])); |
||
60 | |||
61 | 2 | if (getInformation !== undefined) { |
|
62 | text = text.replace('[user]' + matchUser[1] + '[/user]', "<a href='" + |
||
63 | getInformation.link + "' target='_blank'>" + |
||
64 | getInformation.name + "</a>"); |
||
65 | matchUser = await reUser.exec(text); |
||
66 | } else { |
||
67 | text = undefined; |
||
68 | return text; |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | |||
73 | 4 | if (matchPost !== null) { |
|
74 | while (matchPost !== null) { |
||
75 | let getInformation = await api.getInformation(matchPost[1]); |
||
76 | |||
77 | 2 | if (getInformation !== undefined) { |
|
78 | text = text.replace('[post]' + matchPost[1] + '[/post]', "<a href='" + |
||
79 | getInformation.link + "' target='_blank'>" + |
||
80 | getInformation.title + "</a>"); |
||
81 | matchPost = await rePost.exec(text); |
||
82 | } else { |
||
83 | text = undefined; |
||
84 | return text; |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | |||
90 | 4 | return text; |
|
91 | }; |
||
92 | |||
93 | 1 | let setSubProtocol = (protocol) => { |
|
94 | 1 | return protocol; |
|
95 | }; |
||
96 | |||
97 | /** |
||
98 | * Log output to web browser. |
||
99 | * |
||
100 | * @param {string} message to output in the browser window. |
||
101 | * |
||
102 | * @return {void} |
||
103 | */ |
||
104 | 1 | let outputLog = async (text, t = null, output) => { |
|
105 | let time; |
||
106 | let now; |
||
107 | let timestamp; |
||
108 | |||
109 | 2 | if (t !== null) { |
|
110 | time = new Date(t); |
||
111 | timestamp = time.toLocaleTimeString(); |
||
112 | } else { |
||
113 | now = new Date(); |
||
114 | timestamp = now.toLocaleTimeString(); |
||
115 | } |
||
116 | |||
117 | |||
118 | let message = await checkText(text); |
||
119 | |||
120 | 2 | if (message !== undefined) { |
|
121 | output.innerHTML += `<div class="chat_output"> |
||
122 | <p class="chat_time">${timestamp}</p> <p class="chat_message">${message}</p><br></div>`; |
||
123 | output.scrollTop = output.scrollHeight; |
||
124 | } |
||
125 | }; |
||
126 | |||
127 | |||
128 | 1 | module.exports = { |
|
129 | checkText: checkText, |
||
130 | setSubProtocol: setSubProtocol, |
||
131 | checkProtocol: checkProtocol, |
||
132 | outputLog: outputLog |
||
133 | }; |
||
134 |