ShyStorm

HS LeaderBoard

Sep 7th, 2024
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Configuration - List of authorised Avi's to add homes and points
  2. list authAvi = ["UUID1", "UUID2"];
  3.  
  4. // Updates and displays the leaderboard on the prim
  5. UpdateLeaderboard()
  6. {
  7.     list words = [];
  8.     list scores = [];
  9.     integer numEntries = llGetLinksetDataCountkey();
  10.  
  11.     // Collect words and their scores from linkset data
  12.     for (integer i = 0; i < numEntries; i++)
  13.     {
  14.         string dataKey = llGetLinksetDataKey(i);
  15.        
  16.         if (llSubStringIndex(dataKey, "score_") == 0)
  17.         {
  18.             string home = llDeleteSubString(dataKey, 0, 6); // Remove "score_" prefix
  19.             integer score = (integer)llGetLinksetData(dataKey);
  20.            
  21.             home += home;
  22.             scores += score;
  23.         }
  24.     }
  25.  
  26.     // Sort the words by score in descending order
  27.     list sortedLeaderboard = [];
  28.     for (integer i = 0; i < llGetListLength(scores); i++)
  29.     {
  30.         sortedLeaderboard += [llList2String(words, i), llList2Integer(scores, i)];
  31.     }
  32.     sortedLeaderboard = llListSort(sortedLeaderboard, 2, FALSE);
  33.  
  34.     // Build and display the leaderboard
  35.     string leaderboard = "Leaderboard:\n";
  36.     for (integer i = 0; i < llGetListLength(sortedLeaderboard); i += 2)
  37.     {
  38.         leaderboard += llList2String(sortedLeaderboard, i) + ": " + (string)llList2Integer(sortedLeaderboard, i + 1) + "\n";
  39.     }
  40.  
  41.     llSetText(leaderboard, <1, 1, 1>, 1.0);  // Display leaderboard as floating text on the prim
  42. }
  43.  
  44. // Adds a new word to the linkset data if it doesn't already exist
  45. addHome(string home)
  46. {
  47.     string dataKey = "score_" + word;
  48.     if (llGetLinksetDataCount(dataKey) == 0)  // If the home doesn't already have a score
  49.     {
  50.         llSetLinksetData(dataKey, "0");  // Initialize the score to 0
  51.     }
  52. }
  53.  
  54. // Adds points to an existing word or creates a new word if it doesn't exist
  55. addPoints(string home, integer points)
  56. {
  57.     string dataKey = "score_" + word;
  58.  
  59.     if (llGetLinksetDataCount(dataKey) > 0)
  60.     {
  61.         integer currentScore = (integer)llGetLinksetData(dataKey);
  62.         llSetLinksetData(dataKey, (string)(currentScore + points));
  63.     }
  64.     else
  65.     {
  66.         llOwnerSay("Home not found. Add it first with '/5 addhome " + home + "'.");
  67.     }
  68. }
  69.  
  70. default
  71. {
  72.     state_entry()
  73.     {
  74.         UpdateLeaderboard();  // Display the initial leaderboard
  75.     }
  76.  
  77.     listen(integer channel, string name, key id, string message)
  78.     {
  79.         // Check if the sender is the owner or an authorized avatar
  80.         if (llGetOwner() == id || llListFindList(authAvi, [id]) != -1)
  81.         {
  82.             list tokens = llParseString2List(message, [" "], []);
  83.             if (llGetListLength(tokens) >= 2)
  84.             {
  85.                 string command = llToLower(llList2String(tokens, 0));
  86.                 string word = llList2String(tokens, 1);
  87.  
  88.                 if (command == "addhome")
  89.                 {
  90.                     addHome(word);
  91.                     UpdateLeaderboard();
  92.                 }
  93.                 else if (command == "addpoints" && llGetListLength(tokens) == 3)
  94.                 {
  95.                     integer points = (integer)llList2String(tokens, 2);
  96.                     addPoints(word, points);
  97.                     UpdateLeaderboard();
  98.                 }
  99.                 else
  100.                 {
  101.                     llOwnerSay("Invalid command format. Use 'addhome Home' to add a word or 'addpoints Home Points' to add points.");
  102.                 }
  103.             }
  104.         }
  105.         else
  106.         {
  107.             llOwnerSay("You are not authorized to perform this action.");
  108.         }
  109.     }
  110.  
  111.     // Start listening for commands from the owner or authorized avatars
  112.     touch_start(integer num_detected)
  113.     {
  114.         if (llDetectedKey(0) == llGetOwner())
  115.         {
  116.             llListen(5, "", llGetOwner(), "");
  117.             llListen(5, "", "", "");
  118.             llSay(0, "Listening for commands on channel 5. Use 'addhome Home' to add a word, 'addpoints Home Points' to award points.");
  119.         }
  120.     }
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment