Commit 2b5924d5 authored by Grzegorz Pietrusza's avatar Grzegorz Pietrusza

checking if not already subbmited flag - stub

parent 592b3bee
package com.telephoners.krakyournet.ctf.repositories; package com.telephoners.krakyournet.ctf.repositories;
import com.telephoners.krakyournet.ctf.beans.Flag;
import com.telephoners.krakyournet.ctf.beans.Solution; import com.telephoners.krakyournet.ctf.beans.Solution;
import com.telephoners.krakyournet.ctf.beans.Team; import com.telephoners.krakyournet.ctf.beans.Team;
import com.telephoners.krakyournet.ctf.beans.tasks.Task; import com.telephoners.krakyournet.ctf.beans.tasks.Task;
...@@ -9,7 +10,9 @@ import javax.inject.Inject; ...@@ -9,7 +10,9 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Singleton @Singleton
...@@ -62,4 +65,22 @@ public class SolutionsRepository implements Repository ...@@ -62,4 +65,22 @@ public class SolutionsRepository implements Repository
.map(solution -> solution.getFlag().getDescription()).collect(Collectors.toList()) .map(solution -> solution.getFlag().getDescription()).collect(Collectors.toList())
)); ));
} }
//todo: use datastore filter
public boolean exists(Solution solution)
{
//todo: refactor, ugly ;(
String value = solution.getFlag().getValue();
Optional<Solution> matchedSolution = getAll().stream()
.filter(new Predicate<Solution>()
{
@Override
public boolean test(Solution solution)
{
return solution.getFlag().getValue().equals(value);
}
})
.findFirst();
return matchedSolution.isPresent();
}
} }
\ No newline at end of file
...@@ -151,6 +151,11 @@ public class TasksRepository implements Repository ...@@ -151,6 +151,11 @@ public class TasksRepository implements Repository
return encodedHash; return encodedHash;
} }
private boolean isAlreadySubmittedSolution(Solution solution)
{
return solutionsRepository.exists(solution);
}
public boolean checkHash(User user, String hashValue) public boolean checkHash(User user, String hashValue)
{ {
//todo: refactor //todo: refactor
...@@ -161,11 +166,14 @@ public class TasksRepository implements Repository ...@@ -161,11 +166,14 @@ public class TasksRepository implements Repository
Pair<Task, Flag> taskFlagPair = taskFlagPairOptional.get(); Pair<Task, Flag> taskFlagPair = taskFlagPairOptional.get();
Task task = taskFlagPair.getKey(); Task task = taskFlagPair.getKey();
Flag flag = taskFlagPair.getValue();
Optional<Team> team = teamsRepository.getTeamByUser(user); Optional<Team> team = teamsRepository.getTeamByUser(user);
if (team.isPresent()) { if (team.isPresent()) {
solutionsRepository.add(new Solution(team.get(), task, flag)); //todo //todo: combine ifs
return true; Solution solution = new Solution(team.get(), task, hashValue); //todo: conflict, solution stores task flag, should store hashValue!!
if (!isAlreadySubmittedSolution(solution)) {
solutionsRepository.add(solution);
return true;
}
} }
return false; return false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment