Compare commits

...

2 Commits

Author SHA1 Message Date
ekzyis 288fa37197
Only validate tipRandom if enabled (#1284)
* Only validate tipRandom if enabled

* Use consistent naming scheme for zap settings

---------

Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-08-03 19:51:15 -05:00
keyan 5d0e071939 better random zaps 2024-08-03 19:37:43 -05:00
3 changed files with 30 additions and 17 deletions

View File

@ -67,29 +67,39 @@ export function DropdownItemUpVote ({ item }) {
)
}
export const defaultTipIncludingRandom = ({ tipDefault, tipRandom, tipRandomMin, tipRandomMax } = {}) =>
tipRandom
? Math.floor((Math.random() * (tipRandomMax - tipRandomMin)) + tipRandomMin)
export const defaultTipIncludingRandom = ({ tipDefault, tipRandom, tipRandomMin, tipRandomMax } = {}) => {
return tipRandom
? Math.floor((Math.random() * (tipRandomMax - tipRandomMin + 1)) + tipRandomMin)
: (tipDefault || 100)
}
export const nextTip = (meSats, { tipDefault, turboTipping, tipRandom, tipRandomMin, tipRandomMax }) => {
// what should our next tip be?
const calculatedDefault = defaultTipIncludingRandom({ tipDefault, tipRandom, tipRandomMin, tipRandomMax })
if (!turboTipping) {
return calculatedDefault
}
let sats = calculatedDefault
if (turboTipping) {
if (tipRandom) {
let pow = 0
// find the first power of 10 that is greater than meSats
while (!(meSats <= tipRandomMax * 10 ** pow)) {
pow++
}
// if meSats is in that power of 10's range already, move into the next range
if (meSats >= tipRandomMin * 10 ** pow) {
pow++
}
// make sure the our range minimum doesn't overlap with the previous range maximum
tipRandomMin = tipRandomMax * 10 ** (pow - 1) >= tipRandomMin * 10 ** pow ? tipRandomMax * 10 ** (pow - 1) + 1 : tipRandomMin * 10 ** pow
tipRandomMax = tipRandomMax * 10 ** pow
return Math.floor((Math.random() * (tipRandomMax - tipRandomMin + 1)) + tipRandomMin) - meSats
}
let sats = defaultTipIncludingRandom({ tipDefault, tipRandom, tipRandomMin, tipRandomMax })
while (meSats >= sats) {
sats *= 10
}
// deduct current sats since turbo tipping is about total zap not making the next zap 10x
sats -= meSats
return sats - meSats
}
return sats
return defaultTipIncludingRandom({ tipDefault, tipRandom, tipRandomMin, tipRandomMax })
}
export default function UpVote ({ item, className }) {

View File

@ -542,17 +542,20 @@ export const actSchema = object({
export const settingsSchema = object().shape({
tipDefault: intValidator.required('required').positive('must be positive'),
tipRandom: boolean(),
tipRandomMin: intValidator.nullable().positive('must be positive')
.when(['tipRandomMax'], ([max], schema) => {
.when(['tipRandom', 'tipRandomMax'], ([enabled, max], schema) => {
let res = schema
if (!enabled) return res
if (max) {
res = schema.required('minimum and maximum must either both be omitted or specified').nonNullable()
}
return res.lessThan(max, 'must be less than maximum')
}),
tipRandomMax: intValidator.nullable().positive('must be positive')
.when(['tipRandomMin'], ([min], schema) => {
.when(['tipRandom', 'tipRandomMin'], ([enabled, min], schema) => {
let res = schema
if (!enabled) return res
if (min) {
res = schema.required('minimum and maximum must either both be omitted or specified').nonNullable()
}

View File

@ -114,7 +114,7 @@ export default function Settings ({ ssrData }) {
tipDefault: settings?.tipDefault || 21,
tipRandom: settings?.tipRandom,
tipRandomMin: settings?.tipRandomMin || 1,
tipRandomMax: settings?.tipRandomMax || settings?.tipDefault || 21,
tipRandomMax: settings?.tipRandomMax || 10,
turboTipping: settings?.turboTipping,
zapUndos: settings?.zapUndos || (settings?.tipDefault ? 100 * settings.tipDefault : 2100),
zapUndosEnabled: settings?.zapUndos !== null,
@ -1040,7 +1040,7 @@ const TipRandomField = () => {
groupClassName='mb-0'
label={
<div className='d-flex align-items-center'>
Enable random zap values
random zaps
<Info>
<ul className='fw-bold'>
<li>Set a minimum and maximum zap amount</li>