Skip to content

Commit

Permalink
MVP 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
MTelling committed Apr 28, 2017
1 parent 9b155b7 commit bb0e54c
Show file tree
Hide file tree
Showing 21 changed files with 758 additions and 683 deletions.
1 change: 0 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Testing/Tests/Challenge_Four.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Reversing input",
"description": "Given a string as input, return the string with the characters in the reverse order.",
"description": "Given a string as input, return the string with the characters in the reverse order.\n\nExample:\nGiven args[0] = \"abc\" return \"cba\".",
"filename": "Challenge_Four",
"tests": [
{
Expand All @@ -23,7 +23,7 @@
}
],
"initialCode" : {
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\treturn \"reversedstring\";\n\t}\n}",
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\t//Type your code here...\n\t\treturn \"reversedstring\";\n\t}\n}",
"javascript" : "function program(args) {\n\treturn \"reversedstring\";\n}"
}
}
4 changes: 2 additions & 2 deletions Testing/Tests/Challenge_One.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Adding numbers",
"description": "Add the two arguments together and return the result",
"description": "Add the two arguments together and return the result.\n\nExample:\nGiven args[0] = \"2\" and args[1] = \"3\" return 5.",
"filename": "Challenge_One",
"tests": [
{
Expand All @@ -19,7 +19,7 @@
}
],
"initialCode" : {
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\treturn -1;\n\t}\n}",
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\t//Type your code here...\n\t\treturn -1;\n\t}\n}",
"javascript" : "function program(args) {\n\treturn -1;\n}"
}
}
16 changes: 14 additions & 2 deletions Testing/Tests/Challenge_Three.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Counting a's",
"description": "The goal of this challenge is to write a program that counts the number of e's in a string supplied in the first argument..",
"description": "The goal of this challenge is to write a program that counts the number of a's in a string supplied in the first argument.\n\nExample:\nGiven args[0] = \"abc\" return 1.",
"filename": "Challenge_Three",
"tests": [
{
Expand All @@ -20,10 +20,22 @@
"Bloomberg"
],
"expectedReturn": 0
},
{
"arguments": [
"aaa"
],
"expectedReturn": 3
},
{
"arguments": [
"aaaaa"
],
"expectedReturn": 5
}
],
"initialCode" : {
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\treturn -1;\n\t}\n}",
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\t//Type your code here...\n\t\treturn -1;\n\t}\n}",
"javascript" : "function program(args) {\n\treturn -1;\n}"
}
}
11 changes: 9 additions & 2 deletions Testing/Tests/Challenge_Two.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Multiplying numbers",
"description": "Multiply the two arguments together and return the result.",
"description": "Multiply the two arguments together and return the result.\n\nExample:\nGiven args[0] = \"2\" and args[1] = \"2\" return 4.",
"filename": "Challenge_Two",
"tests": [
{
Expand All @@ -16,10 +16,17 @@
"2"
],
"expectedReturn": 2
},
{
"arguments": [
"-1",
"2"
],
"expectedReturn": -2
}
],
"initialCode" : {
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\treturn -1;\n\t}\n}",
"java" : "public class Program {\n\tpublic Object run(String[] args) {\n\t\t//Type your code here...\n\t\treturn -1;\n\t}\n}",
"javascript" : "function program(args) {\n\treturn -1;\n}"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"react-emoji": "^0.5.0",
"react-form": "^1.2.6",
"react-monaco-editor": "^0.8.1",
"react-newline-to-break": "^1.0.6",
"react-router": "^3.0.5",
"react-tap-event-plugin": "^2.0.1",
"webpack": "^2.4.1"
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/htg/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
@SpringBootApplication
public class Application {

public static String urlToRunner = "http://localhost:4567";


public static void main(String[] args) {

if (args.length > 0) urlToRunner = args[0];
SpringApplication.run(Application.class, args);
}

Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/htg/Challenge.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,33 @@ public String getExternalName() {
public void setExternalName(String externalName) {
this.externalName = externalName;
}



public static void main(String[] args) {
int n = 25000;
int count = 0;
int current = 1;

while (count != n) {
current++;
if (isPrime(current)) {
count++;
System.out.println(current);
}
}

System.out.println(current);
}
private static boolean isPrime(int x) {
for (int i = 2; i < x / 2; i++) {
if (x % i == 0 && i != x) {
return false;
}
}

return true;
}

}

134 changes: 58 additions & 76 deletions src/main/java/com/htg/PMController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.htg;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.stereotype.Controller;

import java.io.*;
import java.nio.file.Paths;
import java.util.Arrays;


@Controller
Expand All @@ -23,65 +24,78 @@ public class PMController {
@Autowired
private ServerState state;

private final Object winLock = new Object();

@MessageMapping("/pm")
@SendToUser("/queue/pm")
public PMResponse codeCheck(PMRequest pmRequest, SimpMessageHeaderAccessor simpMessageHeaderAccessor) throws Exception {
String sessionID = simpMessageHeaderAccessor.getSessionAttributes().get("sessionID").toString();
String challengeID = state.getCurrentChallengeDescription().getFilename();

if (sessionID == null) return new PMResponse("NO USER IN!!");
if (sessionID == null) return new PMResponse("You need to be logged in!!!");

System.out.println("In the pmcontroller");
String[] compileResults;
String[] runtimeResults;
String code = pmRequest.getCode();
String challenge = state.getCurrentChallengeDescription().getFilename();

String code = pmRequest.getCode(),
testingPath = "Testing",
compilePath = "",
path = Paths.get("").toAbsolutePath().toString(),
classNamePrefix = "P",
className = classNamePrefix + sessionID,
fileName = className + ".java";
String resultFromCall = callRunManager(code, sessionID, challenge);

code = "package Testing; " + code;
code = code.replace("public class Program", "public class " + className);
// Check if all tests are completed
try {
RunnerResult runnerResult = new Gson().fromJson(resultFromCall, RunnerResult.class);

// Write code to java file
try( PrintWriter out = new PrintWriter( testingPath + "/" + fileName ) ) { out.println( code ); }
if ( runnerResult.isSuccess()) {

// Compiles given code, returns if it failed compiling
if (challengeID.equals(state.getCurrentChallengeDescription().getFilename())) {

compileResults = run("javac", path + "/" + testingPath + "/" + fileName);
System.out.println("Compile result: " + Arrays.toString(compileResults));
if (compileResults[1].length() > 0) {
return new PMResponse(compileResults[1]);
}
// This makes sure that another player cannot submit a success if someone else just won.
// This is quite important because otherwise a player can win the next challenge, with the result
// from the previous challenge.
synchronized (winLock) {
if (challengeID.equals(state.getCurrentChallengeDescription().getFilename())) {
state.goToNextChallenge();
state.updateUserScore(sessionID, 1);
announceWin(state.getUser(sessionID).getName());

// Runs tests
System.out.println(path + "Compiler.jar");
runtimeResults = run("java", "-jar", path + "/Runner.jar", state.getCurrentChallengeDescription().getFilename(), className);
System.out.println("Run result: " + Arrays.toString(runtimeResults));
return new PMResponse(createOutputString(runnerResult));
}
}

// If there are any errors, show them.
if ( runtimeResults[1].length() > 0 ) {
return new PMResponse(runtimeResults[1]);
}
} else {
return new PMResponse("You succeeded, but someone beat you to it!");
}
}

// Build result
String output = runtimeResults[0];
if (!challengeID.equals(state.getCurrentChallengeDescription().getFilename())) {
return new PMResponse("Sorry, your code didn't succeed but another player's did.");
}

return new PMResponse(createOutputString(runnerResult));

} catch (JsonParseException e) {

return new PMResponse(resultFromCall);

// Check if all tests are completed
RunnerResult runnerResult = new Gson().fromJson(output, RunnerResult.class);
if ( runnerResult.isSuccess() ) {
System.out.println("session id is: " + sessionID);
state.updateUserScore(sessionID, 1);
announceWin(state.getUser(sessionID).getName());
}

return new PMResponse(createOutputString(runnerResult));


}


private String callRunManager(String code, String sessionID, String challenge) throws UnirestException {
RunRequest runRequest = new RunRequest(sessionID, code, challenge);

String url = Application.urlToRunner;

HttpResponse jsonResponse = Unirest.post(url)
.header("accept", "application/json")
.body(new Gson().toJson(runRequest)).asString();

return jsonResponse.getBody().toString();
}

private String createOutputString(RunnerResult runnerResult) {
final String NEW_LINE = ", ";
final String NEW_LINE = "\n";
StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("Succeeded: ").append(runnerResult.isSuccess() ? "Yes": "No").append(NEW_LINE);
Expand All @@ -105,44 +119,12 @@ private String createOutputString(RunnerResult runnerResult) {
}


private String[] run(String... args) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder();
pb.command(args);
Process proc = pb.start();
BufferedReader errors = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
BufferedReader output = new BufferedReader(new InputStreamReader(proc.getInputStream()));
proc.waitFor();

StringBuilder errorBuilder = new StringBuilder();
StringBuilder outputBuilder = new StringBuilder();

String currentLine = null;
while ((currentLine = errors.readLine()) != null) {
errorBuilder.append(currentLine);
}

while ((currentLine = output.readLine()) != null) {
outputBuilder.append(currentLine);
}

try {
errors.close();
output.close();
} catch (Exception e) {
System.out.println("Couldn't close BReaders");
e.printStackTrace();
}


return new String[]{outputBuilder.toString(), errorBuilder.toString()};
}


public void announceWin(String winnerUsername) {
System.out.println("Announcing new winner");
state.goToNextChallenge();
simpMessagingTemplate.convertAndSend("/topic/news", new NewsResponse("win", winnerUsername, state.getLeaderboard()));
}




}
40 changes: 40 additions & 0 deletions src/main/java/com/htg/RunRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.htg;

/**
* Created by Morten on 27/04/2017.
*/
public class RunRequest {
private String sessionID;
private String code;
private String challenge;

public RunRequest(String sessionID, String code, String challenge) {
this.sessionID = sessionID;
this.code = code;
this.challenge = challenge;
}

public String getSessionID() {
return sessionID;
}

public void setSessionID(String sessionID) {
this.sessionID = sessionID;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getChallenge() {
return challenge;
}

public void setChallenge(String challenge) {
this.challenge = challenge;
}
}
Loading

0 comments on commit bb0e54c

Please sign in to comment.