Skip to content

Commit

Permalink
Merge pull request #19 from alyssaruth/ktor-3
Browse files Browse the repository at this point in the history
Ktor 3
  • Loading branch information
alyssaruth authored Sep 19, 2024
2 parents e5a2d02 + 3c79396 commit a762917
Show file tree
Hide file tree
Showing 48 changed files with 601 additions and 1,531 deletions.
2 changes: 1 addition & 1 deletion client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ application {
task<JavaExec>("runDev") {
configure(
closureOf<JavaExec> {
group = "run"
group = "application"
classpath = project.the<SourceSetContainer>()["main"].runtimeClasspath
mainClass = "EntropyMain"
args = listOf("devMode")
Expand Down
2 changes: 0 additions & 2 deletions client/src/main/java/EntropyMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ else if (!bindOnPort(BIND_PORT_NUMBER))
return;
}

EncryptionUtil.failedDecryptionLogging = true;

checkForUpdatesIfRequired();

MainScreen application = ScreenCache.getMainScreen();
Expand Down
6 changes: 0 additions & 6 deletions client/src/main/java/online/screen/EntropyLobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,6 @@ public GameRoom getGameRoomForName(String roomName)
return hmGameRoomByRoomName.get(roomName);
}

public void removeRoom(String roomName)
{
hmRoomByRoomName.remove(roomName);
hmGameRoomByRoomName.remove(roomName);
}

public void addOrUpdateRoom(String roomName, RoomWrapper room)
{
hmRoomByRoomName.put(roomName, room);
Expand Down
3 changes: 3 additions & 0 deletions client/src/main/java/screen/MainScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ public String processCommand(String command)

if (command.equals("health")) {
Globals.INSTANCE.getHealthCheckApi().doHealthCheck();
} else if (command.startsWith("server ")) {
var serverCommand = command.replace("server ", "");
Globals.INSTANCE.getDevApi().doServerCommand(serverCommand);
}
else if (command.equals("simulator"))
{
Expand Down
14 changes: 14 additions & 0 deletions client/src/main/kotlin/http/DevApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package http

import http.dto.DevCommandRequest
import kong.unirest.HttpMethod

class DevApi(private val httpClient: HttpClient) {
fun doServerCommand(commandString: String) {
httpClient.doCall<Unit>(
HttpMethod.POST,
Routes.DEV_COMMAND,
DevCommandRequest(commandString)
)
}
}
4 changes: 2 additions & 2 deletions client/src/main/kotlin/http/HealthCheckApi.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package http

import dto.HealthCheckResponse
import http.dto.HealthCheckResponse
import kong.unirest.HttpMethod

class HealthCheckApi(private val httpClient: HttpClient) {
fun doHealthCheck() {
httpClient.doCall<HealthCheckResponse>(HttpMethod.GET, "/health-check")
httpClient.doCall<HealthCheckResponse>(HttpMethod.GET, Routes.HEALTH_CHECK)
}
}
5 changes: 2 additions & 3 deletions client/src/main/kotlin/http/HttpClient.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package http

import dto.ClientErrorResponse
import http.dto.ClientErrorResponse
import java.util.*
import kong.unirest.HttpMethod
import kong.unirest.HttpResponse
Expand All @@ -9,10 +9,9 @@ import kong.unirest.Unirest
import kong.unirest.UnirestException
import logging.Severity
import org.apache.http.HttpHeaders
import util.Globals
import utils.InjectedThings.logger

class HttpClient(val baseUrl: String = Globals.baseUrl) {
class HttpClient(val baseUrl: String) {
val jsonObjectMapper = JsonObjectMapper()

inline fun <reified T : Any?> doCall(
Expand Down
8 changes: 5 additions & 3 deletions client/src/main/kotlin/util/Globals.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package util

import http.DevApi
import http.HealthCheckApi
import http.HttpClient

object Globals {
private val httpClient = HttpClient()
val baseUrl = "http://localhost:8080"
var healthCheckApi = HealthCheckApi(httpClient)
private val baseUrl = "http://localhost:8080"
private val httpClient = HttpClient(baseUrl)
val healthCheckApi = HealthCheckApi(httpClient)
val devApi = DevApi(httpClient)
}
2 changes: 1 addition & 1 deletion client/src/test/kotlin/http/HttpClientTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package http

import dto.ClientErrorResponse
import http.dto.ClientErrorResponse
import io.kotest.assertions.throwables.shouldNotThrowAny
import io.kotest.matchers.maps.shouldContain
import io.kotest.matchers.maps.shouldContainKeys
Expand Down
23 changes: 3 additions & 20 deletions core/src/main/java/util/EncryptionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@

public class EncryptionUtil
{
//public static final String DEV_MODE_SYMMETRIC_KEY_STR = "nISBSiWGpVInzurAhSc/Kg==";
public static final int MINIMUM_PASSWORD_LENGTH = 7;

private static final String ALGORITHM_RSA_ECB_PKCS1PADDING = "RSA/ECB/PKCS1Padding";
private static final String ALGORITHM_AES_ECB_PKCS5PADDING = "AES/ECB/PKCS5Padding";

public static boolean failedDecryptionLogging = false;
public static Base64Interface base64Interface = null;

/*public static SecretKey getDevModeKey()
{
return reconstructKeyFromString(DEV_MODE_SYMMETRIC_KEY_STR);
}*/

public static String convertSecretKeyToString(SecretKey secretKey)
{
byte[] keyBytes = secretKey.getEncoded();
Expand Down Expand Up @@ -84,16 +76,7 @@ public static String encrypt(String messageString, Key key, boolean asymmetric)

return encryptedString;
}

public static String decryptIfPossible(String encryptedMessage, Key key)
{
if (key == null)
{
return encryptedMessage;
}

return decrypt(encryptedMessage, key);
}

public static String decrypt(String encryptedMessage, Key key)
{
return decrypt(encryptedMessage, key, false);
Expand All @@ -115,9 +98,9 @@ public static String decrypt(String encryptedMessage, Key key, boolean asymmetri
byte[] messageBytes = cipher.doFinal(cipherData);
messageString = new String(messageBytes);
}
catch (Throwable t)
catch (Throwable ignored)
{
Debug.append("Caught " + t + " trying to decrypt message: " + encryptedMessage, failedDecryptionLogging);

}

if (messageString != null)
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/kotlin/http/Routes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package http

object Routes {
const val HEALTH_CHECK = "/health-check"
const val DEV_COMMAND = "/dev-command"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package dto
package http.dto

data class ClientErrorResponse(val errorCode: String, val errorMessage: String)
3 changes: 3 additions & 0 deletions core/src/main/kotlin/http/dto/DevCommandRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package http.dto

data class DevCommandRequest(val command: String)
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package dto
package http.dto

data class HealthCheckResponse(val version: String)
14 changes: 14 additions & 0 deletions core/src/main/kotlin/utils/MathsUtil.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import kotlin.math.log
import kotlin.math.pow

fun getPercentage(count: Number, total: Number, digits: Int = 1) =
Expand All @@ -18,3 +19,16 @@ private fun round(number: Double, decimalPlaces: Int): Double {

return rounded / powerOfTen
}

fun Long.formatAsFileSize(): String {
if (this == 0L) return "0 B"

val magnitudeIndex = log(toDouble(), 1024.0).toInt()
val precision = if (magnitudeIndex == 0) 0 else 1
val units = listOf("B", "KB", "MB", "GB", "TB")

return String.format(
"%.${precision}f ${units[magnitudeIndex]}",
toDouble() / 1024.0.pow(magnitudeIndex)
)
}
11 changes: 11 additions & 0 deletions core/src/test/kotlin/utils/MathsUtilTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package utils

import formatAsFileSize
import getPercentage
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -36,4 +37,14 @@ class MathsUtilTest : AbstractTest() {
getPercentage(1, total) shouldBe 33.3
getPercentage(2, total) shouldBe 66.7
}

@Test
fun `Format as file size`() {
0L.formatAsFileSize() shouldBe "0 B"
568L.formatAsFileSize() shouldBe "568 B"
1024L.formatAsFileSize() shouldBe "1.0 KB"
34304L.formatAsFileSize() shouldBe "33.5 KB"
5242880L.formatAsFileSize() shouldBe "5.0 MB"
107374182400L.formatAsFileSize() shouldBe "100.0 GB"
}
}
19 changes: 5 additions & 14 deletions server/src/main/java/object/Room.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package object;

import auth.UserConnection;
import org.w3c.dom.Document;
import server.EntropyServer;
import util.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

import org.w3c.dom.Document;

import server.EntropyServer;
import util.CardsUtil;
import util.Debug;
import util.EntropyUtil;
import util.StatisticsUtil;
import util.XmlBuilderServer;
import util.XmlConstants;

/**
* Server-side version of a Room
*/
Expand Down Expand Up @@ -245,10 +240,6 @@ public void initialiseGame()
if (currentGame != null)
{
previousGame = currentGame.factoryCopy();
if (previousGame.getRoundNumber() > 1)
{
StatisticsUtil.saveGlobalStatistics(roomName, previousGame);
}
}

currentGame = new GameWrapper(gameId);
Expand Down
2 changes: 2 additions & 0 deletions server/src/main/java/object/ServerRunnable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package object;


import auth.UserConnection;

public interface ServerRunnable extends Runnable
{
public abstract String getDetails();
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/object/ServerThread.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package object;

import util.Debug;
import auth.UserConnection;

public class ServerThread extends Thread
{
Expand Down
Loading

0 comments on commit a762917

Please sign in to comment.