Skip to content

Instantly share code, notes, and snippets.

@SplenectomY
Last active August 29, 2015 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SplenectomY/1f198a3114903d7a3271 to your computer and use it in GitHub Desktop.
Save SplenectomY/1f198a3114903d7a3271 to your computer and use it in GitHub Desktop.
Make Your Time: Persistent 24-hour clock and timer for Roll20.net
/////////////////////////////////////////////////
/***********************************************/
var MakeYourTime = {
author: {
name: "John C." || "Echo" || "SplenectomY",
company: "Team Asshat" || "The Alehounds",
contact: "echo@TeamAsshat.com",
},
version: "0.2",
gist: "https://gist.github.com/SplenectomY/1f198a3114903d7a3271",
forum: "https://app.roll20.net/forum/post/1386741/",
/***********************************************/
/////////////////////////////////////////////////
setMods: function setMods() {
if(state.alehounds.clock.hour < 10) {
var hour_mod = "0";
} else {
var hour_mod = "";
}
if(state.alehounds.clock.minute < 10) {
var min_mod = "0";
} else {
var min_mod = "";
}
if(state.alehounds.clock.second < 10) {
var sec_mod = "0";
} else {
var sec_mod = "";
}
return "/w gm Time: " + hour_mod + state.alehounds.clock.hour + ":" + min_mod + state.alehounds.clock.minute + ":" + sec_mod + state.alehounds.clock.second + ".";
},
checkError: function checkError(type) {
if (state.alehounds.clock.hour > 23 || state.alehounds.clock.hour < 0) {
if (type = "hour") {
state.alehounds.clock.hour = 0;
}
return "/w gm ERROR: The value must be an integer between 0 and 23";
} else if (state.alehounds.clock.minute > 59 || state.alehounds.clock.second > 59 || state.alehounds.clock.minute < 0 || state.alehounds.clock.second < 0) {
if (type = "minute") {
state.alehounds.clock.minute = 0;
}
else if (type = "second") {
state.alehounds.clock.second = 0;
}
return "/w gm ERROR: The value must be an integer between 0 and 59";
} else return "/w gm Value set.";
}
};
if (!_.has(state, 'alehounds')) {
state.alehounds = {
clock: {
hour: 0,
minute: 0,
second: 0
},
timers: []
};
}
on("chat:message", function(msg) {
var value;
// Begin timer functionality
if(msg.type == "api" && msg.content.indexOf("!timer") !== -1) {
timerArray = msg.content.split(" ");
var command = timerArray[timerArray.indexOf("!timer") + 1];
// Syntax: !timer set [name] (position) [value]
if (command == "set") {
if (timerArray.length == 5) {
position = timerArray[timerArray.indexOf("!timer") + 3]
value = timerArray[timerArray.indexOf("!timer") + 4]
} else {
position = state.alehounds.timers.length
value = String(timerArray[timerArray.indexOf("!timer") + 3])
}
valueArray = value.split(":")
state.alehounds.timers[position] = timerArray[2]
state.alehounds.timers[position] = {
value: (parseInt(valueArray[0]) * 3600) + (parseInt(valueArray[1]) * 60) + parseInt(valueArray[2]),
name: timerArray[2],
}
sendChat(msg.who, "/w gm Timer [" + position + ", " + timerArray[2] + "] set for " + valueArray[0] + ":" + valueArray[1] + ":" + valueArray[2])
// Syntax: !timer rem (position)
} else if (command == "rem") {
if (timerArray.length == 3) {
position = timerArray[timerArray.indexOf("!timer") + 2]
} else {
position = parseInt(state.alehounds.timers.length) - 1
}
sendChat(msg.who, "/w gm Timer [" + position + ", " + state.alehounds.timers[position].name + "] removed.")
state.alehounds.timers.splice(position,1);
}
}
//set the hour of day
if (msg.type == "api" && msg.content.indexOf("!ttsethour ") !== -1) {
state.alehounds.clock.hour = parseInt(msg.content.replace("!ttsethour ", ""));
sendChat(msg.who, MakeYourTime.checkError("hour"));
sendChat(msg.who, MakeYourTime.setMods());
}
//set the minute of the hour
if(msg.type == "api" && msg.content.indexOf("!ttsetmin ") !== -1) {
state.alehounds.clock.minute = parseInt(msg.content.replace("!ttsetmin ", ""));
sendChat(msg.who, MakeYourTime.checkError("minute"));
sendChat(msg.who, MakeYourTime.setMods());
}
//set the second of the minute
if (msg.type == "api" && msg.content.indexOf("!ttsetsec ") !== -1) {
state.alehounds.clock.second = parseInt(msg.content.replace("!ttsetsec ", ""));
sendChat(msg.who, MakeYourTime.checkError("second"));
sendChat(msg.who, MakeYourTime.setMods());
}
//Adjust the seconds by a value
if(msg.type == "api" && msg.content.indexOf("!ttaddsec ") !== -1) {
for (i = 0; i < state.alehounds.timers.length; i++) {
state.alehounds.timers[i].value = state.alehounds.timers[i].value - parseInt(msg.content.replace("!ttaddsec ", ""));
if (state.alehounds.timers[i].value > 0) {
state.alehounds.timers[i].hours = Math.floor(state.alehounds.timers[i].value / 3600);
state.alehounds.timers[i].valueNew = state.alehounds.timers[i].value - (state.alehounds.timers[i].hours * 3600);
state.alehounds.timers[i].mins = Math.floor(state.alehounds.timers[i].valueNew / 60);
state.alehounds.timers[i].valueNew = state.alehounds.timers[i].valueNew - (state.alehounds.timers[i].mins * 60);
if (state.alehounds.timers[i].hours == 0) {
state.alehounds.timers[i].hours = "00"
} else {
state.alehounds.timers[i].hours = String(state.alehounds.timers[i].hours / 100)
if (!state.alehounds.timers[i].hours.charAt(3)) {
state.alehounds.timers[i].hours = state.alehounds.timers[i].hours + "0"
}
}
if (state.alehounds.timers[i].mins == 0) {
state.alehounds.timers[i].mins = "00"
} else {
state.alehounds.timers[i].mins = String(state.alehounds.timers[i].mins / 100)
if (!state.alehounds.timers[i].mins.charAt(3)) {
state.alehounds.timers[i].mins = state.alehounds.timers[i].mins + "0"
}
}
state.alehounds.timers[i].valueNew = String(state.alehounds.timers[i].valueNew / 100);
if (!state.alehounds.timers[i].valueNew.charAt(3)) {
state.alehounds.timers[i].valueNew = state.alehounds.timers[i].valueNew + "0"
}
sendChat(msg.who, "/w gm [" + i + ", " + state.alehounds.timers[i].name + "] : " + state.alehounds.timers[i].hours.replace("0.","") + ":" + state.alehounds.timers[i].mins.replace("0.","") + ":" + state.alehounds.timers[i].valueNew.replace("0.","") + " remains.")
} else {
sendChat(msg.who, "/w gm [" + i + ", " + state.alehounds.timers[i].name + "] has ended!")
state.alehounds.timers.splice(i,1)
}
}
state.alehounds.clock.second = state.alehounds.clock.second + parseInt(msg.content.replace("!ttaddsec ", ""));
//If minutes or seconds are over 60, translate to hours or minutes and add the remainder
if (state.alehounds.clock.second > 59) {
state.alehounds.clock.minute = state.alehounds.clock.minute + Math.floor(state.alehounds.clock.second / 60)
state.alehounds.clock.second = state.alehounds.clock.second - (Math.floor(state.alehounds.clock.second / 60) * 60);
}
if (state.alehounds.clock.minute > 59) {
state.alehounds.clock.hour = state.alehounds.clock.hour + Math.floor(state.alehounds.clock.minute / 60);
state.alehounds.clock.minute = state.alehounds.clock.minute - (Math.floor(state.alehounds.clock.minute / 60) * 60);
}
//If hours are over 23, reset the hour timer and add the remainder
if (state.alehounds.clock.hour > 23) {
state.alehounds.clock.hour = state.alehounds.clock.hour - (Math.floor(state.alehounds.clock.hour / 24) * 24);
}
//Add pretty formatting for single digits
if(state.alehounds.clock.hour < 10) {
var hour_mod = "0";
}
if(state.alehounds.clock.minute < 10) {
var min_mod = "0";
}
if(state.alehounds.clock.second < 10) {
var sec_mod = "0";
}
if(state.alehounds.clock.hour > 9) {
var hour_mod = "";
}
if(state.alehounds.clock.minute > 9) {
var min_mod = "";
}
if(state.alehounds.clock.second > 9) {
var sec_mod = "";
}
sendChat(msg.who, "/w gm Time: " + hour_mod + state.alehounds.clock.hour + ":" + min_mod + state.alehounds.clock.minute + ":" + sec_mod + state.alehounds.clock.second + ".");
}
//Show the in-game time by using !time
if(msg.type == "api" && msg.content.indexOf("!tttime") !== -1) {
if(state.alehounds.clock.hour < 10) {
var hour_mod = "0"
}
if(state.alehounds.clock.minute < 10) {
var min_mod = "0"
}
if(state.alehounds.clock.second < 10) {
var sec_mod = "0"
}
if(state.alehounds.clock.hour > 9) {
var hour_mod = ""
}
if(state.alehounds.clock.minute > 9) {
var min_mod = ""
}
if(state.alehounds.clock.second > 9) {
var sec_mod = ""
}
sendChat(msg.who, "/w gm Time: " + hour_mod + state.alehounds.clock.hour + ":" + min_mod + state.alehounds.clock.minute + ":" + sec_mod + state.alehounds.clock.second + ".");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment