Advertisement
MadManMarkAu

[CC] Auto-Jukebox

May 29th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1.  
  2. -- List of all known tracks, and their lengths.
  3. local tTracks = {
  4.     {TrackName = "C418 - 13", Length = 2 * 60 + 58},
  5.     {TrackName = "C418 - cat", Length = 3 * 60 + 05},
  6.     {TrackName = "C418 - blocks", Length = 5 * 60 + 45},
  7.     {TrackName = "C418 - chirp", Length = 3 * 60 + 05},
  8.     {TrackName = "C418 - far", Length = 2 * 60 + 54},
  9.     {TrackName = "C418 - mall", Length = 3 * 60 + 17},
  10.     {TrackName = "C418 - mellohi", Length = 1 * 60 + 36},
  11.     {TrackName = "C418 - stal", Length = 2 * 60 + 30},
  12.     {TrackName = "C418 - strad", Length = 3 * 60 + 08},
  13.     {TrackName = "C418 - ward", Length = 4 * 60 + 11},
  14.     {TrackName = "C418 - 11", Length = 1 * 60 + 11},
  15.     {TrackName = "C418 - wait", Length = 3 * 60 + 58}
  16. }
  17.  
  18. local tJukeBoxes = {}
  19. -- tJukeBoxes format: Array of objects.
  20. --   DeviceName: (String) Name of peripheral
  21. --   TrackName: (String) Name of the loaded disc
  22. --   TrackLength: (Number) Length of the loaded disc
  23.  
  24. -- Finds all Auto-Jukeboxes attached to the network, and saves them, with their loaded disc, to the tJukeBoxes table.
  25. local function BuildJukeBoxList()
  26.     local tPeripherals
  27.     local nIndex
  28.     local tStack
  29.     local nTrackIndex
  30.    
  31.     tJukeBoxes = {}
  32.    
  33.     tPeripherals = peripheral.getNames()
  34.     for nIndex = 1, #tPeripherals do
  35.         if peripheral.getType(tPeripherals[nIndex]) == "auto_jukebox" then
  36.             tStack = peripheral.call(tPeripherals[nIndex], "getStackInSlot", 1)
  37.            
  38.             if tStack.disk ~= nil then
  39.                 -- Find the length of the track.
  40.                 for nTrackIndex = 1, #tTracks do
  41.                     if tTracks[nTrackIndex].TrackName == tStack.disk.label then
  42.                         tJukeBoxes[#tJukeBoxes + 1] = {DeviceName = tPeripherals[nIndex], TrackName = tStack.disk.label, TrackLength = tTracks[nTrackIndex].Length}
  43.                         break
  44.                     end
  45.                 end
  46.             end
  47.         end
  48.     end
  49. end
  50.  
  51. -- Plays the disc loaded in the given jukebox.
  52. local function PlayTrack(tJukeBox)
  53.     peripheral.call(tJukeBox.DeviceName, "play")
  54. end
  55.  
  56. -- Stops all playing tracks.
  57. local function StopAll()
  58.     local nIndex
  59.    
  60.     for nIndex = 1, #tJukeBoxes do
  61.         peripheral.call(tJukeBoxes[nIndex].DeviceName, "stop")
  62.     end
  63. end
  64.  
  65. -- Takes a number of seconds and returns a formatted string in m:ss format.
  66. local function FormatTime(nSeconds)
  67.     local nMinutesOut
  68.     local nSecondsOut
  69.     local sSeconds
  70.    
  71.     nMinutesOut = math.floor(nSeconds / 60)
  72.     nSecondsOut = nSeconds - (nMinutesOut * 60)
  73.    
  74.     sSeconds = tostring(nSecondsOut)
  75.     if #sSeconds < 2 then
  76.         sSeconds = "0" .. sSeconds
  77.     end
  78.    
  79.     return tostring(nMinutesOut) .. ":" .. sSeconds
  80. end
  81.  
  82. local nTrack
  83.  
  84. BuildJukeBoxList()
  85.  
  86. print("Track list:")
  87. for nTrack = 1, #tJukeBoxes do
  88.     print("  " .. tJukeBoxes[nTrack].TrackName .. " (" .. FormatTime(tJukeBoxes[nTrack].TrackLength) .. ")")
  89. end
  90.  
  91. print()
  92.  
  93. while true do
  94.     for nTrack = 1, #tJukeBoxes do
  95.         StopAll()
  96.         PlayTrack(tJukeBoxes[nTrack])
  97.         print("Playing \"" .. tJukeBoxes[nTrack].TrackName .. "\" (" .. FormatTime(tJukeBoxes[nTrack].TrackLength) .. ")")
  98.         os.sleep(tJukeBoxes[nTrack].TrackLength)
  99.     end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement