11package org.worldcubeassociation.tnoodle.server.zip
22
33import org.worldcubeassociation.tnoodle.server.pdf.ScrambleSheet
4- import org.worldcubeassociation.tnoodle.server.zip.util.StringUtil.toFileSafeString
54import org.worldcubeassociation.tnoodle.server.wcif.model.Competition
65import org.worldcubeassociation.tnoodle.server.zip.model.ZipArchive
76import 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
89import java.time.LocalDateTime
9- import java.util.Locale
10+ import java.util.*
1011
1112data 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