rt

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

servertask.java (6936B)


      1 package space.nocebo;
      2 
      3 import java.io.*;
      4 import java.net.*;
      5 import java.lang.Math;
      6 import java.util.concurrent.locks.*;
      7 
      8 public class servertask implements Runnable {
      9 	private Socket connection;
     10 	private acc player;
     11 	private group pgroup;
     12 	private script pscript;
     13 	private int loggedin = 0; // status var
     14 	private static String[][] chars = new String[3][2];
     15 	private static int roundAvg = 0;
     16 	private String lastMsg = "";
     17 	private String mainMenu = "";
     18 	private	Lock lock = new ReentrantLock();
     19 	private game pgame;
     20 
     21 	private void printfdbg(String m) {
     22 		String dbg = "dbg " + player.getUname() + " " + m;
     23 		System.out.println(dbg);
     24 	}
     25 
     26 	private int login(String m) {
     27 		String[] inp = m.split(" ", 2);
     28 		for (acc c : server.Accounts)
     29 			if (((inp[0].compareTo(c.getUname())) == 0) &&
     30 			    ((inp[1].compareTo(c.getPass())) == 0)) {
     31 				if (c.getOnline() == true)
     32 					return -1;
     33 				player = c;
     34 				player.setOnline();
     35 				return 1;
     36 			}
     37 		return 0;
     38 	}
     39 
     40 	private void register(String m) {
     41 		String[] inp = m.split(" ", 2);
     42 		player = new acc(inp[0], inp[1], util.getRandomScore());
     43 		server.Accounts.add(player);
     44 	}
     45 
     46 	private void notifyClient(DataOutputStream str, String m) {
     47 		try {
     48 			str.writeUTF(m + "\n");
     49 			str.flush();
     50 		} catch (IOException ex) {
     51 			System.out.println("Connection error: " + ex);
     52 			System.exit(1);
     53 		}
     54 	}
     55 
     56 	private synchronized boolean selChar(DataInputStream inputStr, DataOutputStream outputStr, int timeout) {
     57 		String message = "";
     58 		int val = 0;
     59 		if (timeout == 0) {
     60 			try {
     61 				if (inputStr.available() > 0) {
     62 					message = inputStr.readUTF();
     63 					if (util.isNumeric(message)) {
     64 						val = Integer.parseInt(message);
     65 						if (val < 4 && val > 0) {
     66 							if (pscript.assignUser(val,player,lock) == true) {
     67 								return true;
     68 							}
     69 							else
     70 								notifyClient(outputStr,
     71 									     "Character already assigned\n" +
     72 									     "Try again");
     73 						}
     74 						else if (val == 4) {
     75 							notifyClient(outputStr,pscript.getAssignedUsers(lock));
     76 						}
     77 					} else {
     78 						notifyClient(outputStr,
     79 							     "Invalid input\n" +
     80 							     "Try again\n");
     81 					}
     82 				}
     83 			} catch (IOException ex) {
     84 				System.out.println("IOException " + ex);
     85 			}
     86 		} else if (timeout == 1) {
     87 			pscript.assignUser(0,player,lock);
     88 			return true;
     89 		}
     90 		return false;
     91 	}
     92 
     93 	private String[][] getChar(group g) {
     94 		roundAvg = Math.round(g.getAvgscr());
     95 		for (script s : server.Scripts) {
     96 			if (roundAvg == s.getDlevel()) {
     97 				pgame = server.getGame(s,g);
     98 				pscript = pgame.getScript();
     99 				return pscript.getRoles();
    100 			}
    101 		}
    102 
    103 		return chars;
    104 	}
    105 
    106 	private void initGame() {
    107 		int gf = 0;
    108 		for (group g : server.Groups) {
    109 			if (g.add(player) == 1) {
    110 				pgroup = g;
    111 				gf = 1;
    112 				break;
    113 			}
    114 		}
    115 		if (gf == 0) {
    116 			System.out.println("Group created");
    117 			server.Groups.add(new group());
    118 			for (group g : server.Groups) {
    119 				if (g.add(player) == 1) {
    120 					pgroup = g;
    121 					gf = 1;
    122 					break;
    123 				}
    124 			}
    125 		}
    126 		while (true) {
    127 			if (pgroup.getNumMem() == 3) {
    128 				chars = getChar(pgroup);
    129 				break;
    130 			}
    131 			try {
    132 				Thread.yield();
    133 				Thread.sleep(2000);
    134 			} catch (InterruptedException ex) {
    135 				System.out.println("InterruptedException " + ex);
    136 			}
    137 		}
    138 	}
    139 
    140 	public servertask(Socket s) {
    141 		connection = s;
    142 	}
    143 
    144 	@Override
    145 	public void run() {
    146 		try {
    147 			DataInputStream inputStr = new DataInputStream(
    148 				new BufferedInputStream(connection.getInputStream()));
    149 			DataOutputStream outputStr = new DataOutputStream(
    150 				new BufferedOutputStream(connection.getOutputStream()));
    151 
    152 			mainMenu = "Welcome to RT\nSelect one:\n1 Signup\n2 Login\nq Quit";
    153 			notifyClient(outputStr,
    154 					  mainMenu);
    155 
    156 			String message = inputStr.readUTF();
    157 			int val = 0;
    158 			int lres;
    159 			while (message.compareTo("q") != 0) { // use 'q' to quit
    160 				if (util.isNumeric(message) && (val = Integer.parseInt(message)) > 0 && val < 3) {
    161 					if (val == 1 && loggedin == 0) {
    162 						outputStr.writeUTF("Enter your username and password" +
    163 								   " in the format:" +
    164 								   " username password\n");
    165 						outputStr.flush();
    166 						message = inputStr.readUTF();
    167 						if (util.isValidCred(message)) {
    168 							register(message);
    169 							notifyClient(outputStr,
    170 									  "Registration Successful\n" +
    171 									  "Please press 2 to login");
    172 						} else
    173 							notifyClient(outputStr, "Invalid Input\nTry again\n" + mainMenu);
    174 					}
    175 					if (val == 2 && loggedin == 0) {
    176 						outputStr.writeUTF("Kindly enter your" +
    177 								   " username and password" +
    178 								   " in the format: username password\n");
    179 						outputStr.flush();
    180 						message = inputStr.readUTF();
    181 						if (util.isValidCred(message)) {
    182 							lres = login(message);
    183 							if (lres == 1) {
    184 								loggedin = 1;
    185 								notifyClient(outputStr, "Login Successful\n" +
    186 									     "Please wait.\n" +
    187 									     "Initializing game world...\n");
    188 								initGame();
    189 								lastMsg = "Please select a character " +
    190 									"in 60 seconds :"   + "\n" +
    191 									"1 " +  chars[0][0] + "\n" +
    192 									"2 " +  chars[1][0] + "\n" +
    193 									"3 " +  chars[2][0] + "\n" +
    194 									"4 " +  "View user selection";
    195 								notifyClient(outputStr, lastMsg);
    196 								int t = 0;
    197 								boolean assigned = false;
    198 								String as;
    199 								while (t < 120) {
    200 									t++;
    201 									if (selChar(inputStr,outputStr,0) == true) {
    202 										notifyClient(outputStr,
    203 											     pscript.getAssignedUsers(lock));
    204 										assigned = true;
    205 										break;
    206 									}
    207 									try {
    208 										Thread.yield();
    209 										Thread.sleep(1000);
    210 									} catch (InterruptedException ex) {
    211 										System.out.println("InterruptedException " + ex);
    212 									}
    213 								}
    214 								if (assigned == false) {
    215 									selChar(inputStr,outputStr,1);
    216 								}
    217 								as = pscript.getResult(lock,player);
    218 								notifyClient(outputStr,as);
    219 								player.setOffline();
    220 								notifyClient(outputStr,"Game Over.\n" +
    221 									     "You have been logged out\n" +
    222 									     "Play again? (Please press 2)");
    223 							} else if (lres == 0) {
    224 								System.out.println("dbg lres == 0");
    225 								notifyClient(outputStr, "Login Failed\n" +
    226 									     "Please try again\n" +
    227 									     mainMenu);
    228 							}
    229 							else if (lres == -1) {
    230 								System.out.println("dbg lres = -1");
    231 								notifyClient(outputStr, "Cheating detected.\n" +
    232 									     "This incident will be reported.\n" + 
    233 									     "Please try again\n" +
    234 									     mainMenu);
    235 							}
    236 						} else
    237 							notifyClient(outputStr, "Invalid format\n" +
    238 								     "Please try again\n" +
    239 								     mainMenu);
    240 					}
    241 				} else
    242 					notifyClient(outputStr, "Invalid Input\n" +
    243 						     "Please try again\n" +
    244 						     mainMenu);
    245 				loggedin = 0;
    246 				message = inputStr.readUTF();
    247 			}
    248 			server.Groups.remove(pgroup);
    249 			inputStr.close();
    250 			outputStr.close();
    251 			connection.close();
    252 		} catch (IOException ex) {
    253 			System.out.println("Couldn't establish connection: " + ex);
    254 		}
    255 	}
    256 }