Skip to content

Commit

Permalink
Fix: change class type of startTime and duration, add code comments, …
Browse files Browse the repository at this point in the history
…fix RoomManager getRoom Logic
  • Loading branch information
Jack-Chagarr committed Oct 17, 2023
1 parent f82c5fb commit 1998631
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 237 deletions.
28 changes: 12 additions & 16 deletions android/RunUsAndroid/app/src/main/java/MultiMode/MultiModeRoom.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package MultiMode;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -9,21 +11,18 @@ public class MultiModeRoom implements Serializable {

private static final long serialVersionUID = 1L;
private int id; // 룸 ID
private List<MultiModeUser> userList;
private List<MultiModeUser> userList; //유저 정보
private MultiModeUser roomOwner; // 방장

private RoomCreateInfo roomCreateInfo; //방 정보

private String title;
private double distance;
private String startTime;
private int numRunners;
private String title; //방 제목
private double distance; //목표 거리
private int numRunners; //제한 인원

private LocalDateTime startTime; //시작 시각


private int runningTimeHour;

private int runningTimeMinute;
private LocalTime duration; //목표 시간(달리는 시간)

public MultiModeRoom(int roomId, RoomCreateInfo roomCreateInfo) { // 유저가 방을 만들때
userList = new ArrayList();
Expand All @@ -32,6 +31,7 @@ public MultiModeRoom(int roomId, RoomCreateInfo roomCreateInfo) { // 유저가
this.title = roomCreateInfo.getTitle();
this.distance = roomCreateInfo.getDistance();
this.startTime = roomCreateInfo.getStartTime();
this.duration = roomCreateInfo.getDuration();
this.numRunners = roomCreateInfo.getNumRunners();
}
public MultiModeRoom(MultiModeUser user ) { // 유저가 방을 만들때
Expand Down Expand Up @@ -95,7 +95,7 @@ public String getTitle() { // 방 이름을 가져옴

public double getDistance() { return distance;}

public String getStartTime() {return startTime;}
public LocalDateTime getStartTime() {return startTime;}

public int getNumRunners() {return numRunners;}

Expand Down Expand Up @@ -131,12 +131,8 @@ public void setRoomOwner(MultiModeUser roomOwner) {
this.roomOwner = roomOwner;
}

public int getRunningTimeHour() {
return runningTimeHour;
}

public int getRunningTimeMinute() {
return runningTimeMinute;
public LocalTime getDuration(){
return duration;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.io.Serializable;
import java.util.List;

public class Packet implements Serializable {
public class Packet implements Serializable { //서버와 통신하기 위해 사용하는 클래스. Protocol + 필요한 정보 넣어서 전송 및 수신
private static final long serialVersionUID = 1L;

// 데이터 유형을 나타내는 필드
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class RoomCreateInfo implements Serializable {
//방을 새로 생성할 때 담기는 정보.
private String title;
private double distance;
private String startTime;
//private String startTime;

private LocalDateTime startTime;
private int numRunners;



private int runningTimeHour;

private int runningTimeMinute;
private LocalTime duration;


// 생성자
public RoomCreateInfo(String title, double distance, String startTime, int numRunners, int runningTimeHour, int runningTimeMinute) {
public RoomCreateInfo(String title, double distance, LocalDateTime startTime, int numRunners, LocalTime duration) {
this.title = title;
this.distance = distance;
this.startTime = startTime;
this.numRunners = numRunners;
this.runningTimeHour = runningTimeHour;
this.runningTimeMinute = runningTimeMinute;
this.duration = duration;
}

// Getter 및 Setter 메서드
Expand All @@ -44,11 +45,17 @@ public void setDistance(double distance) {
this.distance = distance;
}

public String getStartTime() {
public LocalDateTime getStartTime() {
return startTime;
}
public LocalTime getDuration() {
return duration;
}

public void setStartTime(String startTime) {
public void setDuration(LocalTime duration) {
this.duration = duration;
}
public void setStartTime(LocalDateTime startTime) {
this.startTime = startTime;
}

Expand All @@ -60,21 +67,7 @@ public void setNumRunners(int numRunners) {
this.numRunners = numRunners;
}

public int getRunningTimeHour() {
return runningTimeHour;
}

public void setRunningTimeHour(int runningTimeHour) {
this.runningTimeHour = runningTimeHour;
}

public int getRunningTimeMinute() {
return runningTimeMinute;
}

public void setRunningTimeMinute(int runningTimeMinute) {
this.runningTimeMinute = runningTimeMinute;
}


}
26 changes: 14 additions & 12 deletions android/RunUsAndroid/app/src/main/java/MultiMode/RoomManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class RoomManager implements Serializable {
private static List<MultiModeRoom> roomList; // 방의 리스트
private static AtomicInteger atomicInteger;
private static AtomicInteger atomicInteger; //방 ID 만들기 위한 AtomicInteger

static {
roomList = new ArrayList();
Expand All @@ -30,23 +30,25 @@ public static MultiModeRoom createRoom(MultiModeUser owner, RoomCreateInfo roomC
}


public static MultiModeRoom getRoom(MultiModeRoom room){
int idx = roomList.indexOf(room);
if(idx >= 0){
return roomList.get(idx);
}else {
return null;
public static MultiModeRoom getRoom(MultiModeRoom room) {
for (MultiModeRoom r : roomList) {
if (r.getId() == room.getId()) {
return r;
}
}
return null;
}

public static MultiModeRoom getRoom(int idx){
if(idx >= 0){
return roomList.get(idx);
}else {
return null;
public static MultiModeRoom getRoom(int id) {
for (MultiModeRoom r : roomList) {
if (r.getId() == id) {
return r;
}
}
return null;
}


public static void removeRoom(MultiModeRoom room){
room.close();
roomList.remove(room);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.Random;

public class MultiModeAdapter extends RecyclerView.Adapter<MultiModeAdapter.ViewHolder> {

//MultiMode List 화면에서 각 Room Button과 관련된 Adapter
private List<MultiModeRoom> roomList;
MultiModeRoom selectedRoom;

Expand Down Expand Up @@ -111,9 +111,11 @@ public ViewHolder(@NonNull View itemView) {
textView = itemView.findViewById(R.id.textViewItem);
}
}

//방 입장시 socket을 통해 서버와 연결
private class EnterRoomTask extends AsyncTask<Void, Void, Boolean> {
Packet packet;
private Context mContext; // Add this line
private Context mContext;

public EnterRoomTask(Context context) {
this.mContext = context;
Expand All @@ -123,24 +125,19 @@ public EnterRoomTask(Context context) {
protected Boolean doInBackground(Void... voids) {
Socket socket = null;
try {
// socket = new Socket("10.0.2.2", 5001);
//
// ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
// ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
socketManager.openSocket(); // 소켓 연결

ObjectOutputStream oos = socketManager.getOOS();
ObjectInputStream ois = socketManager.getOIS();
MultiModeUser user = new MultiModeUser(1, "chocochip"); // Update this as needed
ObjectOutputStream oos = socketManager.getOOS(); //서버로 바이트스트림을 직렬화하기 위해 필요.
ObjectInputStream ois = socketManager.getOIS(); //서버로부터 받는 바이트스트림을 역직렬화하기 위해 필요.
MultiModeUser user = new MultiModeUser(1, "chocochip"); // 유저 정보 임시로 더미데이터 활용


if (selectedRoom.getUserList().size() < selectedRoom.getNumRunners()) {
Packet requestPacket = new Packet(Protocol.ENTER_ROOM, user, selectedRoom);
oos.writeObject(requestPacket);
oos.writeObject(requestPacket); //서버로 패킷 전송
oos.flush();

//Object firstreceivedObject = ois.readObject(); //server의 broadcastNewClientInfo를
Object receivedObject = ois.readObject();
Object receivedObject = ois.readObject(); //서버로부터 패킷 수신
if (receivedObject instanceof Packet) {
packet = (Packet) receivedObject;
}
Expand All @@ -155,15 +152,15 @@ protected Boolean doInBackground(Void... voids) {
}

@Override
protected void onPostExecute(Boolean success) {
protected void onPostExecute(Boolean success) { //doInBackground()의 return값에 따라 작업 수행. 룸 리스트 업데이트, 입장하는 방 정보 업데이트
super.onPostExecute(success);
if (success) {
Log.d("SendPacket", "Packet sent successfully!");
setRoomList(packet.getRoomList());
selectedRoom = packet.getSelectedRoom();

} else {
showFullRoomToast(mContext); // unwrap 함수를 여전히 사용하실 수 있습니다.
showFullRoomToast(mContext); // 방이 이미 꽉 찼다는 Toast 띄우기
Log.e("SendPacket", "Failed to send packet!");
}
}
Expand Down
Loading

0 comments on commit 1998631

Please sign in to comment.