mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-24 06:48:02 +08:00
Refactored random number getting code, added random from array and object (#2700)
* using new functions * updated types
This commit is contained in:
parent
bb294db746
commit
a8d4159520
5 changed files with 31 additions and 20 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import Config from "../config";
|
||||
import Howler, { Howl } from "howler";
|
||||
import * as ConfigEvent from "../observables/config-event";
|
||||
import { randomElementFromArray } from "../utils/misc";
|
||||
|
||||
type ClickSounds = {
|
||||
[key: string]: {
|
||||
|
|
@ -236,12 +237,10 @@ export function playClick(): void {
|
|||
if (Config.playSoundOnClick === "off") return;
|
||||
if (clickSounds === null) init();
|
||||
|
||||
const rand = Math.floor(
|
||||
Math.random() * (clickSounds as ClickSounds)[Config.playSoundOnClick].length
|
||||
const randomSound = randomElementFromArray(
|
||||
(clickSounds as ClickSounds)[Config.playSoundOnClick]
|
||||
);
|
||||
const randomSound = (clickSounds as ClickSounds)[Config.playSoundOnClick][
|
||||
rand
|
||||
];
|
||||
|
||||
randomSound.counter++;
|
||||
if (randomSound.counter === 2) randomSound.counter = 0;
|
||||
randomSound.sounds[randomSound.counter].seek(0);
|
||||
|
|
|
|||
|
|
@ -210,7 +210,7 @@ export function randomizeTheme(): void {
|
|||
}
|
||||
|
||||
const previousTheme = randomTheme;
|
||||
randomTheme = randomList[Math.floor(Math.random() * randomList.length)];
|
||||
randomTheme = Misc.randomElementFromArray(randomList);
|
||||
|
||||
// if (Config.randomTheme === "custom") {
|
||||
// changeCustomTheme(randomTheme, true);
|
||||
|
|
|
|||
|
|
@ -228,7 +228,7 @@ export function punctuateWord(
|
|||
} else if (Math.random() < 0.25 && currentLanguage == "code") {
|
||||
const specials = ["{", "}", "[", "]", "(", ")", ";", "=", "+", "%", "/"];
|
||||
|
||||
word = specials[Math.floor(Math.random() * 10)];
|
||||
word = Misc.randomElementFromArray(specials);
|
||||
}
|
||||
}
|
||||
return word;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import { randomElementFromArray, randomIntFromRange } from "../utils/misc";
|
||||
|
||||
let currentWordset: Wordset | null = null;
|
||||
let currentWordGenerator: WordGenerator | null = null;
|
||||
|
||||
|
|
@ -10,7 +12,7 @@ export class Wordset {
|
|||
}
|
||||
|
||||
public randomWord(): string {
|
||||
return this.words[Math.floor(Math.random() * this.length)];
|
||||
return randomElementFromArray(this.words);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -34,7 +36,7 @@ class CharDistribution {
|
|||
}
|
||||
|
||||
public randomChar(): string {
|
||||
const randomIndex = Math.floor(Math.random() * this.count);
|
||||
const randomIndex = randomIntFromRange(0, this.count - 1);
|
||||
let runningCount = 0;
|
||||
for (const [char, charCount] of Object.entries(this.chars)) {
|
||||
runningCount += charCount;
|
||||
|
|
|
|||
|
|
@ -459,10 +459,10 @@ export function findLineByLeastSquares(values_y: number[]): number[][] {
|
|||
}
|
||||
|
||||
export function getGibberish(): string {
|
||||
const randLen = Math.floor(Math.random() * 7) + 1;
|
||||
const randLen = randomIntFromRange(1, 7);
|
||||
let ret = "";
|
||||
for (let i = 0; i < randLen; i++) {
|
||||
ret += String.fromCharCode(97 + Math.floor(Math.random() * 26));
|
||||
ret += String.fromCharCode(97 + randomIntFromRange(0, 25));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -492,17 +492,17 @@ export function secondsToString(
|
|||
}
|
||||
|
||||
export function getNumbers(len: number): string {
|
||||
const randLen = Math.floor(Math.random() * len) + 1;
|
||||
const randLen = randomIntFromRange(1, len);
|
||||
let ret = "";
|
||||
for (let i = 0; i < randLen; i++) {
|
||||
const randomNum = Math.floor(Math.random() * 10);
|
||||
const randomNum = randomIntFromRange(0, 9);
|
||||
ret += randomNum.toString();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
export function getSpecials(): string {
|
||||
const randLen = Math.floor(Math.random() * 7) + 1;
|
||||
const randLen = randomIntFromRange(1, 7);
|
||||
let ret = "";
|
||||
const specials = [
|
||||
"!",
|
||||
|
|
@ -530,17 +530,17 @@ export function getSpecials(): string {
|
|||
"|",
|
||||
];
|
||||
for (let i = 0; i < randLen; i++) {
|
||||
ret += specials[Math.floor(Math.random() * specials.length)];
|
||||
ret += randomElementFromArray(specials);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
export function getASCII(): string {
|
||||
const randLen = Math.floor(Math.random() * 10) + 1;
|
||||
const randLen = randomIntFromRange(1, 10);
|
||||
let ret = "";
|
||||
for (let i = 0; i < randLen; i++) {
|
||||
let ran = 33 + Math.floor(Math.random() * 94);
|
||||
while (ran == 96 || ran == 94) ran = 33 + Math.floor(Math.random() * 94); //todo remove when input rewrite is fixed
|
||||
let ran = 33 + randomIntFromRange(0, 93);
|
||||
while (ran == 96 || ran == 94) ran = 33 + randomIntFromRange(0, 93); //todo remove when input rewrite is fixed
|
||||
ret += String.fromCharCode(ran);
|
||||
}
|
||||
return ret;
|
||||
|
|
@ -551,9 +551,9 @@ export function getArrows(): string {
|
|||
let arrowWord = "";
|
||||
let lastchar;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
let random = arrowArray[Math.floor(Math.random() * arrowArray.length)];
|
||||
let random = randomElementFromArray(arrowArray);
|
||||
while (random === lastchar) {
|
||||
random = arrowArray[Math.floor(Math.random() * arrowArray.length)];
|
||||
random = randomElementFromArray(arrowArray);
|
||||
}
|
||||
lastchar = random;
|
||||
arrowWord += random;
|
||||
|
|
@ -997,3 +997,13 @@ export function shuffle<T>(elements: T[]): void {
|
|||
elements[i] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
export function randomElementFromArray<T>(array: T[]): T {
|
||||
return array[randomIntFromRange(0, array.length - 1)];
|
||||
}
|
||||
|
||||
export function randomElementFromObject<T extends object>(
|
||||
object: T
|
||||
): T[keyof T] {
|
||||
return randomElementFromArray(Object.values(object));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue