Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container text-white mx-auto w-full max-w-xl mt-10 p-4 rounded-2xl">
  <h1 class="text-2xl text-center font-bold mb-6">BitNile Bingo Verifier</h1>

  <label for="serverSeed">Server Seed</label>
  <input id="serverSeed" type='text' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />

  <label for="clientSeed">Client Seed</label>
  <input id="clientSeed" type='text' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />
  
  <label for="nonce">Nonce</label>
  <input id="nonce" type='number' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />  
  
  <label for="numCards">Number of Cards</label>
  <input id="numCards" type='number' class='w-full py-2 px-3 rounded-xl mt-1 mb-2' />  

  <div id="verify" class='bg-white hover:bg-white/90 cursor-pointer text-center p-3 rounded-xl mt-3 text-black text-md font-medium'>
     Verify Game
  </div>
  
  <div id="result" class='my-6'>
  </div>
</div>
              
            
!

CSS

              
                body, input, select {
  background: #2a2e31;
}

.container {
  background: #1B1B1D;
}

label {
  color: #aaa;
}
              
            
!

JS

              
                import lodash from "https://esm.sh/lodash";

const createHmacString = (privateKey, message) => {
    const key = CryptoJS.enc.Utf8.parse(privateKey);
    const hmac = CryptoJS.HmacSHA256(message, key);
    return CryptoJS.enc.Hex.stringify(hmac);
}

function* byteGenerator({
  serverSeed,
  clientSeed,
  nonce,
  cursor,
}: {
  serverSeed: string;
  clientSeed: string;
  nonce: number;
  cursor: number;
}) {
  let currentRound = Math.floor(cursor / 32);
  let currentRoundCursor = cursor;
  currentRoundCursor -= currentRound * 32;

  while (true) {
    const message = `${clientSeed}:${nonce}:${currentRound}`;
    const hmacHex = createHmacString(serverSeed, message);
    const buffer = CryptoJS.enc.Hex.parse(hmacHex);

    while (currentRoundCursor < 32) {
      yield Number(buffer.words[Math.floor(currentRoundCursor / 4)] >> (8 * (3 - (currentRoundCursor % 4))) & 0xff);
      currentRoundCursor += 1;
    }
    currentRoundCursor = 0;
    currentRound += 1;
  }
}

function generateFloats({
  serverSeed,
  clientSeed,
  nonce,
  cursor,
  count,
}: {
  serverSeed: string;
  clientSeed: string;
  nonce: number;
  cursor: number;
  count: number;
}): number[] {
  const generator = byteGenerator({ serverSeed, clientSeed, nonce, cursor });
  const bytes = [];
  while (bytes.length < count * 4) {
    const nextValue = generator.next().value;
    if (typeof nextValue === "number") {
      bytes.push(nextValue);
    } else {
      throw new Error("Generator did not yield a number");
    }
  }

  return lodash.chunk(bytes, 4).map((bytesChunk) => {
    return bytesChunk.reduce((result, value, i) => {
      const divider = 256 ** (i + 1);
      const partialResult = value / divider;
      return result + partialResult;
    }, 0);
  });
}

function generateBingoCards(numberOfBingoCards: number, seeds: Seeds): Promise<BingoCard[]> {
  const cards: BingoCard[] = [];

  let floatIndex = 0;
  const floats = generateFloats({
    serverSeed: seeds.serverSeed,
    clientSeed: seeds.clientSeed,
    nonce: seeds.nonce,
    cursor: 76, // Starting cursor
    count: numberOfBingoCards * 50, // Number of random floats to generate
  });

  // Helper function to generate unique random numbers within a range
  const getRandomNumbers = (min: number, max: number, count: number): number[] => {
    const numbers = new Set<number>();
    while (numbers.size < count) {
      const newNumber = Math.floor(floats[floatIndex] * (max - min + 1)) + min;
      if (!numbers.has(newNumber)) {
        numbers.add(newNumber);
      }
      floatIndex++;
    }
    return Array.from(numbers);
  };

  for (let i = 0; i < numberOfBingoCards; i++) {
    const card = {
      id: i,
      b: getRandomNumbers(1, 15, 5), // Column B (1-15)
      i: getRandomNumbers(16, 30, 5), // Column I (16-30)
      n: getRandomNumbers(31, 45, 4), // Column N (31-45), only 4 numbers (free space in middle)
      g: getRandomNumbers(46, 60, 5), // Column G (46-60)
      o: getRandomNumbers(61, 75, 5), // Column O (61-75)
    };

    // Add the free space (-1) in the middle of the "n" column (index 2)
    card.n.splice(2, 0, -1);

    cards.push(card);
  }

  return cards;
}

function generateNumbers(seeds: Seeds): Promise<number[]> {
  // Generate 75 random floats using the provided generateFloats function
  const floats = generateFloats({
    serverSeed: seeds.serverSeed,
    clientSeed: seeds.clientSeed,
    nonce: seeds.nonce,
    cursor: 0, // Starting cursor
    count: 75, // Number of random floats to generate
  });

  // Create an array of numbers from 1 to 75
  const numbers = Array.from({ length: 75 }, (_, i) => i + 1);

  // Pair each number with its corresponding float
  const paired = numbers.map((number, index) => ({
    number,
    float: floats[index],
  }));

  // Sort the paired array based on the random floats to shuffle the numbers
  paired.sort((a, b) => a.float - b.float);

  // Extract the sorted numbers into a new array
  const shuffledNumbers = paired.map((pair) => pair.number);

  return shuffledNumbers;
}


document.getElementById('verify').onclick = () => {
  const serverSeed = (document.getElementById('serverSeed') as HTMLInputElement).value;
  const nonce = Number((document.getElementById('nonce') as HTMLInputElement).value);
  const clientSeed = (document.getElementById('clientSeed') as HTMLInputElement).value;
  const numCards = Number((document.getElementById('numCards') as HTMLInputElement).value);
  
  // Input validation
  if (!serverSeed) return error('Server seed is required');
  if (isNaN(nonce)) return error('Nonce must be a number');
  if (!clientSeed) return error('Client seed is required');
  if (!numCards) return error('Number of cards is required');
  if (numCards > 8) return error('Number of cards exceeds the maximum allowed');
  
  // Generate Bingo cards and called numbers
  const cards = generateBingoCards(numCards, { serverSeed, clientSeed, nonce });
  const numbers = generateNumbers({ serverSeed, clientSeed, nonce });
  
  // Function to create HTML table for a single Bingo card
  const createCardHTML = (card: BingoCard) => {
    return `
      <div class="bingo-card" style="display: inline-block; margin: 10px;">
        <h3>Card ${card.id + 1}</h3>
        <table border="1" cellpadding="5" cellspacing="0">
          <thead>
            <tr>
              <th>B</th>
              <th>I</th>
              <th>N</th>
              <th>G</th>
              <th>O</th>
            </tr>
          </thead>
          <tbody>
            ${[0,1,2,3,4].map(row => `
              <tr>
                <td>${card.b[row]}</td>
                <td>${card.i[row]}</td>
                <td>${card.n[row] === -1 ? 'FREE' : card.n[row]}</td>
                <td>${card.g[row]}</td>
                <td>${card.o[row]}</td>
              </tr>
            `).join('')}
          </tbody>
        </table>
      </div>
    `;
  };
  
  // Generate HTML for all cards
  const cardsHTML = cards.map(createCardHTML).join('');
  
  // Generate HTML for called numbers
  const numbersHTML = numbers.join(', ');
  
  // Update the result container with formatted HTML
  (document.getElementById('result') as HTMLElement).innerHTML = `
    <div class="text-center">
      <div class="text-gray-300">
        <h2>Bingo Cards:</h2>
        <div>${cardsHTML}</div>
        <h2>Called Numbers:</h2>
        <div>${numbersHTML}</div>
      </div>
    </div>
  `;
};
              
            
!
999px

Console