Advertisement
Terrah

Mariomaker

Feb 6th, 2016
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.03 KB | None | 0 0
  1. --Ensure existance of nessesary tables
  2. MOD = MOD or {};
  3. MOD.Codes = MOD.Codes or {};
  4.  
  5. --Class start definition
  6. local class = {last=nil;};
  7.  
  8. --Push (add) a user + code to the queue
  9. function class:PushCode(user,code,name)
  10.  
  11.     local len = self:Length();
  12.  
  13.     if type(user)=="table" then
  14.         MOD.Codes[len+1]=user;
  15.     else
  16.  
  17.         user=user:lower();
  18.         local entry = {code=code,user=user,name=name};
  19.         MOD.Codes[len+1]=entry;
  20.     end
  21. end
  22.  
  23. --Pop (remove) from the queue and return it
  24. --this will attempt avoiding levels from the same user twice in a row
  25. --if atslot is higher then 0 it'll atempt poping that slot
  26. function class:PopCode(atslot)
  27.  
  28.     local slot = 1;
  29.     local usingfilter = false;
  30.  
  31.     --If we're looking to pop in a set slot we should do that instead
  32.     if type(atslot)=="number" and atslot>0 then
  33.         slot = atslot;
  34.         usingfilter=true;
  35.     end
  36.  
  37.     --Look for the next in the queue if we're attempting to Pop
  38.     --a level from a user we just poped off
  39.     if not usingfilter and self.last and MOD.Codes[slot] and MOD.Codes[slot].user==self.last then
  40.  
  41.         while MOD.Codes[slot] do
  42.  
  43.             if MOD.Codes[slot+1] and MOD.Codes[slot+1].user==MOD.Codes[slot].user then
  44.                 slot = slot + 1;
  45.             else
  46.                 slot = slot + 1;
  47.                 break;
  48.             end
  49.         end
  50.     end
  51.  
  52.     --None exists
  53.     --Or they're all by the same user
  54.     if not MOD.Codes[slot] then
  55.  
  56.         --return nil if we're using a filter, it was invalid
  57.         if usingfilter then
  58.             return nil;
  59.         elseif slot == 1 then
  60.             return nil;
  61.         else
  62.             slot = 1;
  63.         end
  64.     end
  65.  
  66.     --Pop up other entires up the queue
  67.     local entry = MOD.Codes[slot];
  68.     local n=slot+1;
  69.     local cursor = MOD.Codes[n];
  70.     while cursor do
  71.         MOD.Codes[n-1] = cursor;
  72.         n = n + 1;
  73.         cursor = MOD.Codes[n];
  74.     end
  75.  
  76.     --Remove the last node in the queue since this has been pushed up already
  77.     MOD.Codes[n-1] = nil;
  78.  
  79.     --Set last
  80.     self.last = entry.user;
  81.  
  82.     --Move all the levels by this user to the bottom
  83.     self:PushUserBottom(entry.user);
  84.  
  85.     --return
  86.     return entry;
  87. end
  88.  
  89. --Moves all the users submitted levels to the bottom
  90. function class:PushUserBottom(user)
  91.  
  92.     --Get which indexes we got this user on
  93.     local indices = {};
  94.  
  95.     local n=1;
  96.     local entry = MOD.Codes[n];
  97.     while entry do
  98.  
  99.         if entry.user==user then
  100.             table.insert(indices,n);
  101.         end
  102.  
  103.         n = n + 1;
  104.         entry = MOD.Codes[n]
  105.     end
  106.     entry = nil;
  107.  
  108.     --If we got any then pop them and push them back
  109.     if #indices > 0 then
  110.  
  111.         for n=1,#indices do
  112.             entry = self:PopCode(indices[n]);
  113.             if entry then
  114.                 self:PushCode(entry);
  115.             end
  116.         end
  117.     end
  118. end
  119.  
  120. --Return the length of the queue
  121. function class:Length()
  122.  
  123.     local highest = 0;
  124.  
  125.     for entry,_ in pairs(MOD.Codes) do
  126.         if entry > highest then
  127.             highest = entry;
  128.         end
  129.     end
  130.  
  131.     return highest;
  132. end
  133.  
  134. --Returns true if the code already exists within the queue
  135. function class:Exists(code)
  136.  
  137.     local n=1;
  138.     local cursor = MOD.Codes[n];
  139.     while cursor do
  140.  
  141.         if cursor.code == code then
  142.             return true;
  143.         end
  144.  
  145.         n = n + 1;
  146.         cursor = MOD.Codes[n];
  147.     end
  148.  
  149.     return false;
  150. end
  151.  
  152. function class:Count(user)
  153.     user = user:lower()
  154.  
  155.     local cnt = 0;
  156.     local n=1;
  157.     local cursor = MOD.Codes[n];
  158.     while cursor do
  159.  
  160.         if cursor.user == user then
  161.             cnt = cnt + 1;
  162.         end
  163.  
  164.         n = n + 1;
  165.         cursor = MOD.Codes[n];
  166.     end
  167.  
  168.     return cnt;
  169. end
  170.  
  171. --Return the first slot that contains a entry by the user
  172. --return 0 if none was found
  173. function class:IndexOfUser(user)
  174.     user = user:lower()
  175.  
  176.     local n = 1;
  177.     local cursor = MOD.Codes[n];
  178.     while cursor do
  179.  
  180.         if cursor.user==user then
  181.             return n;
  182.         end
  183.  
  184.         n = n + 1;
  185.         cursor = MOD.Codes[n];
  186.     end
  187.  
  188.     return 0;
  189. end
  190.  
  191. --Remove all levels by user
  192. function class:Remove(user)
  193.  
  194.     local removed = {};
  195.     local n = self:IndexOfUser(user);
  196.     local entry;
  197.     while n>0 do
  198.         entry = self:PopCode(n);
  199.         table.insert(removed,"["..entry.code.."] "..entry.name);
  200.         n = self:IndexOfUser(user);
  201.     end
  202.  
  203.     return removed;
  204. end
  205.  
  206. --Returns true if the user is allowed to submit
  207. function class:Allowed(user)
  208.  
  209.     local isBroadcaster = user:lower() == Channel():sub(2):lower();
  210.     if isBroadcaster then
  211.         return true;
  212.     end
  213.  
  214.     user = user:lower();
  215.  
  216.     local data = MOD.GetVar("mm_"..user,Channel());
  217.  
  218.     if data then
  219.         return true;
  220.     else
  221.         return false;
  222.     end
  223. end
  224.  
  225. function class:IsValid(code)
  226.  
  227.     local url= "https://supermariomakerbookmark.nintendo.net/courses/"..code;
  228.     local body, code, headers, status = MOD.HTTPSGet(url);
  229.  
  230.     if code~=200 or body==nil or body=="" then
  231.         return nil;
  232.     end
  233.  
  234.  
  235.     local pattern = body:match([[<meta property="og:title" content="(.-)" />]]);
  236.     if not pattern then
  237.         return nil;
  238.     end
  239.     local name = pattern:match([[SUPER MARIO MAKER BOOKMARK | (.-) %-]]);
  240.     if not name or name=="" then
  241.         return nil;
  242.     else
  243.         return name;
  244.     end
  245. end
  246.  
  247. --Store class
  248. MOD.MarioMaker = class;
  249.  
  250. --This is the function we return back (the actual command)
  251. local Submit = function(msg,usr,chan)
  252.  
  253.     if not MOD.MarioMaker:Allowed(usr) then
  254.         return;
  255.     end
  256.  
  257.     if msg==nil or msg=="" then
  258.         print("@"..usr.." you have to enter a code!");
  259.     else
  260.         msg = msg:upper();
  261.     end
  262.  
  263.     if MOD.MarioMaker:Exists(msg) then
  264.         print("@"..usr.." that level already exists in the queue!");
  265.     elseif MOD.MarioMaker:Count(usr)>=3 then
  266.         print("@"..usr.." you already have 3 or more levels in the queue!");
  267.     else
  268.         local name = MOD.MarioMaker:IsValid(msg);
  269.         if not name then
  270.             print("@"..usr.." level "..msg.." is not a valid level");
  271.         else
  272.             MOD.MarioMaker:PushCode(usr,msg,name);
  273.             print("@"..usr.." level "..name.." has been added to the queue at position "..tostring(MOD.MarioMaker:Length()).." link: ".."https://supermariomakerbookmark.nintendo.net/courses/"..msg);
  274.             MOD.Save();
  275.         end
  276.     end
  277. end
  278.  
  279. local Take = function(msg,usr,chan)
  280.  
  281.     local isBroadcaster = usr:lower() == Channel():sub(2):lower();
  282.  
  283.     if not isBroadcaster then
  284.         return;
  285.     end
  286.  
  287.     if  msg==nil or msg=="" then
  288.  
  289.         local entry = MOD.MarioMaker:PopCode();
  290.         local left = MOD.MarioMaker:Length();
  291.  
  292.         if not entry then
  293.  
  294.             print("@"..usr.." there are no levels in the queue");
  295.         else
  296.  
  297.             print("@"..usr.." name: "..entry.name.." code: "..entry.code.." submitted by: "..entry.user.." left in the queue: "..tostring(left).." link: https://supermariomakerbookmark.nintendo.net/courses/"..entry.code);
  298.             MOD.Save();
  299.         end
  300.     else
  301.         local removed = MOD.MarioMaker:Remove(msg);
  302.         if #removed<=0 then
  303.             print("@"..usr.." "..msg.." has no codes in the queue");
  304.         else
  305.             print("@"..usr);
  306.             for n=1,#removed do
  307.                 print(removed[n]);
  308.             end
  309.         end
  310.         MOD.Save();
  311.     end
  312. end
  313.  
  314. local AddRemove = function(msg,usr,chan)
  315.  
  316.     if msg == nil or msg == "" then
  317.         return;
  318.     else
  319.         msg = msg:lower();
  320.     end
  321.  
  322.     local isBroadcaster = usr:lower() == Channel():sub(2):lower();
  323.     local ok = MOD.MarioMaker:Allowed(msg);
  324.  
  325.     if isBroadcaster then
  326.         if ok then
  327.             MOD.DelVar("mm_"..msg);
  328.             print("@"..usr.." "..msg.." has been removed from the level submit list");
  329.         else
  330.             MOD.SetVar("mm_"..msg,true,"mm");
  331.             print("@"..usr.." "..msg.." has been added to the level submit list");
  332.         end
  333.     end
  334. end
  335.  
  336. MOD.AddCom("take",Take);
  337. MOD.AddCom("mariomaker",AddRemove);
  338.  
  339. return Submit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement