Be sure you cannot create two same groups or join two groups at the same event - TO BE TESTED
This commit is contained in:
parent
91ded5efdd
commit
7f41864406
4 changed files with 74 additions and 68 deletions
|
@ -21,7 +21,7 @@ public class Group implements Comparable<Group> {
|
|||
this.linkToPuzzle = ((Number) json.get("puzzle")).intValue();
|
||||
}
|
||||
|
||||
public Group(String name, int initChap, int initPuzz) {
|
||||
public Group(String name, Integer initChap, Integer initPuzz) {
|
||||
this.name = name;
|
||||
this.linkToChapter = initChap;
|
||||
this.linkToPuzzle = initPuzz;
|
||||
|
@ -84,9 +84,9 @@ public class Group implements Comparable<Group> {
|
|||
groupJSON.put("name", name);
|
||||
if (rank != null)
|
||||
groupJSON.put("rank", rank);
|
||||
else if (linkToChapter > 0)
|
||||
else if (linkToChapter != null)
|
||||
groupJSON.put("chapter", linkToChapter);
|
||||
else if (linkToPuzzle > 0)
|
||||
else if (linkToPuzzle != null)
|
||||
groupJSON.put("puzzle", linkToPuzzle);
|
||||
if (players != null) {
|
||||
JSONArray groupsPlayerJSON = new JSONArray();
|
||||
|
|
|
@ -31,7 +31,7 @@ public class DatabaseRepository {
|
|||
|
||||
public DatabaseRepository(Configuration config) {
|
||||
this.config = config;
|
||||
}
|
||||
}
|
||||
// testTrigger();
|
||||
// }
|
||||
//
|
||||
|
@ -94,7 +94,7 @@ public class DatabaseRepository {
|
|||
// System.out.println("[LOG] "+result.getString("message"));
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
private void ensureConnection() throws SQLException {
|
||||
if (con == null || (!con.isValid(5))) {
|
||||
this.con = DriverManager.getConnection(
|
||||
|
@ -198,40 +198,43 @@ public class DatabaseRepository {
|
|||
return null;
|
||||
}
|
||||
|
||||
public int getScore(int user, int puzzle){
|
||||
public int getScore(int user, int puzzle) {
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.SCORE_GROUP.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, puzzle);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if(result.next()) return result.getInt("score");
|
||||
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
|
||||
stmt = DatabaseQuery.SCORE.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, puzzle);
|
||||
|
||||
result = stmt.executeQuery();
|
||||
if(result.next()) return result.getInt("score");
|
||||
if (result.next())
|
||||
return result.getInt("score");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public Completion getCompletionGroup(int user, int puzzle){
|
||||
|
||||
public Completion getCompletionGroup(int user, int puzzle) {
|
||||
try {
|
||||
PreparedStatement stmt = DatabaseQuery.GET_COMPLETION_GROUP.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setInt(2, puzzle);
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next()) return makeCompletion(user, puzzle, result);
|
||||
} catch (SQLException e){
|
||||
if (result.next())
|
||||
return makeCompletion(user, puzzle, result);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return getCompletion(user, puzzle);
|
||||
}
|
||||
|
||||
|
||||
public Completion getCompletion(int playerId, int puzzleId) {
|
||||
try {
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_COMPLETION.prepare(this.con);
|
||||
|
@ -493,7 +496,7 @@ public class DatabaseRepository {
|
|||
ResultSet inserted = playerStatement.getGeneratedKeys();
|
||||
if (inserted.next()) {
|
||||
int newPlayerId = inserted.getInt(1);
|
||||
if(!sgroup.isEmpty()){
|
||||
if (!sgroup.isEmpty()) {
|
||||
try (PreparedStatement containsGroupsStatement = con
|
||||
.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
|
||||
containsGroupsStatement.setInt(1, newPlayerId);
|
||||
|
@ -571,74 +574,72 @@ public class DatabaseRepository {
|
|||
statement.executeUpdate();
|
||||
}
|
||||
|
||||
public boolean insertGroup(Group group, User creator){
|
||||
try {
|
||||
public boolean insertGroup(Group group, User creator) throws SQLException {
|
||||
Integer groupId = getGroupId(group);
|
||||
if (groupId == null)
|
||||
ensureConnection();
|
||||
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
|
||||
statement.setString(1, group.getName());
|
||||
statement.setObject(2, group.getLinkToChapter());
|
||||
statement.setObject(3, group.getLinkToPuzzle());
|
||||
if(statement.executeUpdate() >= 0) return insertUserInGroup(group, creator);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (statement.executeUpdate() >= 0)
|
||||
return insertUserInGroup(group, creator);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Group getPlayerGroup(int user, Integer chapter, Integer puzzle){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setObject(2, chapter);
|
||||
stmt.setObject(3, puzzle);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if(result.next()) return makeGroup(result);
|
||||
}catch(Exception e){}
|
||||
|
||||
public Group getPlayerGroup(int user, Integer chapter, Integer puzzle) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con);
|
||||
stmt.setInt(1, user);
|
||||
stmt.setObject(2, chapter);
|
||||
stmt.setObject(3, puzzle);
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if (result.next())
|
||||
return makeGroup(result);
|
||||
return null;
|
||||
}
|
||||
|
||||
private int getGroupId(Group group) throws Exception{
|
||||
|
||||
public Integer getGroupId(Group group) throws SQLException {
|
||||
ensureConnection();
|
||||
PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con);
|
||||
stmt.setString(1, group.getName());
|
||||
stmt.setObject(2, group.getLinkToChapter());
|
||||
stmt.setObject(3, group.getLinkToPuzzle());
|
||||
|
||||
|
||||
ResultSet result = stmt.executeQuery();
|
||||
if(result.next()) return result.getInt("id_group");
|
||||
throw new NullPointerException();
|
||||
if (result.next())
|
||||
return result.getInt("id_group");
|
||||
throw null;
|
||||
}
|
||||
|
||||
public boolean insertUserInGroup(Group group, User user){
|
||||
try {
|
||||
int id = getGroupId(group);
|
||||
|
||||
public boolean insertUserInGroup(Group group, User user) throws SQLException {
|
||||
Integer id = getGroupId(group);
|
||||
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter(), group.getLinkToPuzzle());
|
||||
if (id != null && alreadyInGroup == null) {
|
||||
PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
|
||||
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean leaveGroup(Group group, User user){
|
||||
try {
|
||||
int id = getGroupId(group);
|
||||
|
||||
public boolean leaveGroup(Group group, User user) throws SQLException {
|
||||
Integer id = getGroupId(group);
|
||||
if (id != null) {
|
||||
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);
|
||||
|
||||
|
||||
stmt.setInt(1, user.getId());
|
||||
stmt.setInt(2, id);
|
||||
|
||||
|
||||
return stmt.executeUpdate() >= 0;
|
||||
}catch(Exception e){}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private void updateCompletion(Completion completionToUpdate) throws SQLException {
|
||||
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
|
||||
statement.setInt(1, completionToUpdate.getTries());
|
||||
|
|
|
@ -28,26 +28,31 @@ public class GroupCreate implements Response {
|
|||
this.locker = locker;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe à été crée")
|
||||
@RouteDoc(path = "/groupCreate", responseCode = 200, responseDescription = "Le groupe a été créé")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dans le groupe / ne peux pas le rejoindre")
|
||||
|
||||
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{
|
||||
Group group = new Group((JSONObject) HttpUtil.readJson(reader));
|
||||
Group newGroup = new Group((JSONObject) HttpUtil.readJson(reader));
|
||||
|
||||
Group userGroup = this.repo.getPlayerGroup(user.getId(), group.getLinkToChapter(), group.getLinkToPuzzle());
|
||||
if(group.equals(userGroup)){
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
if (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter(), newGroup.getLinkToPuzzle()) == null) {
|
||||
try {
|
||||
this.repo.getGroupId(newGroup);
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
return;
|
||||
} catch (NullPointerException e) {
|
||||
// if group not exist create it
|
||||
if (this.repo.insertGroup(newGroup, user)) {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
|
||||
locker.setValue(newGroup);
|
||||
} else {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.repo.insertGroup(group, user)) {
|
||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||
|
||||
locker.setValue(group);
|
||||
} else {
|
||||
else {
|
||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class GroupJoin implements Response{
|
|||
this.repo = repo;
|
||||
}
|
||||
|
||||
@RouteDoc(path = "/groupJoin", responseCode = 200, responseDescription = "L'utilisateur à rejoind le groupe")
|
||||
@RouteDoc(path = "/groupJoin", responseCode = 200, responseDescription = "L'utilisateur a rejoint le groupe")
|
||||
@RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dedans / ne peux pas le rejoindre")
|
||||
|
||||
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true)
|
||||
|
|
Loading…
Add table
Reference in a new issue