sloodle_quiz_router.lslp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * sloodle_quiz_router.lsl
  3. * Part of the Sloodle project (www.sloodle.org)
  4. *
  5. * Copyright (c) 2011-06 contributors (see below)
  6. * Released under the GNU GPL v3
  7. * -------------------------------------------
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * All scripts must maintain this copyrite information, including the contributer information listed
  23. *
  24. * Contributors:
  25. * Paul Preibisch
  26. *
  27. * DESCRIPTION
  28. * This script will send messages to waiting quizServers. When a player enters the game, this router
  29. * sends a message to all listening quizServers asking if anyone is available.
  30. * The quiz servers will report if they are AVAILABLE or BUSY.
  31. * The router will select an available quizServer and then send that quizServer the id of the player.
  32. *
  33. * Contributors:
  34. * Edmund Edgar
  35. * Paul Preibisch
  36. */
  37. string SLOODLE_EOF = "sloodleeof";
  38. integer eof = FALSE; // Have we reached the end of the configuration data?
  39. integer SLOODLE_CHANNEL_OBJECT_DIALOG = -3857343;//configuration channel
  40. integer isconfigured = 0;
  41. list enemies;
  42. integer TIMELIMIT=300; //five minutes
  43. vector RED =<1.00000, 0.00000, 0.00000>;
  44. vector ORANGE=<1.00000, 0.43763, 0.02414>;
  45. vector YELLOW=<1.00000, 1.00000, 0.00000>;
  46. vector GREEN=<0.00000, 1.00000, 0.00000>;
  47. vector BLUE=<0.00000, 0.00000, 1.00000>;
  48. vector BABYBLUE=<0.00000, 1.00000, 1.00000>;
  49. vector PINK=<1.00000, 0.00000, 1.00000>;
  50. vector PURPLE=<0.57338, 0.25486, 1.00000>;
  51. vector BLACK= <0.00000, 0.00000, 0.00000>;
  52. vector WHITE= <1.00000, 1.00000, 1.00000>;
  53. vector AVCLASSBLUE= <0.06274,0.247058,0.35294>;
  54. vector AVCLASSLIGHTBLUG=<0.8549,0.9372,0.9686>;//#daeff7
  55. integer SLOODLE_ROUTER=-1639271139;
  56. integer SLOODLE_PLAYERSERVER=-1639271140;
  57. integer SLOODLE_DEREZ=-1639271141;
  58. list waitingToPlay;
  59. list playing;
  60. string hover;
  61. string ENEMY_TYPE;
  62. integer sloodle_handle_command(string str)
  63. {
  64. // llOwnerSay("handling command "+str);
  65. list bits = llParseString2List(str,["|"],[]);
  66. integer numbits = llGetListLength(bits);
  67. string name;
  68. if (numbits >= 1 ) {
  69. name = llList2String(bits,0);
  70. string value1 = "";
  71. string value2 = "";
  72. if (numbits > 1) value1 = llList2String(bits,1);
  73. if (numbits > 2) value2 = llList2String(bits,2);
  74. } if (name == "set:jellyfishenemy") {
  75. integer type = (integer)llList2String(bits,1);
  76. if (type==1){
  77. ENEMY_TYPE="Jelly Fish";
  78. }else{
  79. ENEMY_TYPE="Sloodle Zombie";
  80. }
  81. }
  82. if ( ENEMY_TYPE!="")isconfigured=1;
  83. return isconfigured;
  84. }
  85. // Strided Functions For working with Strided Lists.
  86. // By Aakanaar LaSalle
  87. // the intStride parameter is the length of the strides within the list
  88. // the intIndex is which stride we're working with.
  89. // the intSubIndex is which element of the stride we're working with.
  90. // Returns number of Strides in a List
  91. integer fncStrideCount(list lstSource, integer intStride)
  92. {
  93. return llGetListLength(lstSource) / intStride;
  94. }
  95. // Find a Stride within a List (returns stride index, and item subindex)
  96. list fncFindStride(list lstSource, list lstItem, integer intStride)
  97. {
  98. integer intListIndex = llListFindList(lstSource, lstItem);
  99. if (intListIndex == -1) { return [-1, -1]; }
  100. integer intStrideIndex = intListIndex / intStride;
  101. integer intSubIndex = intListIndex % intStride;
  102. return [intStrideIndex, intSubIndex];
  103. }
  104. // Deletes a Stride from a List
  105. list fncDeleteStride(list lstSource, integer intIndex, integer intStride)
  106. {
  107. integer intNumStrides = fncStrideCount(lstSource, intStride);
  108. if (intNumStrides != 0 && intIndex < intNumStrides)
  109. {
  110. integer intOffset = intIndex * intStride;
  111. return llDeleteSubList(lstSource, intOffset, intOffset + (intStride - 1));
  112. }
  113. return lstSource;
  114. }
  115. // Returns a Stride from a List
  116. list fncGetStride(list lstSource, integer intIndex, integer intStride)
  117. {
  118. integer intNumStrides = fncStrideCount(lstSource, intStride);
  119. if (intNumStrides != 0 && intIndex < intNumStrides)
  120. {
  121. integer intOffset = intIndex * intStride;
  122. return llList2List(lstSource, intOffset, intOffset + (intStride - 1));
  123. }
  124. return [];
  125. }
  126. // Replace a Stride in a List
  127. list fncReplaceStride(list lstSource, list lstStride, integer intIndex, integer intStride)
  128. {
  129. integer intNumStrides = fncStrideCount(lstSource, intStride);
  130. if (llGetListLength(lstStride) != intStride) { return lstSource; }
  131. if (intNumStrides != 0 && intIndex < intNumStrides)
  132. {
  133. integer intOffset = intIndex * intStride;
  134. return llListReplaceList(lstSource, lstStride, intOffset, intOffset + (intStride - 1));
  135. }
  136. return lstSource;
  137. }
  138. // Retrieve a single element from a Stride within a List
  139. list fncGetElement(list lstSource, integer intIndex, integer intSubIndex, integer intStride)
  140. {
  141. if (intSubIndex >= intStride) { return []; }
  142. integer intNumStrides = fncStrideCount(lstSource, intStride);
  143. if (intNumStrides != 0 && intIndex < intNumStrides)
  144. {
  145. integer intOffset = (intIndex * intStride) + intSubIndex;
  146. return llList2List(lstSource, intOffset, intOffset);
  147. }
  148. return [];
  149. }
  150. // Update a single item in a Stride within a List
  151. list fncReplaceElement(list lstSource, list lstItem, integer intIndex, integer intSubIndex, integer intStride)
  152. {
  153. integer intNumStrides = fncStrideCount(lstSource, intStride);
  154. if (llGetListLength(lstItem) != 1) { return lstSource; }
  155. if (intNumStrides != 0 && intIndex < intNumStrides)
  156. {
  157. integer intOffset = (intIndex * intStride) + intSubIndex;
  158. return llListReplaceList(lstSource, lstItem, intOffset, intOffset);
  159. }
  160. return lstSource;
  161. }
  162. debug(integer channel, string message){
  163. return;
  164. /* string c;
  165. if (channel==SLOODLE_ROUTER){
  166. c="SLOODLE_ROUTER";
  167. llSay(0,"Message came in on: "+c+" : "+message);
  168. }else
  169. if (channel==SLOODLE_PLAYERSERVER){
  170. c="SLOODLE_PLAYERSERVER";
  171. llSay(0,"Message came in on: "+c+" : "+message);
  172. }else{
  173. llSay(0,message);
  174. }
  175. */
  176. }
  177. default {
  178. state_entry() {
  179. llListen(SLOODLE_ROUTER, "" , "", "");
  180. llSetTimerEvent(5);
  181. llSetText("Ready", RED, 1);
  182. }
  183. touch_start(integer num_detected) {
  184. integer j;
  185. key player;
  186. for (j=0;j<num_detected;j++){
  187. if (llDetectedKey(j)==llGetOwner()){
  188. llRezObject(ENEMY_TYPE, llGetPos()+<0,0,3>, <0,0,1>, ZERO_ROTATION, 1);
  189. }
  190. }
  191. llSetText("Touch to Rezz Killer Jelly Fish", YELLOW, 1);
  192. }
  193. object_rez(key id) {
  194. enemies+=id;
  195. }
  196. link_message(integer sender_num, integer num, string str, key id) {
  197. if (num==SLOODLE_DEREZ){
  198. integer j;
  199. integer len = llGetListLength(enemies);
  200. for (j=0;j<len;j++){
  201. llRegionSayTo(llList2Key(enemies,j),SLOODLE_DEREZ,"");
  202. }
  203. llDie();
  204. }
  205. if (num == SLOODLE_CHANNEL_OBJECT_DIALOG) {
  206. // Split the message into lines
  207. list lines = llParseString2List(str, ["\n"], []);
  208. integer numlines = llGetListLength(lines);
  209. integer i = 0;
  210. for (; i < numlines; i++) {
  211. isconfigured = sloodle_handle_command(llList2String(lines, i));
  212. }
  213. }
  214. }
  215. listen(integer channel, string name, key id, string message) {
  216. debug(channel,message);
  217. list data = llParseString2List(message, ["|"], []);
  218. string cmd = llList2String(data,0);
  219. if (cmd=="ENEMY CLICKED"){
  220. list result;
  221. list playerData;
  222. //see if player is already playing
  223. key player = llList2Key(data,1);
  224. result = fncFindStride(playing, [player], 3);
  225. if (llList2Integer(result,0)==-1||llList2Integer(result,1)==-1){
  226. llRegionSay(SLOODLE_PLAYERSERVER, "ARE YOU AVAILABLE?|"+(string)player+"|"+(string)id);
  227. waitingToPlay+=player;
  228. hover="";
  229. return;
  230. }
  231. key clicker = llList2Key(data,1);
  232. debug(0,"fncFindStride("+llDumpList2String(playing,"***")+","+"["+(string)clicker+"], 3)");
  233. result = fncFindStride(playing, [clicker], 3);
  234. if (llList2Integer(result,0)!=-1||llList2Integer(result,1)!=-1){
  235. debug(0,"searching for "+(string)clicker + " in "+llDumpList2String(playing, ","));debug(0,"result is: "+llDumpList2String(result, ","));
  236. playerData = fncGetStride(playing,llList2Integer(result,0),3);
  237. debug(0,"playerDatais: "+llDumpList2String(playerData, ","));
  238. }
  239. if (llList2Integer(playerData,0)!=-1){
  240. llRegionSayTo(llList2Key (playerData,1),SLOODLE_PLAYERSERVER,"ASK QUESTION|"+(string)id);
  241. debug(0,"sending a message to: "+llList2String(playerData,1)+" ASK QUESTION for "+llKey2Name(llList2Key(playerData,0)));
  242. }
  243. }else
  244. if (cmd=="QUIZ FINISHED"){
  245. key player= llList2Key(data,1);
  246. list result = fncFindStride(playing, [player], 3);
  247. if (llList2Integer(result,0)!=-1||llList2Integer(result,1)!=-1){
  248. playing = fncDeleteStride(playing, llList2Integer(result,0), 3);
  249. debug(0,"Deleted player from playing: "+llDumpList2String(playing, "**"));
  250. llSetText("Ready", RED, 1);
  251. hover="";
  252. }
  253. }
  254. else
  255. if (cmd=="AVAILABLE"){
  256. if (llGetListLength(waitingToPlay)>0){
  257. key selectedPlayer = llList2Key(waitingToPlay,0);
  258. llRegionSayTo(id,SLOODLE_PLAYERSERVER,"GUEST TRANSFER|"+(string)selectedPlayer);
  259. waitingToPlay= llDeleteSubList(waitingToPlay, 1, llGetListLength(waitingToPlay)-1);
  260. hover+=llKey2Name(selectedPlayer)+"\n";
  261. playing+=[selectedPlayer,id,llGetUnixTime()];
  262. }
  263. }else{
  264. hover+="AVAILABLE: "+(string)id+"\n";
  265. }
  266. llSetText(hover, YELLOW, 1);
  267. }
  268. timer(){
  269. integer j;
  270. integer len = llGetListLength(playing)/3;
  271. hover="";
  272. if (len >0){
  273. for (j=0;j<len;j++){
  274. list playerGameData= fncGetStride(playing, j, 3);
  275. integer timePlayed = llGetUnixTime()- llList2Integer(playerGameData,2);
  276. if (timePlayed>TIMELIMIT){
  277. llRegionSayTo(llList2Key(playerGameData,1),SLOODLE_PLAYERSERVER, "STOP GAME");
  278. playing = fncDeleteStride(playing, j, 3);
  279. }
  280. }
  281. }
  282. }
  283. }
  284. // Please leave the following line intact to show where the script lives in Git:
  285. // SLOODLE LSL Script Git Location: mod/interaction-1.0/objects/enemyrezzer/assets/sloodle_quiz_router.lslp