Skip to content

Commit

Permalink
feat: 서버 메모리에 유저 좌표 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
yunuo46 committed May 11, 2024
1 parent bf20c83 commit ab30315
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public void handleWebSocketConnectListener(SessionConnectEvent event) {

@EventListener
public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) {
System.out.println(event.getSessionId());
messagingTemplate.convertAndSend("/topic/map/remove", mapService.removeUser(sessionId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@Getter
@Setter
public class MoveUserDto {
private int id;
private String username;
private int x;
private int y;
Expand Down
25 changes: 10 additions & 15 deletions src/main/java/getaguitar/site/demo/Service/MapServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,22 @@
@Service
public class MapServiceImpl implements MapService {
private final UserRepository userRepository;
private int x;
private int y;

public MapServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public UserEntity createUser(String sessionId, ReqNewUserDto newUser) {
x=400;
y=300;
return userRepository.save(UserEntity.builder()
.sessionId(sessionId)
.username(newUser.getUsername())
.x(400)
.y(300)
.x(x)
.y(y)
.direction("down")
.build());
}
Expand All @@ -47,41 +51,32 @@ public String removeUser(String sessionId) {
@Override
@Transactional
public MoveUserDto moveUser(MoveUserDto userInfo) {
int id = userInfo.getId();
UserEntity user = userRepository.findById(id).orElseThrow();

String username = userInfo.getUsername();
int x = userInfo.getX();
int y = userInfo.getY();
String direction = userInfo.getDirection();

switch (direction) {
case "up" -> {
y -= 4;
user.setY(y);
}
case "down" -> {
y += 4;
user.setY(y);
}
case "left" -> {
x -= 4;
user.setX(x);
}
case "right" -> {
x += 4;
user.setX(x);
}
}
return new MoveUserDto(id, username, x, y, direction);
return new MoveUserDto(username, x, y, direction);
}

@Override
public ResStopUserDto stopUser(ReqStopUserDto stopUser) {
System.out.println("stop");
String username = stopUser.getUsername();
int id = stopUser.getId();
UserEntity user = userRepository.findById(id).orElseThrow();
return new ResStopUserDto(username, user.getX(), user.getY());
userRepository.save(UserEntity.builder()
.id(id).x(x).y(y).build());
return new ResStopUserDto(username, x, y);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# LocalTest
#spring.datasource.url=jdbc:mysql://localhost:3306/getaguitar?useSSL=false&allowPublicKeyRetrieval=true
#spring.datasource.username=root
#spring.datasource.password=8320
#spring.datasource.password=zx8320

0 comments on commit ab30315

Please sign in to comment.