Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ghvfgfcht committed Dec 19, 2023
2 parents 7405f27 + e64a2e2 commit b0a7765
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 97 deletions.
43 changes: 21 additions & 22 deletions src/Controller/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,33 @@ public class Client implements Runnable{
private Object input = null;
private GameController gameController;


public Client(GameController gameController){
this.gameController = gameController;
}

// 서버에 객체 전달
public synchronized void returnObj (SerializeObject obj){
this.obj = obj;
public void setObject(SerializeObject object){
obj = object;
}

public void connect(String address, int port, String name) throws UnknownHostException, NotEmptySpaceException, OnProgressException{
public Client(GameController gameController, String address, int port, String name) throws UnknownHostException, NotEmptySpaceException, OnProgressException{
this.gameController = gameController;
try {
System.out.println("연결을 시도합니다.");
curSocket = new Socket(address, port);
System.out.println(curSocket);

this.objectOutputStream = new ObjectOutputStream(curSocket.getOutputStream());
this.objectInputStream = new ObjectInputStream(curSocket.getInputStream());

SerializeObject answer = (SerializeObject) objectInputStream.readObject();
System.out.println("받은 객체 : " + answer + " " + answer.getObjectType());
String answerStr = (String)answer.getEventObject();
returnObj(new SerializeObject("received", "String"));
if(answerStr.equals("방이 가득찼습니다.")) {
throw new NotEmptySpaceException();
}
else if(answerStr.equals("현재 게임이 진행중입니다.")){
throw new OnProgressException();
}

setObject(new SerializeObject("received", "String"));
Thread clientThread = new Thread(this);
clientThread.start();
System.out.println("서버에 연결되었습니다.");
setObject(new SerializeObject(name, "Name"));

objectOutputStream.writeObject(new SerializeObject(name, "String"));
}catch (IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
Expand All @@ -57,14 +53,17 @@ else if(answerStr.equals("현재 게임이 진행중입니다.")){
@Override
public void run() {
try {
while (obj != null || (input = objectInputStream.readObject()) != null) {
// 입력
if(obj != null) {
objectOutputStream.writeObject(obj);
obj = null;
}
else {
gameController.excuteQuery((SerializeObject) input, -1);
System.out.println("클라이언트 소켓 통신 중...");
while (true) {
if(obj != null || (input = objectInputStream.readObject()) != null) {
// 입력
if (obj != null) {
objectOutputStream.writeObject(obj);
System.out.println(obj.getObjectType() + " " + obj.getEventObject() + " 전달");
obj = null;
} else {
gameController.excuteQuery((SerializeObject) input, -1);
}
}
}
} catch (IOException | ClassNotFoundException e) {
Expand Down
24 changes: 14 additions & 10 deletions src/Controller/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class ClientHandler implements Runnable {


// 클라이언트에 객체 전달
public void returnObj (SerializeObject obj){
public void setObject(SerializeObject obj){
this.obj = obj;
}

public void update(SerializeObject object){
// 현재 오브젝트 파싱 및 처리
server.notifiObservers(this, object);
server.excute(object, index);
}


Expand All @@ -40,14 +40,18 @@ public ClientHandler(Socket socket, Server server, int index) {
@Override
public void run() {
try {
while (obj != null || (input = objectInputStream.readObject()) != null) {
// 입력
if(obj != null) {
objectOutputStream.writeObject(obj);
obj = null;
}
else {
update((SerializeObject) input);
while (true) {
if(obj != null || (input = objectInputStream.readObject()) != null) {
// 입력
if (obj != null) {
System.out.println("출력 추가됨 " + obj.getEventObject() + " ");
objectOutputStream.writeObject(obj);
obj = null;
} else {
SerializeObject sinput = (SerializeObject) input;
System.out.println("입력 추가됨 " + sinput.getEventObject());
update((SerializeObject) input);
}
}
}
} catch (IOException | ClassNotFoundException e) {
Expand Down
40 changes: 32 additions & 8 deletions src/Controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ public enum GameState{

private Player[] players = new Player[MAX_PLAYER_COUNT];

public String playersStr() {
String tmp = "";
for(int i = 0 ; i < MAX_PLAYER_COUNT ; i ++){
if(players[i] == null){
tmp += "null\n";
}
else {
tmp += players[i].toString() + "\n";
tmp += view.getRoomNameLabel()[i].getText() + "\n";
}
}
return tmp;
}

public GameController(GameView view) {
this.view = view;
view.setGameController(this);
Expand Down Expand Up @@ -68,6 +82,9 @@ protected void setPlayers(Player[] players) {

// 방 만드는 함수 - 서버
public void makeRoom() {
players[0] = new Player(view.getNameTF().getText());
players[0].setReadyState(Player.ReadyState.READY);
view.updatePlayers(players);
server = new Server(PORT, this);
isServer = true;
// 뷰에서 화면 전환함수
Expand All @@ -76,22 +93,19 @@ public void makeRoom() {


public void connectRoom(String address) {
client = new Client(this);
view.getLoginErrorLabel().setText("연결 중 입니다...");
isServer = false;
String name = view.getNameTF().getText();
view.getLoginErrorLabel().setText("연결 중 입니다...");
try {
// TODO
client.connect(address, PORT, name);

view.changePanel("RoomPanel");
client = new Client(this, address, PORT, name);
} catch (UnknownHostException e) {
view.getLoginErrorLabel().setText("IP주소로 접속할 수 없습니다. IP주소를 다시 확인해주세요.");
} catch (NotEmptySpaceException e){
view.getLoginErrorLabel().setText("현재 방이 가득 차 참가할 수 없습니다.");
} catch (OnProgressException e){
view.getLoginErrorLabel().setText("현재 게임이 진행 중인 방입니다.");
}
view.changePanel("RoomPanel");
}


Expand All @@ -108,6 +122,11 @@ else if(readyState == Player.ReadyState.NOT_READY && players[index].getReadyStat
view.updateRoomReadyPanel(players[index].getReadyState(), index);
}

// 서버로 플레이어 추가시 서버 뷰 추가
public void updatePlayers(){
view.updatePlayers(players);
}

// 플레이어 정보 변경 함수
public void changePlayers(Player[] getPlayers){
// Model 플레이어 변경
Expand All @@ -120,29 +139,34 @@ public void changePlayers(Player[] getPlayers){

// 클라이언트로 넘어온 쿼리 실행 함수
public void excuteQuery(SerializeObject object, int index){
System.out.println("받은 객체 : " + object.getEventObject());
switch (object.getObjectType()){
case "String" :
String content = (String)object.getEventObject();

break;

case "Player[]":
Player[] getPlayers = (Player[]) object.getEventObject();
changePlayers(getPlayers);
break;

case "Name":
if(isServer) {
System.out.println(object.getEventObject() + " 플레이어 추가");
String name = (String) object.getEventObject();
players[index].setName(name);
server.notifiObservers(players[index].getClientHandler(), new SerializeObject(players, "Player[]"));
view.updateNameLabel(name, index);
System.out.println(playersStr());
server.notifiObservers(new SerializeObject(players, "Player[]"));
}
break;

case "ReadyState" :
if(isServer) {
Player.ReadyState readyState = (Player.ReadyState) object.getEventObject();
changeReadyState(index, readyState);
server.notifiObservers(players[index].getClientHandler(), new SerializeObject(players, "Player[]"));
server.notifiObservers(new SerializeObject(players, "Player[]"));
break;
}
default:
Expand Down
98 changes: 53 additions & 45 deletions src/Controller/Server.java
Original file line number Diff line number Diff line change
@@ -1,83 +1,91 @@
package Controller;

import Model.Game;
import Model.Player;

import java.io.*;
import java.net.*;

public class Server {


static int clientCnt = 0;
static int clientCnt = 1;
static int port;
static Player[] players;
static ClientHandler[] handlers = new ClientHandler[GameController.MAX_PLAYER_COUNT];
static private GameController gameController;


public Server(int port, GameController gameController) {
this.port = port;
this.gameController = gameController;
players = gameController.getPlayers();
openServer();
}


public void notifiObservers(ClientHandler clientHandler, SerializeObject object){
for(int i = 0 ; i < GameController.MAX_PLAYER_COUNT ; i ++){
if(players[i] == null || players[i].getClientHandler() == clientHandler) continue;
ClientHandler curHandler = players[i].getClientHandler();
curHandler.returnObj(object);
public void notifiObservers(SerializeObject object) {
for (int i = 1; i < GameController.MAX_PLAYER_COUNT; i++) {
if (players[i] == null) continue;
System.out.println("notify => " + object.getEventObject());
handlers[i].setObject(object);
}
}

private void addPlayer(Socket clientSocket){
public void excute(SerializeObject object, int index){
gameController.excuteQuery(object, index);
}

private void addPlayer(Socket clientSocket) throws SocketException {
clientCnt++;
int idx = clientCnt - 1;
ClientHandler curHandler = new ClientHandler(clientSocket, this, idx);
Thread clientThread = new Thread(curHandler);
curHandler.setObject(new SerializeObject("connected", "String"));
clientThread.start();
curHandler.returnObj(new SerializeObject("connected", "String"));
players[idx] = new Player("", curHandler);
handlers[idx] = curHandler;
players[idx] = new Player("");
System.out.println("플레이어 추가됨");
gameController.updatePlayers();
}

private void openServer() {

Thread serverWaitThread = new Thread(new Runnable() {
@Override
public void run() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port);
System.out.println("server 소켓 생성, IP : " + serverSocket.getLocalSocketAddress());
System.out.println(InetAddress.getLocalHost().getHostAddress());

InetAddress serverLocalIpAddress = serverSocket.getInetAddress();
System.out.println("Serversocket Local IP Address : " + serverLocalIpAddress.getHostAddress());
while(true) {
System.out.println("기다리는 중");
Socket clientSocket = serverSocket.accept();
System.out.println("클라이언트 연결됨: " + clientSocket.getInetAddress().getHostAddress());

if (clientCnt < 4 || GameController.gameState == GameController.GameState.FINISHED) {
// 클라이언트와 통신하는 스레드 생성
addPlayer(clientSocket);
Thread serverWaitThread = new Thread(new Runnable() {
@Override
public void run() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(port);
System.out.println("server 소켓 생성, IP : " + serverSocket.getLocalSocketAddress());
System.out.println(InetAddress.getLocalHost().getHostAddress());

InetAddress serverLocalIpAddress = serverSocket.getInetAddress();
System.out.println("Serversocket Local IP Address : " + serverLocalIpAddress.getHostAddress());
while (true) {
System.out.println("기다리는 중");
Socket clientSocket = serverSocket.accept();
System.out.println("클라이언트 연결됨: " + clientSocket.getInetAddress().getHostAddress());

if (clientCnt < 4 || GameController.gameState == GameController.GameState.FINISHED) {
// 클라이언트와 통신하는 스레드 생성
addPlayer(clientSocket);
} else {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
if (clientCnt >= 4) {
out.println("방이 가득찼습니다.");
} else {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
if(clientCnt >= 4) {
out.println("방이 가득찼습니다.");
}
else{
out.println("현재 게임이 진행중입니다.");
}
clientSocket.close();
out.close();
out.println("현재 게임이 진행중입니다.");
}
clientSocket.close();
out.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
});
serverWaitThread.start();
}
}
});
serverWaitThread.start();
}


}
3 changes: 2 additions & 1 deletion src/Model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import Controller.GameController;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

public class Board {
public class Board implements Serializable {
final int BOARD_WIDTH = GameController.BOARD_WIDTH; // 보드판 가로 길이
final int BOARD_HEIGHT = GameController.BOARD_HEIGHT; // 보드판 세로 길이

Expand Down
Loading

0 comments on commit b0a7765

Please sign in to comment.