Skip to content

Commit

Permalink
Merge pull request #1241 from Armored-Dragon/fix/ac_timestamps
Browse files Browse the repository at this point in the history
Change ArmoredChat timestamps to use real timestamps
  • Loading branch information
ksuprynowicz authored Nov 28, 2024
2 parents eefeaf3 + bb91e87 commit a59047d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 24 deletions.
72 changes: 52 additions & 20 deletions scripts/communityScripts/armored-chat/armored_chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,26 @@
if (channel !== "chat") return;
message = JSON.parse(message);

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

if (!message.channel) message.channel = "domain"; // We don't know where to put this message. Assume it is a domain wide message.
if (message.forApp) return; // Floofchat

// Floofchat compatibility hook
message = floofChatCompatibilityConversion(message);
message.channel = message.channel.toLowerCase(); // Make sure the "local", "domain", etc. is formatted consistently
message.channel = message.channel.toLowerCase();

// Check the channel. If the channel is not one we have, do nothing.
if (!channels.includes(message.channel)) return;

// If message is local, and if player is too far away from location, do nothing.
if (message.channel == "local" && isTooFar(message.position)) return;

if (!channels.includes(message.channel)) return; // Check the channel
if (
message.channel == "local" &&
Vec3.distance(MyAvatar.position, message.position) >
maxLocalDistance
)
return; // If message is local, and if player is too far away from location, don't do anything
// Format the timestamp
message.timeString = timeArray[0];
message.dateString = timeArray[1];

// Update qml view of to new message
_emitEvent({ type: "show_message", ...message });
Expand All @@ -127,20 +133,25 @@

// Save message to history
let savedMessage = message;

// Remove unnecessary data.
delete savedMessage.position;
savedMessage.timeString = new Date().toLocaleTimeString(undefined, {
hour12: false,
});
savedMessage.dateString = new Date().toLocaleDateString(undefined, {
year: "numeric",
month: "long",
day: "numeric",
});
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) {
return Vec3.distance(MyAvatar.position, messagePosition) > maxLocalDistance;
}
}
function fromQML(event) {
switch (event.type) {
Expand Down Expand Up @@ -215,9 +226,7 @@

// Get the display name of the user
let displayName = "";
displayName =
AvatarManager.getPalData([sessionId])?.data[0]
?.sessionDisplayName || null;
displayName = AvatarManager.getPalData([sessionId])?.data[0]?.sessionDisplayName || null;
if (displayName == null) {
for (let i = 0; i < palData.length; i++) {
if (palData[i].sessionUUID == sessionId) {
Expand All @@ -228,6 +237,9 @@

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

// Show new message on screen
Expand All @@ -250,7 +262,9 @@
if (messageHistory) {
// Load message history
messageHistory.forEach((message) => {
delete message.action;
const timeArray = _formatTimestamp(_getTimestamp());
message.timeString = timeArray[0];
message.dateString = timeArray[1];
_emitEvent({ type: "show_message", ...message });
});
}
Expand All @@ -262,6 +276,24 @@
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;
}

/**
* Emit a packet to the HTML front end. Easy communication!
Expand Down
6 changes: 2 additions & 4 deletions scripts/communityScripts/armored-chat/armored_chat.qml
Original file line number Diff line number Diff line change
Expand Up @@ -587,15 +587,13 @@ Rectangle {

// Messages from script
function fromScript(message) {
let time = new Date().toLocaleTimeString(undefined, { hour12: false });
let date = new Date().toLocaleDateString(undefined, { year: "numeric", month: "long", day: "numeric", });

switch (message.type){
case "show_message":
addMessage(message.displayName, message.message, `[ ${message.timeString || time} - ${message.dateString || date} ]`, message.channel, "chat");
addMessage(message.displayName, message.message, `[ ${message.timeString} - ${message.dateString} ]`, message.channel, "chat");
break;
case "notification":
addMessage("SYSTEM", message.message, `[ ${time} - ${date} ]`, "domain", "notification");
addMessage("SYSTEM", message.message, `[ ${message.timeString} - ${message.dateString} ]`, "domain", "notification");
break;
case "clear_messages":
local.clear();
Expand Down

0 comments on commit a59047d

Please sign in to comment.