mikebald

Happiness and Status

Mar 12th, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  -- Startup Stuff
  2. m = peripheral.wrap("top")
  3. m.setTextScale(1.0 )
  4. -- End Startup Stuff
  5.  
  6. function SortJob(a, b)
  7.     return a["job"] < b["job"]
  8. end
  9.  
  10. function FirstName(name)
  11.     returnStr = ""
  12.     for i=1, #name do
  13.         local char = string.sub(name, i, i)
  14.         if (char == " " or i == 9) then
  15.             return returnStr
  16.         end
  17.         returnStr = returnStr .. char
  18.     end
  19.     return returnStr
  20. end
  21.  
  22. function GetHappiness(happiness)
  23.     local mult = 10 -- # of places
  24.     happiness = math.floor(happiness * mult + 0.5) / mult
  25.     if (happiness == 10.0) then
  26.         m.setTextColor(colors.green)
  27.     else
  28.         m.setTextColor(colors.pink)
  29.     end
  30.     m.write(happiness)
  31. end
  32.  
  33. function GetJobStatus(status)
  34.     local statusText = {
  35.         ["Working"] = "Working",
  36.         ["Farming"] = "Working",
  37.         ["Delivering"] = "Working",
  38.         ["Mining"] = "Working",
  39.         ["Composting"] = "Working",
  40.         ["Searching for trees"] = "Working"
  41.     }
  42.    
  43.     if (statusText[status] == nil) then
  44.         m.setTextColor(colors.red)
  45.         m.write(status)
  46.     else
  47.         m.setTextColor(colors.green)
  48.         m.write(statusText[status])
  49.     end
  50.    
  51. end
  52.  
  53. function SetJobColor(job)
  54.     local jobColors = {
  55.         ["Knight"] = colors.magenta,
  56.         ["Courier"] = colors.yellow,
  57.         ["Archer"] = colors.pink,
  58.         ["Builder"] = colors.brown,
  59.         ["Druid"] = colors.lime,
  60.         ["Enchanter"] = colors.purple,
  61.         ["Farmer"] = colors.cyan,
  62.         ["Student"] = colors.orange,
  63.         ["Researcher"] = colors.lightBlue        
  64.     }
  65.     if (jobColors[job] == nil) then
  66.         m.setTextColor(colors.blue)
  67.     else
  68.         m.setTextColor(jobColors[job])
  69.     end
  70. end
  71.  
  72. function ShowCitizens()
  73.     m.clear()
  74.     local counter = 1
  75.     local row = 1
  76.     local column = 1
  77.      local citizens = colony.getCitizens()
  78.     table.sort(citizens, SortJob)
  79.     for _, citizen in ipairs(citizens) do
  80.         local status = citizen["status"]
  81.         if (row > 40) then
  82.            row = 1
  83.            column = 42      
  84.         end
  85.         m.setCursorPos(column, row)
  86.         m.setTextColor(colors.blue)
  87.         m. write(FirstName(citizen["name"]))
  88.  
  89.         m.setCursorPos(column + 10, row)
  90.         SetJobColor(citizen["job"])
  91.         m.write(citizen["job"])
  92.  
  93.         m.setCursorPos(column + 30 , row)
  94.         GetJobStatus(status)
  95.        
  96.         m.setCursorPos(column + 25, row)
  97.         GetHappiness(citizen["happiness"])
  98.        
  99.         row = row + 1
  100.     end
  101. end
  102.  
  103.  while true do
  104.     ShowCitizens()
  105.     sleep(5)
  106. end
  107.  
  108.    
Add Comment
Please, Sign In to add comment