Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new API Script #1932

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions Related Token Movement/0.1/Related Token Movement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const decodeEditorText = (t, o) =>{
let w = t;
o = Object.assign({ separator: '\r\n', asArray: false },o);
if(/^%3Cp%3E/.test(w)){
w = unescape(w);
}
if(/^<p>/.test(w)){
let lines = w.match(/<p>.*?<\/p>/g)
.map( l => l.replace(/^<p>(.*?)<\/p>$/,'$1'));
return o.asArray ? lines : lines.join(o.separator);
}
return t;
};

on("chat:message", function(msg) {
if (msg.type == "api" && msg.content.indexOf("!rtm create ") !== -1) {
if (!msg.selected) {
sendChat('RTM', "/w " + msg.who + " ERROR: Token not selected")
} else {
let i = 0;
while (i < msg.selected.length) {
obj = getObj(msg.selected[i]._type, msg.selected[i]._id);
var rtm_id = getRTMId(obj)[0]
var name = msg.content.replace("!rtm create ", "")
if ( rtm_id === false ) {
obj.set('gmnotes', "Related_Token_Movement/" + name + "/Related_Token_Movement" + "<br>" + obj.get('gmnotes'));
} else {
if ( getRTMId(obj)[1] === true ) {
obj.set('gmnotes', obj.get('gmnotes').replace("Related_Token_Movement/" + rtm_id + "/Related_Token_Movement", "Related_Token_Movement/" + name + "/Related_Token_Movement"));
} else if ( getRTMId(obj)[1] === false ) {
obj.set('gmnotes', obj.get('gmnotes').replace("Related_Token_Movement_Off/" + rtm_id + "/Related_Token_Movement_Off", "Related_Token_Movement_Off/" + name + "/Related_Token_Movement_Off"));
}
}
i++;
}
sendChat('RTM', "/w " + msg.who + " New ID: \"" + name + "\". Number of modified tokens: " + msg.selected.length)
}
} else if (msg.type == "api" && msg.content.indexOf("!rtm switch") !== -1) {
if (!msg.selected) {
sendChat('RTM', "/w " + msg.who + " ERROR: Token not selected")
} else {
let i = 0;
while (i < msg.selected.length) {
obj = getObj(msg.selected[i]._type, msg.selected[i]._id);
var rtm_state = getRTMId(obj)[1]
if ( rtm_state === true ) {
obj.set('gmnotes', obj.get('gmnotes').replace("Related_Token_Movement/", "Related_Token_Movement_Off/"));
sendChat('RTM', "/w " + msg.who + " " + obj.get('name') + " new RTM state is \"Off\"")
} else if ( rtm_state === false ) {
obj.set('gmnotes', obj.get('gmnotes').replace("Related_Token_Movement_Off/", "Related_Token_Movement/"));
sendChat('RTM', "/w " + msg.who + " " + obj.get('name') + " new RTM state is \"On\"")
}
i++;
}
}
} else if (msg.type == "api" && msg.content.indexOf("!rtm delete") !== -1) {
if (!msg.selected) {
sendChat('RTM', "/w " + msg.who + " ERROR: Token not selected")
} else {
let i = 0;
while (i < msg.selected.length) {
obj = getObj(msg.selected[i]._type, msg.selected[i]._id);
var rtm_state = getRTMId(obj)[1]
if ( rtm_state === true ) {
obj.set('gmnotes', obj.get('gmnotes').slice( 0, obj.get('gmnotes').indexOf("Related_Token_Movement/") ) + obj.get('gmnotes').slice( obj.get('gmnotes').indexOf("/Related_Token_Movement") + 27 ) );
} else if ( rtm_state === false ) {
obj.set('gmnotes', obj.get('gmnotes').slice( 0, obj.get('gmnotes').indexOf("Related_Token_Movement_Off/") ) + obj.get('gmnotes').slice( obj.get('gmnotes').indexOf("/Related_Token_Movement") + 27 ) );
}
sendChat('RTM', "/w " + msg.who + " " + obj.get('name') + " RTM ID deleted")
i++;
}
}
}
});

function getRTMId(obj) {
const text = decodeEditorText(obj.get('gmnotes'))

if ( text.includes("Related_Token_Movement_Off") ) {
var rtm_id = text.slice( text.indexOf("Related_Token_Movement_Off") + 25 )
rtm_id = rtm_id.slice( 0, rtm_id.indexOf("/Related_Token_Movement") )
return [rtm_id, false];
} else if ( text.includes("Related_Token_Movement") ) {
var rtm_id = text.slice( text.indexOf("Related_Token_Movement") + 23 )
rtm_id = rtm_id.slice( 0, rtm_id.indexOf("/Related_Token_Movement") )
return [rtm_id, true];
} else {
return [false, "none"];
}
}

on("change:graphic", function(obj, prev) {
var rtm_id = getRTMId(obj)[0]

if ( rtm_id !== false && getRTMId(obj)[1]) {
var token_list = findObjs({ subtype: 'token' });

token_list.forEach(element => {
var element_id = getRTMId(element)[0]
if ( element.get("id") !== obj.get("id") && element_id !== false && getRTMId(element)[1] && element_id === rtm_id ) {
element.set({
left: element.get("left") + obj.get("left") - prev["left"],
top: element.get("top") + obj.get("top") - prev["top"],
rotation: element.get("rotation") + obj.get("rotation") - prev["rotation"]
});
}
});
}
});
8 changes: 8 additions & 0 deletions Related Token Movement/script.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "Related Token Movement",
"script": "Related Token Movement.js",
"version": "0.1",
"description": "Related Token Movement - is a simple script that allows linking up the movement of several tokens with each other.\r\rInstructions:\r!rtm create ID - creates a link between selected token and the token with specified ID. Before exectuing command, token must be selected (multiple selections are supported). ID must not contain spaces and HTML elements.\r!rtm switch - alters the state of the selected token's link, allowing to ignore its movement and movement of other tokens on it. Before exectuing command, token must be selected (multiple selections are supported).\r!rtm delete - removes the links of the selected token. Before exectuing command, token must be selected (multiple selections are supported).\r\rLink between tokens is maintained through the use of GMNotes of the tokens. Entries contained there are saved with respective formatting.\r\r\r(RU) Related Token Movement - это простой скрипт, позволяющий связать движение нескольких токенов между собой.\r\rКоманды:\r!rtm create ID - создаёт связь между выбранным токеном и другими, имеющими такой же ID. Перед вводом токен должен быть выбран (поддерживается мноественный выбор). ID не должен содержать пробелы и элементы HTML.\r!rtm switch - переключает состояние связи токена, позволяя, при необходимости, игнорировать его передвижение и влияние передвижения других связанных токенов на него. Перед вводом токен должен быть выбран (поддерживается мноественный выбор).\r!rtm delete - удаляет связь токена с другими.Перед вводом токен должен быть выбран (поддерживается мноественный выбор).\r\rСвязь между токенами поддерживается через GMNotes токенов. Записи, находящиеся там, сохраняются вместе с форматированием.",
"authors": "nPocTo_4eJI",
"roll20userid": "6453048"
}
Loading