Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.SocketTimeoutException;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.regex.Pattern;
- public class Server extends Thread {
- protected static boolean serverContinue = true;
- protected static int clientAmount = 0;
- protected static ArrayList<Server> servers = new ArrayList<Server>(24);
- protected Socket clientSocket;
- protected PrintWriter out;
- protected BufferedReader in;
- protected int ID = 0, points = 0, plays = 0, streak = 0;
- public static void main(String[] args) throws IOException {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(10008);
- System.out.println("Connection Socket Created");
- try {
- while (serverContinue) {
- serverSocket.setSoTimeout(10000);
- System.out.println("Waiting for Connection");
- try {
- Server serv = new Server(serverSocket.accept());
- servers.add(serv);
- castServerMessage("User #" + clientAmount
- + " has joined the session");
- } catch (SocketTimeoutException ste) {
- System.out.println("Timeout Occurred");
- }
- }
- } catch (IOException e) {
- System.err.println("Accept failed.");
- System.exit(1);
- }
- } catch (IOException e) {
- System.err.println("Could not listen on port: 10008.");
- System.exit(1);
- } finally {
- try {
- System.out.println("Closing Server Connection Socket");
- serverSocket.close();
- } catch (IOException e) {
- System.err.println("Could not close port: 10008.");
- System.exit(1);
- }
- }
- }
- public static void castUserMessage(String message, int ID) {
- castMessage("User #" + ID, message);
- }
- public static void castServerMessage(String message) {
- castMessage("Server", message);
- }
- public static void castAdminMessage(String message) {
- castMessage("Admin", message);
- }
- public static void castMessage(String sender, String message) {
- for (Server serv : servers) {
- if (serv.out != null)
- serv.out.println("@" + sender + ": " + message);
- }
- }
- private Server(Socket clientSoc) {
- clientSocket = clientSoc;
- start();
- }
- public void run() {
- System.out.println("New Communication Thread Started");
- ID = clientAmount++;
- try {
- out = new PrintWriter(clientSocket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(
- clientSocket.getInputStream()));
- String inputLine;
- int r = 0;
- boolean hasRequested = false;
- while ((inputLine = in.readLine()) != null) {
- if (inputLine.equals("quit")) {
- out.println("@Server: Bye!");
- break;
- }
- if (inputLine.equals("?")) {
- out.println("\"quit\" ends connection");
- out.println("\"request\" requests equation");
- out.println("\"points\" shows points");
- out.println("\"users\" shows people playing");
- out.println("\"msg [message]\" casts a message to all players");
- out.println("You simply type the result of the equation you've been given to answer it");
- out.println("Answering correctly will result in streaks which will give you bonus points");
- continue;
- }
- if (inputLine.startsWith("msg ")) {
- if (inputLine.length() > 5) {
- castUserMessage(
- inputLine.substring(4, inputLine.length()), ID);
- } else {
- out.println("@Syntax: msg [message]");
- }
- continue;
- }
- if (inputLine.startsWith("adm ")) {
- if (inputLine.length() > 5) {
- castAdminMessage(inputLine.substring(4,
- inputLine.length()));
- } else {
- out.println("@Syntax: adm [message]");
- }
- continue;
- }
- if (inputLine.equals("points")) {
- out.println("@Server: You have " + points + " points and "
- + plays + " requests!");
- continue;
- }
- if (inputLine.equals("users")) {
- if (clientAmount == 1) {
- out.println("@Server: You are alone here!");
- } else {
- out.println("@Server: There are " + clientAmount
- + " people playing!");
- }
- int count = 0;
- for (Server serv : servers) {
- count++;
- out.println("@Server: User #" + serv.ID + " with "
- + serv.points + " points and " + serv.plays
- + " requests");
- }
- continue;
- }
- if (inputLine.equals("request")) {
- if (hasRequested) {
- out.println("@Server: You already have a request!");
- } else {
- Random rng = new Random();
- int a = 50 - rng.nextInt(101), b = 50 - rng
- .nextInt(101);
- r = a + b;
- hasRequested = true;
- plays++;
- out.println("@Server: " + a + " + " + b + " = ?");
- }
- continue;
- }
- if (inputLine.equals("" + r)) {
- if (hasRequested) {
- hasRequested = false;
- points += 2 + streak;
- out.println("@Server: Correct! +2 points"
- + (streak > 0 ? " and +" + streak
- + " more for being on a streak!" : ""));
- streak++;
- } else {
- out.println("@Server: That's the answer before!");
- }
- continue;
- } else if (!inputLine.equals("" + r)
- && Pattern.matches("-?[0-9]+", inputLine)
- && hasRequested) {
- points--;
- hasRequested = false;
- out.println("@Server: Wrong answer! -1 points"
- + (streak > 0 ? ", you also aren't on a streak anymore"
- : ""));
- streak = 0;
- continue;
- }
- }
- clientAmount--;
- servers.remove(this);
- out.close();
- in.close();
- clientSocket.close();
- castServerMessage("User #" + ID + " has quit the session");
- } catch (IOException e) {
- System.err.println("Problem with Communication Server");
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement