Skip to content

Commit

Permalink
Feat: connect reult screen with play screen and build process when ga…
Browse files Browse the repository at this point in the history
…me finishes
  • Loading branch information
Jack-Chagarr committed Nov 1, 2023
1 parent 0f65e95 commit 720ee64
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

Expand All @@ -17,6 +18,8 @@ public class MultiModeRoom implements Serializable {
private final int GAME_NOT_STARTED = 0;
private final transient List<ObjectOutputStream> clientOutputStreams = new ArrayList<>();
private final HashSet<Integer> finishedUserSet = new HashSet<>();
private final Queue<UserDistance> updateQueue = new LinkedList<>();
UserDistance[] top3UserDistances;
private int status = GAME_NOT_STARTED;
private int finishCount = 0;
private int id; // 룸 ID
Expand All @@ -26,13 +29,9 @@ public class MultiModeRoom implements Serializable {
private String title; //방 제목
private double distance; //목표 거리
private int numRunners; //제한 인원

private LocalDateTime startTime; //시작 시각

private Duration duration; //목표 시간(달리는 시간)

private Queue<UserDistance> updateQueue;


public MultiModeRoom(int roomId, RoomCreateInfo roomCreateInfo) { // 유저가 방을 만들때
userList = new ArrayList<MultiModeUser>();
Expand Down Expand Up @@ -72,6 +71,7 @@ public int exitUser(MultiModeUser user) {
if (muser.getId() == user.getId()) {
userToRemove = muser;
index = i;

break;
}
}
Expand All @@ -80,9 +80,23 @@ public int exitUser(MultiModeUser user) {
if (userToRemove != null) {
System.out.println("remove user: " + userToRemove.getNickName());
userList.remove(userToRemove);
if (top3UserDistances != null) {
UserDistance[] newTop3UserDistance = new UserDistance[Math.min(3, userList.size())];
int arrindex = 0;
for (int i = 0; i < top3UserDistances.length; i++) {
if (userList.contains(top3UserDistances[i].getUser())) {
newTop3UserDistance[arrindex++] = top3UserDistances[i];
}
}
top3UserDistances = newTop3UserDistance;
}
}

if (userList.size() < 1) {
System.out.println("exitroom - roomId is " + id);
System.out.println();
System.out.println();
System.out.println();
RoomManager.removeRoom(this);
return index;
}
Expand All @@ -97,9 +111,9 @@ public int exitUser(MultiModeUser user) {
public void close() {
for (MultiModeUser user : userList) {
user.exitRoom(this);
this.userList.clear();
this.userList = null;
}
this.userList.clear();
this.userList = null;
}

public void broadcast(byte[] data) {
Expand Down Expand Up @@ -220,7 +234,9 @@ public int compare(UserDistance user1, UserDistance user2) {
});

// 상위 3개의 UserDistance 객체를 배열에 저장
UserDistance[] top3UserDistances = new UserDistance[Math.min(3, topUserDistances.size())];
if (top3UserDistances == null) {
top3UserDistances = new UserDistance[Math.min(3, topUserDistances.size())];
}
for (int i = 0; i < top3UserDistances.length; i++) {
top3UserDistances[i] = topUserDistances.get(i);
}
Expand All @@ -231,8 +247,15 @@ public int compare(UserDistance user1, UserDistance user2) {
return null;
}

public UserDistance[] getResultTop3UserDistances() {
if (top3UserDistances != null) {
return top3UserDistances;
} else return null;
}

public void addFinishCount(MultiModeUser user) {
if (!finishedUserSet.contains(user.getId())) {
finishedUserSet.add(user.getId());
finishCount++;
}
}
Expand All @@ -245,6 +268,10 @@ public void startGame() {
status = GAME_STARTED;
}

public boolean canUpdate() {
return updateQueue.size() >= userList.size();
}


@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
public class MultiModeFragment extends Fragment {


public static MultiModeUser user = new MultiModeUser(1, "choco");
//public static MultiModeUser user = new MultiModeUser(2, "berry"); // 유저 정보 임시로 더미데이터 활용
//public static MultiModeUser user = new MultiModeUser(1, "choco");
public static MultiModeUser user = new MultiModeUser(2, "berry"); // 유저 정보 임시로 더미데이터 활용
//public static MultiModeUser user = new MultiModeUser(3, "apple");
private final SocketManager socketManager = SocketManager.getInstance(); // SocketManager 인스턴스를 가져옴
Dialog dialog;
Expand Down Expand Up @@ -105,8 +105,8 @@ public void onClick(View v) {
int timePickerCurrentHour = time_picker.getCurrentHour();
int timePickerCurrentMinute = time_picker.getCurrentMinute();

Duration duration = Duration.ofHours(numberPickerHour.getValue()).plusMinutes(numberPickerMinute.getValue());
//Duration duration = Duration.ofHours(0).plusMinutes(0).plusSeconds(30);
//Duration duration = Duration.ofHours(numberPickerHour.getValue()).plusMinutes(numberPickerMinute.getValue());
Duration duration = Duration.ofHours(0).plusMinutes(0).plusSeconds(8);
LocalDate today = LocalDate.now();
LocalDateTime startTime = LocalDateTime.of(today, LocalTime.of(timePickerCurrentHour, timePickerCurrentMinute));
// 현재 시간보다 선택한 시간이 느린 경우 하루 뒤로 설정
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ public class MultiModePlayFragment extends Fragment {

Button playLeaveButton;
SocketListenerThread socketListenerThread = null;
Handler timeHandler;
Runnable timeRunnable;
Handler sendDataHandler;
Runnable sendDataRunnable;
private TextView timePresentContentTextView;
private Handler timeHandler;
private Runnable timeRunnable;

private Handler sendDataHandler;

private Runnable sendDataRunnable;
private int isFinished = 0;
private ObjectInputStream ois;

Expand Down Expand Up @@ -306,6 +304,7 @@ public void onResume() {
socketListenerThread = (SocketListenerThread) getArguments().getSerializable("socketListenerThread"); //waitFragment의 socketListenrThread객체 가져와서 이어서 사용
socketListenerThread.addPlayFragment(this);
socketListenerThread.resumeListening();
Log.d("response", "start play screen");
//top3UpdateHandler.postDelayed(sendDataRunnable, 5000);

fusedLocationClient.removeLocationUpdates(locationCallback);
Expand Down Expand Up @@ -396,7 +395,6 @@ protected Boolean doInBackground(Void... voids) {
} finally {
timeHandler.removeCallbacks(timeRunnable);
sendDataHandler.removeCallbacks(sendDataRunnable);
Log.d("response", "socket closed");
}
return success;
}
Expand Down Expand Up @@ -459,4 +457,6 @@ protected void onPostExecute(Boolean success) {
}

}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.runusandroid.ui.multi_mode;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -18,10 +19,10 @@
import com.example.runusandroid.R;

import java.io.ObjectOutputStream;
import java.util.Locale;

import MultiMode.MultiModeRoom;
import MultiMode.MultiModeUser;
import MultiMode.UserDistance;

public class MultiModeResultFragment extends Fragment {

Expand All @@ -47,8 +48,9 @@ public class MultiModeResultFragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

selectedRoom = (MultiModeRoom) getArguments().getSerializable("room");

View view = inflater.inflate(R.layout.fragment_multi_room_play, container, false); //각종 view 선언
View view = inflater.inflate(R.layout.fragment_multi_room_result, container, false); //각종 view 선언
if (selectedRoom != null) {
timeGoalContentTextView = view.findViewById(R.id.time_goal_content);
goldNickNameTextView = view.findViewById(R.id.gold_nickname);
Expand All @@ -58,18 +60,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
bronzeNickNameTextView = view.findViewById(R.id.bronze_nickname);
bronzeDistanceTextView = view.findViewById(R.id.bronze_distance);
progressBar = view.findViewById(R.id.linear_progress_bar);
playLeaveButton = view.findViewById(R.id.play_leaveButton);
//목표 시간 계산하기 위한 코드
long secondsRemaining = selectedRoom.getDuration().getSeconds();

// 시간, 분으로 변환
long hours = secondsRemaining / 3600;
long minutes = (secondsRemaining % 3600) / 60;
long seconds = secondsRemaining % 60;
String formattedTime = String.format(Locale.getDefault(), "%02d:%02d:%02d",
hours, minutes, seconds);

timeGoalContentTextView.setText(formattedTime);
playLeaveButton = view.findViewById(R.id.result_leaveButton);

}

Expand All @@ -85,4 +76,74 @@ public void onClick(View v) {
return view;

}

public void updateTop3UserDistance(UserDistance[] userDistances) { // 화면에 표시되는 top3 유저 정보 업데이트. socketListenerThread에서 사용
UserDistance[] top3UserDistance = userDistances;
for (int i = 0; i < userDistances.length; i++) {

Log.d("response", "In updateTop3UserDistance, top3user " + i + " : " + top3UserDistance[0].getUser().getNickName() + " , distance : " + userDistances[0].getDistance());
Log.d("response", "In updateTop3UserDistance, user " + i + " : " + userDistances[0].getUser().getNickName() + " , distance : " + userDistances[0].getDistance());

}
double goldDistance = 0;

if (top3UserDistance.length == 1) {
goldNickNameTextView.setText(top3UserDistance[0].getUser().getNickName());
goldDistance = top3UserDistance[0].getDistance();
String goldDistanceString = String.format("%.3fkm", goldDistance);
goldDistanceTextView.setText(goldDistanceString);

silverNickNameTextView.setText("-");
silverDistanceTextView.setText("-");

bronzeNickNameTextView.setText("-");
bronzeDistanceTextView.setText("-");
} else if (top3UserDistance.length == 2) {
goldNickNameTextView.setText(top3UserDistance[0].getUser().getNickName());
goldDistance = top3UserDistance[0].getDistance();
String goldDistanceString = String.format("%.3fkm", goldDistance);
goldDistanceTextView.setText(goldDistanceString);

silverNickNameTextView.setText(top3UserDistance[1].getUser().getNickName());
double silverDistance = top3UserDistance[1].getDistance();
String silverDistanceString = String.format("%.3fkm", silverDistance);
silverDistanceTextView.setText(silverDistanceString);

bronzeNickNameTextView.setText("-");
bronzeDistanceTextView.setText("-");
} else {
goldNickNameTextView.setText(top3UserDistance[0].getUser().getNickName());
goldDistance = top3UserDistance[0].getDistance();
String goldDistanceString = String.format("%.3fkm", goldDistance);
goldDistanceTextView.setText(goldDistanceString);

silverNickNameTextView.setText(top3UserDistance[1].getUser().getNickName());
double silverDistance = top3UserDistance[1].getDistance();
String silverDistanceString = String.format("%.3fkm", silverDistance);
silverDistanceTextView.setText(silverDistanceString);

bronzeNickNameTextView.setText(top3UserDistance[2].getUser().getNickName());
double bronzeDistance = top3UserDistance[2].getDistance();
String bronzeDistanceString = String.format("%.3fkm", bronzeDistance);
bronzeDistanceTextView.setText(bronzeDistanceString);
}


progressBar.setProgress(100);

}

@Override
public void onResume() {
super.onResume();
socketListenerThread = (SocketListenerThread) getArguments().getSerializable("socketListenerThread"); //waitFragment의 socketListenrThread객체 가져와서 이어서 사용
socketListenerThread.addResultFragment(this);
socketListenerThread.resumeListening();
UserDistance[] top3UserDistance = (UserDistance[]) getArguments().getSerializable("top3UserDistance");
updateTop3UserDistance(top3UserDistance);
distance = (double) getArguments().getSerializable("userDistance");
Log.d("response", "here is room result screen");


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class SocketListenerThread extends Thread implements Serializable { //
private final MultiModeRoom selectedRoom;
private MultiModeWaitFragment waitFragment = null;
private MultiModePlayFragment playFragment = null;
private MultiModeResultFragment resultFragment = null;
private boolean isPaused = false; // 일시 중지 상태 관리 플래그

public SocketListenerThread(MultiModeWaitFragment waitFragment, Handler handler, MultiModeRoom selectedRoom, ObjectInputStream ois) {
Expand All @@ -50,6 +51,10 @@ public void addPlayFragment(MultiModePlayFragment playFragment) {
this.playFragment = playFragment;
}

public void addResultFragment(MultiModeResultFragment resultFragment) {
this.resultFragment = resultFragment;
}

@Override
public void run() {
try {
Expand Down Expand Up @@ -101,6 +106,7 @@ public void run() {
bundle.putSerializable("room", selectedRoom);
bundle.putSerializable("socketListenerThread", SocketListenerThread.this);
NavController navController = Navigation.findNavController(waitFragment.requireView());
Log.d("response", "goto play screen");
navController.navigate(R.id.navigation_multi_room_play, bundle);
}
});
Expand All @@ -126,10 +132,30 @@ public void run() {
}
});
} else if (packet.getProtocol() == Protocol.CLOSE_GAME) {
Log.d("response", "got close game packet!!!!!");
handler.post(new Runnable() {
@Override
public void run() {
UserDistance[] userDistance = packet.getTop3UserDistance();

playFragment.timeHandler.removeCallbacks(playFragment.timeRunnable);
playFragment.sendDataHandler.removeCallbacks(playFragment.sendDataRunnable);
Bundle bundle = new Bundle();
bundle.putSerializable("room", selectedRoom);
bundle.putSerializable("socketListenerThread", SocketListenerThread.this);
bundle.putSerializable("top3UserDistance", packet.getTop3UserDistance());
bundle.putSerializable("userDistance", playFragment.distance);
Log.d("response", "go to room result screen");
NavController navController = Navigation.findNavController(playFragment.requireView());
navController.navigate(R.id.navigation_multi_room_result, bundle);
Log.d("response", packet.getTop3UserDistance() + " ");
Log.d("response", resultFragment + "");

// resultFragment.updateTop3UserDistance(packet.getTop3UserDistance());
try {
playFragment.socketManager.closeSocket();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="1km"
android:text="0.0km"
android:textAppearance="@style/multi_room_play_present" />
</LinearLayout>

Expand All @@ -254,7 +254,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="00:20:00"
android:text="00:00:00"
android:textAppearance="@style/multi_room_play_present" />
</LinearLayout>
</LinearLayout>
Expand Down
Loading

0 comments on commit 720ee64

Please sign in to comment.