mail.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. easyvend.purchase_reports = {}
  2. local tb = easyvend.purchase_reports
  3. local RECORD_MERGE_TIME = 60 * 10
  4. local MAIL_FROM = "SERVER"
  5. local SUBJECT_SELL = "Vendor Sold Stuff"
  6. local SUBJECT_BUY = "Depositor Bought Stuff"
  7. local SUBJECT_OFF = "Shop Closed"
  8. local MSG_SELL = "You sold %s from your vending machine near \"%s\" at %s."
  9. local MSG_BUY = "You bought %s through your depositing machine near \"%s\" at %s."
  10. local MSG_OFF = "Your %s %s near \"%s\" at %s is closed."
  11. -- Get the nearest named region, if there is one.
  12. local function nearest_city(pos)
  13. local cityinfo = "outlands"
  14. local cityblock = city_block:nearest_named_region(pos)
  15. if cityblock and cityblock[1] and cityblock[1].area_name then
  16. cityinfo = cityblock[1].area_name
  17. end
  18. return cityinfo
  19. end
  20. -- Check if this record was already recorded during this session.
  21. -- A record is considered "same" if positions are the same.
  22. -- It is necessarily the case that vendors and depositors MUST have different
  23. -- positions, likewise different items require machines with different positions.
  24. -- BUT! If the current time is much newer than the last record, we can record it.
  25. local function record_exists(data)
  26. for k, v in ipairs(tb) do
  27. if vector.equals(v.pos, data.pos) then
  28. if os.time() < (v.record_time + RECORD_MERGE_TIME) then
  29. return true
  30. end
  31. end
  32. end
  33. end
  34. -- To be called when a purchase is made.
  35. function easyvend.record_purchase(data)
  36. if record_exists(data) then
  37. return
  38. end
  39. tb[#tb + 1] = data
  40. tb[#tb].record_time = os.time()
  41. local idef = minetest.registered_items[data.item]
  42. if not idef or not idef.description then
  43. return
  44. end
  45. local to = data.owner
  46. local message = MSG_SELL:format(
  47. utility.get_short_desc(idef.description),
  48. nearest_city(data.pos),
  49. rc.pos_to_namestr(data.pos)
  50. )
  51. email.send_mail_single(MAIL_FROM, to, SUBJECT_SELL, message)
  52. end
  53. -- To be called when a deposit is made.
  54. function easyvend.record_deposit(data)
  55. if record_exists(data) then
  56. return
  57. end
  58. tb[#tb + 1] = data
  59. tb[#tb].record_time = os.time()
  60. local idef = minetest.registered_items[data.item]
  61. if not idef or not idef.description then
  62. return
  63. end
  64. local to = data.owner
  65. local message = MSG_BUY:format(
  66. utility.get_short_desc(idef.description),
  67. nearest_city(data.pos),
  68. rc.pos_to_namestr(data.pos)
  69. )
  70. email.send_mail_single(MAIL_FROM, to, SUBJECT_BUY, message)
  71. end
  72. -- To be called when a vending machine is disabled (usually because out of stock).
  73. function easyvend.record_disable(data)
  74. tb[#tb + 1] = data
  75. tb[#tb].record_time = os.time()
  76. local idef = minetest.registered_items[data.item]
  77. if not idef or not idef.description then
  78. return
  79. end
  80. local to = data.owner
  81. local message = MSG_OFF:format(
  82. utility.get_short_desc(idef.description),
  83. data.machine_type,
  84. nearest_city(data.pos),
  85. rc.pos_to_namestr(data.pos)
  86. )
  87. email.send_mail_single(MAIL_FROM, to, SUBJECT_OFF, message)
  88. end