Skip to content

Commit

Permalink
Housekeeping: data formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Armored-Dragon committed Dec 20, 2024
1 parent 6dbd63e commit 27fa5da
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 54 deletions.
72 changes: 18 additions & 54 deletions scripts/system/domainChat/domainChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
(() => {
("use strict");

Script.include([
"./formatting.js"
])

var appIsVisible = false;
var settings = {
external_window: false,
Expand Down Expand Up @@ -93,47 +97,33 @@
// Is the message a chat message?
channel = channel.toLowerCase();
if (channel !== "chat") return;
message = JSON.parse(message);

// Get the message data
const currentTimestamp = _getTimestamp();
const timeArray = _formatTimestamp(currentTimestamp);


if ((message = formatting.toJSON(message)) == null) return; // Make sure we are working with a JSON object we expect, otherwise kill
message = formatting.addTimeAndDateStringToPacket(message);

if (!message.channel) message.channel = "domain"; // We don't know where to put this message. Assume it is a domain wide message.
message.channel = message.channel.toLowerCase(); // Only recognize channel names as lower case.

if (!channels.includes(message.channel)) return; // Check the channel. If the channel is not one we have, do nothing.
if (message.channel == "local" && isTooFar(message.position)) return; // If message is local, and if player is too far away from location, do nothing.

// Format the timestamp
message.timeString = timeArray[0];
message.dateString = timeArray[1];


let formattedMessagePacket = { ...message };
formattedMessagePacket.message = await _parseMessage(message.message)

_emitEvent({ type: "show_message", ...formattedMessagePacket }); // Update qml view of to new message.
_notificationCoreMessage(message.displayName, message.message) // Show a new message on screen.

// Create a new variable based on the message that will be saved.
let savedMessage = message;
let trimmedPacket = formatting.trimPacketToSave(message);
messageHistory.push(trimmedPacket);

// Remove unnecessary data.
delete savedMessage.position;
delete savedMessage.timeString;
delete savedMessage.dateString;
delete savedMessage.action;

savedMessage.timestamp = currentTimestamp;

messageHistory.push(savedMessage);
while (messageHistory.length > settings.maximum_messages) {
messageHistory.shift();
}
Settings.setValue("ArmoredChat-Messages", messageHistory);

// Check to see if the message is close enough to the user
function isTooFar(messagePosition) {
// Check to see if the message is close enough to the user
return Vec3.distance(MyAvatar.position, messagePosition) > maxLocalDistance;
}
}
Expand Down Expand Up @@ -218,10 +208,7 @@
}

// Format the packet
let message = {};
const timeArray = _formatTimestamp(_getTimestamp());
message.timeString = timeArray[0];
message.dateString = timeArray[1];
let message = addTimeAndDateStringToPacket({});
message.message = `${displayName} ${type}`;

// Show new message on screen
Expand All @@ -242,43 +229,20 @@
if (messageHistory) {
// Load message history
for (message of messageHistory) {
const timeArray = _formatTimestamp(_getTimestamp());
messagePacket = { ...message };
messagePacket.timeString = timeArray[0];
messagePacket.dateString = timeArray[1];
messagePacket = { ...message }; // Create new variable
messagePacket = formatting.addTimeAndDateStringToPacket(messagePacket); // Add timestamp
messagePacket.message = await _parseMessage(messagePacket.message); // Parse the message for the UI

let formattedMessagePacket = {...messagePacket};
formattedMessagePacket.message = await _parseMessage(messagePacket.message);

_emitEvent({ type: "show_message", ...formattedMessagePacket });
_emitEvent({ type: "show_message", ...messagePacket }); // Send message to UI
}
}

// Send current settings to the app
_emitEvent({ type: "initial_settings", settings: settings });
_emitEvent({ type: "initial_settings", settings: settings }); // Send current settings to the app
}
function _saveSettings() {
console.log("Saving config");
Settings.setValue("ArmoredChat-Config", settings);
}
function _getTimestamp(){
return Date.now();
}
function _formatTimestamp(timestamp){
let timeArray = [];

timeArray.push(new Date().toLocaleTimeString(undefined, {
hour12: false,
}));

timeArray.push(new Date(timestamp).toLocaleDateString(undefined, {
year: "numeric",
month: "long",
day: "numeric",
}));

return timeArray;
}
function _notificationCoreMessage(displayName, message){
Messages.sendLocalMessage(
"Floof-Notif",
Expand Down
64 changes: 64 additions & 0 deletions scripts/system/domainChat/formatting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// formatting.js
//
// Created by Armored Dragon, 2024.
// Copyright 2024 Overte e.V.
//
// This just does some basic formatting and minor housekeeping for the domainChat.js application
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html

const formatting = {
toJSON: function(data) {
if (typeof data == "object") return data; // Already JSON

try {
const parsedData = JSON.parse(data);
return parsedData;
} catch (e) {
console.log('Failed to convert data to JSON.')
return null; // Could not convert to json, some error;
}
},
addTimeAndDateStringToPacket: function(packet) {
// Gets the current time and adds it to a given packet
const timeArray = formatting.helpers._timestampArray(packet.timestamp);
packet.timeString = timeArray[0];
packet.dateString = timeArray[1];
return packet;
},
trimPacketToSave: function(packet) {
// Takes a packet, and returns a packet containing only what is needed to save.
let newPacket = {
channel: packet.channel || "",
displayName: packet.displayName || "",
message: packet.message || "",
timestamp: packet.timestamp || formatting.helpers.getTimestamp(),
};
return newPacket;
},

helpers: {
// Small functions that are used often in the other functions.
_timestampArray: function(timestamp) {
const currentDate = timestamp || formatting.helpers.getTimestamp();
let timeArray = [];

timeArray.push(new Date(currentDate).toLocaleTimeString(undefined, {
hour12: false,
}));

timeArray.push(new Date(currentDate).toLocaleDateString(undefined, {
year: "numeric",
month: "long",
day: "numeric",
}));

return timeArray;
},
getTimestamp: function(){
return Date.now();
}
}
}

0 comments on commit 27fa5da

Please sign in to comment.