That_Shaman

Guild Wars 2 currency chat link generator

Mar 7th, 2013
1,513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title>GW2 Coinage</title>
  5.     <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  6. </head>
  7. <body>
  8.     <input type="text" class="coinage" id="gold" value="0" maxlength="6" ondrop="return false"
  9.         onpaste="return false" autocomplete="off" />
  10.     gold
  11.     <br />
  12.     <input type="text" class="coinage" id="silver" value="0" maxlength="2" ondrop="return false"
  13.         onpaste="return false" autocomplete="off" />
  14.     silver<br />
  15.     <input type="text" class="coinage" id="copper" value="0" maxlength="2" ondrop="return false"
  16.         onpaste="return false" autocomplete="off" />
  17.     copper
  18.     <div id="chatCode">
  19.     </div>
  20.     <script type="text/javascript">
  21.  
  22.         /**
  23.         *
  24.         * Generates coinage link codes for Guild Wars 2
  25.         *
  26.         * @author     That Shaman <http://www.reddit.com/user/that_shaman>
  27.         * @license    http://opensource.org/licenses/MIT
  28.         * @link       http://thatshaman.blogspot.com
  29.         * @version    1.0.0
  30.         *
  31.         */
  32.  
  33.         // Constant for unsigned long integer
  34.         var ULONG_MAX = 4294967295;
  35.  
  36.         // The wallet object can store and convert coins to link codes
  37.         var wallet = {
  38.             copper: 0, silver: 0, gold: 0,
  39.             getCoinTotal: function () // Gets total ammount of coin (in copper)
  40.             {
  41.                 return (this.copper + (this.silver * 100) + (this.gold * 10000));
  42.             },
  43.             getCoinLink: function (coin) // Converts coin to GW2 chat link code
  44.             {
  45.                 // If coin is undefined use the current ammount in wallet
  46.                 coin = coin || this.getCoinTotal();
  47.  
  48.                 // Clamp the amount of coin between 0 and ULONG_MAX
  49.                 coin = (coin < 0 ? 0 : (coin > ULONG_MAX ? ULONG_MAX : coin));
  50.  
  51.                 // Coinage link starts with chr(01)
  52.                 var chatCode = String.fromCharCode(1);
  53.  
  54.                 // Perform bitwise operation on coins and append character to string
  55.                 for (i = 0; i < 4; i++) chatCode += String.fromCharCode((coin >> (i * 8)) & 255);
  56.  
  57.                 // Return base64 string with chat code tags
  58.                 return "[&" + btoa(chatCode) + "]";
  59.             }
  60.         }
  61.  
  62.         // Validate keyboard input
  63.         function addKeyBoardListener(inputField)
  64.         {
  65.             inputField.keydown(function (event)
  66.             {
  67.                 var key = event.which;
  68.  
  69.                 // Allowed keystrokes: Number keys (48<->57), Numpad (96<->105), Arrow keys (37<->40), delete (46), escape (27), backspace (8), tab (9)            
  70.                 if ((key >= 48 && key <= 57) || (key >= 96 && key <= 105) || (key >= 37 && key <= 40) || key == 46 || key == 27 || key == 8 || key == 9)
  71.                 {
  72.                     return true;
  73.                 }
  74.                 else
  75.                 {
  76.                     event.preventDefault();
  77.                     return false;
  78.                 }
  79.             });
  80.  
  81.             inputField.keyup(function (event)
  82.             {
  83.                 updateFields();
  84.             });
  85.         }
  86.  
  87.         // Update the coinage textfields and chat code label
  88.         function updateFields()
  89.         {
  90.             // Update the wallet with user input
  91.             wallet.copper = parseInt($('#copper').val()) || 0;
  92.             wallet.silver = parseInt($('#silver').val()) || 0;
  93.             wallet.gold = parseInt($('#gold').val()) || 0;
  94.  
  95.             // Update chat link
  96.             $('#chatCode').text(wallet.getCoinLink());
  97.  
  98.             // Reset input fields (cheap trim with integer parsing)
  99.             if (wallet.getCoinTotal() <= ULONG_MAX)
  100.             {
  101.                 $('#copper').val(wallet.copper.toString());
  102.                 $('#silver').val(wallet.silver.toString());
  103.                 $('#gold').val(wallet.gold.toString());
  104.             }
  105.             else // Maximum reached
  106.             {
  107.                 $('#copper').val("95");
  108.                 $('#silver').val("72");
  109.                 $('#gold').val("429496");
  110.             }
  111.         }
  112.  
  113.         // Get this thing up and running
  114.         $(document).ready(function ()
  115.         {
  116.             // Add keyboard listeners to the textfields
  117.             addKeyBoardListener($('.coinage'));
  118.  
  119.             // Reset all textfields and chat code to 0
  120.             updateFields();
  121.         });
  122.     </script>
  123. </body>
  124. </html>
Add Comment
Please, Sign In to add comment