Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /**
  2. * This is the Insulin class
  3. * Create a new instance of this class for basal and fast-acting insulins.
  4. *
  5. * param {number} onsetDelay - the number of seconds before the insulin effect kicks instance.
  6. * param {number} duration - the number of seconds of the total effective duration of this insulin.
  7. * param {number} peak - the number of seconds to the peak action of the insulin. Set to 0 for a flat (consistent) effect. Is modelled as a bell curve centered on the peak.
  8. * param {number} power - the effect of the insulin. This is the peak power for insulins wiht a peak response.
  9. */
  10. const magik = magikcraft.io;
  11. const secondsPerTick = 1;
  12.  
  13. function Insulin(onsetDelay = 0, duration, power, peak = 0) {
  14. this.onsetDelay = onsetDelay;
  15. this.duration = duration;
  16. this.peak = peak;
  17. this.power = power;
  18. }
  19.  
  20. Insulin.prototype.calculateInsulinEffect = function(elapsedTime) {
  21. if (this.peak === 0) {
  22. return this.power;
  23. }
  24. const a = Math.atan(this.power / (this.duration * 0.5));
  25. const getE = () => {
  26. if (elapsedTime <= this.duration / 2) {
  27. return a * elapsedTime;
  28. } else {
  29. return a * this.duration - elapsedTime;
  30. }
  31. };
  32. const e = getE();
  33. const effect = e * a;
  34. }
  35.  
  36. Insulin.prototype.take = function(amount) {
  37. let elapsedTime = this.onsetDelay;
  38. const magik = magikcraft.io
  39. const mct1 = magik.global('mct1');
  40.  
  41. magik.setTimeout(() => {
  42. // the insulin starts to act
  43. let _loop = magik.setInterval(
  44. () => {
  45. if (elapsedTime >= this.duration - this.onsetDelay) {
  46. // insulin effect exhausted
  47. magik.clearInterval(_loop);
  48. return;
  49. }
  50. // == Do Insulin effect ==
  51. // calculate insulin power
  52. const effect = this.calculateInsulinEffect(elapsedTime);
  53. mct1.mutateBGL(effect);
  54. elapsedTime += secondsPerTick;
  55. },
  56. secondsPerTick
  57. );
  58. }, this.onsetDelay);
  59. }
  60.  
  61. module.exports = Insulin;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement