add counter for puzzles

This commit is contained in:
Zhora Shalyapin 2025-03-26 10:01:11 +00:00
parent a4ac19e516
commit 3f6daf97eb

89
main.js
View File

@ -312,7 +312,7 @@
}) })
} }
async function getPointIdByNumber(numbers) { async function getPointsByNumber(numbers) {
let params = new URLSearchParams() let params = new URLSearchParams()
for (const number of numbers) { for (const number of numbers) {
params.append("number[]", number) params.append("number[]", number)
@ -1175,7 +1175,7 @@
on: { on: {
click: async function () { click: async function () {
let number = document.querySelector("#go-to-cp").value let number = document.querySelector("#go-to-cp").value
let json = await getPointIdByNumber([number]) let json = await getPointsByNumber([number])
let id = json[number].cp_id let id = json[number].cp_id
if (id == null) { if (id == null) {
@ -2158,11 +2158,37 @@
return statsSpan return statsSpan
} }
function countCp() { function getPointCells() {
return [...document.querySelectorAll(`#props table td:first-child`)]
}
function getPointNumberFromCell(cell) {
return cell.textContent.trim().match(/^\d+/)?.[0]
}
function getPointNumbers() {
let pointNumbers = {}
for (const [rowIndex, pointCell] of getPointCells().entries()) {
let pointNumber = getPointNumberFromCell(pointCell)
if (pointNumber != null)
pointNumbers[rowIndex] = pointNumber
}
return pointNumbers
}
async function getPointsFromRows() {
return await getPointsByNumber(Object.values(getPointNumbers()))
}
function countCp(points) {
let rows = getRows() let rows = getRows()
let fullCount = 0 let fullCount = 0
let stageCount = 0 let stageCount = 0
let fullPuzzleCount = 0
let stagePuzzleCount = 0
let stageRow = null let stageRow = null
for (const [i, row] of rows.entries()) { for (const [i, row] of rows.entries()) {
if (i < 2) continue if (i < 2) continue
@ -2170,21 +2196,32 @@
if (isFinish || row.querySelector(`th`)) { if (isFinish || row.querySelector(`th`)) {
if (stageRow) { if (stageRow) {
stageRow.querySelector("th").append(makeStatsSpan(`КП в этапе: ${stageCount}`)) let stageHeader = stageRow.querySelector("th")
stageHeader.append(makeStatsSpan(`КП в этапе: ${stageCount}`))
stageHeader.append(makeStatsSpan(`Загадок: ${stagePuzzleCount}`))
} }
if (isFinish) break if (isFinish) break
stageRow = row stageRow = row
stageCount = 0 stageCount = 0
stagePuzzleCount = 0
continue continue
} }
fullCount++ fullCount++
stageCount++ stageCount++
let pointNumber = getPointNumberFromCell(row.querySelector("td"))
if (pointNumber && points[pointNumber].is_puzzle) {
fullPuzzleCount++
stagePuzzleCount++
}
} }
document.querySelector("table tr:nth-child(2) th").append(makeStatsSpan(`Всего КП: ${fullCount}`)) let tableHeader = document.querySelector("table tr:nth-child(2) th")
tableHeader.append(makeStatsSpan(`Всего КП: ${fullCount}`))
tableHeader.append(makeStatsSpan(`Загадок: ${fullPuzzleCount}`))
addStylesToHead(` addStylesToHead(`
.stats { .stats {
@ -2194,30 +2231,21 @@
`) `)
} }
function addLinksToCp() { function addLinksToCp(points) {
let pointCells = [...document.querySelectorAll(`#props table td:first-child`)] let pointCells = getPointCells()
let pointNumbers = {}
for (const [i, pointCell] of pointCells.entries()) { for (const [i, pointCell] of pointCells.entries()) {
let pointNumber = pointCell.textContent.trim().match(/^\d+/)?.[0] let pointNumber = getPointNumberFromCell(pointCell)
if (pointNumber != null) if (pointNumber == null) continue
pointNumbers[i] = pointNumber
let cellContent = pointCell.textContent
pointCell.innerHTML = ''
pointCell.append(Tag.a({
href: cpLink(points[pointNumber].cp_id),
target: "_blank",
textContent: cellContent
}))
} }
;(async () => {
let cpIds = await getPointIdByNumber(Object.values(pointNumbers))
for (const [i, pointCell] of pointCells.entries()) {
if (pointNumbers[i] == null) continue
let cellContent = pointCell.textContent
pointCell.innerHTML = ''
pointCell.append(Tag.a({
href: cpLink(cpIds[pointNumbers[i]].cp_id),
target: "_blank",
textContent: cellContent
}))
}
})()
} }
function getRouteName() { function getRouteName() {
@ -2261,8 +2289,11 @@
hideDescription() hideDescription()
hideStartRow() hideStartRow()
useColspanForFinishWarning() useColspanForFinishWarning()
countCp() ;(async () => {
addLinksToCp() let points = await getPointsFromRows()
countCp(points)
addLinksToCp(points)
})()
sendRouteData() sendRouteData()
showUnknownPoints() showUnknownPoints()
} }