🔀 Merge branch 'website/epic-games' into next
[rhynodge.git] / src / main / kotlin / net / pterodactylus / rhynodge / filters / webpages / epicgames / FreeGamesState.kt
diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/FreeGamesState.kt b/src/main/kotlin/net/pterodactylus/rhynodge/filters/webpages/epicgames/FreeGamesState.kt
new file mode 100644 (file)
index 0000000..47a78fd
--- /dev/null
@@ -0,0 +1,67 @@
+package net.pterodactylus.rhynodge.filters.webpages.epicgames
+
+import kotlinx.html.body
+import kotlinx.html.div
+import kotlinx.html.dom.createHTMLDocument
+import kotlinx.html.dom.serialize
+import kotlinx.html.head
+import kotlinx.html.html
+import kotlinx.html.img
+import kotlinx.html.style
+import kotlinx.html.unsafe
+import net.pterodactylus.rhynodge.states.AbstractState
+import java.time.ZoneId
+import java.util.Comparator.comparing
+
+class FreeGamesState(val games: Set<FreeGame>, private val triggered: Boolean = false, private val timezone: ZoneId = ZoneId.systemDefault()) : AbstractState(true) {
+
+       override fun plainText() = games
+               .sortedWith(comparing(FreeGame::startDate).thenBy(FreeGame::title))
+               .joinToString("\n") { game ->
+                       "${game.title}: ${"%tF %<tT".format(game.startDate.atZone(timezone))} - ${"%tF %<tT".format(game.endDate.atZone(timezone))} (${game.imageUrl})"
+               }
+
+       override fun htmlText() = createHTMLDocument().html {
+               head {
+                       style("text/css") {
+                               unsafe {
+                                       raw(
+                                               """
+                                               .game { display: inline-grid; width: 200px; height: 350px; grid-template-rows: 0fr 1fr 0fr 0fr; font-family: 'Recursive Sans Linear Static', Roboto, serif; color: white; text-shadow: 2px 2px black; margin: 0 1ex 1ex 0; }
+                                               .game .game-image { grid-area: 1/1/5/2; }
+                                               .game .game-image img { object-fit: cover; width: 100%; height: 100%; }
+                                               .game .game-title { grid-area: 1/1/2/2; padding: 1ex; font-size: 150%; font-weight: bold; }
+                                               .game .game-start { grid-area: 3/1/4/2; padding: 1ex 1ex 0ex 1ex; }
+                                               .game .game-end { grid-area: 4/1/5/2; padding: 0ex 1ex 1ex 1ex; }
+                                       """
+                                       )
+                               }
+                       }
+               }
+               body {
+                       div("games") {
+                               games
+                                       .sortedWith(comparing(FreeGame::startDate).thenBy(FreeGame::title))
+                                       .forEach { game ->
+                                               div("game") {
+                                                       div("game-image") {
+                                                               img(src = game.imageUrl)
+                                                       }
+                                                       div("game-title") {
+                                                               +game.title
+                                                       }
+                                                       div("game-start") {
+                                                               +"%tF %<tT".format(game.startDate.atZone(timezone))
+                                                       }
+                                                       div("game-end") {
+                                                               +"%tF %<tT".format(game.endDate.atZone(timezone))
+                                                       }
+                                               }
+                                       }
+                       }
+               }
+       }.serialize(prettyPrint = true)
+
+       override fun triggered() = triggered
+
+}