Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

140 rindas
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. targetId: this.obj.id,
  43. cpn: 'workbench',
  44. method: 'access'
  45. }
  46. });
  47. this.obj.instance.syncer.queue('onCloseWorkbench', null, [obj.serverId]);
  48. },
  49. enterArea: function (obj) {
  50. if (!obj.player)
  51. return;
  52. let msg = `Press U to ${this.noticeMessage || `access the ${this.obj.name}`}`;
  53. obj.syncer.setArray(true, 'serverActions', 'addActions', {
  54. key: 'u',
  55. name: 'access workbench',
  56. action: {
  57. targetId: this.obj.id,
  58. cpn: 'workbench',
  59. method: 'open'
  60. }
  61. });
  62. this.obj.instance.syncer.queue('onGetAnnouncement', {
  63. src: this.obj.id,
  64. msg: msg
  65. }, [obj.serverId]);
  66. },
  67. open: async function (msg) {
  68. if (!msg.has('sourceId'))
  69. return;
  70. let obj = this.obj.instance.objects.objects.find(o => o.serverId === msg.sourceId);
  71. if ((!obj) || (!obj.player))
  72. return;
  73. let thisObj = this.obj;
  74. if ((Math.abs(thisObj.x - obj.x) > 1) || (Math.abs(thisObj.y - obj.y) > 1))
  75. return;
  76. const unlocked = await io.getAsync({
  77. key: obj.name,
  78. table: 'recipes',
  79. isArray: true
  80. });
  81. this.obj.instance.syncer.queue('onOpenWorkbench', {
  82. workbenchId: this.obj.id,
  83. name: this.obj.name,
  84. recipes: recipes.getList(this.craftType, unlocked)
  85. }, [obj.serverId]);
  86. },
  87. getRecipe: function (msg) {
  88. let obj = this.obj.instance.objects.objects.find(o => o.serverId === msg.sourceId);
  89. if ((!obj) || (!obj.player))
  90. return;
  91. const sendRecipe = buildRecipe(this.craftType, obj, msg);
  92. this.resolveCallback(msg, sendRecipe);
  93. },
  94. craft: function (msg) {
  95. const result = craft(this, msg);
  96. if (result)
  97. this.resolveCallback(msg, result);
  98. },
  99. resolveCallback: function (msg, result) {
  100. let callbackId = (msg.has('callbackId')) ? msg.callbackId : msg;
  101. result = result || [];
  102. if (!callbackId)
  103. return;
  104. process.send({
  105. module: 'atlas',
  106. method: 'resolveCallback',
  107. msg: {
  108. id: callbackId,
  109. result: result
  110. }
  111. });
  112. }
  113. };