commit e5a085dfc078c472caa508e1593819b5bc0b3c6c
parent 766ac5bd0cb2d1afc459e82632fba61eaf98e340
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date: Mon, 25 Nov 2019 17:34:12 +0100
Implement selChar(), Synchronize using locks and Fix a couple of things
Diffstat:
M | servertask.java | | | 101 | ++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 64 insertions(+), 37 deletions(-)
diff --git a/servertask.java b/servertask.java
@@ -3,15 +3,18 @@ package space.nocebo;
import java.io.*;
import java.net.*;
import java.lang.Math;
+import java.util.concurrent.locks.*;
public class servertask implements Runnable {
private Socket connection;
private acc player;
private group pgroup;
+ private script pscript;
private int loggedin = 0; // status var
- private int charsel = 0; // status var
private static String[][] chars = new String[3][2];
private static int roundAvg = 0;
+ private String lastMsg = "";
+ private Lock lock = new ReentrantLock();
private int login(String m) {
String[] inp = m.split(" ", 2);
@@ -137,15 +140,16 @@ public class servertask implements Runnable {
@Override
public void run() {
try {
- System.out.println("TCP Client connected " + connection);
+ // System.out.println("TCP Client connected " + connection);
DataInputStream inputStr = new DataInputStream(
new BufferedInputStream(connection.getInputStream()));
DataOutputStream outputStr = new DataOutputStream(
new BufferedOutputStream(connection.getOutputStream()));
+ lastMsg = "Welcome to RT\nSelect one:\n1 Signup\n2 Login\nq Quit";
notifyClient(outputStr,
- "Welcome to RT\nSelect one:\n1.Register\n2.Signup");
+ lastMsg);
String message = inputStr.readUTF();
int val = 0;
@@ -163,12 +167,12 @@ public class servertask implements Runnable {
notifyClient(outputStr,
"Registration Successful\n" +
"Please press 2 to login");
- }
- else
- notifyClient(outputStr, "Invalid Input");
- } else if (val == 2 && loggedin == 0) {
+ } else
+ notifyClient(outputStr, "Invalid Input\nTry again\n" + lastMsg);
+ }
+ if (val == 2 && loggedin == 0) {
System.out.println("in 2");
- outputStr.writeUTF("Kindly enter your preferred" +
+ outputStr.writeUTF("Kindly enter your" +
" username and password" +
" in the format: username password\n");
outputStr.flush();
@@ -177,39 +181,64 @@ public class servertask implements Runnable {
if (login(message) == 1) {
loggedin = 1;
notifyClient(outputStr, "Login Successful\n" +
- "Please wait. Initializing world...");
+ "Please wait.\n" +
+ " Initializing game world...\n");
initGame();
System.out.println("here");
- notifyClient(outputStr,
- "Please select a character in 60 seconds : \n" +
- "1. " + chars[0][0] + "\n" +
- "2. " + chars[1][0] + "\n" +
- "3. " + chars[2][0] + "\n" );
- // Thread.sleep(10000);
- charsel = 1;
-
- }
- else
+ lastMsg = "Please select a character " +
+ "in 60 seconds :" + "\n" +
+ "1 " + chars[0][0] + "\n" +
+ "2 " + chars[1][0] + "\n" +
+ "3 " + chars[2][0] + "\n" +
+ "4 " + "View user selection";
+ notifyClient(outputStr, lastMsg);
+ int t = 0;
+ boolean assigned = false;
+ String as;
+ while (t < 120) {
+ t++;
+ if (selChar(inputStr,outputStr,0) == true) {
+ notifyClient(outputStr,
+ pscript.getAssignedUsers(lock));
+ assigned = true;
+ break;
+ }
+ try {
+ Thread.yield();
+ Thread.sleep(1000);
+ } catch (InterruptedException ex) {
+ System.out.println("InterruptedException " + ex);
+ }
+ }
+ if (assigned == false) {
+ // as = pscript.assignUser(0,player);
+ // notifyClient(outputStr,as);
+ selChar(inputStr,outputStr,1);
+ }
+ System.out.println("dbg: flow " + player.getUname());
+ as = pscript.getResult(lock,player);
+ System.out.println("result: " + as);
+ notifyClient(outputStr,as);
+ notifyClient(outputStr,"Game Over.\n" +
+ "You have been logged out\n" +
+ "Play again? (Please press 2)");
+ } else
notifyClient(outputStr, "Login Failed\n" +
- "Please try again later");
- }
- else
- notifyClient(outputStr, "Invalid input");
- } else if (loggedin == 1 && charsel == 1) {
- if (util.isNumeric(message)) {
- val = Integer.parseInt(message);
- if (val == 1)
- ;
- }
+ "Please try again\n" +
+ "Welcome to RT\nSelect one:\n1 Signup\n2 Login\nq Quit");
+ } else
+ notifyClient(outputStr, "Invalid format\n" +
+ "Please try again\n" +
+ "Welcome to RT\nSelect one:\n1 Signup\n2 Login\nq Quit");
}
-
- } else {
- notifyClient(outputStr, "Invalid Input");
- }
- System.out.println("TCP Client> " + message);
+ } else
+ notifyClient(outputStr, "Invalid Input\n" +
+ "Please try again\n" +
+ "Welcome to RT\nSelect one:\n1 Signup\n2 Login\nq Quit");
+ // System.out.println("TCP Client> " + message);
message = inputStr.readUTF();
}
-
+ server.Groups.remove(pgroup);
inputStr.close();
outputStr.close();
connection.close();
@@ -218,6 +247,4 @@ public class servertask implements Runnable {
System.out.println("Couldn't establish connection: " + ex);
}
}
-
}
-