UE23
This commit is contained in:
@@ -1,130 +0,0 @@
|
||||
package UE23_270525_Chatserver;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ChatClient extends Thread {
|
||||
Socket client;
|
||||
String name = "";
|
||||
|
||||
public ChatClient(Socket s) {
|
||||
client = s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()))) {
|
||||
while (true) {
|
||||
wr.write("Willkommen beim Mathe-Quiz\n\rWie heftig hättest du es gerne?");
|
||||
wr.flush();
|
||||
while (true) {
|
||||
int level;
|
||||
wr.write("\n\rBitte gibt eine Zahl zwischen 1 und 3 ein. (Leer für 1): ");
|
||||
wr.flush();
|
||||
try {
|
||||
String input = br.readLine();
|
||||
if (input == null || input.trim().isEmpty()) break;
|
||||
level = Integer.parseInt(input);
|
||||
} catch (Exception _) {
|
||||
continue;
|
||||
}
|
||||
if (level < 1 || level > 3) continue;
|
||||
this.level = level;
|
||||
break;
|
||||
}
|
||||
while (true) {
|
||||
int anz;
|
||||
wr.write("\n\rWie viele Fragen meochtest du loesen (1 - 999) (Leer für 10): ");
|
||||
wr.flush();
|
||||
try {
|
||||
String input = br.readLine();
|
||||
if (input == null || input.trim().isEmpty()) break;
|
||||
anz = Integer.parseInt(input);
|
||||
} catch (Exception _) {
|
||||
continue;
|
||||
}
|
||||
if (anz < 1 || anz > 999) continue;
|
||||
this.anzCals = anz;
|
||||
break;
|
||||
}
|
||||
for (int i = 1; i <= anzCals; i++) {
|
||||
int type = randomInRange(1, 4), solution, a, b;
|
||||
String operator;
|
||||
switch (level) {
|
||||
case 1 -> {
|
||||
a = randomInRange(1, 10);
|
||||
b = randomInRange(1, 10);
|
||||
}
|
||||
case 2 -> {
|
||||
a = randomInRange(-100, 100);
|
||||
b = randomInRange(-100, 100);
|
||||
}
|
||||
case 3 -> {
|
||||
a = randomInRange(-10_000, 10_000);
|
||||
b = randomInRange(-10_000, 10_000);
|
||||
}
|
||||
default -> throw new IllegalStateException("Unexpected value: " + level);
|
||||
}
|
||||
switch (type) {
|
||||
case 1 -> {
|
||||
operator = "+";
|
||||
solution = a + b;
|
||||
}
|
||||
case 2 -> {
|
||||
operator = "-";
|
||||
solution = a - b;
|
||||
}
|
||||
case 3 -> {
|
||||
operator = "*";
|
||||
solution = a * b;
|
||||
}
|
||||
case 4 -> {
|
||||
operator = "/";
|
||||
solution = a / (b == 0 ? 1 : b);
|
||||
}
|
||||
default -> throw new IllegalStateException("Unexpected value: " + type);
|
||||
}
|
||||
String question = "(Level " + level + ") Frage " + i + ": " + a + " " + operator + " " + b + " = ";
|
||||
wr.write(question);
|
||||
wr.flush();
|
||||
while (true) {
|
||||
String message = "\n\rFalsch :( .. versuche es noch einmal\n\n\r" + question;
|
||||
try {
|
||||
String input = br.readLine();
|
||||
if (input == null || input.trim().isEmpty() || Integer.parseInt(input) != solution) {
|
||||
wr.write(message);
|
||||
wr.flush();
|
||||
continue;
|
||||
}
|
||||
} catch (Exception _) {
|
||||
wr.write(message);
|
||||
wr.flush();
|
||||
continue;
|
||||
}
|
||||
wr.write("\n\rrichtig :)\n\n\r");
|
||||
wr.flush();
|
||||
break;
|
||||
}
|
||||
}
|
||||
wr.write("\n\n\n\rHerzlichen Glückwunsch du hast es geschafft!!\n");
|
||||
wr.flush();
|
||||
boolean end = true;
|
||||
while (true) {
|
||||
wr.write("\n\rMoechtest du noch einmal spielen? [y/N]: ");
|
||||
wr.flush();
|
||||
String input = br.readLine();
|
||||
if (input == null || input.trim().isEmpty() || input.trim().equalsIgnoreCase("n")) break;
|
||||
if (input.trim().equalsIgnoreCase("y")) {
|
||||
end = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (end) break;
|
||||
}
|
||||
} catch (Exception _) {
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package UE23_270525_Chatserver;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ChatServer {
|
||||
public static void main(String[] args) {
|
||||
try (ServerSocket server = new ServerSocket(1000)) {
|
||||
while (true) {
|
||||
System.out.println("Ready to connect");
|
||||
Socket client = server.accept();
|
||||
new ChatClient(client).start();
|
||||
}
|
||||
} catch (IOException _) {
|
||||
}
|
||||
}
|
||||
}
|
||||
69
src/UE23_270525_Chatserver/Client.java
Normal file
69
src/UE23_270525_Chatserver/Client.java
Normal file
@@ -0,0 +1,69 @@
|
||||
package UE23_270525_Chatserver;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Client extends Thread {
|
||||
private static final String STZ_DAVOR = new String(new byte[]{0x0b, 0x1b, '[', '1', 'A', 0x1b, '7', 0x1b, '[', '1', 'L', '\r'});
|
||||
private static final String STZ_DANACH = new String(new byte[]{0x1b, '8', 0x1b, '[', '1', 'B'});
|
||||
Socket client;
|
||||
String username = "";
|
||||
int anzMessages = 0;
|
||||
private BufferedWriter wr;
|
||||
|
||||
public Client(Socket s) {
|
||||
client = s;
|
||||
this.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()))) {
|
||||
this.wr = writer;
|
||||
|
||||
wr.write("Willkommen beim Chat-Server der 3AI\n\rUm die Verbindung zu beenden gib \"quit\" ein.\n\rWelchen Spitznamen moechtest du haben: ");
|
||||
wr.flush();
|
||||
|
||||
while (true) {
|
||||
String input = br.readLine();
|
||||
if (input == null || input.isBlank()) continue;
|
||||
this.username = input.trim();
|
||||
if (Server.anmelden(this)) break;
|
||||
wr.write("\n\rDer Spitzname \"" + input + "\" ist leider schon vergeben. Waehle einen anderen: ");
|
||||
wr.flush();
|
||||
}
|
||||
while (true) {
|
||||
wr.write(this.username + "> ");
|
||||
wr.flush();
|
||||
String input = br.readLine();
|
||||
if (input == null || input.isBlank()) continue;
|
||||
input = input.trim();
|
||||
if (input.equalsIgnoreCase("quit")) break;
|
||||
if (input.equalsIgnoreCase("list")) {
|
||||
for (String s : Server.getClients().stream().map(c -> c.username).toList())
|
||||
nachrichtSenden(s + "\n\r");
|
||||
continue;
|
||||
}
|
||||
if (input.equalsIgnoreCase("stat")) {
|
||||
for (String s : Server.getClients().stream().map(c -> c.username + ": " + c.anzMessages + " Messages").toList())
|
||||
nachrichtSenden(s + "\n\r");
|
||||
continue;
|
||||
}
|
||||
Server.nachrichtVerteilen(this, this.username + ": " + input);
|
||||
this.anzMessages++;
|
||||
}
|
||||
} catch (Exception _) {
|
||||
} finally {
|
||||
Server.abmelden(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void nachrichtSenden(String message) {
|
||||
try {
|
||||
wr.write(STZ_DAVOR + message + STZ_DANACH);
|
||||
wr.flush();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
63
src/UE23_270525_Chatserver/Server.java
Normal file
63
src/UE23_270525_Chatserver/Server.java
Normal file
@@ -0,0 +1,63 @@
|
||||
package UE23_270525_Chatserver;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Server {
|
||||
private static final List<Client> clients = new ArrayList<>();
|
||||
|
||||
public static List<Client> getClients() {
|
||||
return clients;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
try (ServerSocket server = new ServerSocket(1000)) {
|
||||
System.out.println("Server ready! (" + server.getInetAddress() + ":" + server.getLocalPort() + ")");
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) {
|
||||
Socket client = server.accept();
|
||||
System.out.println("New client connected: " + client.getInetAddress() + ":" + client.getPort());
|
||||
new Client(client);
|
||||
}
|
||||
} catch (IOException _) {
|
||||
}
|
||||
}
|
||||
|
||||
public static void nachrichtVerteilen(Client client, String message) {
|
||||
String[] strings = message.split(":");
|
||||
if (strings.length > 2) {
|
||||
int index = message.indexOf(":") + 1;
|
||||
String sender = message.substring(0, index);
|
||||
String msg = message.substring(message.indexOf(":", index) + 1);
|
||||
String send = sender + " " + msg.trim();
|
||||
|
||||
Optional<Client> receiver = clients.stream().filter(c -> c.username.equalsIgnoreCase(strings[1].trim())).findFirst();
|
||||
if (receiver.isPresent()) {
|
||||
receiver.get().nachrichtSenden(send);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
clients.stream().filter(c -> !c.username.equalsIgnoreCase(client.username)).forEach(c -> c.nachrichtSenden(message));
|
||||
}
|
||||
|
||||
public static boolean anmelden(Client client) {
|
||||
if (clients.stream().noneMatch(c -> c.username.equalsIgnoreCase(client.username))) {
|
||||
clients.add(client);
|
||||
nachrichtVerteilen(client, "\"" + client.username + "\" hat den Raum betreten.\n\r");
|
||||
System.out.println(client.username + " joined the chat room.");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void abmelden(Client client) {
|
||||
clients.remove(client);
|
||||
nachrichtVerteilen(client, "\"" + client.username + "\" hat den Raum verlassen.\n\r");
|
||||
System.out.println(client.username + " left the chat room.");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user