Skip to content

Commit 938f28b

Browse files
committed
refactoring and improvements
1 parent 95f64a7 commit 938f28b

11 files changed

Lines changed: 208 additions & 244 deletions

File tree

game_server/src/main/kotlin/com/imsproject/gameserver/ExtensionFunctions.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ fun String.toResponseEntity (): ResponseEntity<String> {
3636

3737
fun LocalDateTime.isMoreThanSecondsAgo(seconds: Long): Boolean {
3838
return this.isAfter(LocalDateTime.now().minusSeconds(seconds))
39+
}
40+
41+
inline fun <reified T> runTimed(lambda: () -> T) : Pair<T,Long> {
42+
val start = System.currentTimeMillis()
43+
val result = lambda()
44+
val end = System.currentTimeMillis()
45+
return result to end - start
3946
}

game_server/src/main/kotlin/com/imsproject/gameserver/api/RestHandler.kt

Lines changed: 165 additions & 206 deletions
Large diffs are not rendered by default.

game_server/src/main/kotlin/com/imsproject/gameserver/business/auth/AuthController.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ class AuthController(
3131

3232
fun createUser(credentials: Credentials) {
3333
val cleanUserId = credentials.userId.lowercase()
34-
val rawPassword = credentials.password ?: run {
35-
throw IllegalArgumentException("No password given")
36-
}
34+
val rawPassword = credentials.password
3735
if (userExists(cleanUserId)) {
3836
throw IllegalArgumentException("user already exists")
3937
}
@@ -43,7 +41,7 @@ class AuthController(
4341

4442
log.debug("Adding user credentials for user {}", cleanUserId)
4543
val hashedPassword = encoder.encode(rawPassword)
46-
val userCredentials = Credentials(cleanUserId, hashedPassword,null)
44+
val userCredentials = Credentials(cleanUserId, hashedPassword)
4745
this@AuthController.credentials[cleanUserId] = userCredentials
4846
}
4947

@@ -83,9 +81,7 @@ class AuthController(
8381
return false
8482
}
8583
log.trace("User {} exists", userId)
86-
val hashedPassword = user.password ?: run {
87-
throw IllegalArgumentException("No password given")
88-
}
84+
val hashedPassword = user.password
8985

9086
// check if the password is correct
9187
if (!isPasswordsMatch(password, hashedPassword)) {

game_server/src/main/kotlin/com/imsproject/gameserver/business/auth/Credentials.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.imsproject.gameserver.business.auth
22

33
data class Credentials(
44
val userId: String,
5-
val password: String?,
6-
val token: String?,
5+
val password: String,
76
)
87

game_server/src/main/kotlin/com/imsproject/gameserver/business/auth/CredentialsController.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.imsproject.gameserver.business.auth
22

33
import org.springframework.stereotype.Component
4-
import java.io.File
54
import java.util.concurrent.ConcurrentHashMap
65

76
private const val USERS_PATH = "/app/data/users/"
@@ -12,8 +11,8 @@ class CredentialsController {
1211
val credentials = ConcurrentHashMap<String,Credentials>()
1312

1413
init {
15-
credentials.put("admin",Credentials("admin","\$2a\$10\$3Dw.gqCvW3tbdHc7qGSLr.4ry49vTMebhTKxu/J5zcNrEBKi4BuGG",null))
16-
credentials.put("user",Credentials("user","\$2a\$10\$3Dw.gqCvW3tbdHc7qGSLr.4ry49vTMebhTKxu/J5zcNrEBKi4BuGG",null))
14+
credentials.put("admin",Credentials("admin", "\$2a\$10\$3Dw.gqCvW3tbdHc7qGSLr.4ry49vTMebhTKxu/J5zcNrEBKi4BuGG"))
15+
credentials.put("user",Credentials("user", "\$2a\$10\$3Dw.gqCvW3tbdHc7qGSLr.4ry49vTMebhTKxu/J5zcNrEBKi4BuGG"))
1716
}
1817

1918
operator fun get(userId: String): Credentials? {
@@ -25,14 +24,20 @@ class CredentialsController {
2524
}
2625

2726
operator fun set(userId: String, credentials: Credentials) {
27+
if (userId == "admin") {
28+
throw IllegalArgumentException("Cannot modify admin user credentials")
29+
}
2830
this.credentials[userId] = credentials
2931
}
3032

3133
fun getAll(): List<String>{
32-
return credentials.keys().toList()
34+
return credentials.keys().toList().filter { it != "admin" }
3335
}
3436

3537
fun remove(userId: String) : Boolean {
38+
if(userId == "admin") {
39+
throw IllegalArgumentException("Cannot delete admin user")
40+
}
3641
return credentials.remove(userId) != null
3742
}
3843
}

game_server/src/main/resources/static/error_page.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
<body>
3636
<div class="error-container">
3737
<h1>Oops!</h1>
38-
<p>[MESSAGE]</p>
39-
<p class="timestamp">Timestamp: [TIME_STAMP]</p>
40-
<p><strong>[STATUS]</strong></p>
38+
<p>{{message}}</p>
39+
<p class="timestamp">Timestamp: {{timestamp}}</p>
40+
<p><strong>{{status}}</strong></p>
4141
</div>
4242
</body>
4343
</html>

manager/src/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,11 @@ def get_all_sessions_route():
480480

481481
@app.route('/experiment_questions', methods=['GET'])
482482
def get_experiment_questions_route():
483-
return render_template('experiment_questions.html')
483+
if RUNNING_LOCAL:
484+
url = "http://localhost:8080"
485+
else:
486+
url = "https://ims-project.cs.bgu.ac.il:8640"
487+
return render_template('experiment_questions.html',URL=url)
484488

485489

486490
if __name__ == '__main__':

manager/src/managers/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
if RUNNING_LOCAL:
1010
URL = "http://localhost:8080"
1111
else:
12-
URL = "http://ims-game-server:8080/"
12+
URL = "http://ims-game-server:8080"
1313

1414
GAL = False
1515

1616
if GAL:
17-
URL = "https://ims-project.cs.bgu.ac.il:8640/"
17+
URL = "https://ims-project.cs.bgu.ac.il:8640"
1818

1919

2020
from flask import session

manager/src/managers/session_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def get_event_data(session_id: str, type_: str = None, subtype: str = None) -> l
2929
js["subtype"] = subtype
3030

3131
res = server_response(
32-
post_auth(f"{URL}data/session/select/events", json=js, timeout=1.0)
32+
post_auth(f"{URL}/data/session/select/events", json=js, timeout=1.0)
3333
)
3434

3535
if not res or not res.get_success():
@@ -244,7 +244,7 @@ def get_swipe_game_frequency(session_id: str):
244244
def get_lobbies_data() -> list[dict]:
245245
try:
246246
res = server_response(
247-
post_auth(URL + "data/experiment/select/names", json={"sessionId": None}, timeout=1.0)
247+
post_auth(URL + "/data/experiment/select/names", json={"sessionId": None}, timeout=1.0)
248248
)
249249
return _decode_list(res.get_payload()) if res.get_success() else []
250250
except Exception as e:
@@ -255,7 +255,7 @@ def get_lobbies_data() -> list[dict]:
255255
def get_sessions_for_lobby(lobby_id: str) -> list[dict]:
256256
try:
257257
res = server_response(
258-
post_auth(URL + "data/session/select", json={"expId": lobby_id}, timeout=1.0)
258+
post_auth(URL + "/data/session/select", json={"expId": lobby_id}, timeout=1.0)
259259
)
260260
return _decode_list(res.get_payload()) if res.get_success() else []
261261
except Exception as e:

manager/src/templates/experiment_questions.html

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@
258258
<button type="submit" class="submit-btn">שלח</button>
259259
</form>
260260
</div>
261-
<script>
262-
document.getElementById('survey').addEventListener('submit', function(event) {
263-
event.preventDefault();
264-
alert('תודה על המענה!');
265-
this.reset();
266-
});
267-
</script>
261+
{# <script>#}
262+
{# document.getElementById('survey').addEventListener('submit', function(event) {#}
263+
{# event.preventDefault();#}
264+
{# alert('תודה על המענה!');#}
265+
{# this.reset();#}
266+
{# });#}
267+
{# </script>#}
268268

269269
<script>
270270
function getQueryParams() {
@@ -303,17 +303,10 @@
303303
const { expId, pid } = getQueryParams();
304304
const qnas = collectFormData();
305305

306-
// if (!expId || !pid || qnas.length === 0) {
307-
// alert("שגיאה: חסר מזהה ניסוי, מזהה משתתף או תשובות.");
308-
// return;
309-
// }
310-
311306
const payload = { expId, pid, qnas };
312307

313308
try {
314-
// URL https://ims-project.cs.bgu.ac.il:8640/
315-
// need to add this /data/experiment/insert/feedback
316-
const res = await fetch('https://ims-project.cs.bgu.ac.il:8640/data/experiment/insert/feedback', {
309+
const res = await fetch('{{URL}}/data/experiment/insert/feedback', {
317310
method: 'POST',
318311
headers: { 'Content-Type': 'application/json' },
319312
body: JSON.stringify(payload)

0 commit comments

Comments
 (0)