Advertisement
Guest User

dynmap-chat.js

a guest
Sep 21st, 2012
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. DynMap Chat Importer
  4. ===
  5.  
  6. Displays server chat pulled from the DynMap plugin
  7. ---
  8.  
  9. by Windigo < http://micro.fragdev.com/windigo/ >
  10.  
  11. This program is free software: you can redistribute it and/or modify
  12. it under the terms of the GNU Affero General Public License as
  13. published by the Free Software Foundation, either version 3 of the
  14. License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful, but
  17. WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. General Public License for more details.
  20.  
  21. You should have received a copy of the GNU Affero General Public
  22. License along with this program.  If not, see
  23. <http://www.gnu.org/licenses/>.
  24.  
  25. */
  26.  
  27. var DynMapChat = {
  28.    
  29.     // ID of page element to be replaced
  30.     chatBox : '#block-block-2 .content',
  31.    
  32.     // Cache of messages
  33.     chatCache : [],
  34.    
  35.     // Length of message cache before trim
  36.     chatCacheLength : 15,
  37.    
  38.     // URL of JSON feed from DynMap plugin
  39.     chatFeed : 'http://public.tekkitcrunch.com/map/standalone/dynmap_public.json',
  40.  
  41.     // Interval, in seconds, to udpate the chat box
  42.     updateInterval : 5,
  43.  
  44.  
  45.     // Initialization function
  46.     Init : function()
  47.     {
  48.         //$('#'+DynMapChat.chatBox+' h2').text('Dynamic Chat');
  49.  
  50.         DynMapChat.UpdateChatBox();
  51.     },
  52.  
  53.  
  54.     // Update Chat box with new values
  55.     UpdateChatBox : function()
  56.     {
  57.         var chatContents = jQuery('<ul></ul>');
  58.  
  59.         // Pull new chat messages from the JSON feed
  60.         jQuery.getJSON(DynMapChat.chatFeed, function(data)
  61.         {
  62.             // Add messages to the cache
  63.             jQuery(data.updates).each(function(i, item)
  64.             {
  65.                 if(item.type == 'chat')
  66.                 {
  67.                     var match = jQuery(DynMapChat.chatCache)
  68.                     .filter(function(j)
  69.                     {
  70.                         return (this.timestamp == item.timestamp &&
  71.                          this.playerName == item.playerName &&
  72.                          this.message == item.message);
  73.                     })
  74.                     .length;
  75.  
  76.                     if(match < 1)
  77.                     {
  78.                         DynMapChat.chatCache.push(item);
  79.                     }
  80.                 }
  81.             });
  82.  
  83.             // Sort cache by timestamp, descending
  84.             DynMapChat.chatCache.sort(function(a, b)
  85.             {
  86.                 if(a.timestamp == b.timestamp)
  87.                 {
  88.                     return 0;
  89.                 }
  90.                 else if(a.timestamp < b.timestamp)
  91.                 {
  92.                     return 1;
  93.                 }
  94.                
  95.                 return -1;
  96.             });
  97.  
  98.             // Trim cache of older values
  99.             if(DynMapChat.chatCache.length > DynMapChat.chatCacheLength)
  100.             {
  101.                 DynMapChat.chatCache = DynMapChat.chatCache.slice(0,
  102.                     DynMapChat.chatCacheLength - 1);
  103.             }
  104.  
  105.  
  106.             // Covert cache to HTML elements
  107.             jQuery(DynMapChat.chatCache).each(function(i, item)
  108.             {
  109.                 chatContents.append('<li><strong>'+item.playerName
  110.                     +'</strong>: '+item.message+'</li>');
  111.             });
  112.  
  113.             // Replace older contents with latest
  114.             jQuery(DynMapChat.chatBox)
  115.             .empty()
  116.             .append(chatContents);
  117.         });
  118.  
  119.         // Refresh chat box on a defined interval
  120.         window.setTimeout(function()
  121.         {
  122.             DynMapChat.UpdateChatBox();
  123.         }, DynMapChat.updateInterval * 1000);
  124.     }
  125. };
  126.  
  127.  
  128.  
  129. // Initialize the DynMap Chat Display
  130. (function ($)
  131. {
  132.     DynMapChat.Init();
  133. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement