-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
28 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
const SPECIAL_DIGIT = 7; | ||
|
||
/** | ||
* Count the number of 7s in any number | ||
* @param {number} number The number to count sevens for | ||
* @returns {number} The number of 7s in the provided number | ||
*/ | ||
export function CountSevens(number) { | ||
return String(number).split(String(SPECIAL_DIGIT)).length - 1; | ||
} | ||
|
||
/** | ||
* Calculate the delta for the next number where the given digit is a 7 | ||
* @param {number} number The starting number to calculate the delta for | ||
* @param {number} digitPlace 1 for ones place, 10 for tens place, 100 for hundreds place, etc | ||
* @returns {number} The calculated delta | ||
*/ | ||
export function CalculateSevenDelta(number, digitPlace) { | ||
const target = SPECIAL_DIGIT * digitPlace; | ||
const modulus = digitPlace * 10; | ||
|
||
let delta = target - (number % modulus) + (number % digitPlace); | ||
if (delta < 0) delta += modulus; | ||
|
||
return delta; | ||
} | ||
|
||
/** | ||
* This function calculates each of the next "lucky" prestige levels | ||
* @param {number} currentLevel The user's prestige level, including levels earned since the last ascension | ||
* @returns {{number}, {number}, {number}} luckyDigit, luckyNumber, luckyPayout The next eligible level for each upgrade | ||
*/ | ||
export default function CalculateLuckyLevels(currentLevel) { | ||
const result = {}; | ||
let sevenCount = CountSevens(currentLevel); | ||
const numberOfDigits = String(currentLevel).length; | ||
|
||
if (sevenCount >= 1) { | ||
result.luckyDigit = currentLevel; | ||
if (sevenCount >= 2) { | ||
result.luckyNumber = currentLevel; | ||
if (sevenCount >= 4) { | ||
result.luckyPayout = currentLevel; | ||
return result; | ||
} | ||
} | ||
} | ||
|
||
// Consider only top 15 digits if it is big number | ||
let localLevel; | ||
if (numberOfDigits >= 16) { | ||
localLevel = Math.ceil(currentLevel / 10 ** (numberOfDigits - 15)); | ||
} else { | ||
localLevel = currentLevel; | ||
} | ||
sevenCount = CountSevens(localLevel); | ||
|
||
if (result.luckyDigit === undefined) { | ||
if (sevenCount < 1) { | ||
const delta = CalculateSevenDelta(localLevel, 1); | ||
|
||
localLevel += delta; | ||
sevenCount = CountSevens(localLevel); | ||
} | ||
|
||
result.luckyDigit = localLevel; | ||
if (numberOfDigits >= 16) { | ||
result.luckyDigit *= 10 ** Number(numberOfDigits - 15); | ||
} | ||
} | ||
|
||
if (result.luckyNumber === undefined) { | ||
while (sevenCount < 2) { | ||
let delta = CalculateSevenDelta(localLevel, 1); | ||
if (delta === 0) delta = CalculateSevenDelta(localLevel, 10); | ||
|
||
localLevel += delta; | ||
sevenCount = CountSevens(localLevel); | ||
} | ||
|
||
result.luckyNumber = localLevel; | ||
if (numberOfDigits >= 16) { | ||
result.luckyNumber *= 10 ** Number(numberOfDigits - 15); | ||
} | ||
} | ||
|
||
let digitPlace = 1; | ||
while (sevenCount < 4) { | ||
const delta = CalculateSevenDelta(localLevel, digitPlace); | ||
if (delta === 0) { | ||
digitPlace *= 10; | ||
} else { | ||
localLevel += delta; | ||
sevenCount = CountSevens(localLevel); | ||
} | ||
} | ||
|
||
result.luckyPayout = localLevel; | ||
if (numberOfDigits >= 16) { | ||
result.luckyPayout *= 10 ** Number(numberOfDigits - 15); | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
|
||
import CalculateLuckyLevels, { | ||
CalculateSevenDelta, | ||
CountSevens, | ||
} from '../../src/Disp/HelperFunctions/CalculateLuckyLevels'; | ||
|
||
describe('CountSevens', () => { | ||
const examples = [ | ||
{ input: 1234567890, output: 1 }, | ||
{ input: 7777777777, output: 10 }, | ||
{ input: 1111111111, output: 0 }, | ||
{ input: 7897897897, output: 4 }, | ||
]; | ||
|
||
examples.forEach((example) => { | ||
it(`Counts sevens in ${example.input}`, () => { | ||
expect(CountSevens(example.input)).to.equal(example.output); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('CalculateSevenDelta', () => { | ||
const examples = [ | ||
{ number: 123, digit: 1, output: 4 }, | ||
{ number: 123, digit: 10, output: 50 }, | ||
{ number: 123, digit: 100, output: 600 }, | ||
{ number: 123, digit: 1000, output: 7000 }, | ||
{ number: 7777, digit: 1, output: 0 }, | ||
{ number: 7777, digit: 10, output: 0 }, | ||
{ number: 7777, digit: 100, output: 0 }, | ||
{ number: 7777, digit: 1000, output: 0 }, | ||
{ number: 9999, digit: 1, output: 8 }, | ||
{ number: 9999, digit: 10, output: 80 }, | ||
{ number: 9999, digit: 100, output: 800 }, | ||
{ number: 9999, digit: 1000, output: 8000 }, | ||
]; | ||
|
||
examples.forEach((example) => { | ||
it(`Calculates delta from ${example.number} to the next 7 in the ${example.digit}s digit`, () => { | ||
expect(CalculateSevenDelta(example.number, example.digit)).to.equal(example.output); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('CalculateLuckyLevels', () => { | ||
const examples = [ | ||
{ input: 0, luckyDigit: 7, luckyNumber: 77, luckyPayout: 7777 }, | ||
{ input: 123, luckyDigit: 127, luckyNumber: 177, luckyPayout: 7777 }, | ||
{ input: 77777, luckyDigit: 77777, luckyNumber: 77777, luckyPayout: 77777 }, | ||
{ input: 799999, luckyDigit: 799999, luckyNumber: 800077, luckyPayout: 807777 }, | ||
{ input: 999999, luckyDigit: 1000007, luckyNumber: 1000077, luckyPayout: 1007777 }, | ||
{ | ||
input: 123456789123456789, | ||
luckyDigit: 123456789123456789, | ||
luckyNumber: 123456789123456789, | ||
luckyPayout: 123456789123777000, | ||
}, | ||
{ | ||
input: 8888888888888888888, | ||
luckyDigit: 8888888888888970000, | ||
luckyNumber: 8888888888889770000, | ||
luckyPayout: 8888888888977770000, | ||
}, | ||
]; | ||
|
||
examples.forEach((example) => { | ||
it(`Calculates the next lucky levels for the starting level ${example.input}`, () => { | ||
expect(CalculateLuckyLevels(example.input)).to.deep.equal({ | ||
luckyDigit: example.luckyDigit, | ||
luckyNumber: example.luckyNumber, | ||
luckyPayout: example.luckyPayout, | ||
}); | ||
}); | ||
}); | ||
}); |