Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: win chance percentage calculation and added xp reward in ui #30

Merged
merged 1 commit into from
Aug 1, 2021

Conversation

arvi0731
Copy link

@arvi0731 arvi0731 commented Jul 31, 2021

I noticed that the winning chance percentage of enemy # 1 is greater than enemy # 4 even if enemy # 1 is stronger. Turns out they are both 100%. Sure win 😅

screenshot-localhost_3000-2021 07 31-13_47_00

Test:

function getWinChance(enemyPower) {
  const playerPower = 6341.099999999999
  const playerMin = playerPower * 0.9;
  const playerMax = playerPower * 1.1;
  const playerRange = playerMax - playerMin;
  const enemyMin = enemyPower * 0.9;
  const enemyMax = enemyPower * 1.1;
  let win = 0;
  let lose = 0;
  for (let playerRoll = Math.floor(playerMin); playerRoll <= playerMax; playerRoll++) {
    for (let enemyRoll = Math.floor(enemyMin); enemyRoll <= enemyMax; enemyRoll++) {
      if (playerRoll >= enemyRoll) {
        win++;
      } else {
        lose++;
      }
    }
  }
  const enemyRange = enemyMax - enemyMin;
  let rollingTotal = 0;
  // shortcut: if it is impossible for one side to win, just say so
  //if (playerMin > enemyMax) return 3;
  if (playerMax < enemyMin) return 0;

  // case 1: player power is higher than enemy power
  if (playerMin >= enemyMin) {
      // case 1: enemy roll is lower than player's minimum
      rollingTotal = (playerMin - enemyMin) / enemyRange;
      // case 2: 1 is not true, and player roll is higher than enemy maximum
      rollingTotal += (1 - rollingTotal) * ((playerMax - enemyMax) / playerRange);
      // case 3: 1 and 2 are not true, both values are in the overlap range. Since values are basically continuous, we assume 50%
      rollingTotal += (1 - rollingTotal) * 0.5;
  } else {
      // case 1: player rolls below enemy minimum
      rollingTotal = (enemyMin - playerMin) / playerRange;
      // case 2: enemy rolls above player maximum
      rollingTotal += (1 - rollingTotal) * ((enemyMax - playerMax) / enemyRange);
      // case 3: 1 and 2 are not true, both values are in the overlap range
      rollingTotal += (1 - rollingTotal) * 0.5;
      // since this is chance the enemy wins, we negate it
      rollingTotal = 1 - rollingTotal;
  }

  console.log({
    playerPower,
    playerMin,
    playerMax,
    enemyPower,
    enemyMin,
    enemyMax,
    rollingTotal,
    winChance: win / (win + lose),
  })
}

getWinChance(5151)
getWinChance(5147)

Output:

{
  playerPower: 6341.099999999999,
  playerMin: 5706.99,
  playerMax: 6975.21,
  enemyPower: 5151,
  enemyMin: 4635.900000000001,
  enemyMax: 5666.1,
  rollingTotal: 0.9993601354025403,
  winChance: 1
}
{
  playerPower: 6341.099999999999,
  playerMin: 5706.99,
  playerMax: 6975.21,
  enemyPower: 5147,
  enemyMin: 4632.3,
  enemyMax: 5661.700000000001,
  rollingTotal: 0.9992144101131548,
  winChance: 1
}

@netlify
Copy link

netlify bot commented Jul 31, 2021

❌ Deploy Preview for cbtracker failed.

🔨 Explore the source changes: 1c8ec0e

🔍 Inspect the deploy log: https://app.netlify.com/sites/cbtracker/deploys/6105e3505e6bfe00080cf552

@arvi0731
Copy link
Author

arvi0731 commented Aug 1, 2021

#26

@ed3ath ed3ath merged commit d9e37da into ed3ath:master Aug 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants