Advertisement
depth1

console_algo

Oct 5th, 2019
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1.  
  2. static inline var
  3. {
  4.  
  5. }
  6.  
  7. var coords
  8. {
  9. chat :
  10. {
  11. start : [1,1],
  12. width : 30,
  13. height : 30,
  14. history_length : 200
  15. },
  16. room :
  17. {
  18. start : [1,1],
  19. width : 30,
  20. height : 1
  21. },
  22. users :
  23. {
  24. start : [1,1],
  25. width : 20,
  26. height : 30
  27. },
  28. input :
  29. {
  30. start : [1,1],
  31. width : 30,
  32. height : 1
  33. },
  34. }
  35.  
  36. var chat_lines = [];
  37. var users_lines = [];
  38.  
  39. room(name)
  40. {
  41. clear_room();
  42.  
  43. position(ROOM);
  44. write(name);
  45. position(INPUT);
  46. }
  47.  
  48. chat(nick, text)
  49. {
  50. // Format width
  51. text = '<${nick}>:$text';
  52. var temp = '';
  53. if(text.length < coords.chat.width)
  54. {
  55. chat_lines.append(text);
  56. }
  57. else
  58. {
  59. for(i in 0...text.length)
  60. {
  61. temp += text.charAt(i);
  62. if(i % coords.chat.width == 0)
  63. {
  64. chat_lines.push(text);
  65. temp = '';
  66. }
  67. }
  68. }
  69.  
  70. // Format history (to avoid overflow)
  71. while(chat_lines.length > coords.chat.history_length)
  72. chat_lines.shift(); // TODO: save into log
  73.  
  74. var current_chat_lines = chat_lines;
  75.  
  76. // Format height
  77. while(current_chat_lines.length > coords.chat.height)
  78. current_chat_lines.shift();
  79.  
  80. // Draw lines
  81. clear_chat();
  82.  
  83. for(i in 0...coords.chat.height)
  84. {
  85. position(CHAT(i));
  86. write(current_chat_lines(i));
  87. }
  88.  
  89. position(INPUT);
  90. }
  91.  
  92. // Add or delete user if exist
  93. user(nick)
  94. {
  95. // If user called and exist, delete it
  96. if(Lambda.exists(users_lines, nick))
  97. users_lines.remove(nick);
  98. else
  99. users_lines.push(nick);
  100.  
  101. // Format height
  102. if(users_lines > coords.users.height)
  103. {
  104. while(users_lines.length > coords.users.height-1) // -1 to add ...
  105. users_lines.pop();
  106. users_line.push('...');
  107. }
  108.  
  109. // Format width
  110. for(line in users_lines)
  111. {
  112. if(line.length > coords.users.width)
  113. {
  114. line = line.substring(0, coords.users.width-3);
  115. line += '...';
  116. }
  117. }
  118.  
  119. // Draw lines
  120. clear_users();
  121.  
  122. for(i in 0...coords.users.height)
  123. {
  124. position(USERS(i));
  125. write(users_line(i));
  126. }
  127.  
  128. position(INPUT);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement