Advertisement
antonsavov

Untitled

Mar 6th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. os.loadAPI('json')
  2.  
  3. --test reading nad writing the buildzones grid to a file
  4. --creates a grid of absolute references (0,0) (1,0)
  5. function buildGrid(w,h)
  6. local grid = {}
  7. for z=0,h-1 do
  8. for x=0,w-1 do
  9. table.insert(grid,{x=x,z=z,played=false})
  10. end
  11. end
  12. return grid
  13. end
  14.  
  15. -- see if the file exists
  16. function file_exists(file)
  17. local f = fs.open(file, "rb")
  18. if f then f:close() end
  19. return f ~= nil
  20. end
  21.  
  22.  
  23. local LOCS = buildGrid(11,27)
  24.  
  25. fs.makeDir("/records")
  26. local filename = "/records/_buildzones-list.yaml"
  27.  
  28. local file = fs.open(filename,"w")
  29. file.write(json.encodePretty(LOCS))
  30. file.close()
  31.  
  32. local id = 10
  33.  
  34. local newLOCS = json.decodeFromFile(filename)
  35. print(#newLOCS)
  36. print(newLOCS[id].x)
  37. print(newLOCS[id].z)
  38. print(newLOCS[id].played)
  39.  
  40. newLOCS[id].played = true
  41.  
  42. local file = fs.open(filename,"w")
  43. file.write(json.encodePretty(newLOCS))
  44. file.close()
  45.  
  46. local changedLOCS = json.decodeFromFile(filename)
  47. print(#changedLOCS)
  48. print(changedLOCS[id].x)
  49. print(changedLOCS[id].z)
  50. print(changedLOCS[id].played)
  51.  
  52. print(file_exists(filename))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement