Skip to content

Add custom error message for runtime errors & Compile code #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ public void updateMatch(@RequestBody @Valid UpdateMatchRequest updateMatchReques
return;
matchService.updateMatch(updateMatchRequest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import delta.codecharacter.server.controller.request.User.*;
import delta.codecharacter.server.controller.response.Match.DetailedMatchStatsResponse;
import delta.codecharacter.server.controller.response.Match.PrivateMatchResponse;
import delta.codecharacter.server.controller.response.Match.MatchResponse;
import delta.codecharacter.server.controller.response.User.PrivateUserResponse;
import delta.codecharacter.server.controller.response.User.PublicUserResponse;
import delta.codecharacter.server.controller.response.UserRatingsResponse;
Expand Down Expand Up @@ -158,7 +158,7 @@ public ResponseEntity<List<UserRatingsResponse>> getUserRatings(@PathVariable St
}

@GetMapping(value = "/match/{pageNo}/{pageSize}")
public ResponseEntity<List<PrivateMatchResponse>> getManualAndAutoExecutedMatches(@PathVariable Integer pageNo, @PathVariable Integer pageSize, Authentication authentication) {
public ResponseEntity<List<MatchResponse>> getManualAndAutoExecutedMatches(@PathVariable Integer pageNo, @PathVariable Integer pageSize, Authentication authentication) {
String email = userService.getEmailFromAuthentication(authentication);
User user = userService.getUserByEmail(email);
Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class MatchResponse {

private String username2;

private Integer avatarId1;
private Integer avatar1;

private Integer avatarId2;
private Integer avatar2;

private Integer score1;

Expand Down

This file was deleted.

63 changes: 42 additions & 21 deletions src/main/java/delta/codecharacter/server/service/MatchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import delta.codecharacter.server.controller.response.GameLogs;
import delta.codecharacter.server.controller.response.Match.DetailedMatchStatsResponse;
import delta.codecharacter.server.controller.response.Match.MatchResponse;
import delta.codecharacter.server.controller.response.Match.PrivateMatchResponse;
import delta.codecharacter.server.model.Match;
import delta.codecharacter.server.model.User;
import delta.codecharacter.server.repository.*;
Expand Down Expand Up @@ -118,8 +117,10 @@ public List<MatchResponse> getTopMatches(Integer PageNumber, Integer PageSize) {
MatchResponse matchResponse = MatchResponse.builder()
.username1(user1.getUsername())
.username2(user2.getUsername())
.avatarId1(user1.getAvatarId())
.avatarId2(user2.getAvatarId())
.avatar1(user1.getAvatarId())
.avatar2(user2.getAvatarId())
.score1(match.getScore1())
.score2(match.getScore2())
.verdict(match.getVerdict())
.matchMode(match.getMatchMode())
.games(gameService.getAllGamesByMatchId(match.getId()))
Expand All @@ -137,15 +138,15 @@ public List<MatchResponse> getTopMatches(Integer PageNumber, Integer PageSize) {
* @param userId UserId of the player
* @return List of paginated manual and auto matches
*/
public List<PrivateMatchResponse> getManualAndAutoExecutedMatchesPaginated(Integer userId, Pageable pageable) {
public List<MatchResponse> getManualAndAutoExecutedMatchesPaginated(Integer userId, Pageable pageable) {
Aggregation aggregation = newAggregation(
match(
new Criteria().andOperator(
new Criteria().andOperator(
new Criteria().orOperator(Criteria.where("player_id_1").is(userId), Criteria.where("player_id_2").is(userId)),
new Criteria().orOperator(Criteria.where("match_mode").is(MatchMode.MANUAL), Criteria.where("match_mode").is(MatchMode.AUTO))
), Criteria.where("status").is("EXECUTED")
)
new Criteria().andOperator(
new Criteria().orOperator(Criteria.where("player_id_1").is(userId), Criteria.where("player_id_2").is(userId)),
new Criteria().orOperator(Criteria.where("match_mode").is(MatchMode.MANUAL), Criteria.where("match_mode").is(MatchMode.AUTO))
), Criteria.where("status").is("EXECUTED")
)
),
sort(Sort.by("createdAt").descending()),
skip((long) pageable.getPageNumber() * pageable.getPageSize()),
Expand All @@ -155,26 +156,28 @@ public List<PrivateMatchResponse> getManualAndAutoExecutedMatchesPaginated(Integ
var groupResults = mongoTemplate.aggregate(aggregation, Match.class, Match.class);
List<Match> matches = groupResults.getMappedResults();

List<PrivateMatchResponse> privateMatchResponse = new ArrayList<>();
List<MatchResponse> matchResponseList = new ArrayList<>();
for (var match : matches) {

User user1 = userRepository.findByUserId(match.getPlayerId1());
User user2 = userRepository.findByUserId(match.getPlayerId2());

var matchResponse = PrivateMatchResponse.builder()
var matchResponse = MatchResponse.builder()
.username1(user1.getUsername())
.username2(user2.getUsername())
.avatar1(user1.getAvatarId())
.avatar2(user2.getAvatarId())
.score1(match.getScore1())
.score2(match.getScore2())
.verdict(match.getVerdict())
.playedAt(match.getCreatedAt())
.matchMode(match.getMatchMode())
.games(gameService.getAllGamesByMatchId(match.getId()))
.build();

privateMatchResponse.add(matchResponse);
matchResponseList.add(matchResponse);
}
return privateMatchResponse;
return matchResponseList;
}

/**
Expand Down Expand Up @@ -392,7 +395,8 @@ public void updateMatch(UpdateMatchRequest updateMatchRequest) {
matchRepository.save(match);

socketService.sendMessage(socketMatchResultDest + match.getPlayerId1(), "Error: " + updateMatchRequest.getError());
socketService.sendMessage(socketAlertMessageDest + match.getPlayerId1(), "Execute Error");
socketService.sendMessage(socketAlertMessageDest + match.getPlayerId1(), "Error: "
+ getErrorNotificationMessage(updateMatchRequest.getErrorType()));
return;
}
Verdict matchVerdict = deduceMatchVerdict(updateMatchRequest.getGameResults());
Expand Down Expand Up @@ -447,13 +451,15 @@ public void updateMatch(UpdateMatchRequest updateMatchRequest) {
gameRepository.save(game);

Integer gameId = gameResult.getId();
LogUtil.createLogRepository(gameId);
var gameLogs = GameLogs.builder()
.gameLog(gameResult.getLog())
.player1Log(gameResult.getPlayer1LogCompressed())
.player2Log(gameResult.getPlayer2LogCompressed())
.build();
LogUtil.setLogs(gameId, gameLogs);
if(match.getMatchMode() == MatchMode.AUTO || match.getMatchMode() == MatchMode.MANUAL){
LogUtil.createLogRepository(gameId);
var gameLogs = GameLogs.builder()
.gameLog(gameResult.getLog())
.player1Log(gameResult.getPlayer1LogCompressed())
.player2Log(gameResult.getPlayer2LogCompressed())
.build();
LogUtil.setLogs(gameId, gameLogs);
}
}

match.setStatus(Status.EXECUTED);
Expand All @@ -462,6 +468,21 @@ public void updateMatch(UpdateMatchRequest updateMatchRequest) {
matchRepository.save(match);
}

private String getErrorNotificationMessage(String errorType) {
switch (errorType) {
case "COMPILER_ERROR":
return "Compilation Error";
case "EXECUTE_PROCESS_ERROR":
return "Execution Error";
case "UNKNOWN_EXECUTE_ERROR":
return "Something went wrong!";
case "PLAYER_RUNTIME_ERROR":
return "Runtime Error";
default:
return errorType;
}
}

private void updateMatchScore(Match match, List<UpdateGameDetails> gameDetails) {
Integer player1Wins = 0, player2Wins = 0;
for (var game : gameDetails) {
Expand Down