Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor task interface, implement RatingDiffTask
- Loading branch information
Showing
5 changed files
with
83 additions
and
14 deletions.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
Duga/duga-aws/src/main/kotlin/net/zomis/duga/tasks/DugaTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package net.zomis.duga.tasks | ||
|
||
import net.zomis.duga.aws.DugaMessage | ||
|
||
interface DugaTask { | ||
fun perform(): Unit | ||
fun perform(): List<DugaMessage> | ||
} |
5 changes: 2 additions & 3 deletions
5
Duga/duga-aws/src/main/kotlin/net/zomis/duga/tasks/MessageTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
package net.zomis.duga.tasks | ||
|
||
import net.zomis.duga.aws.Duga | ||
import net.zomis.duga.aws.DugaMessage | ||
import java.time.Instant | ||
|
||
class MessageTask(private val room: String, private val message: String) : DugaTask { | ||
|
||
override fun perform() { | ||
override fun perform(): List<DugaMessage> { | ||
val msg = message.replace("%time%", Instant.now().toString()) | ||
Duga().send(DugaMessage(room, msg)) | ||
return listOf(DugaMessage(room, msg)) | ||
} | ||
|
||
} |
64 changes: 62 additions & 2 deletions
64
Duga/duga-aws/src/main/kotlin/net/zomis/duga/tasks/RatingDiffTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,69 @@ | ||
package net.zomis.duga.tasks | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import net.zomis.duga.aws.DugaMessage | ||
import net.zomis.duga.utils.StackExchangeAPI | ||
import java.io.IOException | ||
|
||
class RatingDiffTask(private val room: String, private val site: String, private val users: List<String>) : DugaTask { | ||
|
||
override fun perform() { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
val stackApi = StackExchangeAPI() | ||
|
||
override fun perform(): List<DugaMessage> { | ||
try { | ||
val usersString = users.joinToString(",") | ||
val result = stackApi.apiCall("users/$usersString", site, "!23IYXA.sS8.otifg5Aq.2") | ||
val users = result["items"] | ||
if (users.size() != 2) { | ||
throw UnsupportedOperationException("Cannot check diff for anything other than two users") | ||
} | ||
|
||
val max = users.maxBy { it["reputation"].asInt() }!! | ||
val min = users.minBy { it["reputation"].asInt() }!! | ||
val str = StringBuilder() | ||
str.append(clearName(max["display_name"].asText()) + " vs. " + clearName(min["display_name"].asText()) + ": ") | ||
str.append(max["reputation"].asInt() - min["reputation"].asInt()) | ||
str.append(" diff. ") | ||
diffStr(str, max, min, "Year") {it["reputation_change_year"].asInt()} | ||
diffStr(str, max, min, "Quarter") {it["reputation_change_quarter"].asInt()} | ||
diffStr(str, max, min, "Month") {it["reputation_change_month"].asInt()} | ||
diffStr(str, max, min, "Week") {it["reputation_change_week"].asInt()} | ||
diffStr(str, max, min, "Day") {it["reputation_change_day"].asInt()} | ||
return listOf(DugaMessage(room, str.toString())) | ||
} catch (e: IOException) { | ||
e.printStackTrace() | ||
return listOf() | ||
} | ||
} | ||
|
||
fun chatName(displayName: String): String { | ||
return clearName(displayName).replace(" ", ""); | ||
} | ||
|
||
fun clearName(dispName: String): String { | ||
var displayName = dispName | ||
while (displayName.contains("&#")) { | ||
var replacement = displayName.substring(displayName.indexOf("&#") + 2) | ||
try { | ||
replacement = replacement.substring(0, replacement.indexOf(';')) | ||
val ch = Integer.parseInt(replacement) | ||
displayName = displayName.replaceFirst("&#\\d+;", ch.toChar().toString()) | ||
} catch (ex: RuntimeException) { | ||
displayName = displayName.replaceFirst("&#", ""); | ||
} | ||
} | ||
return displayName | ||
} | ||
|
||
fun diffStr(str: StringBuilder, max: JsonNode, min: JsonNode, string: String, function: (JsonNode) -> Int) { | ||
str.append(string) | ||
str.append(": ") | ||
val maxValue = function(max) | ||
val minValue = function(min) | ||
val diff = maxValue - minValue | ||
str.append(if (diff > 0) "+" else "") | ||
str.append(diff) | ||
str.append(". ") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters