Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const { nanoid } = require('nanoid')
  2. const US_LIQUID_GALLONS_TO_CUBIC_INCHES = 231
  3. const CUBIC_INCHES_TO_US_LIQUID_GALLONS = 1 / US_LIQUID_GALLONS_TO_CUBIC_INCHES
  4. const toLiquid = cubicInches => cubicInches * CUBIC_INCHES_TO_US_LIQUID_GALLONS
  5. const fromLiquid = gallons => gallons * US_LIQUID_GALLONS_TO_CUBIC_INCHES
  6. const makeContainerT = (inputs, outputs, container) => [
  7. inputs,
  8. outputs,
  9. container,
  10. ]
  11. const fromConfig = {
  12. tank_1: {
  13. inputs: [1],
  14. outputs: ['foo'],
  15. dims: { l: 24, w: 12, h: 12 },
  16. },
  17. sump_1: {
  18. inputs: [],
  19. outputs: [],
  20. dims: { l: 18, w: 12, h: 12 },
  21. },
  22. ato_1: {
  23. inputs: [],
  24. outputs: [],
  25. dims: { l: 6, w: 12, h: 12 },
  26. },
  27. }
  28. const digestConfig = config => {
  29. const sys = new System()
  30. for (const [containerName, inputsOutputsDims] of Object.entries(config)) {
  31. const { inputs, outputs, dims } = inputsOutputsDims
  32. const containerT = makeContainerT(
  33. inputs,
  34. outputs,
  35. new Container(dims, 0, containerName)
  36. )
  37. sys.add(containerT[2].id, containerT)
  38. }
  39. return sys
  40. }
  41. class InOut {
  42. constructor() {
  43. this.value = null
  44. }
  45. }
  46. /** Holds systems or containers */
  47. class Controller {}
  48. /** Holds systems or containers */
  49. class System {
  50. constructor() {
  51. this.inventory = {}
  52. }
  53. get inputs() {
  54. return Object.values(this.inventory).map(containerT => containerT[0])
  55. }
  56. get outputs() {
  57. return Object.values(this.inventory).map(containerT => containerT[1])
  58. }
  59. get containers() {
  60. return Object.values(this.inventory).map(containerT => containerT[2])
  61. }
  62. add(id, containerT) {
  63. this.inventory[id] = containerT
  64. }
  65. inputsFor({ id }) {
  66. return Object.values(this.inventory).find(
  67. containerT => containerT[2].id == id
  68. )[0]
  69. }
  70. outputsFor({ id }) {
  71. return Object.values(this.inventory).find(
  72. containerT => containerT[2].id == id
  73. )[1]
  74. }
  75. }
  76. class Container {
  77. constructor({ l, w, h }, level = 0, label) {
  78. this._id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
  79. this._label = label ? label : ''
  80. this.l = l
  81. this.w = w
  82. this.h = h
  83. this.level = level
  84. }
  85. get id() {
  86. return this._id
  87. }
  88. get label() {
  89. return this._label
  90. }
  91. get liquidVolumeCapacity() {
  92. return toLiquid(this.l * this.w * this.h)
  93. }
  94. get liquidVolumeFilled() {
  95. return toLiquid(this.l * this.w * this.level)
  96. }
  97. get liquidVolumeRemaining() {
  98. return this.liquidVolumeCapacity - this.liquidVolumeFilled
  99. }
  100. setLevel(updatedLevel) {
  101. this.level = updatedLevel
  102. }
  103. }
  104. const _changeLevel = (amount, container) => {
  105. const updatedLevel =
  106. fromLiquid(container.liquidVolumeFilled + amount) /
  107. container.l /
  108. container.w
  109. container.setLevel(updatedLevel)
  110. }
  111. const fill = ({ container, amount }) => _changeLevel(amount, container)
  112. const drain = ({ container, amount }) => _changeLevel(-1 * amount, container)
  113. mySystem = digestConfig(fromConfig)
  114. const tanks = mySystem.containers
  115. console.log('mySystem :>> ', mySystem.inventory)
  116. console.log('tank :>> ', tanks[0].liquidVolumeCapacity)
  117. console.log('tank :>> ', tanks[0].liquidVolumeFilled)
  118. fill({ container: tanks[0], amount: 10 })
  119. console.log('tank :>> ', tanks[0].liquidVolumeFilled)
  120. console.log('refs intact?', mySystem.containers[0] === tanks[0])
  121. drain({ container: tanks[0], amount: 3 })
  122. console.log('tank :>> ', tanks[0].liquidVolumeFilled)
  123. console.log('inputs :>> ', mySystem.inputsFor(tanks[0]))
  124. console.log('outputs :>> ', mySystem.outputsFor(tanks[0]))