1x_Gacha

Classroom Module | Server

Jan 22nd, 2021
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.87 KB | None | 0 0
  1. local Players = game:GetService('Players')
  2. local ReplicatedStorage = game:GetService('ReplicatedStorage')
  3. local ServerStorage = game:GetService('ServerStorage')
  4. local ServerScriptServce = game:GetService('ServerScriptService')
  5. local Workspace = game:GetService('Workspace')
  6.  
  7. local Gui = ServerScriptServce:WaitForChild('Core'):WaitForChild('Gui')
  8.  
  9. local Remotes = ReplicatedStorage:WaitForChild('ClassroomRemotes')
  10.  
  11. local RemoteHostSetup = Remotes:WaitForChild('HostSetup')
  12. local QuizSetup = Remotes:WaitForChild('QuizSetup')
  13. local RequestSeatInformation = Remotes:WaitForChild('RequestSeatInformation')
  14. local RequestSeatFix = Remotes:WaitForChild('RequestSeatFix')
  15.  
  16. local _Settings = require(ServerScriptServce:WaitForChild('Core'):WaitForChild('Settings'))
  17. local _BackupData = require(ServerScriptServce:WaitForChild('Core'):WaitForChild('BackupData'))
  18.  
  19. local HostQuiz = Gui:WaitForChild('HostQuiz')
  20. local PlayerQuiz = Gui:WaitForChild('PlayerQuiz')
  21.  
  22. local c = {}
  23.  
  24. local folder;
  25.  
  26. c.ClassRooms = {}
  27.  
  28. local function GetOccupiedSeats(class)
  29.     local t = {}
  30.     --warn('Array Occupied Seats')
  31.     if folder:FindFirstChild(class) then
  32.         --warn('Find Class')
  33.         for i,v in pairs(folder[class]['Seats']:GetChildren())do
  34.             --print('Check')
  35.             if v.Occupant ~= nil then
  36. --              print(v.Occupant.Parent.Name)
  37.                 v.Occupant.JumpPower = 0
  38.                 c.ClassRooms[tonumber(class)]['Seats'][tonumber(v.Name)]['Occupant'] = Players:GetPlayerFromCharacter(v.Occupant.Parent).Name
  39.                 c.ClassRooms[tonumber(class)]['Seats'][tonumber(v.Name)]['Testing'] = 'Testing'
  40.                 table.insert(t, tonumber(v.Name), {player = v.Occupant.Parent.Name; seat = v.Name})
  41.             else
  42.                 v.Disabled = true
  43.             end
  44.         end
  45.     end
  46.     return t
  47. end
  48.  
  49. local function GetSeatList(class)
  50.     local t = {}
  51.    
  52.     if folder:FindFirstChild(class)then
  53.         for i,v in pairs(folder[class]['Seats']:GetChildren())do
  54.             if v.Occupant ~= nil then
  55.                 table.insert(t, tonumber(v.Name), {player = v.Occupant.Parent.Name; seat = v.Name})
  56.             end
  57.         end
  58.     end
  59.    
  60.     return t
  61. end
  62.  
  63. local function CleanClassroom(class)
  64. --  print('Clean Classroom')
  65.     for i,v in pairs(c.ClassRooms[tonumber(class)].Remotes)do
  66.         v:Disconnect()
  67.     end
  68.    
  69.     for i,v in pairs(folder[class]['Seats']:GetChildren())do
  70.         v.Disabled = false
  71.     end
  72.    
  73.     c.ClassRooms[tonumber(class)] = {Seats = {}, Test = nil, Host = nil, Status = "Free", Remotes = {}, Serial = 0}
  74.     for i,v in pairs(folder[class].Seats:GetChildren())do
  75.         c.ClassRooms[tonumber(class)]['Seats'][tonumber(v.Name)] = {Occupant = 'NIL', Status = 'NIL', Score = 0}
  76.         --table.insert(c.ClassRooms[tonumber(class)]['Seats'], tonumber(v.Name), )
  77.     end
  78.     --warn('Classroom Cleaned')
  79. end
  80.  
  81. local function SortClassrooms()
  82.    
  83.     if Workspace:WaitForChild('Classrooms') then
  84.         folder = Workspace:FindFirstChild('Classrooms')
  85.     else
  86.         repeat wait() until Workspace:FindFirstChild('Classrooms')
  87.         folder = Workspace:FindFirstChild('Classrooms')
  88.     end
  89.    
  90.     for i,v in ipairs(folder:GetChildren())do
  91.         table.insert(c.ClassRooms, tonumber(v.Name), {Seats = {}, Test = nil, Host = nil, Status = "Free", Remotes = {}, Serial = 0})
  92.         for a,b in pairs(v.Seats:GetChildren())do
  93.             c.ClassRooms[tonumber(v.Name)]['Seats'][tonumber(b.Name)] = {Occupant = 'NIL', Status = 'NIL', Score = 0}
  94.             --table.insert(c.ClassRooms[tonumber(v.Name)]['Seats'], tonumber(b.Name), {Occupant = 'NIL'})
  95.         end
  96.     end
  97. end
  98.  
  99. local function FindClassHost(player)
  100.     for i,v in pairs(c.ClassRooms) do
  101.         if v.Host == player then
  102.             return true
  103.         end
  104.     end
  105.     return false
  106. end
  107.  
  108. local function ClearPotentialHost()
  109.     for i,v in pairs(game.Players:GetPlayers())do
  110.         if v.PlayerGui:FindFirstChild('HostQuiz') and not FindClassHost(v) then
  111.             v.PlayerGui:FindFirstChild('HostQuiz'):Destroy()
  112.         end
  113.     end
  114. end
  115.  
  116. local function GradeTest(player, Answers, Class, Seat)
  117.     local test = _Settings.Tests[c.ClassRooms[tonumber(Class)].Test]
  118.     local grade = 0
  119.     if Answers ~= nil then 
  120.         for i,v in pairs(Answers)do
  121.             grade += tonumber(v)
  122.         end
  123.     else
  124.         grade = 0
  125.     end
  126.     local Final = (grade / #test)*100
  127.     c.ClassRooms[tonumber(Class)]['Seats'][tonumber(Seat)].Status = 'Submitted'
  128.     c.ClassRooms[tonumber(Class)]['Seats'][tonumber(Seat)].Score = Final
  129.     game.Workspace:FindFirstChild('Classrooms'):FindFirstChild(Class):FindFirstChild('Remotes'):FindFirstChild('TestSubmitted'):FireClient(player, Final)
  130.     game.Workspace:FindFirstChild('Classrooms'):FindFirstChild(Class):FindFirstChild('Remotes'):FindFirstChild('HostTestSubmitted'):FireAllClients(Seat)
  131.    
  132.    
  133. end
  134.  
  135. local function EndTest(player, class)
  136.     warn('End Test')
  137.     if c.ClassRooms[tonumber(class)] then
  138.         --warn('Check Classroom')
  139.         local f = c.ClassRooms[tonumber(class)]
  140.         if f.Status == 'Testing' then
  141.             warn('Check Status')
  142.             f.Status = 'Ending'
  143.             Workspace:FindFirstChild('Classrooms'):FindFirstChild(class):FindFirstChild('Remotes'):FindFirstChild('TestEnded'):FireAllClients(c.ClassRooms[tonumber(class)].Serial)
  144.         end
  145.     end
  146. end
  147.  
  148. local function RequestSeatInfo(player, Class, seat)
  149.     if c.ClassRooms[tonumber(Class)]['Seats'][tonumber(seat)].Occupant ~= 'NIL' then
  150.         return {c.ClassRooms[tonumber(Class)]['Seats'][tonumber(seat)].Occupant, c.ClassRooms[tonumber(Class)]['Seats'][tonumber(seat)].Status, c.ClassRooms[tonumber(Class)]['Seats'][tonumber(seat)].Score}
  151.     end
  152.     return nil
  153. end
  154.  
  155. function c:GetScore(player, class)
  156.    
  157. end
  158.  
  159. local function PlayerLeaving(player, class, seat)
  160.     c.ClassRooms[class]['Seats'][seat] = {Occupant = 'NIL', Status = 'NIL', Score = 0}
  161.     Workspace:FindFirstChild('Classrooms'):FindFirstChild(tostring(class)):FindFirstChild('Remotes'):FindFirstChild('PlayerLeft'):FireAllClients(seat)
  162. end
  163.  
  164. local function EndClassroom(player, class)
  165.     --warn('End Classroom')
  166.     local players = GetOccupiedSeats(class)
  167.     if players then
  168.         --warn('Players')
  169.         for i,v in pairs(players)do
  170. --          print('For Players', v.player)
  171.             Players:FindFirstChild(v.player).PlayerGui:FindFirstChild('PlayerQuiz'):Destroy()
  172.             Players:FindFirstChild(v.player).Character.Humanoid.JumpPower = 50
  173.         end
  174.     end
  175.     player.PlayerGui:FindFirstChild('HostQuiz'):Destroy()
  176.     CleanClassroom(class)
  177.     --warn('Classroom Ended')
  178. end
  179.  
  180. local function SeatFixList(player, class)
  181.     if class then
  182.         if c.ClassRooms[tonumber(class)] then
  183.             if c.ClassRooms[tonumber(class)].Status ~= 'Free' then
  184.                 return GetSeatList(class)
  185.             end
  186.         end
  187.     end
  188. end
  189.  
  190. function c:GetClassroomInformation(class)
  191.     return c.ClassRooms[tonumber(class)]
  192. end
  193.  
  194. function c:HostSetup(host, class)
  195.     --warn(host, class, ' - Host Set up Check')
  196.     local f = c.ClassRooms[tonumber(class.Name)]
  197.     if f.Host == nil then
  198.         --warn(host, class, ' - No Host Check')
  199.         if host.PlayerGui:FindFirstChild('HostQuiz') then
  200.             warn('Player currently holds a HostQuiz Gui, checking if currently Host.') 
  201.             if FindClassHost(host) then
  202.                 return warn('Player Currently hosting a Class, can not host two classes at one time!')
  203.             else
  204.                 print('Player Does NOT Host a Class, sending new Class Information')
  205.                 RemoteHostSetup:FireClient(host, class)
  206.             end
  207.         else
  208.             --warn(host, class, ' - No Host Gui Check')
  209.             local c = HostQuiz:Clone()
  210.             c.Parent = host.PlayerGui
  211.             RemoteHostSetup:FireClient(host, class)
  212.         end
  213.     end
  214. end
  215.  
  216. function c:StartClassroom(host, class, test)
  217.     --warn(host, class, ' - Start Class Check')
  218.     if host:IsA('Player') and c.ClassRooms[tonumber(class.Name)]['Host'] == nil then
  219.         --warn(host, class, ' - No Host Check')
  220.         c.ClassRooms[tonumber(class.Name)]['Host'] = host
  221.         ClearPotentialHost()
  222.         if _Settings.Tests[test] then
  223.             --warn(host, class, ' - Test Exists Check')
  224.             c.ClassRooms[tonumber(class.Name)]['Test'] = test
  225.             c.ClassRooms[tonumber(class.Name)]['Status'] = 'Occupied'
  226.         end
  227.     end
  228. end
  229.  
  230. function c:BeginTest(class, host)
  231.     --warn('BEGIN TEST')
  232.     if c.ClassRooms[tonumber(class.Name)]['Test'] ~= nil then
  233.         --warn('TEST IS NOT NIL')
  234.         c.ClassRooms[tonumber(class.Name)].Serial = math.random(100,999)
  235.         local f = c.ClassRooms[tonumber(class.Name)]
  236.         local players = GetOccupiedSeats(class.Name)
  237.         if players then
  238.             --warn('TEST PLAYERS EXIST')
  239.             for i,v in pairs(players)do --   ["player"] = Player1
  240.                 --print('Handing Quiz Gui', v.player)
  241.                 local clone = PlayerQuiz:Clone()
  242.                 clone.Parent = Players:FindFirstChild(v.player).PlayerGui
  243.                 QuizSetup:FireClient(Players:FindFirstChild(v.player), c.ClassRooms[tonumber(class.Name)]['Host'], _Settings.Tests[c.ClassRooms[tonumber(class.Name)]['Test']], c.ClassRooms[tonumber(class.Name)]['Test'], class.Name, v.seat, c.ClassRooms[tonumber(class.Name)].Serial)
  244.             end
  245.            
  246.         end
  247.        
  248.         c.ClassRooms[tonumber(class.Name)].Remotes['A'] = Workspace:FindFirstChild('Classrooms'):FindFirstChild(class.Name):FindFirstChild('Remotes'):FindFirstChild('TestSubmitted').OnServerEvent:Connect(GradeTest)
  249.         c.ClassRooms[tonumber(class.Name)].Remotes['B'] = Workspace:FindFirstChild('Classrooms'):FindFirstChild(class.Name):FindFirstChild('Remotes'):FindFirstChild('TestEnded').OnServerEvent:Connect(EndTest)
  250.         c.ClassRooms[tonumber(class.Name)].Remotes['C'] = Workspace:FindFirstChild('Classrooms'):FindFirstChild(class.Name):FindFirstChild('Remotes'):FindFirstChild('EndClass').OnServerEvent:Connect(EndClassroom)
  251.        
  252.         Workspace:FindFirstChild('Classrooms'):FindFirstChild(class.Name):FindFirstChild('Remotes'):FindFirstChild('ClassBoard'):FireClient(host, players) -- This is to get the list of players in the class, to the Host/Client
  253.        
  254.    
  255.         --
  256.         f.Status = 'Testing'
  257.     end
  258. end
  259.  
  260. function c:OnModeSwitched()
  261.     for i,v in pairs(c.ClassRooms)do
  262.         if v.Status ~= 'Free' then
  263.             EndClassroom(v.Host, tonumber(tostring(i)))
  264.         end
  265.     end
  266. end
  267.  
  268. function c:CheckPlayer(player)
  269.     for i,v in pairs(c.ClassRooms)do
  270.         if v.Status == 'Testing' then
  271.             for a,b in pairs(v.Seats)do
  272.                 if b.Occupant == player then
  273.                     PlayerLeaving(player, tonumber(tostring(i)), tonumber(tostring(a)))
  274.                 end
  275.             end
  276.         end
  277.     end
  278. end
  279.  
  280. SortClassrooms()
  281.  
  282. RequestSeatInformation.OnServerInvoke = RequestSeatInfo
  283. RequestSeatFix.OnServerInvoke = SeatFixList
  284.  
  285. return c
Advertisement
Add Comment
Please, Sign In to add comment