Skip to content
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

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -14,7 +14,7 @@
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {

@Value("${client.request.origin}")
@Value("${client.request.cors.origin}")
private String origin;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final Logger LOG = Logger.getLogger(WebSecurityConfig.class.getName());

//routes which do not require authentication
String[] ignoringAntMatchers = {"/", "/login/**", "/error/**", "/logout", "/user", "/user/**", "/match/top/**", "/match", "/socket/**"};
String[] ignoringAntMatchers = {"/", "/login/**", "/error/**", "/logout", "/user", "/user/**", "/match/top/**", "/match", "/socket/**", "/code/lock"};

@Autowired
private BCryptPasswordEncoder bCryptPasswordEncoder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package delta.codecharacter.server.controller.api;

import delta.codecharacter.server.controller.request.Codeversion.CommitResponse;
import delta.codecharacter.server.controller.request.LockCodeRequest;
import delta.codecharacter.server.model.User;
import delta.codecharacter.server.service.UserService;
import delta.codecharacter.server.service.VersionControlService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.logging.Logger;

@Controller
@RestController
@RequestMapping(value = "/code")
public class CodeVersionController {
Expand All @@ -26,6 +31,9 @@ public class CodeVersionController {
@Autowired
UserService userService;

@Value("${compilebox.secret-key}")
private String secretKey;

@GetMapping(value = "/latest")
@SneakyThrows
public ResponseEntity<String> getLatestCode(Authentication authentication) {
Expand All @@ -38,7 +46,6 @@ public ResponseEntity<String> getLatestCode(Authentication authentication) {
}

@PutMapping(value = "")
@SneakyThrows
public ResponseEntity<String> saveCode(@RequestBody @Valid String code, Authentication authentication) {
String email = userService.getEmailFromAuthentication(authentication);
User user = userService.getUserByEmail(email);
Expand All @@ -48,17 +55,6 @@ public ResponseEntity<String> saveCode(@RequestBody @Valid String code, Authenti
return new ResponseEntity<>("Saved Code", HttpStatus.OK);
}

@PutMapping(value = "/submit")
@SneakyThrows
public ResponseEntity<String> setLockedCode(Authentication authentication) {
String email = userService.getEmailFromAuthentication(authentication);
User user = userService.getUserByEmail(email);
if (user == null) return new ResponseEntity<>("User not found", HttpStatus.UNAUTHORIZED);
if (!versionControlService.setLockedCode(user.getUserId()))
return new ResponseEntity<>("Code repository not created", HttpStatus.FORBIDDEN);
return new ResponseEntity<>("Code Submitted", HttpStatus.OK);
}

@PostMapping(value = "/commit")
@SneakyThrows
public ResponseEntity<String> commit(@RequestBody @Valid String commitMessage, Authentication authentication) {
Expand Down Expand Up @@ -112,4 +108,18 @@ public ResponseEntity<String> forkCommitByHash(@PathVariable String commitHash,
return new ResponseEntity<>("Code repository not created", HttpStatus.FORBIDDEN);
return new ResponseEntity<>("Forked successfully", HttpStatus.OK);
}

@MessageMapping("/code/submit")
public void submitCode(Authentication authentication) {
String email = userService.getEmailFromAuthentication(authentication);
User user = userService.getUserByEmail(email);
versionControlService.submitCode(user.getUserId());
}

@PostMapping(value = "/lock")
public void lockCode(@RequestBody LockCodeRequest lockCodeRequest) {
if (!lockCodeRequest.getSecretKey().equals(secretKey))
return;
versionControlService.lockCode(lockCodeRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

import delta.codecharacter.server.controller.response.GameLogs;
import delta.codecharacter.server.service.GameService;
import delta.codecharacter.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

@RequestMapping(value = "/game")
@RestController
@CrossOrigin("*")
@RequestMapping("/game")
public class GameController {

@Autowired
private GameService gameService;

@Autowired
private UserService userService;

@GetMapping(value = "/log/{gameId}")
public ResponseEntity<GameLogs> getGameLog(@PathVariable Integer gameId) {
return new ResponseEntity<>(gameService.getGameLog(gameId), HttpStatus.OK);
public ResponseEntity<GameLogs> getGameLog(@PathVariable Integer gameId, Authentication authentication) {
String email = userService.getEmailFromAuthentication(authentication);
var user = userService.getUserByEmail(email);
return new ResponseEntity<>(gameService.getGameLog(gameId, user.getUserId()), HttpStatus.OK);
}
}
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
@@ -0,0 +1,22 @@
package delta.codecharacter.server.controller.request;

import lombok.Data;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import java.util.List;

@Data
@JsonIgnoreProperties(value = "true")
public class LockCodeRequest {
private String secretKey;

private Integer userId;

private Boolean success;

private List<String> playerDLLs;

private String errorType;

private String error;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package delta.codecharacter.server.controller.request.Simulation;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class CompileCodeRequest {
private String jobType;

private Integer userId;

private String secretKey;

private String code;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class UpdateGameDetails {

private Verdict verdict;

private String winType;

private Integer matchId;

private Integer points1;
Expand All @@ -24,5 +26,7 @@ public class UpdateGameDetails {
private String player1LogCompressed;

private String player2LogCompressed;

private String errorType;
}

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.

4 changes: 4 additions & 0 deletions src/main/java/delta/codecharacter/server/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class Game {
@Builder.Default
private Verdict verdict = Verdict.TIE;

private String winType;

private String errorType;

@Field("points_1")
@Builder.Default
private Integer points1 = 0;
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/delta/codecharacter/server/service/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import delta.codecharacter.server.repository.MatchRepository;
import delta.codecharacter.server.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -19,6 +20,12 @@ public class GameService {
@Autowired
private MatchRepository matchRepository;

@Autowired
private SocketService socketService;

@Value("/socket/response/alert/")
private String socketAlertMessageDest;

/**
* Create a new game for the given matchId
*
Expand All @@ -45,12 +52,16 @@ public Game createGame(Integer matchId, Integer mapId) {
* @param gameId Game Id of the game
* @return game log, player1 log and player2 log of the game
*/
public GameLogs getGameLog(Integer gameId) {
public GameLogs getGameLog(Integer gameId, Integer userId) {
var game = gameRepository.findFirstById(gameId);
if (!game.getWinType().equals("SCORE")) {
socketService.sendMessage(socketAlertMessageDest + userId, game.getVerdict() + " won by " + game.getWinType());
return null;
}
var gameLogs = LogUtil.getLogs(gameId);
if (gameLogs == null)
return null;

var game = gameRepository.findFirstById(gameId);
var match = matchRepository.findFirstById(game.getMatchId());
gameLogs.setPlayerId1(match.getPlayerId1());

Expand Down
Loading