rt

An obligatory Java Project
Log | Files | Refs | LICENSE

commit 4befec5ed55ac9481f2d1fdd0685087c951aee2d
parent aae8ab3b01beba54ca1deb97278b8a933307a4f0
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Sun, 24 Nov 2019 21:21:26 +0100

Fix race condition

Diffstat:
Mgroup.java | 8++++----
Mservertask.java | 34++++++++++++++++++++--------------
2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/group.java b/group.java @@ -14,7 +14,7 @@ public class group { tscr = 0.0f; } - public int add(acc m) { + public synchronized int add(acc m) { if (nelem < 3) { System.out.println("group add"); for (int i = 0; i < nelem; i++) @@ -31,18 +31,18 @@ public class group { return 0; } - public int getNumMem() { + public synchronized int getNumMem() { return nelem; } - public float getAvgscr() { + public synchronized float getAvgscr() { tscr = 0.0f; for (int i = 0; i < nelem; i++) tscr += gAccounts[i].getScore(); return tscr/nelem; } - public void getAccUname() { + public synchronized void getAccUname() { String s = "dbg group: "; for (int i = 0; i < nelem; i++) { s += " " + gAccounts[i].getUname(); diff --git a/servertask.java b/servertask.java @@ -8,8 +8,9 @@ public class servertask implements Runnable { private Socket connection; private acc player; private group pgroup; - private int loggedin = 0; - private static String[] chars = new String[3]; + 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 int login(String m) { @@ -39,7 +40,7 @@ public class servertask implements Runnable { } } - private String[] getChar(group g) { + private String[][] getChar(group g) { roundAvg = Math.round(g.getAvgscr()); System.out.println("dbg " + roundAvg); for (script s : server.Scripts) { @@ -74,17 +75,13 @@ public class servertask implements Runnable { } } while (true) { + // pgroup.getAccUname(); if (pgroup.getNumMem() == 3) { System.out.println("getchar"); chars = getChar(pgroup); - System.out.println("dbg chars: " + chars[0] + chars[1] + chars[2]); + System.out.println("dbg chars: " + chars[0][0] + chars[1][0] + chars[2][0]); break; } - try { - Thread.sleep(2000); - } catch (InterruptedException ex) { - System.out.println("InterruptedException " + ex); - } } } @@ -139,9 +136,14 @@ public class servertask implements Runnable { "Please wait. Initializing world..."); initGame(); System.out.println("here"); - notifyClient(outputStr, chars[0] + "\n" + - chars[1] + "\n" + - chars[2] + "\n"); + 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 notifyClient(outputStr, "Login Failed\n" + @@ -149,8 +151,12 @@ public class servertask implements Runnable { } else notifyClient(outputStr, "Invalid input"); - } else if (loggedin == 1) { - + } else if (loggedin == 1 && charsel == 1) { + if (util.isNumeric(message)) { + val = Integer.parseInt(message); + if (val == 1) + ; + } } } else {