You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

144 line
2.8 KiB

  1. const recipes = require('../config/recipes/recipes');
  2. const buildRecipe = require('./workbench/buildRecipe');
  3. const craft = require('./workbench/craft');
  4. module.exports = {
  5. type: 'workbench',
  6. craftType: null,
  7. noticeMessage: null,
  8. init: function (blueprint) {
  9. this.craftType = blueprint.type;
  10. this.noticeMessage = blueprint.noticeMessage;
  11. this.obj.instance.objects.buildObjects([{
  12. properties: {
  13. x: this.obj.x - 1,
  14. y: this.obj.y - 1,
  15. width: 3,
  16. height: 3,
  17. cpnNotice: {
  18. actions: {
  19. enter: {
  20. cpn: 'workbench',
  21. method: 'enterArea',
  22. targetId: this.obj.id,
  23. args: []
  24. },
  25. exit: {
  26. cpn: 'workbench',
  27. method: 'exitArea',
  28. targetId: this.obj.id,
  29. args: []
  30. }
  31. }
  32. }
  33. }
  34. }]);
  35. },
  36. exitArea: function (obj) {
  37. if (!obj.player)
  38. return;
  39. obj.syncer.setArray(true, 'serverActions', 'removeActions', {
  40. key: 'u',
  41. action: {
  42. cpn: 'workbench',
  43. method: 'open',
  44. data: {
  45. targetId: this.obj.id
  46. }
  47. }
  48. });
  49. this.obj.instance.syncer.queue('onCloseWorkbench', null, [obj.serverId]);
  50. },
  51. enterArea: function (obj) {
  52. if (!obj.player)
  53. return;
  54. let msg = `Press U to ${this.noticeMessage || `access the ${this.obj.name}`}`;
  55. obj.syncer.setArray(true, 'serverActions', 'addActions', {
  56. key: 'u',
  57. name: 'access workbench',
  58. action: {
  59. cpn: 'workbench',
  60. method: 'open',
  61. data: {
  62. targetId: this.obj.id
  63. }
  64. }
  65. });
  66. this.obj.instance.syncer.queue('onGetAnnouncement', {
  67. src: this.obj.id,
  68. msg: msg
  69. }, [obj.serverId]);
  70. },
  71. open: async function (msg) {
  72. if (!msg.has('sourceId'))
  73. return;
  74. let obj = this.obj.instance.objects.objects.find(o => o.serverId === msg.sourceId);
  75. if ((!obj) || (!obj.player))
  76. return;
  77. let thisObj = this.obj;
  78. if ((Math.abs(thisObj.x - obj.x) > 1) || (Math.abs(thisObj.y - obj.y) > 1))
  79. return;
  80. const unlocked = await io.getAsync({
  81. key: obj.name,
  82. table: 'recipes',
  83. isArray: true
  84. });
  85. this.obj.instance.syncer.queue('onOpenWorkbench', {
  86. workbenchId: this.obj.id,
  87. name: this.obj.name,
  88. recipes: recipes.getList(this.craftType, unlocked)
  89. }, [obj.serverId]);
  90. },
  91. getRecipe: function (msg) {
  92. let obj = this.obj.instance.objects.objects.find(o => o.serverId === msg.sourceId);
  93. if ((!obj) || (!obj.player))
  94. return;
  95. const sendRecipe = buildRecipe(this.craftType, obj, msg);
  96. this.resolveCallback(msg, sendRecipe);
  97. },
  98. craft: function (msg) {
  99. const result = craft(this, msg);
  100. if (result)
  101. this.resolveCallback(msg, result);
  102. },
  103. resolveCallback: function (msg, result) {
  104. let callbackId = (msg.has('callbackId')) ? msg.callbackId : msg;
  105. result = result || [];
  106. if (!callbackId)
  107. return;
  108. process.send({
  109. module: 'atlas',
  110. method: 'resolveCallback',
  111. msg: {
  112. id: callbackId,
  113. result: result
  114. }
  115. });
  116. }
  117. };