breakout71/src/pure_functions.ts

110 lines
2.9 KiB
TypeScript
Raw Normal View History

2025-04-15 21:28:00 +02:00
import { getSettingValue } from "./settings";
2025-04-21 13:16:16 +02:00
import {GameState} from "./types";
import {ballTransparency} from "./game_utils";
2025-04-15 16:47:04 +02:00
export function clamp(value: number, min: number, max: number) {
2025-04-09 11:28:32 +02:00
return Math.max(min, Math.min(value, max));
}
export function comboKeepingRate(level: number) {
2025-04-09 11:28:32 +02:00
return clamp(1 - (1 / (1 + level)) * 1.5, 0, 1);
2025-03-29 21:28:05 +01:00
}
export function hoursSpentPlaying() {
2025-04-09 11:28:32 +02:00
try {
2025-04-15 21:28:00 +02:00
const timePlayed = getSettingValue("breakout_71_total_play_time", 0);
2025-04-15 16:47:04 +02:00
return Math.floor(timePlayed / 1000 / 60 / 60);
2025-04-09 11:28:32 +02:00
} catch (e) {
return 0;
}
2025-03-31 20:08:17 +02:00
}
2025-04-21 13:16:16 +02:00
export function shouldCoinsStick(gameState:GameState){
return gameState.perks.sticky_coins && (!gameState.lastExplosion || gameState.lastExplosion < gameState.levelTime - 300 * gameState.perks.sticky_coins)
}
export function coinsBoostedCombo(gameState:GameState){
let boost = 1+gameState.perks.sturdy_bricks / 2 + gameState.perks.smaller_puck/2
if(gameState.perks.transparency){
let min=1;
gameState.balls.forEach(ball=>{
const bt=ballTransparency(ball, gameState)
if(bt<min){
min=bt
}
})
boost+=min*gameState.perks.transparency / 2
}
return Math.ceil(Math.max(gameState.combo,gameState.lastCombo) * boost)
}
2025-03-31 20:08:17 +02:00
export function miniMarkDown(md: string) {
2025-04-09 11:28:32 +02:00
let html: { tagName: string; text: string }[] = [];
let lastNode: { tagName: string; text: string } | null = null;
2025-03-31 20:08:17 +02:00
2025-04-09 11:28:32 +02:00
md.split("\n").forEach((line) => {
const titlePrefix = line.match(/^#+ /)?.[0];
2025-03-31 20:08:17 +02:00
2025-04-09 11:28:32 +02:00
if (titlePrefix) {
if (lastNode) html.push(lastNode);
lastNode = {
tagName: "h" + (titlePrefix.length - 1),
text: line.slice(titlePrefix.length),
};
} else if (line.startsWith("- ")) {
if (lastNode?.tagName !== "ul") {
if (lastNode) html.push(lastNode);
lastNode = { tagName: "ul", text: "" };
}
lastNode.text += "<li>" + line.slice(2) + "</li>";
} else if (!line.trim()) {
if (lastNode) html.push(lastNode);
lastNode = null;
} else {
if (lastNode?.tagName !== "p") {
if (lastNode) html.push(lastNode);
lastNode = { tagName: "p", text: "" };
}
lastNode.text += line + " ";
2025-03-31 20:08:17 +02:00
}
2025-04-09 11:28:32 +02:00
});
if (lastNode) {
html.push(lastNode);
}
return html
.map(
(h) =>
"<" +
h.tagName +
">" +
h.text.replace(
/\bhttps?:\/\/[^\s<>]+/gi,
(a) => `<a href="${a}">${a}</a>`,
) +
"</" +
h.tagName +
">",
)
.join("\n");
}
2025-04-08 14:03:38 +02:00
export function firstWhere<Input, Output>(
2025-04-09 11:28:32 +02:00
arr: Input[],
mapper: (item: Input, index: number) => Output | undefined,
2025-04-08 14:03:38 +02:00
): Output | undefined {
2025-04-09 11:28:32 +02:00
for (let i = 0; i < arr.length; i++) {
const result = mapper(arr[i], i);
if (typeof result !== "undefined") return result;
}
2025-04-08 14:03:38 +02:00
}
2025-04-08 21:54:19 +02:00
2025-04-09 11:28:32 +02:00
export const wallBouncedBest = 3,
wallBouncedGood = 10,
levelTimeBest = 30,
levelTimeGood = 60,
catchRateBest = 95,
catchRateGood = 90,
missesBest = 3,
missesGood = 6;