Skip to content

Commit 57f23a5

Browse files
campos20gregorbg
andauthored
Add option to sort passwords (#924)
* Add DS_Store to gitignore * Add ordered passcode * Also match activities with only 1 group * Recognize simonkellly as inspiration for sorted passwords * Remove redundant helper class * Simplify matching algorithm --------- Co-authored-by: Gregor Billing <[email protected]>
1 parent 8e1a39b commit 57f23a5

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ gradle.properties
2626

2727
.idea/
2828
.vscode/
29+
.DS_Store

server/src/main/kotlin/org/worldcubeassociation/tnoodle/server/zip/ComputerDisplayZip.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.worldcubeassociation.tnoodle.server.zip
22

33
import org.worldcubeassociation.tnoodle.server.pdf.ScrambleSheet
4-
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.randomPasscode
54
import org.worldcubeassociation.tnoodle.server.zip.model.ZipArchive
65
import org.worldcubeassociation.tnoodle.server.zip.model.dsl.zipArchive
76

8-
data class ComputerDisplayZip(val scrambleSets: Map<String, ScrambleSheet>, val competitionTitle: String) {
9-
val passcodes = scrambleSets.mapValues { randomPasscode() }
10-
7+
data class ComputerDisplayZip(
8+
val scrambleSheetsWithCode: Map<String, Pair<ScrambleSheet, String>>,
9+
val competitionTitle: String
10+
) {
1111
/**
1212
* Computer display zip
1313
*
@@ -17,8 +17,8 @@ data class ComputerDisplayZip(val scrambleSets: Map<String, ScrambleSheet>, val
1717
*/
1818
fun assemble(): ZipArchive {
1919
return zipArchive {
20-
for ((uniqueTitle, scrambleDoc) in scrambleSets) {
21-
val passcode = passcodes.getValue(uniqueTitle)
20+
for ((uniqueTitle, scrambleDocWithCode) in scrambleSheetsWithCode) {
21+
val (scrambleDoc, passcode) = scrambleDocWithCode
2222
val pdfBytes = scrambleDoc.render(passcode)
2323

2424
file("$uniqueTitle.pdf", pdfBytes)

server/src/main/kotlin/org/worldcubeassociation/tnoodle/server/zip/ScrambleZip.kt

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.worldcubeassociation.tnoodle.server.zip
22

33
import org.worldcubeassociation.tnoodle.server.pdf.ScrambleSheet
4-
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.toFileSafeString
54
import org.worldcubeassociation.tnoodle.server.wcif.model.Competition
65
import org.worldcubeassociation.tnoodle.server.zip.model.ZipArchive
76
import org.worldcubeassociation.tnoodle.server.zip.model.dsl.zipArchive
7+
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.randomPasscode
8+
import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.toFileSafeString
89
import java.time.LocalDateTime
9-
import java.util.Locale
10+
import java.util.*
1011

1112
data class ScrambleZip(
1213
val wcif: Competition,
@@ -16,8 +17,15 @@ data class ScrambleZip(
1617
) {
1718
private val globalTitle get() = wcif.shortName
1819

19-
fun assemble(generationDate: LocalDateTime, versionTag: String, pdfPassword: String?, generationUrl: String?): ZipArchive {
20-
val computerDisplayZip = ComputerDisplayZip(namedSheets, globalTitle)
20+
fun assemble(
21+
generationDate: LocalDateTime,
22+
versionTag: String,
23+
pdfPassword: String?,
24+
generationUrl: String?
25+
): ZipArchive {
26+
val sheetsWithRandomCode = namedSheets.mapValues { it.value to randomPasscode() }
27+
28+
val computerDisplayZip = ComputerDisplayZip(sheetsWithRandomCode, globalTitle)
2129
val computerDisplayZipBytes = computerDisplayZip.assemble()
2230

2331
val interchangeFolder = InterchangeFolder(wcif, namedSheets, globalTitle)
@@ -26,13 +34,34 @@ data class ScrambleZip(
2634
val printingFolder = PrintingFolder(wcif, namedSheets, fmcTranslations, watermark)
2735
val printingFolderNode = printingFolder.assemble(pdfPassword)
2836

29-
val passcodeList = computerDisplayZip.passcodes.entries
30-
.joinToString("\r\n") { "${it.key}: ${it.value}" }
31-
32-
val passcodeListingTxt = this::class.java.getResourceAsStream(TXT_PASSCODE_TEMPLATE)
37+
val resourceTemplate = this::class.java.getResourceAsStream(TXT_PASSCODE_TEMPLATE)
3338
.bufferedReader().readText()
3439
.replace("%%GLOBAL_TITLE%%", globalTitle)
35-
.replace("%%PASSCODES%%", passcodeList)
40+
41+
val passcodeList = sheetsWithRandomCode.entries
42+
.joinToString("\r\n") { "${it.key}: ${it.value.second}" }
43+
44+
val passcodeListingTxt =
45+
resourceTemplate.replace("%%PASSCODES%%", passcodeList)
46+
47+
// This sorts passwords so delegates can linearly read them.
48+
// This is inspired by https://github.com/simonkellly/scramble-organizer
49+
// which may become deprecated after this so we are giving credit here.
50+
51+
val passcodesOrdered = wcif.schedule.activitiesWithLocalStartTimes.entries
52+
.sortedBy { it.value }
53+
.mapNotNull {
54+
sheetsWithRandomCode.values.find { v ->
55+
v.first.scrambleSetId == it.key.scrambleSetId
56+
}
57+
}
58+
.distinct()
59+
60+
val orderedPasscodeList = passcodesOrdered
61+
.joinToString("\r\n") { "${it.first.title}: ${it.second}" }
62+
63+
val orderedPasscodeListingTxt =
64+
resourceTemplate.replace("%%PASSCODES%%", orderedPasscodeList)
3665

3766
val filesafeGlobalTitle = globalTitle.toFileSafeString()
3867

@@ -42,6 +71,7 @@ data class ScrambleZip(
4271

4372
file("$filesafeGlobalTitle - Computer Display PDFs.zip", computerDisplayZipBytes.compress())
4473
file("$filesafeGlobalTitle - Computer Display PDF Passcodes - SECRET.txt", passcodeListingTxt)
74+
file("$filesafeGlobalTitle - Ordered Computer Display PDF Passcodes - SECRET.txt", orderedPasscodeListingTxt)
4575
}
4676
}
4777

0 commit comments

Comments
 (0)