rt

An obligatory Java Project
Log | Files | Refs | LICENSE

commit 5d6d77dccdb964df40e1290e90f96f1262a2d84d
parent d189b24ec42c186ef4b78f33674533a1e4d8da2d
Author: zerous Naveen Narayanan <zerous@nocebo.space>
Date:   Sat, 23 Nov 2019 02:25:54 +0100

Implement client/server, group, script, servertask logic

Observed race conditions
Remove inadvertent bins

Diffstat:
MMakefile | 13+++++++++++--
Dacc.class | 0
Macc.java | 22++++++++++------------
Dclient.class | 0
Agroup.java | 43+++++++++++++++++++++++++++++++++++++++++++
Ascript.java | 21+++++++++++++++++++++
Dserver.class | 0
Mserver.java | 112+++++++++++++------------------------------------------------------------------
Aservertask.java | 173+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dutil.class | 0
Mutil.java | 9+++++++++
11 files changed, 285 insertions(+), 108 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,8 +1,11 @@ -all: server client util acc +all: server client util acc servertask script group server: javac server.java +servertask: + javac servertask.java + client: javac client.java @@ -12,8 +15,14 @@ util: acc: javac acc.java +script: + javac script.java + +group: + javac group.java + .PHONY: clean run clean: - rm server.class client.class util.class acc.class + rm server.class client.class util.class acc.class servertask.class script.class diff --git a/acc.class b/acc.class Binary files differ. diff --git a/acc.java b/acc.java @@ -1,7 +1,5 @@ package space.nocebo; -import java.util.Random; - public class acc { private String uname; private String pass; @@ -12,7 +10,15 @@ public class acc { public acc(String n, String p) { uname = n; pass = p; - score = getRandomInt(); + score = util.getRandomInt(); + online = true; + gid = 0; + } + + public acc(String n, String p, int s) { + uname = n; + pass = p; + score = s; online = true; gid = 0; } @@ -36,12 +42,4 @@ public class acc { public int getGid() { return gid; } - - private int getRandomInt() { - int res = 1; - Random r = new Random(); - while ((res = r.nextInt(5)) != 0) - return res; - return res; - } -}; +} diff --git a/client.class b/client.class Binary files differ. diff --git a/group.java b/group.java @@ -0,0 +1,43 @@ +package space.nocebo; + +import java.lang.Math; + +public class group { + private acc[] gAccounts = new acc[3]; + private int nelem; + private int cmet; // condition met flag - when set conditions are met + private float tscr; + + public group() { + nelem = 0; + cmet = 1; + tscr = 0.0f; + } + + public int add(acc m) { + if (nelem < 3) { + System.out.println("group add"); + for (int i = 0; i < nelem; i++) + if (Math.abs(gAccounts[i].getScore() - m.getScore()) > 2) + cmet = 0; + if (cmet == 1) { + System.out.println("cond met"); + gAccounts[nelem] = m; + nelem++; + cmet = 1; + return 1; + } + } + return 0; + } + + public int getNumMem() { + return nelem; + } + + public float getAvgscr() { + for (int i = 0; i < nelem; i++) + tscr += gAccounts[i].getScore(); + return tscr/nelem; + } +} diff --git a/script.java b/script.java @@ -0,0 +1,21 @@ +package space.nocebo; + +public class script { + private int dlevel; + private String[] roles = new String[3]; + + public script(String c1, String c2, String c3, int d) { + dlevel = d; + roles[0] = c1; + roles[1] = c2; + roles[2] = c3; + } + + public int getDlevel() { + return dlevel; + } + + public String[] getRoles() { + return roles; + } +} diff --git a/server.class b/server.class Binary files differ. diff --git a/server.java b/server.java @@ -3,106 +3,30 @@ package space.nocebo; import java.net.*; import java.io.*; import java.util.*; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import space.nocebo.util; public class server { - private ArrayList<acc> Accounts = new ArrayList<acc>(); - - private DataOutputStream outputStr; - - private int login(String m) { - String[] inp = m.split(" ", 2); - for (acc c : Accounts) - if (((inp[0].compareTo(c.getUname())) == 0) && - ((inp[1].compareTo(c.getPass())) ==0)) - return 1; - return 0; - } - - private void register(String m) { - String[] inp = m.split(" ", 2); - Accounts.add(new acc(inp[0], inp[1])); - } - - private void notifyClient(DataOutputStream str, String m) { - try { - str.writeUTF(m + "\n"); - str.flush(); - } catch (IOException ex) { - System.out.println("Connection error: " + ex); - System.exit(1); - } - } + public static ArrayList<acc> Accounts = new ArrayList<acc>(); + public static ArrayList<script> Scripts = new ArrayList<script>(); + public static ArrayList<group> Groups = new ArrayList<group>(); public static void main(String[] args) throws IOException { - try { - ServerSocket tcpserver = new ServerSocket(1234); - System.out.println("Wait for connections"); - Socket connection = tcpserver.accept(); - server serv = new server(); - System.out.println("TCP Client connected " + connection); - - DataInputStream inputStr = new DataInputStream( - new BufferedInputStream(connection.getInputStream())); - DataOutputStream outputStr = new DataOutputStream( - new BufferedOutputStream(connection.getOutputStream())); - - serv.notifyClient(outputStr, - "Welcome to RT\nSelect one:\n1.Register\n2.Signup"); - - String message = inputStr.readUTF(); - int val = 0; - while (message.compareTo("q") != 0) { // use 'q' to quit - if (util.isNumeric(message)) { - val = Integer.parseInt(message); - if (val == 1) { - System.out.println("in 1"); - outputStr.writeUTF("Enter your username and password" + - " in the format:" + - " username password\n"); - outputStr.flush(); - message = inputStr.readUTF(); - if (util.isValidCred(message)) { - serv.register(message); - serv.notifyClient(outputStr, - "Registration Successful\n" + - "Please press 2 to login"); - } - else - serv.notifyClient(outputStr, "Invalid Input"); - } else if (val == 2) { - System.out.println("in 2"); - outputStr.writeUTF("Kindly enter your preferred" + - " username and password" + - " in the format: username password\n"); - outputStr.flush(); - message = inputStr.readUTF(); - if (util.isValidCred(message)) { - if (serv.login(message) == 1) - serv.notifyClient(outputStr, "Login Successful"); - else - serv.notifyClient(outputStr, "Login Failed\n" + - "Please try again later"); - } - else - serv.notifyClient(outputStr, "Invalid input"); - } - } else { - serv.notifyClient(outputStr, "Invalid Input"); - } - System.out.println("TCP Client> " + message); - message = inputStr.readUTF(); - } - - inputStr.close(); - outputStr.close(); - connection.close(); - tcpserver.close(); - - } catch (IOException ex) { - System.out.println("Couldn't establish connection: " + ex); + server.Scripts.add(new script("Dark Knight", "Gordon", "Alfred", 1)); + server.Scripts.add(new script("Andy", "Red", "Brooks", 2)); + server.Scripts.add(new script("Don", "Michael", "Sonny", 3)); + Socket connection; + ServerSocket serverSocket; + + ExecutorService executor = Executors.newFixedThreadPool(10); + int port = 1234; + serverSocket = new ServerSocket(port); + while (true) { + System.out.println("Waiting for connections"); + connection = serverSocket.accept(); + executor.execute(new servertask(connection)); } - } } diff --git a/servertask.java b/servertask.java @@ -0,0 +1,173 @@ +package space.nocebo; + +import java.io.*; +import java.net.*; +import java.lang.Math; + +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 static int roundAvg = 0; + + private int login(String m) { + String[] inp = m.split(" ", 2); + for (acc c : server.Accounts) + if (((inp[0].compareTo(c.getUname())) == 0) && + ((inp[1].compareTo(c.getPass())) == 0)) { + player = c; + return 1; + } + return 0; + } + + private void register(String m) { + String[] inp = m.split(" ", 2); + player = new acc(inp[0], inp[1], 1); + server.Accounts.add(player); + } + + private void notifyClient(DataOutputStream str, String m) { + try { + str.writeUTF(m + "\n"); + str.flush(); + } catch (IOException ex) { + System.out.println("Connection error: " + ex); + System.exit(1); + } + } + + private String[] getChar(group g) { + roundAvg = Math.round(g.getAvgscr()); + System.out.println("dbg " + roundAvg); + for (script s : server.Scripts) { + if (roundAvg == s.getDlevel()) { + System.out.println("dbg " + s.getDlevel()); + return s.getRoles(); + } + } + + return chars; + } + + private void initGame() { + int gf = 0; + for (group g : server.Groups) { + System.out.println("gs"); + if (g.add(player) == 1) { + pgroup = g; + gf = 1; + break; + } + } + if (gf == 0) { + System.out.println("Group created"); + server.Groups.add(new group()); + for (group g : server.Groups) { + if (g.add(player) == 1) { + pgroup = g; + gf = 1; + break; + } + } + } + while (true) { + if (pgroup.getNumMem() == 3) { + System.out.println("getchar"); + chars = getChar(pgroup); + System.out.println("dbg chars: " + chars[0] + chars[1] + chars[2]); + break; + } + try { + Thread.sleep(2000); + } catch (InterruptedException ex) { + System.out.println("InterruptedException " + ex); + } + } + } + + public servertask(Socket s) { + connection = s; + } + + @Override + public void run() { + try { + System.out.println("TCP Client connected " + connection); + + DataInputStream inputStr = new DataInputStream( + new BufferedInputStream(connection.getInputStream())); + DataOutputStream outputStr = new DataOutputStream( + new BufferedOutputStream(connection.getOutputStream())); + + notifyClient(outputStr, + "Welcome to RT\nSelect one:\n1.Register\n2.Signup"); + + String message = inputStr.readUTF(); + int val = 0; + while (message.compareTo("q") != 0) { // use 'q' to quit + if (util.isNumeric(message)) { + val = Integer.parseInt(message); + if (val == 1 && loggedin == 0) { + System.out.println("in 1"); + outputStr.writeUTF("Enter your username and password" + + " in the format:" + + " username password\n"); + outputStr.flush(); + message = inputStr.readUTF(); + if (util.isValidCred(message)) { + register(message); + notifyClient(outputStr, + "Registration Successful\n" + + "Please press 2 to login"); + } + else + notifyClient(outputStr, "Invalid Input"); + } else if (val == 2 && loggedin == 0) { + System.out.println("in 2"); + outputStr.writeUTF("Kindly enter your preferred" + + " username and password" + + " in the format: username password\n"); + outputStr.flush(); + message = inputStr.readUTF(); + if (util.isValidCred(message)) { + if (login(message) == 1) { + loggedin = 1; + notifyClient(outputStr, "Login Successful\n" + + "Please wait. Initializing world..."); + initGame(); + System.out.println("here"); + notifyClient(outputStr, chars[0] + "\n" + + chars[1] + "\n" + + chars[2] + "\n"); + } + else + notifyClient(outputStr, "Login Failed\n" + + "Please try again later"); + } + else + notifyClient(outputStr, "Invalid input"); + } else if (loggedin == 1) { + + } + + } else { + notifyClient(outputStr, "Invalid Input"); + } + System.out.println("TCP Client> " + message); + message = inputStr.readUTF(); + } + + inputStr.close(); + outputStr.close(); + connection.close(); + + } catch (IOException ex) { + System.out.println("Couldn't establish connection: " + ex); + } + } + +} + diff --git a/util.class b/util.class Binary files differ. diff --git a/util.java b/util.java @@ -1,6 +1,7 @@ package space.nocebo; import java.util.regex.PatternSyntaxException; +import java.util.Random; public class util { public static boolean isNumeric(String s) { @@ -29,4 +30,12 @@ public class util { } return true; } + + public static int getRandomInt() { + int res = 1; + Random r = new Random(); + while ((res = r.nextInt(5)) != 0) + return res; + return res; + } }