output.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. class Output {
  2. toServer(message) {
  3. //process.stdout.write(message);
  4. console.log(message);
  5. }
  6. toServerError(message, error) {
  7. //process.stderr.write(message);
  8. console.error(message, error, error.stack);
  9. }
  10. toMobile(mobileId, message, source) {
  11. if(!Array.isArray(mobileId)) {
  12. mobileId = [mobileId];
  13. }
  14. for(var mobileIndex in mobileId) {
  15. var mobile = session.mobiles[mobileId[mobileIndex]];
  16. if(mobile == null || mobile == {}) {
  17. continue;
  18. }
  19. var messages = message.split("\r\n");
  20. for(var messageIndex in messages) {
  21. mobile.mud_sendMessage(source, messages[messageIndex]);
  22. }
  23. }
  24. }
  25. toRoom(roomName, message, source) {
  26. var room = session.rooms[roomName];
  27. if(room == null || room == {}) {
  28. return;
  29. }
  30. for(var mobileIndex in room.mobiles) {
  31. var mobileId = room.mobiles[mobileIndex];
  32. if(this.isIncludedInSources(mobileId, source)) {
  33. continue;
  34. }
  35. this.toMobile(mobileId, message, source);
  36. }
  37. }
  38. isIncludedInSources(mobileId, source) {
  39. var sources = [];
  40. if(Array.isArray(source)) {
  41. sources = source;
  42. } else {
  43. sources = [source];
  44. }
  45. for(var sourceIndex in sources) {
  46. var sourceObj = sources[sourceIndex];
  47. if(sourceObj != null && sourceObj.id == mobileId) {
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53. matchesId(sources, id) {
  54. for(var i = 0; i < sources.length; i++) {
  55. if(sources[i].id == id) {
  56. return sources[i];
  57. }
  58. }
  59. return sources[0];
  60. }
  61. worldToMobiles(message, source) {
  62. this.toMobile(session.mobiles, message, source);
  63. }
  64. toBuildersError(message, error) {
  65. for(var mobileId in session.mobiles) {
  66. var mobile = session.mobiles[mobileId];
  67. if(mobile == null || mobile == {}) {
  68. continue;
  69. }
  70. if(world.mud_mobileHasRole(mobile.id, "builder")) {
  71. this.toMobile(mobile.id, message);
  72. this.toMobile(mobile.id, error.stack);
  73. }
  74. }
  75. }
  76. }
  77. module.exports = new Output();