rt

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

script.java (2470B)


      1 package space.nocebo;
      2 
      3 import java.util.concurrent.locks.*;
      4 
      5 public class script {
      6 	private int dlevel;
      7 	private String[][] roles = new String[3][3];
      8 
      9 	public script(String c1, String c2, String c3, int d) {
     10 		dlevel = d;
     11 		roles[0][0] = c1;
     12 		roles[1][0] = c2;
     13 		roles[2][0] = c3;
     14 		roles[0][1] = "";
     15 		roles[1][1] = "";
     16 		roles[2][1] = "";
     17 		roles[0][2] = "";
     18 		roles[1][2] = "";
     19 		roles[2][2] = "";
     20 	}
     21 
     22 	public synchronized int getDlevel() {
     23 		return dlevel;
     24 	}
     25 
     26 	public synchronized String[][] getRoles() {
     27 		return roles;
     28 	}
     29 
     30 	public synchronized void resetUsers() {
     31 		for (int i = 0, j = 1; i < 3; i++)
     32 			roles[i][j] = "";
     33 	}
     34 
     35 	public synchronized void resetScores() {
     36 		for (int i = 0, j = 2; i < 3; i++)
     37 			roles[i][j] = "";
     38 	}
     39 	
     40 	public String getAssignedUsers(Lock lock) {
     41 		lock.lock();
     42 		try {
     43 			String s = "";
     44 			for (int i = 0, j = 1; i < 3; i++)
     45 				if ((roles[i][j].isEmpty() == false)) {
     46 					s += roles[i][j] + " assigned to " + roles[i][0] + "\n";
     47 				}
     48 			return s;
     49 		} finally {
     50 			lock.unlock();
     51 		}
     52 	}
     53 
     54 	public boolean assignUser(int p, acc a, Lock lock) {
     55 		lock.lock();
     56 		try {
     57 			if (p == 0)
     58 			{
     59 				for (int i = 0, j = 1; i < 3; i++) {
     60 					if (roles[i][j].isEmpty() == true) {
     61 						a.setScore();
     62 						roles[i][j] = a.getUname();
     63 						roles[i][j+1] = Integer.toString(a.getScore());
     64 						System.out.println("dbg: " + roles[i][0] + a.getUname() + roles[i][2]);
     65 						return true;
     66 					}
     67 				}
     68 			} else if (p > 0) {
     69 				if (roles[p - 1][1].isEmpty() == true) {
     70 					a.setScore();
     71 					roles[p - 1][1] = a.getUname();
     72 					roles[p - 1][2] = Integer.toString(a.getScore());
     73 					System.out.println("dbg: " + roles[p - 1][0] + a.getUname() + roles[p - 1][2]);
     74 					return true;
     75 				}
     76 			}
     77 			return false;
     78 		} finally {
     79 			lock.unlock();
     80 		}
     81 	}
     82 
     83 	public String getResult(Lock lock, acc a) {
     84 		lock.lock();
     85 		String s = "";
     86 		try {
     87 			while (true) {
     88 				if (roles[0][1].isEmpty() || roles[1][1].isEmpty() || roles[2][1].isEmpty()) {
     89 					try {
     90 						Thread.sleep(2000);
     91 					} catch (InterruptedException ex) {
     92 						System.out.println("InterruptedException " + ex);
     93 					}
     94 				}
     95 				else
     96 					break;
     97 			}
     98 			s = roles[0][0] + " played by " + roles[0][1] + " got " +  roles[0][2] + " points." + "\n" +
     99 				roles[1][0] + " played by " + roles[1][1] + " got " +  roles[1][2] + " points." + "\n" +
    100 				roles[2][0] + " played by " + roles[2][1] + " got " +  roles[2][2] + " points." + "\n";
    101 			return s;
    102 		} finally {
    103 			lock.unlock();
    104 		}
    105 	}
    106 }