Implemented starting everything from NightTrainToBudapest class.
This commit is contained in:
parent
5a36988cd2
commit
004f70ba44
14
README.md
14
README.md
@ -18,9 +18,19 @@ Votes are held in the night for the ghosts to decide who will be next and during
|
||||
* ...
|
||||
|
||||
## Console Commands
|
||||
* /c "message" - sends a chat message to all connected players
|
||||
* /n "name" - changes player name
|
||||
* /c _message_ - sends a chat message to all players in the same lobby. If you are not in a lobby, the message will be sent to all players who are also not in a lobby.
|
||||
* /b _message_ - broadcasts a message to all connected clients, regardless of lobbies.
|
||||
* /w _username$message_ - sends a message to the specified user only.
|
||||
* /g - create (&join) a new lobby.
|
||||
* /j _1_ - join lobby 1. To join lobby 2, use /j _2_, etc.
|
||||
* /l - list all connected clients and all lobbies
|
||||
* /p - list all players in your lobby.
|
||||
* /e - exit your lobby
|
||||
* /n _name_ - changes player name. If unavailable, it adds a fun and quirky suffix
|
||||
* /q - quit
|
||||
* /s - start game in your current lobby.
|
||||
* /v 1 - vote for person 1 (same for other numbers)
|
||||
|
||||
|
||||
## Installation
|
||||
...
|
||||
|
||||
@ -11,8 +11,8 @@ javafx {
|
||||
}
|
||||
|
||||
group 'ch.unibas.dmi.dbis'
|
||||
version '0.0.1-ALPHA'
|
||||
mainClassName = 'ch.unibas.dmi.dbis.cs108.multiplayer.client.Client'
|
||||
version '0.0.2'
|
||||
mainClassName = 'ch.unibas.dmi.dbis.cs108.NightTrainToBudapest'
|
||||
|
||||
java {
|
||||
toolchain {
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
rootProject.name = 'Night Train to Budapest'
|
||||
rootProject.name = 'NightTrainToBudapest'
|
||||
|
||||
|
||||
@ -0,0 +1,39 @@
|
||||
package ch.unibas.dmi.dbis.cs108;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.client.Client;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.Server;
|
||||
import java.net.InetAddress;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.core.tools.picocli.CommandLine.Help.IParameterRenderer;
|
||||
|
||||
public class NightTrainToBudapest {
|
||||
|
||||
public static void main(String[] args){
|
||||
try{
|
||||
String clientOrServer = args[0];
|
||||
if (clientOrServer.equalsIgnoreCase("client")) {
|
||||
String addrString = args[1].substring(0,args[1].indexOf(":"));
|
||||
InetAddress addr = InetAddress.getByName(addrString);
|
||||
int port = Integer.parseInt(args[1].substring(args[1].indexOf(":") + 1));
|
||||
String username = null;
|
||||
if (args.length > 2) { //if the client provided a username
|
||||
//StringBuilder usernamebuilder = new StringBuilder(); todo: support username with spaces.
|
||||
username = args[2];
|
||||
}
|
||||
Client.main(addr, port, username);
|
||||
} else if (clientOrServer.equalsIgnoreCase("server")) {
|
||||
int port = Integer.parseInt(args[1]);
|
||||
Server.main(port);
|
||||
} else {
|
||||
System.out.println("invalid arguments!");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println("Please give more arguments.");
|
||||
System.out.println("Syntax:");
|
||||
System.out.println("client <hostadress>:<port> [<username>] | server <port>");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.ClientPinger;
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.helpers.Protocol;
|
||||
|
||||
import ch.unibas.dmi.dbis.cs108.multiplayer.server.JServerProtocolParser;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.io.*;
|
||||
import java.net.UnknownHostException;
|
||||
@ -31,7 +32,7 @@ public class Client {
|
||||
int position = Integer.MAX_VALUE;
|
||||
|
||||
|
||||
public Client(Socket socket) {
|
||||
public Client(Socket socket, String username) {
|
||||
try {
|
||||
this.socket = socket;
|
||||
this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
@ -39,12 +40,16 @@ public class Client {
|
||||
|
||||
//sending the initial name to server.
|
||||
String systemName;
|
||||
try {
|
||||
systemName = System.getProperty("user.name");
|
||||
} catch (Exception e) {
|
||||
systemName = "U.N. Owen";
|
||||
if (username == null) {
|
||||
try {
|
||||
systemName = System.getProperty("user.name");
|
||||
} catch (Exception e) {
|
||||
systemName = "U.N. Owen";
|
||||
}
|
||||
if (systemName == null) systemName = "U.N. Owen";
|
||||
} else {
|
||||
systemName = username;
|
||||
}
|
||||
if (systemName == null) systemName = "U.N. Owen";
|
||||
sendMsgToServer(Protocol.clientLogin + "$" + systemName);
|
||||
|
||||
clientPinger = new ClientPinger(this, this.socket);
|
||||
@ -183,7 +188,7 @@ public class Client {
|
||||
public static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
String hostname;
|
||||
int port = 42069; //can be set via argument later if needed.
|
||||
int port = 1873;
|
||||
if (args.length < 1) {
|
||||
System.out.println("Enter the host's IP address (or type l for localhost)");
|
||||
hostname = sc.next();
|
||||
@ -196,7 +201,7 @@ public class Client {
|
||||
Socket socket;
|
||||
try {
|
||||
socket = new Socket(hostname, 42069);
|
||||
Client client = new Client(socket);
|
||||
Client client = new Client(socket, null);
|
||||
client.chatListener();
|
||||
Thread cP = new Thread(client.clientPinger);
|
||||
cP.start();
|
||||
@ -209,6 +214,22 @@ public class Client {
|
||||
|
||||
}
|
||||
|
||||
public static void main(InetAddress address, int port, String username) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
Socket socket;
|
||||
try {
|
||||
socket = new Socket(address, port);
|
||||
Client client = new Client(socket, username);
|
||||
client.chatListener();
|
||||
Thread cP = new Thread(client.clientPinger);
|
||||
cP.start();
|
||||
client.userInputListener(); //this one blocks.
|
||||
} catch (UnknownHostException e) {
|
||||
System.out.println("Invalid host IP");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
||||
|
||||
public class Client_01 {
|
||||
public static void main(String[] args) {
|
||||
Client.main(args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
package ch.unibas.dmi.dbis.cs108.multiplayer.client;
|
||||
|
||||
public class Client_02 {
|
||||
public static void main(String[] args) {
|
||||
Client.main(args);
|
||||
}
|
||||
}
|
||||
@ -86,7 +86,7 @@ public class Protocol {
|
||||
public static final String clientQuitRequest = "QUITR";
|
||||
|
||||
/**
|
||||
* Client sends this message when they want to create a new game.
|
||||
* Client sends this message when they want to create a new lobby (& automatically join it).
|
||||
* Client issues this command in {@link ch.unibas.dmi.dbis.cs108.multiplayer.client.MessageFormatter}
|
||||
* using "/g".
|
||||
* First a lobby {@link Lobby} is created of which the requesting client is the admin of.
|
||||
@ -94,7 +94,7 @@ public class Protocol {
|
||||
public static final String createNewLobby = "CRTLB";
|
||||
|
||||
/**
|
||||
* Represents a clients' request for a list of lobbies
|
||||
* Represents a clients' request for a list of lobbies, plus what players are in them.
|
||||
*/
|
||||
public static final String listLobbies = "LISTL";
|
||||
|
||||
|
||||
@ -115,6 +115,7 @@ public class Lobby {
|
||||
*/
|
||||
public synchronized boolean addPlayer(ClientHandler client) {
|
||||
if (lobbyClients.size() < MAX_NO_OF_CLIENTS) {
|
||||
//todo: check that game hasn't started yet
|
||||
if (clientIsInLobby(client) == -1) {
|
||||
lobbyClients.add(client);
|
||||
ClientHandler.broadcastAnnouncementToAll(client.getClientUserName() + " has joined lobby " + this.getLobbyID());
|
||||
|
||||
@ -14,7 +14,7 @@ public class Server {
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
public static final BudaLogConfig l = new BudaLogConfig(LOGGER);
|
||||
|
||||
private static final int gamePort = 42069;
|
||||
private static int gamePort;
|
||||
private HashSet<ClientHandler> connectedClients = new HashSet<>();
|
||||
private ServerSocket serverSocket;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
@ -28,7 +28,7 @@ public class Server {
|
||||
*/
|
||||
public void startServer() {
|
||||
try {
|
||||
System.out.println("Port 42069 is open.");
|
||||
System.out.println("Port " + gamePort + " is open.");
|
||||
while (!serverSocket.isClosed()) {
|
||||
Socket socket = serverSocket.accept();
|
||||
ClientHandler nextClient = new ClientHandler(socket, socket.getInetAddress());
|
||||
@ -53,6 +53,19 @@ public class Server {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ServerSocket serverSocket = null;
|
||||
gamePort = 1873;
|
||||
try {
|
||||
serverSocket = new ServerSocket(gamePort);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Server server = new Server(serverSocket);
|
||||
server.startServer();
|
||||
}
|
||||
|
||||
public static void main(int port) {
|
||||
gamePort = port;
|
||||
ServerSocket serverSocket = null;
|
||||
try {
|
||||
serverSocket = new ServerSocket(gamePort);
|
||||
|
||||
Reference in New Issue
Block a user