output.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. var mobile = session.mobiles[mobileId];
  12. if(mobile == null || mobile == {}) {
  13. return;
  14. }
  15. if(source != null && Array.isArray(source)) {
  16. for(var i = 0; i < source.length; i++) {
  17. if(source[i].id == mobileId) {
  18. return;
  19. }
  20. }
  21. }
  22. mobile.mud_sendMessage(source, message);
  23. }
  24. toRoom(roomName, message, source) {
  25. var room = session.rooms[roomName];
  26. if(room == null || room == {}) {
  27. return;
  28. }
  29. for(var i = 0; i < room.mobiles.length; i++) {
  30. var mobileId = room.mobiles[i];
  31. if(source != null && source.id == mobileId) {
  32. continue;
  33. }
  34. this.toMobile(mobileId, message, source);
  35. }
  36. }
  37. matchesId(sources, id) {
  38. for(var i = 0; i < sources.length; i++) {
  39. if(sources[i].id == id) {
  40. return sources[i];
  41. }
  42. }
  43. return sources[0];
  44. }
  45. worldToMobiles(message, source) {
  46. for(var mobileId in session.mobiles) {
  47. var mobile = session.mobiles[mobileId];
  48. if(mobile == null || mobile == {}) {
  49. continue;
  50. }
  51. if(source != null && mobile.id === source.id) {
  52. continue;
  53. }
  54. this.toMobile(mobile.id, message, source);
  55. }
  56. }
  57. }
  58. module.exports = new Output();