sloodle_mod_tracker-1.0.lslp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // This file is part of SLOODLE Tracker.
  4. // Copyright (c) 2009-11 Sloodle community (various contributors)
  5. // SLOODLE Tracker is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // SLOODLE Tracker is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License.
  14. // If not, see <http://www.gnu.org/licenses/>
  15. //
  16. // Contributors:
  17. // Peter R. Bloomfield
  18. // Julio Lopez (SL: Julio Solo)
  19. // Michael Callaghan (SL: HarmonyHill Allen)
  20. // Kerri McCusker (SL: Kerri Macchi)
  21. // A project developed by the Serious Games and Virtual Worlds Group.
  22. // Intelligent Systems Research Centre.
  23. // University of Ulster, Magee
  24. // IN-WORLD CONFIGURATION //
  25. string NAME = "Unnamed task";
  26. string DESCRIPTION = "-";
  27. integer PREDEFINED_ORDER = 0; // Does the task have to be done in a specific order? 0 -> NO, 1 -> YES
  28. integer POSITION = 0; // Position of the task in the pre-defined sequence
  29. integer TOTAL = 0; // Number of tasks in the assignment
  30. // END IN-WORLD CONFIGURATION //
  31. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  32. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  33. string SLOODLE_TRACKER_LINKER = "/mod/sloodle/mod/tracker-1.0/linker.php";
  34. string SLOODLE_TOOL_LINKER = "/mod/sloodle/mod/tracker-1.0/auth_tool_linker.php";
  35. string SLOODLE_EOF = "sloodleeof";
  36. integer CHANNEL = 447851; // Channel for the tasks to comunicate
  37. string SLOODLE_OBJECT_TYPE = "tracker-1.0";
  38. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  39. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  40. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  41. string sloodleserverroot = "";
  42. string sloodlepwd = "";
  43. integer sloodlecontrollerid = 0;
  44. integer sloodlemoduleid = 0;
  45. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  46. integer sloodleobjectaccesslevelctrl = 0; // Who can control this object?
  47. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  48. integer sloodleautodeactivate = 1; // Should the WebIntercom auto-deactivate when not in use?
  49. list allowed = []; // List of avatars who have completed the previous task
  50. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  51. integer eof = FALSE; // Have we reached the end of the configuration data?
  52. key httpinteraction = NULL_KEY; // Request used to send interaction
  53. key httpreg = NULL_KEY; // Request used to register Tracker tool
  54. ///// TRANSLATION /////
  55. // Link message channels
  56. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  57. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  58. // Translation output methods
  59. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  60. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  61. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  62. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  63. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  64. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  65. string SLOODLE_TRANSLATE_DIALOG = "dialog"; // Recipient avatar should be identified in link message keyval. At least 2 output parameters: first the channel number for the dialog, and then 1 to 12 button label strings.
  66. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  67. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  68. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  69. // Send a translation request link message
  70. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  71. {
  72. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  73. }
  74. ///// ----------- /////
  75. ///// FUNCTIONS /////
  76. // Has the current avatar completed the previous task?
  77. integer isAllowed(key k) {
  78. return (llListFindList(allowed,[k])>=0);
  79. }
  80. sloodle_debug(string msg)
  81. {
  82. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  83. }
  84. // Configure by receiving a linked message from another script in the object
  85. // Returns TRUE if the object has all the data it needs
  86. integer sloodle_handle_command(string str)
  87. {
  88. list bits = llParseString2List(str,["|"],[]);
  89. integer numbits = llGetListLength(bits);
  90. string name = llList2String(bits,0);
  91. string value1 = "";
  92. string value2 = "";
  93. if (numbits > 1) value1 = llList2String(bits,1);
  94. if (numbits > 2) value2 = llList2String(bits,2);
  95. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  96. else if (name == "set:sloodlepwd") {
  97. // The password may be a single prim password, or a UUID and a password
  98. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  99. else sloodlepwd = value1;
  100. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  101. else if (name == "set:sloodlemoduleid") sloodlemoduleid = (integer)value1;
  102. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  103. else if (name == "set:sloodleobjectaccesslevelctrl") sloodleobjectaccesslevelctrl = (integer)value1;
  104. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  105. else if (name == "set:sloodleautodeactivate") sloodleautodeactivate = (integer)value1;
  106. else if (name == SLOODLE_EOF) eof = TRUE;
  107. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0 && sloodlemoduleid > 0);
  108. }
  109. // Checks if the given agent is permitted to control this object
  110. // Returns TRUE if so, or FALSE if not
  111. integer sloodle_check_access_ctrl(key id)
  112. {
  113. // Check the access mode
  114. if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  115. return llSameGroup(id);
  116. } else if (sloodleobjectaccesslevelctrl == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  117. return TRUE;
  118. }
  119. // Assume it's owner mode
  120. return (id == llGetOwner());
  121. }
  122. // Checks if the given agent is permitted to user this object
  123. // Returns TRUE if so, or FALSE if not
  124. integer sloodle_check_access_use(key id)
  125. {
  126. // Check the access mode
  127. if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_GROUP) {
  128. return llSameGroup(id);
  129. } else if (sloodleobjectaccessleveluse == SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC) {
  130. return TRUE;
  131. }
  132. // Assume it's owner mode
  133. return (id == llGetOwner());
  134. }
  135. // Default state - waiting for configuration
  136. default
  137. {
  138. state_entry()
  139. {
  140. // Starting again with a new configuration
  141. llSetText("", <0.0,0.0,0.0>, 0.0);
  142. isconfigured = FALSE;
  143. eof = FALSE;
  144. allowed = [];
  145. // Reset our data
  146. sloodleserverroot = "";
  147. sloodlepwd = "";
  148. sloodlecontrollerid = 0;
  149. sloodlemoduleid = 0;
  150. sloodleobjectaccessleveluse = 0;
  151. sloodleobjectaccesslevelctrl = 0;
  152. sloodleserveraccesslevel = 0;
  153. }
  154. link_message( integer sender_num, integer num, string str, key id)
  155. {
  156. // Check the channel
  157. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  158. // Split the message into lines
  159. list lines = llParseString2List(str, ["\n"], []);
  160. integer numlines = llGetListLength(lines);
  161. integer i = 0;
  162. for (i = 0; i < numlines; i++)
  163. {
  164. isconfigured = sloodle_handle_command(llList2String(lines, i));
  165. }
  166. // If we've got all our data AND reached the end of the configuration data, then move on
  167. if (eof == TRUE) {
  168. if (isconfigured == TRUE) {
  169. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  170. llSleep(2.0);
  171. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  172. body += "&sloodlepwd=" + sloodlepwd;
  173. body += "&sloodleobjuuid="+(string)llGetKey();
  174. body += "&sloodleobjname="+llGetObjectName();
  175. body += "&sloodleobjtype="+SLOODLE_OBJECT_TYPE;
  176. body += "&sloodlemoduleid="+(string)sloodlemoduleid;
  177. body += "&sloodledescription="+DESCRIPTION;
  178. body += "&sloodletaskname="+NAME;
  179. httpreg = llHTTPRequest(sloodleserverroot + SLOODLE_TOOL_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  180. state ready;
  181. } else {
  182. // Go all configuration but, it's not complete... request reconfiguration
  183. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  184. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  185. eof = FALSE;
  186. }
  187. }
  188. }
  189. }
  190. touch_start(integer num_detected)
  191. {
  192. // Attempt to request a reconfiguration
  193. if (llDetectedKey(0) == llGetOwner()) {
  194. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  195. }
  196. }
  197. }
  198. state ready
  199. {
  200. state_entry()
  201. {
  202. llListen(CHANNEL, "", NULL_KEY, "");
  203. }
  204. listen(integer channel, string name, key id, string msg)
  205. {
  206. // Is the message in the correct channel?
  207. if (channel == CHANNEL)
  208. {
  209. // Ignore anything from a different owner
  210. if (llGetOwnerKey(id) != llGetOwner()) return;
  211. // Get the different parts of the message
  212. list elements = llParseStringKeepNulls(msg,["|"],[]);
  213. if (llGetListLength(elements) < 3) return;
  214. // Ignore anything attached to a different module ID
  215. integer incomingModuleID = (integer)llList2String(elements, 2);
  216. if (incomingModuleID != sloodlemoduleid) return;
  217. // If the message cames from the previous task, insert the avatar UUID in the list so we know they are allowed to continue
  218. if ((integer)llList2String(elements,0) == (POSITION-1))
  219. {
  220. allowed += [llList2Key(elements,1)];
  221. }
  222. }
  223. }
  224. link_message(integer sender_num, integer num, string sval, key kval)
  225. {
  226. //llOwnerSay("link message (" + (string)num + "):\n" + sval + "\nKey: " + (string)kval);
  227. // Was this a tracker message?
  228. if (num == CHANNEL)
  229. {
  230. // Another script is informing us that an interaction has taken place
  231. // e.g. a button click or a scanner
  232. // Extract incoming data
  233. key id_avatar = kval;
  234. string name_avatar = llKey2Name(id_avatar);
  235. list fields = llParseStringKeepNulls(sval, ["|"], []);
  236. integer numfields = llGetListLength(fields);
  237. // Make sure it was an incoming interaction message
  238. if (llList2String(fields, 0) != "INTERACTION") return;
  239. string type = llList2String(fields, 1);
  240. integer suppressOrderWarning = FALSE;
  241. if (numfields >= 3)
  242. {
  243. suppressOrderWarning = ((integer)llList2String(fields, 2)) != 0;
  244. }
  245. // If there is a predefined order and the avatar has not completed the previous task
  246. // he can't complete the current task
  247. if (PREDEFINED_ORDER && suppressOrderWarning == FALSE && !(isAllowed(id_avatar)) && (POSITION > 2))
  248. {
  249. llSay(0, name_avatar + ", please make sure you have completed the previous task, or make sure you have not completed this task already.");
  250. } else {
  251. // We can remove this person from the allowed list
  252. integer pos = llListFindList(allowed, [id_avatar]);
  253. if (pos >= 0) allowed = llDeleteSubList(allowed, pos, pos);
  254. // If the current task is not the last one, the next task is notified
  255. if (POSITION < TOTAL)
  256. {
  257. string msg = (string)POSITION;
  258. msg += "|"+(string)id_avatar+"|"+(string)sloodlemoduleid;
  259. llSay(CHANNEL, msg);
  260. }
  261. // Send a response message back to the tool to indicate that the interaction was processed
  262. llMessageLinked(LINK_THIS, CHANNEL, "INTERACTION_RESPONSE", id_avatar);
  263. // Notify Moodle of the interaction
  264. key id_object = llGetKey();
  265. string body = "sloodlemoduleid=" + (string)sloodlemoduleid;
  266. body += "&sloodlecontrollerid=" + (string)sloodlecontrollerid;
  267. body += "&sloodlepwd=" + (string)sloodlepwd;
  268. body += "&sloodleobjuuid=" + (string) id_object;
  269. body += "&sloodleuuid=" + (string) id_avatar;
  270. body += "&sloodleavname=" + name_avatar;
  271. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  272. httpinteraction = llHTTPRequest(sloodleserverroot + SLOODLE_TRACKER_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  273. llSleep(1.0);
  274. }
  275. }
  276. }
  277. }
  278. // Please leave the following line intact to show where the script lives in Git:
  279. // SLOODLE LSL Script Git Location: mod/tracker-1.0/objects/default/assets/sloodle_mod_tracker-1.0.lslp