Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to sort passwords #924

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ gradle.properties

.idea/
.vscode/
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.worldcubeassociation.tnoodle.server.zip

import org.worldcubeassociation.tnoodle.server.wcif.model.ActivityCode

class ActivityPasscode(
val passcode: String,
var activityCode: ActivityCode? = null,
var title: String? = ""
) {
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.worldcubeassociation.tnoodle.server.zip

import org.worldcubeassociation.tnoodle.server.pdf.ScrambleSheet
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.randomPasscode
import org.worldcubeassociation.tnoodle.server.zip.model.ZipArchive
import org.worldcubeassociation.tnoodle.server.zip.model.dsl.zipArchive
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.randomPasscode

data class ComputerDisplayZip(val scrambleSets: Map<String, ScrambleSheet>, val competitionTitle: String) {
val passcodes = scrambleSets.mapValues { randomPasscode() }
data class ComputerDisplayZip(
val scrambleSets: Map<String, ScrambleSheet>,
val competitionTitle: String
) {
val passcodes = scrambleSets.mapValues { ActivityPasscode(randomPasscode()) }

/**
* Computer display zip
Expand All @@ -19,7 +22,10 @@ data class ComputerDisplayZip(val scrambleSets: Map<String, ScrambleSheet>, val
return zipArchive {
for ((uniqueTitle, scrambleDoc) in scrambleSets) {
val passcode = passcodes.getValue(uniqueTitle)
val pdfBytes = scrambleDoc.render(passcode)
passcode.activityCode = scrambleDoc.activityCode
passcode.title = uniqueTitle

val pdfBytes = scrambleDoc.render(passcode.passcode)

file("$uniqueTitle.pdf", pdfBytes)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.worldcubeassociation.tnoodle.server.zip

import org.worldcubeassociation.tnoodle.server.pdf.ScrambleSheet
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.toFileSafeString
import org.worldcubeassociation.tnoodle.server.wcif.model.Competition
import org.worldcubeassociation.tnoodle.server.zip.model.ZipArchive
import org.worldcubeassociation.tnoodle.server.zip.model.dsl.zipArchive
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.toFileSafeString
import java.time.LocalDateTime
import java.util.Locale
import java.util.*

data class ScrambleZip(
val wcif: Competition,
Expand All @@ -16,32 +16,76 @@ data class ScrambleZip(
) {
private val globalTitle get() = wcif.shortName

fun assemble(generationDate: LocalDateTime, versionTag: String, pdfPassword: String?, generationUrl: String?): ZipArchive {
fun assemble(
generationDate: LocalDateTime,
versionTag: String,
pdfPassword: String?,
generationUrl: String?
): ZipArchive {
val computerDisplayZip = ComputerDisplayZip(namedSheets, globalTitle)
val computerDisplayZipBytes = computerDisplayZip.assemble()

val interchangeFolder = InterchangeFolder(wcif, namedSheets, globalTitle)
val interchangeFolderNode = interchangeFolder.assemble(generationDate, versionTag, generationUrl)
val interchangeFolderNode =
interchangeFolder.assemble(generationDate, versionTag, generationUrl)

val printingFolder = PrintingFolder(wcif, namedSheets, fmcTranslations, watermark)
val printingFolderNode = printingFolder.assemble(pdfPassword)

val passcodeList = computerDisplayZip.passcodes.entries
.joinToString("\r\n") { "${it.key}: ${it.value}" }

val passcodeListingTxt = this::class.java.getResourceAsStream(TXT_PASSCODE_TEMPLATE)
val resourceTemplate = this::class.java.getResourceAsStream(TXT_PASSCODE_TEMPLATE)
.bufferedReader().readText()
.replace("%%GLOBAL_TITLE%%", globalTitle)
.replace("%%PASSCODES%%", passcodeList)

val passcodeList = computerDisplayZip.passcodes.entries
.joinToString("\r\n") { "${it.key}: ${it.value.passcode}" }

val passcodeListingTxt = resourceTemplate.replace("%%PASSCODES%%", passcodeList)

// This sorts passwords so delegates can linearly read them.
// This is inspired by https://github.com/simonkellly/scramble-organizer
// which may become deprecated after this so we are giving credit here.

val passcodesOrdered = wcif.schedule.activitiesWithLocalStartTimes.entries
.sortedBy { it.value }
.map {
computerDisplayZip.passcodes.entries.find { e ->
e.value.activityCode?.activityCodeString == it.key.activityCode.activityCodeString
|| e.value.activityCode?.activityCodeString?.replace(
// When the event has attempts split (eg FMC) or only one group,
// activityCodeString hide the -g1, but it is still present in the activityCode.
"-g1",
""
) == it.key.activityCode.activityCodeString
}?.value
}
.distinct()

val orderedPasscodeList = passcodesOrdered
.filterNotNull()
.joinToString("\r\n") { "${it.title}: ${it.passcode}" }

val orderedPasscodeListingTxt =
resourceTemplate.replace("%%PASSCODES%%", orderedPasscodeList)

val filesafeGlobalTitle = globalTitle.toFileSafeString()

return zipArchive {
folder(printingFolderNode)
folder(interchangeFolderNode)

file("$filesafeGlobalTitle - Computer Display PDFs.zip", computerDisplayZipBytes.compress())
file("$filesafeGlobalTitle - Computer Display PDF Passcodes - SECRET.txt", passcodeListingTxt)
file(
"$filesafeGlobalTitle - Computer Display PDFs.zip",
computerDisplayZipBytes.compress()
)
file(
"$filesafeGlobalTitle - Computer Display PDF Passcodes - SECRET.txt",
passcodeListingTxt
)
file(
"$filesafeGlobalTitle - Ordered Computer Display PDF Passcodes - SECRET.txt",
orderedPasscodeListingTxt
)
}
}

Expand Down
Loading