Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

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