81818367-8525-f04d-efb9-b722be7076cb_script.lsl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // The line above should be left blank to avoid script errors in OpenSim.
  3. // Sloodle ZZText Scoreboard
  4. //
  5. // Part of the Sloodle project (www.sloodle.org)
  6. //
  7. // Copyright (c) 2011-06 contributors (see below)
  8. // Released under the GNU GPL v3
  9. //
  10. // Contributors:
  11. // Edmund Edgar
  12. // Paul Preibisch
  13. //
  14. integer SLOODLE_CHANNEL_SCOREBOARD_UPDATE_COMPLETE = 1639271140;
  15. integer SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST=-1828374651; // this channel is used to send status codes for translation to the error_messages lsl script
  16. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;
  17. integer SLOODLE_CHANNEL_AVATAR_DIALOG = 1001;
  18. integer SLOODLE_AWARDS_POINTS_CHANGE_NOTIFICATION= 10601;
  19. integer SLOODLE_CHANNEL_SCOREBOARD_SHARED_MEDIA_SET_ADMIN_URL_CHANNEL= -1639271128; // This is the channel that the scoreboard shouts out its admin URL
  20. integer SLOODLE_CHANNEL_SCOREBOARD_SHARED_MEDIA_CHANGE_ADMIN_URL_CHANNEL= -1639271129; // This is the channel that the scoreboard shouts out its admin URL WHEN It has changed due to a region event (lost its url etc)
  21. integer SLOODLE_SCOREBOARD_CONNECT_HUD= -1639271130; // channel which gets sent a linked message by the connect a hud button when it is touched.
  22. string SLOODLE_SCOREBOARD_LINKER = "/mod/sloodle/mod/scoreboard-1.0/linker.php";
  23. string SLOODLE_EOF = "sloodleeof";
  24. integer SLOODLE_SCOREBOARD_OPEN_IN_BROWSER= -1639277000;
  25. string SLOODLE_OBJECT_TYPE = "scoreboard-1.0";
  26. integer SLOODLE_OBJECT_ACCESS_LEVEL_PUBLIC = 0;
  27. integer SLOODLE_OBJECT_ACCESS_LEVEL_OWNER = 1;
  28. integer SLOODLE_OBJECT_ACCESS_LEVEL_GROUP = 2;
  29. key httpscoreboard = NULL_KEY; // Request used to receive scores
  30. string sloodleserverroot = "";
  31. string sloodlepwd = "";
  32. integer sloodlecontrollerid = 0;
  33. integer sloodlemoduleid = 0;
  34. integer sloodlelistentoobjects = 0; // Should this object listen to other objects?
  35. integer sloodleobjectaccessleveluse = 0; // Who can use this object?
  36. integer sloodleobjectaccesslevelctrl = 0; // Who can control this object?
  37. integer sloodleserveraccesslevel = 0; // Who can use the server resource? (Value passed straight back to Moodle)
  38. integer sloodlegroupid = 0;
  39. integer sloodleshowallcontrollers = 0;
  40. integer sloodleroundid = 0;
  41. integer sloodlerefreshtime = 0;
  42. integer isconfigured = FALSE; // Do we have all the configuration data we need?
  43. integer eof = FALSE; // Have we reached the end of the configuration data?
  44. integer lastmessagetimestamp = 0;
  45. integer messagetimestamp = 0;
  46. ///// TRANSLATION /////
  47. // Link message channels
  48. integer SLOODLE_CHANNEL_TRANSLATION_REQUEST = -1928374651;
  49. integer SLOODLE_CHANNEL_TRANSLATION_RESPONSE = -1928374652;
  50. // Translation output methods
  51. string SLOODLE_TRANSLATE_LINK = "link"; // No output parameters - simply returns the translation on SLOODLE_TRANSLATION_RESPONSE link message channel
  52. string SLOODLE_TRANSLATE_SAY = "say"; // 1 output parameter: chat channel number
  53. string SLOODLE_TRANSLATE_WHISPER = "whisper"; // 1 output parameter: chat channel number
  54. string SLOODLE_TRANSLATE_SHOUT = "shout"; // 1 output parameter: chat channel number
  55. string SLOODLE_TRANSLATE_REGION_SAY = "regionsay"; // 1 output parameter: chat channel number
  56. string SLOODLE_TRANSLATE_OWNER_SAY = "ownersay"; // No output parameters
  57. 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.
  58. string SLOODLE_TRANSLATE_LOAD_URL = "loadurl"; // Recipient avatar should be identified in link message keyval. 1 output parameter giving URL to load.
  59. string SLOODLE_TRANSLATE_HOVER_TEXT = "hovertext"; // 2 output parameters: colour <r,g,b>, and alpha value
  60. string SLOODLE_TRANSLATE_IM = "instantmessage"; // Recipient avatar should be identified in link message keyval. No output parameters.
  61. integer MENU_CHANNEL; //random channel used for dialogs
  62. integer listenHandle;
  63. string paramstr;
  64. string view_url;
  65. string admin_url;
  66. // Send a translation request link message
  67. sloodle_translation_request(string output_method, list output_params, string string_name, list string_params, key keyval, string batch)
  68. {
  69. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_TRANSLATION_REQUEST, output_method + "|" + llList2CSV(output_params) + "|" + string_name + "|" + llList2CSV(string_params) + "|" + batch, keyval);
  70. }
  71. ///// ----------- /////
  72. ///// FUNCTIONS /////
  73. integer randInt(integer n)
  74. {
  75. return (integer)llFrand(n + 1);
  76. }
  77. integer randIntBetween(integer min, integer max)
  78. {
  79. return min + randInt(max - min);
  80. }
  81. sloodle_error_code(string method, key avuuid,integer statuscode)
  82. {
  83. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_ERROR_TRANSLATION_REQUEST, method+"|"+(string)avuuid+"|"+(string)statuscode, NULL_KEY);
  84. }
  85. sloodle_debug(string msg)
  86. {
  87. llMessageLinked(LINK_THIS, DEBUG_CHANNEL, msg, NULL_KEY);
  88. }
  89. debug (string message){
  90. list params = llGetPrimitiveParams ([PRIM_MATERIAL ]);
  91. if (llList2Integer (params ,0)==PRIM_MATERIAL_FLESH){
  92. llOwnerSay(llGetObjectName()+"."+llGetScriptName()+": "+message);
  93. }
  94. }
  95. requestScores(){
  96. paramstr = "&sloodleobjuuid=" + (string)llGetKey();
  97. // Inform the Moodle chatroom
  98. string body = "sloodlecontrollerid=" + (string)sloodlecontrollerid;
  99. body += "&sloodlepwd=" + sloodlepwd;
  100. body += "&sloodlemoduleid=" + (string)sloodlemoduleid;
  101. body += "&sloodleuuid=" + (string)llGetKey();
  102. body += "&sloodleavname=" + llEscapeURL(llGetObjectName());
  103. body += "&sloodleserveraccesslevel=" + (string)sloodleserveraccesslevel;
  104. body += "&sloodleisobject=true";
  105. body += "&format=paddedtext";
  106. httpscoreboard = llHTTPRequest(sloodleserverroot + SLOODLE_SCOREBOARD_LINKER, [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], body);
  107. }
  108. // Configure by receiving a linked message from another script in the object
  109. // Returns TRUE if the object has all the data it needs
  110. integer sloodle_handle_command(string str)
  111. {
  112. // llOwnerSay(str);
  113. list bits = llParseString2List(str,["|"],[]);
  114. integer numbits = llGetListLength(bits);
  115. string name = llList2String(bits,0);
  116. string value1 = "";
  117. string value2 = "";
  118. if (numbits > 1) value1 = llList2String(bits,1);
  119. if (numbits > 2) value2 = llList2String(bits,2);
  120. if (name == "set:sloodleserverroot") sloodleserverroot = value1;
  121. else if (name == "set:sloodlepwd") {
  122. // The password may be a single prim password, or a UUID and a password
  123. if (value2 != "") sloodlepwd = value1 + "|" + value2;
  124. else sloodlepwd = value1;
  125. } else if (name == "set:sloodlecontrollerid") sloodlecontrollerid = (integer)value1;
  126. else if (name == "set:sloodleobjectaccessleveluse") sloodleobjectaccessleveluse = (integer)value1;
  127. else if (name == "set:sloodleserveraccesslevel") sloodleserveraccesslevel = (integer)value1;
  128. else if (name == "set:sloodleobjectaccesslevelctrl") sloodleobjectaccesslevelctrl = (integer)value1;
  129. else if (name == "set:sloodlerefreshtime") sloodlerefreshtime = (integer)value1;
  130. else if (name == "set:sloodleshowallcontrollers") sloodleshowallcontrollers = (integer)value1;
  131. else if (name == "set:sloodlegroupid") sloodlegroupid = (integer)value1;
  132. else if (name == "set:sloodleroundid") sloodleroundid = (integer)value1;
  133. else if (name == SLOODLE_EOF) eof = TRUE;
  134. return (sloodleserverroot != "" && sloodlepwd != "" && sloodlecontrollerid > 0);
  135. }
  136. // Default state - waiting for configuration
  137. integer face=4;
  138. default
  139. {
  140. on_rez( integer param)
  141. {
  142. llResetScript();
  143. }
  144. state_entry()
  145. {
  146. // Starting again with a new configuration
  147. isconfigured = FALSE;
  148. eof = FALSE;
  149. // Reset our data
  150. sloodleserverroot = "";
  151. sloodlepwd = "";
  152. sloodlecontrollerid = 0;
  153. sloodlemoduleid = 0;
  154. sloodleobjectaccessleveluse = 0;
  155. sloodleobjectaccesslevelctrl = 0;
  156. sloodlegroupid = 0;
  157. sloodleshowallcontrollers = 0;
  158. sloodleroundid = 0;
  159. sloodlerefreshtime = 0;
  160. }
  161. link_message( integer sender_num, integer num, string str, key id)
  162. {
  163. // Check the channel
  164. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  165. // Split the message into lines
  166. list lines = llParseString2List(str, ["\n"], []);
  167. integer numlines = llGetListLength(lines);
  168. integer i = 0;
  169. for (i=0; i < numlines; i++) {
  170. isconfigured = sloodle_handle_command(llList2String(lines, i));
  171. }
  172. // If we've got all our data AND reached the end of the configuration data, then move on
  173. if (eof == TRUE) {
  174. if (isconfigured == TRUE) {
  175. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  176. state ready;
  177. } else {
  178. // Go all configuration but, it's not complete... request reconfiguration
  179. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  180. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  181. eof = FALSE;
  182. }
  183. }
  184. }
  185. }
  186. touch_start(integer num_detected)
  187. {
  188. // Attempt to request a reconfiguration
  189. if (llDetectedKey(0) == llGetOwner()) {
  190. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:requestconfig", NULL_KEY);
  191. }
  192. }
  193. }
  194. state ready
  195. {
  196. on_rez( integer param)
  197. {
  198. llResetScript();
  199. }
  200. state_entry()
  201. {
  202. requestScores();
  203. }
  204. http_response(key id, integer status, list meta, string body){
  205. // Is this the expected data?
  206. if (id != httpscoreboard) return;
  207. httpscoreboard = NULL_KEY;
  208. // Make sure the request worked
  209. if (status != 200) {
  210. sloodle_debug("Failed HTTP response. Status: " + (string)status);
  211. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,status); //send message to error_message.lsl
  212. return;
  213. }
  214. // Make sure there is a body to the request
  215. if (llStringLength(body) == 0) return;
  216. // Debug output:
  217. sloodle_debug("Receiving chat data:\n" + body);
  218. // Split the data up into lines
  219. list lines = llParseStringKeepNulls(body, ["\n"], []);
  220. integer numlines = llGetListLength(lines);
  221. // Extract all the status fields
  222. list statusfields = llParseStringKeepNulls( llList2String(lines,0), ["|"], [] );
  223. // Get the statuscode
  224. integer statuscode = llList2Integer(statusfields,0);
  225. // Was it an error code?
  226. if (statuscode <= 0) {
  227. sloodle_error_code(SLOODLE_TRANSLATE_SAY, NULL_KEY,statuscode); //send message to error_message.lsl
  228. // Do we have an error message to go with it?
  229. string msg = "ERROR: linker script responded with status code " + (string)statuscode;
  230. if (numlines > 1) {
  231. msg += "\n" + llList2String(lines,1);
  232. }
  233. sloodle_debug(msg);
  234. return;
  235. }
  236. llMessageLinked(LINK_SET, SLOODLE_CHANNEL_SCOREBOARD_UPDATE_COMPLETE, body, NULL_KEY);
  237. }
  238. state_exit()
  239. {
  240. llSetTimerEvent(0.0);
  241. }
  242. link_message( integer sender_num, integer num, string str, key id)
  243. {
  244. debug("linked message: "+str);
  245. // Check the channel
  246. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  247. // Split the message into lines
  248. list lines = llParseString2List(str, ["\n"], []);
  249. integer numlines = llGetListLength(lines);
  250. integer i = 0;
  251. for (i=0; i < numlines; i++) {
  252. isconfigured = sloodle_handle_command(llList2String(lines, i));
  253. }
  254. if (eof == TRUE) {
  255. if (isconfigured == TRUE) {
  256. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configurationreceived", [], NULL_KEY, "");
  257. admin_url = sloodleserverroot+"/mod/sloodle/mod/scoreboard-1.0/shared_media/index.php?" + paramstr + "&mode=admin";
  258. requestScores();
  259. } else {
  260. // Go all configuration but, it's not complete... request reconfiguration
  261. sloodle_translation_request(SLOODLE_TRANSLATE_SAY, [0], "configdatamissing", [], NULL_KEY, "");
  262. llMessageLinked(LINK_THIS, SLOODLE_CHANNEL_OBJECT_DIALOG, "do:reconfigure", NULL_KEY);
  263. eof = FALSE;
  264. }
  265. }
  266. } else if (num == SLOODLE_CHANNEL_SCOREBOARD_UPDATE_COMPLETE) { // Awards points change notification
  267. // Nothing to do here: The sloodle_rezzer_object gets this and sends it directly to the scoreboard.
  268. } else if (num == SLOODLE_SCOREBOARD_OPEN_IN_BROWSER){
  269. llOwnerSay("You can access the scoreboard administration screen by going to: "+(string)admin_url);
  270. llLoadURL(llGetOwner(),"You can load your scoreboard administration screen by going to the following url:",(string)admin_url);
  271. } else if (num == SLOODLE_SCOREBOARD_CONNECT_HUD){
  272. llOwnerSay("Contacting owner's HUD");
  273. llRegionSay(SLOODLE_CHANNEL_SCOREBOARD_SHARED_MEDIA_SET_ADMIN_URL_CHANNEL, (string)admin_url+"|"+(string)id+"|"+(string)llGetKey());
  274. llShout(SLOODLE_CHANNEL_SCOREBOARD_SHARED_MEDIA_SET_ADMIN_URL_CHANNEL,(string)admin_url+"|"+(string)id+"|"+(string)llGetKey());
  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/scoreboard-1.0/objects/default/assets/zztext_scoreboard-1.0.lslp