MiniGame System Test (TCP only)

Nach jeder Runde (jeder spieler hat einmal gewürfelt) wird ein zufälliges minigame geladen. Ist das minigame beendet (stand jz nach 1 sekunde fix) wird die Map in Ihrem vorherigen zustand neu geladen. Die Kommunikation zwischen Client und Server im Minigame erfolgt unabhängig der Kommunikation in der Map
This commit is contained in:
brausjonas
2023-04-15 03:52:15 +02:00
parent 7c0d3ac7ae
commit 412490ea85
30 changed files with 1055 additions and 229 deletions
+6
View File
@@ -26,6 +26,12 @@ public class Client
this.characterID = characterID;
}
public Client(DataOutputStream output, DataInputStream input, Socket socket) {
this.output = output;
this.input = input;
this.socket = socket;
}
public void close()
{
try
+102
View File
@@ -0,0 +1,102 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class Minigame
{
protected Room room;
private List<Thread> threads = new ArrayList<>();
private Client[] players;
public Minigame(Room room)
{
this.room = room;
players = new Client[room.players.length];
}
public void addClient(Client client, int index)
{
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
players[index] = client;
DataInputStream input = client.input;
DataOutputStream output = client.output;
byte[] readData = new byte[10];
//TEST
try
{
output.write(new byte[]{1, 0, 0, 0, 0, 0, 0, 0, 0, 0});
} catch (IOException e)
{
}
while (true)
{
try
{
input.read(readData);
switch (readData[0])
{
//TEST
case 1:
System.out.println("testtttttttttttttt");
Timer t = new Timer();
TimerTask tt = new TimerTask()
{
@Override
public void run()
{
room.closeMinigame();
}
};
t.schedule(tt, 1000);
break;
//TEST END
}
} catch (Exception e)
{
}
}
}
});
threads.add(t);
t.start();
}
public void end()
{
for (int i = 0; i < threads.size(); i++)
{
try
{
threads.get(i).interrupt();
} catch (Exception e)
{
e.printStackTrace();
}
}
for(int i = 0; i < players.length; i++)
{
players[i].close();
}
threads.clear();
}
}
+324 -181
View File
@@ -11,7 +11,8 @@ import java.util.TimerTask;
/**
* this class handels all operations / syncs performed in a single room
*/
public class Room {
public class Room
{
//tells how many clients can connect to a room
public Client[] players = new Client[2];
//the array of players start positions
@@ -29,9 +30,12 @@ public class Room {
//tells players turn (array index)
private int currentPlayer = 0;
private int playersPlayedCount = 0;
private Minigame currentMiniGame;
private boolean inMiniGame = false;
public Room(String roomCode, Server server) {
public Room(String roomCode, Server server)
{
this.roomCode = roomCode;
startPositions = new Vector3[4];
@@ -43,6 +47,69 @@ public class Room {
startPositions[3] = new Vector3(92, 0, 128);
this.server = server;
}
public void addPlayerToCurrentMiniGame(int index, Client tempClient)
{
currentMiniGame.addClient(tempClient, index);
}
private void activateMinigame()
{
inMiniGame = true;
int randomGame = (int) (Math.random() * 1);
currentMiniGame = new Minigame(this);
try
{
for (int i = 0; i < players.length; i++)
{
players[i].output.write(new byte[]{100, (byte) i, (byte) randomGame, 0, 0, 0, 0, 0, 0, 0});
}
} catch (Exception e)
{
}
}
public void closeMinigame()
{
if(currentMiniGame != null)
{
System.out.println("close minigame");
currentMiniGame.end();
currentMiniGame = null;
inMiniGame = false;
try
{
for (int i = 0; i < players.length; i++)
{
players[i].output.write(new byte[]{101, 0, 0, 0, 0, 0, 0, 0, 0, 0});
}
} catch (Exception e)
{
}
}
//sync the activation of the current player to ALL clients
for (int i = 0; i < players.length; i++)
{
//!!data processing!!
//----------------------------action--------the next player-----------free space------
try
{
players[i].output.write(new byte[]{2, (byte) currentPlayer, 0, 0, 0, 0, 0, 0, 0, 0});
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
/**
@@ -50,12 +117,15 @@ public class Room {
*
* @param client
*/
public void addClient(Client client) {
public void addClient(Client client)
{
//loops through all players existing and sends the needed information
for (int i = 0; i < players.length; i++) {
for (int i = 0; i < players.length; i++)
{
//places the new joined player in the next free slot
if (players[i] == null) {
if (players[i] == null)
{
players[i] = client;
playerCount++;
@@ -63,37 +133,48 @@ public class Room {
System.out.println(client.name.replace(" ", "") + " joined room " + roomCode + " successfully as player " + i);
//creates a thread for the connection with the joined player
Thread t = new Thread(new Runnable() {
Thread t = new Thread(new Runnable()
{
@Override
public void run() {
public void run()
{
//assignes a player id
int player = playerCount - 1;
DataInputStream input = null;
try {
try
{
//tries to get the input stream (TCP)
input = new DataInputStream(players[player].socket.getInputStream());
} catch (IOException e) {
} catch (IOException e)
{
server.deleteRoom(roomCode);
}
for (int i = 0; i < players.length; i++) {
if (players[i] != null) {
try {
for (int i = 0; i < players.length; i++)
{
if (players[i] != null)
{
try
{
players[i].output.write(new byte[]{11, (byte) playerCount, 0, 0, 0, 0, 0, 0, 0, 0});
} catch (IOException e) {
} catch (IOException e)
{
}
}
}
while (true) {
try {
//this is a buffer for all data being received by the player
byte[] readData = new byte[10];
while (true)
{
try
{
//read the data to the buffer
input.read(readData);
//this is a buffer for all data being received by the player
byte[] readData = new byte[10];
//read the data to the buffer
input.read(readData);
/*
@@ -105,152 +186,184 @@ public class Room {
----------------------------------------
----------------------------------------
*/
//Checks which action (array[0]) is received
switch (readData[0]) {
//Checks which action (array[0]) is received
switch (readData[0])
{
/**
* //spieler hat gewürfelt
*/
case 3:
for (int i = 0; i < players.length; i++) {
//sync to all OTHER players
if (i != player) {
/**
* //spieler hat gewürfelt
*/
case 3:
for (int i = 0; i < players.length; i++)
{
//sync to all OTHER players
if (i != player)
{
//!!data processing!!
//----------------------------action---player-------rolled number----free space--------
players[i].output.write(new byte[]{3, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
}
}
break;
/**
* //pfeil wurde ausgewählt
*/
case 4:
for (int i = 0; i < players.length; i++)
{
//!!data processing!!
//----------------------------action---player-------rolled number----free space--------
players[i].output.write(new byte[]{3, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
//sync to all OTHER players
if (i != player)
{
//-------------------------------action----player-----arrow index--------free space-----
players[i].output.write(new byte[]{4, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
}
}
}
break;
/**
* //pfeil wurde ausgewählt
*/
case 4:
for (int i = 0; i < players.length; i++) {
//!!data processing!!
//sync to all OTHER players
if (i != player) {
//-------------------------------action----player-----arrow index--------free space-----
players[i].output.write(new byte[]{4, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
}
}
break;
break;
/**
* //Spieler ist fertig mit seinem Zug
*/
case 5:
//calculate the next player
currentPlayer++;
if (currentPlayer >= players.length) {
currentPlayer = 0;
}
//sync the activation of the current player to ALL clients
for (int i = 0; i < players.length; i++) {
//!!data processing!!
//----------------------------action--------the next player-----------free space------
players[i].output.write(new byte[]{2, (byte) currentPlayer, 0, 0, 0, 0, 0, 0, 0, 0});
}
break;
/**
* //Es wurde auf ein CoinField getreten (Rot oder Blau)
*/
case 6:
for (int i = 0; i < players.length; i++) {
//sync to OTHER players
if (i != player) {
//!!data processing!!
//-----------------------------action--player-------losse or get------------free space--
players[i].output.write(new byte[]{6, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
/**
* //Spieler ist fertig mit seinem Zug
*/
case 5:
//calculate the next player
currentPlayer++;
if (currentPlayer >= players.length)
{
currentPlayer = 0;
}
}
break;
/**
* //Eventstop (ItemShop, BuyStar, ...) ist fertig
*/
case 7:
//sync to ALL players
for (int i = 0; i < players.length; i++) {
//!!simple sync!!
//------------------------------action---player---------free space--------
players[i].output.write(new byte[]{7, (byte) player, 0, 0, 0, 0, 0, 0, 0, 0});
}
break;
/**
* //Spieler hat einen Stern gekauft
*/
case 8:
for (int i = 0; i < players.length; i++) {
//!!simple sync!!
//sync to OTHER players
if (i != player) {
//-----------------------------action----player----------free space------
players[i].output.write(new byte[]{8, (byte) player, 0, 0, 0, 0, 0, 0, 0, 0});
if (currentPlayer != 0)
{
//sync the activation of the current player to ALL clients
for (int i = 0; i < players.length; i++)
{
//!!data processing!!
//----------------------------action--------the next player-----------free space------
players[i].output.write(new byte[]{2, (byte) currentPlayer, 0, 0, 0, 0, 0, 0, 0, 0});
}
} else
{
activateMinigame();
}
}
break;
break;
/**
* //Spieler hat ein Item Gekauft
*/
case 9:
for (int i = 0; i < players.length; i++) {
//Sync to OTHER players
if (i != player) {
//!!data processing!!
//------------------------------action---player-------item ID--------free space--------
players[i].output.write(new byte[]{9, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
/**
* //Es wurde auf ein CoinField getreten (Rot oder Blau)
*/
case 6:
for (int i = 0; i < players.length; i++)
{
//sync to OTHER players
if (i != player)
{
//!!data processing!!
//-----------------------------action--player-------losse or get------------free space--
players[i].output.write(new byte[]{6, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
}
}
}
break;
break;
case 10:
for (int i = 0; i < players.length; i++) {
//Sync to OTHER players
if (i != player) {
//!!data processing!!
//------------------------------action---player-------item index--------free space--------
players[i].output.write(new byte[]{10, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
/**
* //Eventstop (ItemShop, BuyStar, ...) ist fertig
*/
case 7:
//sync to ALL players
for (int i = 0; i < players.length; i++)
{
//!!simple sync!!
//------------------------------action---player---------free space--------
players[i].output.write(new byte[]{7, (byte) player, 0, 0, 0, 0, 0, 0, 0, 0});
}
}
break;
case 12:
for (int i = 0; i < players.length; i++) {
//Sync to OTHER players
if (i != player) {
//!!data processing!!
//------------------------------action---player-------item index--------free space--------
players[i].output.write(new byte[]{12, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
break;
/**
* //Spieler hat einen Stern gekauft
*/
case 8:
for (int i = 0; i < players.length; i++)
{
//!!simple sync!!
//sync to OTHER players
if (i != player)
{
//-----------------------------action----player----------free space------
players[i].output.write(new byte[]{8, (byte) player, 0, 0, 0, 0, 0, 0, 0, 0});
}
}
}
break;
//Coins Sync
case 13:
for (int i = 0; i < players.length; i++) {
//Sync to OTHER players
if (i != player) {
players[i].output.write(new byte[]{13, readData[1], readData[2], (byte) player, 0, 0, 0, 0, 0, 0});
}
}
break;
break;
//Start Rotator AND stop Rotator
case 14: case 15:
for (int i = 0; i < players.length; i++) {
//Sync to OTHER players
if (i != player) {
players[i].output.write(readData);
/**
* //Spieler hat ein Item Gekauft
*/
case 9:
for (int i = 0; i < players.length; i++)
{
//Sync to OTHER players
if (i != player)
{
//!!data processing!!
//------------------------------action---player-------item ID--------free space--------
players[i].output.write(new byte[]{9, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
}
}
}
break;
case 126:
server.deleteRoom(roomCode);
break;
}
break;
} catch (IOException e) {
case 10:
for (int i = 0; i < players.length; i++)
{
//Sync to OTHER players
if (i != player)
{
//!!data processing!!
//------------------------------action---player-------item index--------free space--------
players[i].output.write(new byte[]{10, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
}
}
break;
case 12:
for (int i = 0; i < players.length; i++)
{
//Sync to OTHER players
if (i != player)
{
//!!data processing!!
//------------------------------action---player-------item index--------free space--------
players[i].output.write(new byte[]{12, (byte) player, readData[1], 0, 0, 0, 0, 0, 0, 0});
}
}
break;
//Coins Sync
case 13:
for (int i = 0; i < players.length; i++)
{
//Sync to OTHER players
if (i != player)
{
players[i].output.write(new byte[]{13, readData[1], readData[2], (byte) player, 0, 0, 0, 0, 0, 0});
}
}
break;
//Start Rotator AND stop Rotator
case 14:
case 15:
for (int i = 0; i < players.length; i++)
{
//Sync to OTHER players
if (i != player)
{
players[i].output.write(readData);
}
}
break;
case 126:
server.deleteRoom(roomCode);
break;
}
} catch (IOException e)
{
//if something is crashing, the room is being deleted
server.deleteRoom(roomCode);
}
@@ -268,9 +381,12 @@ public class Room {
}
//Make clients add all players to the world
if (playerCount == players.length) {
for (int j = 0; j < players.length; j++) {
for (int k = 0; k < players.length; k++) {
if (playerCount == players.length)
{
for (int j = 0; j < players.length; j++)
{
for (int k = 0; k < players.length; k++)
{
//0 1 create new player
//1 0 no playable player
//1 1 playable character
@@ -278,8 +394,10 @@ public class Room {
//5 player index
//if the player should be playable
if (j == k) {
try {
if (j == k)
{
try
{
//sends the inital message to the player
players[j].output.write(new byte[]{1, 1, (byte) startPositions[k].x, (byte) startPositions[k].y, (byte) startPositions[k].z, (byte) k, 0, 0, 0, (byte) players[k].characterID});
@@ -289,31 +407,37 @@ public class Room {
send[0] = 5;
send[1] = (byte) k;
for (int i = 2; i < send.length; i++) {
for (int i = 2; i < send.length; i++)
{
send[i] = name[i - 2];
}
players[j].output.write(send);
//stop sending the name................................................................................
} catch (IOException e) {
} catch (IOException e)
{
server.deleteRoom(roomCode);
}
}
//if the player should be a bot
else {
try {
else
{
try
{
players[j].output.write(new byte[]{1, 0, (byte) startPositions[k].x, (byte) startPositions[k].y, (byte) startPositions[k].z, (byte) k, 0, 0, 0, (byte) players[k].characterID});
byte[] name = players[k].name.substring(0, 8).getBytes(StandardCharsets.US_ASCII);
byte[] send = new byte[10];
send[0] = 5;
send[1] = (byte) k;
for (int i = 2; i < send.length; i++) {
for (int i = 2; i < send.length; i++)
{
send[i] = name[i - 2];
}
players[j].output.write(send);
} catch (IOException e) {
} catch (IOException e)
{
server.deleteRoom(roomCode);
}
}
@@ -324,11 +448,14 @@ public class Room {
//1 x, x = idx
//send the inital activation message
try {
for (Client c : players) {
try
{
for (Client c : players)
{
c.output.write(new byte[]{2, 0, 0, 0, 0, 0, 0, 0, 0, 0});
}
} catch (IOException e) {
} catch (IOException e)
{
server.deleteRoom(roomCode);
}
}
@@ -337,53 +464,69 @@ public class Room {
/**
* Closes the room
*/
public void closeRoom() {
public void closeRoom()
{
//try to cick all the players (still connected) of the room
try {
try
{
if (players[0] != null) {
if (players[0] != null)
{
//127 is the action for room cick
players[0].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
players[0].close();
}
} catch (Exception ex) {
} catch (Exception ex)
{
}
try {
if (players[1] != null) {
try
{
if (players[1] != null)
{
players[1].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
players[1].close();
}
} catch (Exception ex) {
} catch (Exception ex)
{
}
try {
if (players[2] != null) {
try
{
if (players[2] != null)
{
players[2].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
players[2].close();
}
} catch (Exception ex) {
} catch (Exception ex)
{
}
try {
if (players[3] != null) {
try
{
if (players[3] != null)
{
players[3].output.write(new byte[]{127, 0, 0, 0, 0, 0, 0, 0, 0, 0});
players[3].close();
}
} catch (Exception ex) {
} catch (Exception ex)
{
}
//try to close all open threads (not crashed yet)
for (int i = 0; i < threads.size(); i++) {
try {
for (int i = 0; i < threads.size(); i++)
{
try
{
threads.get(i).interrupt();
System.out.println("thread " + threads.get(i).getName() + " stopped!");
} catch (Exception e) {
} catch (Exception e)
{
}
}
+13
View File
@@ -189,6 +189,19 @@ public class Server
client = null;
break;
}
break;
//Player wats to join a minigame
case 5:
int playerIDInRoom = readData[1];
input.read(readData);
String roomCodeOriginal = new String(readData, StandardCharsets.US_ASCII);
Client miniGameConnectorClient = new Client(output, input, client);
rooms.get(roomCodeOriginal).addPlayerToCurrentMiniGame(playerIDInRoom, miniGameConnectorClient);
break;
}
System.out.println("Init Thread finished");