rt

Reader's Theatre
Log | Files | Refs | README | LICENSE

commit fe118a2bee84543d4c7f7b797e8cfcbf3e3c86a0
parent 903efad6408c0fe6fb120b23c4d20de49b76cb05
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Sat, 14 Dec 2019 19:47:41 +0100

Make sure that a user can't login from multiple clients at the same time using the same credentials.

Diffstat:
Macc.java | 12++++++++++--
Mservertask.java | 19+++++++++++++++++--
2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/acc.java b/acc.java @@ -11,7 +11,7 @@ public class acc { uname = n; pass = p; score = util.getRandomInt(); - online = true; + online = false; gid = 0; } @@ -19,7 +19,7 @@ public class acc { uname = n; pass = p; score = s; - online = true; + online = false; gid = 0; } @@ -43,6 +43,14 @@ public class acc { return online; } + public void setOnline() { + online = true; + } + + public void setOffline() { + online = false; + } + public int getGid() { return gid; } diff --git a/servertask.java b/servertask.java @@ -28,7 +28,10 @@ public class servertask implements Runnable { for (acc c : server.Accounts) if (((inp[0].compareTo(c.getUname())) == 0) && ((inp[1].compareTo(c.getPass())) == 0)) { + if (c.getOnline() == true) + return -1; player = c; + player.setOnline(); return 1; } return 0; @@ -152,6 +155,7 @@ public class servertask implements Runnable { String message = inputStr.readUTF(); int val = 0; + int lres; while (message.compareTo("q") != 0) { // use 'q' to quit if (util.isNumeric(message) && (val = Integer.parseInt(message)) > 0 && val < 3) { if (val == 1 && loggedin == 0) { @@ -175,7 +179,8 @@ public class servertask implements Runnable { outputStr.flush(); message = inputStr.readUTF(); if (util.isValidCred(message)) { - if (login(message) == 1) { + lres = login(message); + if (lres == 1) { loggedin = 1; notifyClient(outputStr, "Login Successful\n" + "Please wait.\n" + @@ -211,13 +216,23 @@ public class servertask implements Runnable { } as = pscript.getResult(lock,player); notifyClient(outputStr,as); + player.setOffline(); notifyClient(outputStr,"Game Over.\n" + "You have been logged out\n" + "Play again? (Please press 2)"); - } else + } else if (lres == 0) { + System.out.println("dbg lres == 0"); notifyClient(outputStr, "Login Failed\n" + "Please try again\n" + mainMenu); + } + else if (lres == -1) { + System.out.println("dbg lres = -1"); + notifyClient(outputStr, "Cheating detected.\n" + + "This incident will be reported.\n" + + "Please try again\n" + + mainMenu); + } } else notifyClient(outputStr, "Invalid format\n" + "Please try again\n" +