From 502a472ea297e3c170dd74c1a4542f492cf2ea3d Mon Sep 17 00:00:00 2001 From: Caleb Mabry Date: Fri, 25 Mar 2022 00:15:36 -0400 Subject: Added support for saving chatlog --- webAO/client.js | 45 ++++++++++++++++++++++++++++++++++++++---- webAO/services/downloadFile.ts | 10 ++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 webAO/services/downloadFile.ts (limited to 'webAO') diff --git a/webAO/client.js b/webAO/client.js index 6a2947c..10153f6 100644 --- a/webAO/client.js +++ b/webAO/client.js @@ -29,7 +29,7 @@ import queryParser from './utils/queryParser.js'; import getAnimLength from './utils/getAnimLength.js'; import getResources from './utils/getResources.js'; import transparentPng from './constants/transparentPng'; - +import downloadFile from './services/downloadFile' const version = process.env.npm_package_version; let client; @@ -559,13 +559,51 @@ class Client extends EventEmitter { } } + saveChatlogHandle = async () => { + const clientLog = document.getElementById('client_log') + const icMessageLogs = clientLog.getElementsByTagName('p') + const messages = [] + + for (let i = 0; i < icMessageLogs.length; i++) { + const SHOWNAME_POSITION = 0 + const TEXT_POSITION = 2 + const showname = icMessageLogs[i].children[SHOWNAME_POSITION].innerHTML + const text = icMessageLogs[i].children[TEXT_POSITION].innerHTML + const message = `${showname}: ${text}` + messages.push(message) + } + const d = new Date(); + let ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d); + let mo = new Intl.DateTimeFormat('en', { month: 'short' }).format(d); + let da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d); + + const filename = `chatlog-${da}-${mo}-${ye}`.toLowerCase() + downloadFile(messages.join('\n'), filename) + + // Reset Chatbox to Empty + document.getElementById('client_inputbox').value = ''; + } + /** * Handles an in-character chat message. * @param {*} args packet arguments */ handleMS(args) { + const msMessage = args[5] + const commands = { + '/save_chatlog': this.saveChatlogHandle + } + const commandsMap = new Map(Object.entries(commands)) + + if (msMessage && commandsMap.has(msMessage.toLowerCase())) { + try { + commandsMap.get(msMessage.toLowerCase())() + } catch (e) { + // Command Not Recognized + } + } // TODO: this if-statement might be a bug. - if (args[4] !== viewport.chatmsg.content) { + else if (args[4] !== viewport.chatmsg.content) { document.getElementById('client_inner_chat').innerHTML = ''; const char_id = Number(args[9]); @@ -699,8 +737,7 @@ class Client extends EventEmitter { if (chatmsg.charid === this.charID) { resetICParams(); } - - viewport.say(chatmsg); // no await + viewport.say(chatmsg); // no await } } } diff --git a/webAO/services/downloadFile.ts b/webAO/services/downloadFile.ts new file mode 100644 index 0000000..3774e9b --- /dev/null +++ b/webAO/services/downloadFile.ts @@ -0,0 +1,10 @@ +const downloadFile = (content: string, filename: string) => { + + const a = document.createElement('a'); + const file = new Blob([content], {type: 'text'}); + + a.href= URL.createObjectURL(file); + a.download = filename; + a.click(); +} +export default downloadFile \ No newline at end of file -- cgit From f7d7e96f3d36bc2bd21d58027a8610a75933eb6c Mon Sep 17 00:00:00 2001 From: Caleb Mabry Date: Fri, 25 Mar 2022 13:14:41 -0400 Subject: added unit tests --- webAO/services/__tests__/downloadFile.test.ts | 27 +++++++++++++++++++++++++++ webAO/services/downloadFile.ts | 2 -- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 webAO/services/__tests__/downloadFile.test.ts (limited to 'webAO') diff --git a/webAO/services/__tests__/downloadFile.test.ts b/webAO/services/__tests__/downloadFile.test.ts new file mode 100644 index 0000000..b217b2c --- /dev/null +++ b/webAO/services/__tests__/downloadFile.test.ts @@ -0,0 +1,27 @@ +import downloadFile from '../downloadFile' +jest + .useFakeTimers() + .setSystemTime(new Date('2020-01-01').getTime()); + + global.URL.createObjectURL = jest.fn(); +(window as any).global.Blob = function (content, options){return ({content, options})} + +describe('downloadFile', () => { + it('Creates an tag', () => { + const createElementSpy = jest.spyOn(document, 'createElement'); + downloadFile('hi', 'filename') + expect(createElementSpy).toBeCalled() + }) + it('Creates the blob with the correct data', () => { + const data = 'writingtestsishard' + global.URL.createObjectURL = jest.fn(() => data); + downloadFile(data, 'filename') + const expected = { + content: [data], + options: { + type: "text" + } + } + expect(global.URL.createObjectURL).toBeCalledWith(expected) + }) +}) \ No newline at end of file diff --git a/webAO/services/downloadFile.ts b/webAO/services/downloadFile.ts index 3774e9b..058075f 100644 --- a/webAO/services/downloadFile.ts +++ b/webAO/services/downloadFile.ts @@ -1,8 +1,6 @@ const downloadFile = (content: string, filename: string) => { - const a = document.createElement('a'); const file = new Blob([content], {type: 'text'}); - a.href= URL.createObjectURL(file); a.download = filename; a.click(); -- cgit From 3b697cd8dba78e840e9d85380bf4e8dab0d7c672 Mon Sep 17 00:00:00 2001 From: Caleb Mabry Date: Fri, 25 Mar 2022 13:56:27 -0400 Subject: Moving command check to OOC --- webAO/client.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) (limited to 'webAO') diff --git a/webAO/client.js b/webAO/client.js index 10153f6..9b11a49 100644 --- a/webAO/client.js +++ b/webAO/client.js @@ -232,7 +232,21 @@ class Client extends EventEmitter { setCookie('OOC_name', document.getElementById('OOC_name').value); const oocName = `${escapeChat(encodeChat(document.getElementById('OOC_name').value))}`; const oocMessage = `${escapeChat(encodeChat(message))}`; - this.sendServer(`CT#${oocName}#${oocMessage}#%`); + + const commands = { + '/save_chatlog': this.saveChatlogHandle + } + const commandsMap = new Map(Object.entries(commands)) + + if (oocMessage && commandsMap.has(oocMessage.toLowerCase())) { + try { + commandsMap.get(oocMessage.toLowerCase())() + } catch (e) { + // Command Not Recognized + } + } else { + this.sendServer(`CT#${oocName}#${oocMessage}#%`); + } } /** @@ -589,21 +603,9 @@ class Client extends EventEmitter { * @param {*} args packet arguments */ handleMS(args) { - const msMessage = args[5] - const commands = { - '/save_chatlog': this.saveChatlogHandle - } - const commandsMap = new Map(Object.entries(commands)) - if (msMessage && commandsMap.has(msMessage.toLowerCase())) { - try { - commandsMap.get(msMessage.toLowerCase())() - } catch (e) { - // Command Not Recognized - } - } // TODO: this if-statement might be a bug. - else if (args[4] !== viewport.chatmsg.content) { + if (args[4] !== viewport.chatmsg.content) { document.getElementById('client_inner_chat').innerHTML = ''; const char_id = Number(args[9]); @@ -754,6 +756,7 @@ class Client extends EventEmitter { oocLog.scrollTop = oocLog.scrollHeight; } } + } /** -- cgit