Be sure you cannot create two same groups or join two groups at the same event - TO BE TESTED

This commit is contained in:
Francois G 2023-04-20 13:35:25 +02:00
parent 91ded5efdd
commit 7f41864406
4 changed files with 74 additions and 68 deletions

View file

@ -21,7 +21,7 @@ public class Group implements Comparable<Group> {
this.linkToPuzzle = ((Number) json.get("puzzle")).intValue(); 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.name = name;
this.linkToChapter = initChap; this.linkToChapter = initChap;
this.linkToPuzzle = initPuzz; this.linkToPuzzle = initPuzz;
@ -84,9 +84,9 @@ public class Group implements Comparable<Group> {
groupJSON.put("name", name); groupJSON.put("name", name);
if (rank != null) if (rank != null)
groupJSON.put("rank", rank); groupJSON.put("rank", rank);
else if (linkToChapter > 0) else if (linkToChapter != null)
groupJSON.put("chapter", linkToChapter); groupJSON.put("chapter", linkToChapter);
else if (linkToPuzzle > 0) else if (linkToPuzzle != null)
groupJSON.put("puzzle", linkToPuzzle); groupJSON.put("puzzle", linkToPuzzle);
if (players != null) { if (players != null) {
JSONArray groupsPlayerJSON = new JSONArray(); JSONArray groupsPlayerJSON = new JSONArray();

View file

@ -31,7 +31,7 @@ public class DatabaseRepository {
public DatabaseRepository(Configuration config) { public DatabaseRepository(Configuration config) {
this.config = config; this.config = config;
} }
// testTrigger(); // testTrigger();
// } // }
// //
@ -94,7 +94,7 @@ public class DatabaseRepository {
// System.out.println("[LOG] "+result.getString("message")); // System.out.println("[LOG] "+result.getString("message"));
// } // }
// } // }
private void ensureConnection() throws SQLException { private void ensureConnection() throws SQLException {
if (con == null || (!con.isValid(5))) { if (con == null || (!con.isValid(5))) {
this.con = DriverManager.getConnection( this.con = DriverManager.getConnection(
@ -198,40 +198,43 @@ public class DatabaseRepository {
return null; return null;
} }
public int getScore(int user, int puzzle){ public int getScore(int user, int puzzle) {
try { try {
ensureConnection(); ensureConnection();
PreparedStatement stmt = DatabaseQuery.SCORE_GROUP.prepare(this.con); PreparedStatement stmt = DatabaseQuery.SCORE_GROUP.prepare(this.con);
stmt.setInt(1, user); stmt.setInt(1, user);
stmt.setInt(2, puzzle); stmt.setInt(2, puzzle);
ResultSet result = stmt.executeQuery(); 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 = DatabaseQuery.SCORE.prepare(this.con);
stmt.setInt(1, user); stmt.setInt(1, user);
stmt.setInt(2, puzzle); stmt.setInt(2, puzzle);
result = stmt.executeQuery(); result = stmt.executeQuery();
if(result.next()) return result.getInt("score"); if (result.next())
return result.getInt("score");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return -1; return -1;
} }
public Completion getCompletionGroup(int user, int puzzle){ public Completion getCompletionGroup(int user, int puzzle) {
try { try {
PreparedStatement stmt = DatabaseQuery.GET_COMPLETION_GROUP.prepare(this.con); PreparedStatement stmt = DatabaseQuery.GET_COMPLETION_GROUP.prepare(this.con);
stmt.setInt(1, user); stmt.setInt(1, user);
stmt.setInt(2, puzzle); stmt.setInt(2, puzzle);
ResultSet result = stmt.executeQuery(); ResultSet result = stmt.executeQuery();
if (result.next()) return makeCompletion(user, puzzle, result); if (result.next())
} catch (SQLException e){ return makeCompletion(user, puzzle, result);
} catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return getCompletion(user, puzzle); return getCompletion(user, puzzle);
} }
public Completion getCompletion(int playerId, int puzzleId) { public Completion getCompletion(int playerId, int puzzleId) {
try { try {
PreparedStatement completionsStmt = DatabaseQuery.GET_COMPLETION.prepare(this.con); PreparedStatement completionsStmt = DatabaseQuery.GET_COMPLETION.prepare(this.con);
@ -493,7 +496,7 @@ public class DatabaseRepository {
ResultSet inserted = playerStatement.getGeneratedKeys(); ResultSet inserted = playerStatement.getGeneratedKeys();
if (inserted.next()) { if (inserted.next()) {
int newPlayerId = inserted.getInt(1); int newPlayerId = inserted.getInt(1);
if(!sgroup.isEmpty()){ if (!sgroup.isEmpty()) {
try (PreparedStatement containsGroupsStatement = con try (PreparedStatement containsGroupsStatement = con
.prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) { .prepareStatement(DatabaseQuery.REGISTER_PLAYER_IN_EXISTING_GROUP.toString())) {
containsGroupsStatement.setInt(1, newPlayerId); containsGroupsStatement.setInt(1, newPlayerId);
@ -571,74 +574,72 @@ public class DatabaseRepository {
statement.executeUpdate(); statement.executeUpdate();
} }
public boolean insertGroup(Group group, User creator){ public boolean insertGroup(Group group, User creator) throws SQLException {
try { Integer groupId = getGroupId(group);
if (groupId == null)
ensureConnection(); ensureConnection();
PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con); PreparedStatement statement = DatabaseQuery.INSERT_GROUP.prepare(this.con);
statement.setString(1, group.getName()); statement.setString(1, group.getName());
statement.setObject(2, group.getLinkToChapter()); statement.setObject(2, group.getLinkToChapter());
statement.setObject(3, group.getLinkToPuzzle()); statement.setObject(3, group.getLinkToPuzzle());
if(statement.executeUpdate() >= 0) return insertUserInGroup(group, creator); if (statement.executeUpdate() >= 0)
} catch (Exception e){ return insertUserInGroup(group, creator);
e.printStackTrace();
}
return false; return false;
} }
public Group getPlayerGroup(int user, Integer chapter, Integer puzzle){ public Group getPlayerGroup(int user, Integer chapter, Integer puzzle) throws SQLException {
try { ensureConnection();
ensureConnection(); PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con);
PreparedStatement stmt = DatabaseQuery.GET_GROUP_FOR_PLAYER.prepare(this.con); stmt.setInt(1, user);
stmt.setInt(1, user); stmt.setObject(2, chapter);
stmt.setObject(2, chapter); stmt.setObject(3, puzzle);
stmt.setObject(3, puzzle);
ResultSet result = stmt.executeQuery();
ResultSet result = stmt.executeQuery(); if (result.next())
if(result.next()) return makeGroup(result); return makeGroup(result);
}catch(Exception e){}
return null; return null;
} }
private int getGroupId(Group group) throws Exception{ public Integer getGroupId(Group group) throws SQLException {
ensureConnection(); ensureConnection();
PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con); PreparedStatement stmt = DatabaseQuery.GET_GROUP_ID_BY_DATA.prepare(this.con);
stmt.setString(1, group.getName()); stmt.setString(1, group.getName());
stmt.setObject(2, group.getLinkToChapter()); stmt.setObject(2, group.getLinkToChapter());
stmt.setObject(3, group.getLinkToPuzzle()); stmt.setObject(3, group.getLinkToPuzzle());
ResultSet result = stmt.executeQuery(); ResultSet result = stmt.executeQuery();
if(result.next()) return result.getInt("id_group"); if (result.next())
throw new NullPointerException(); return result.getInt("id_group");
throw null;
} }
public boolean insertUserInGroup(Group group, User user){ public boolean insertUserInGroup(Group group, User user) throws SQLException {
try { Integer id = getGroupId(group);
int 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); PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
stmt.setInt(1, user.getId()); stmt.setInt(1, user.getId());
stmt.setInt(2, id); stmt.setInt(2, id);
return stmt.executeUpdate() >= 0; return stmt.executeUpdate() >= 0;
}catch(Exception e){
e.printStackTrace();
} }
return false; return false;
} }
public boolean leaveGroup(Group group, User user){ public boolean leaveGroup(Group group, User user) throws SQLException {
try { Integer id = getGroupId(group);
int id = getGroupId(group); if (id != null) {
PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con); PreparedStatement stmt = DatabaseQuery.LEAVE_GROUP.prepare(this.con);
stmt.setInt(1, user.getId()); stmt.setInt(1, user.getId());
stmt.setInt(2, id); stmt.setInt(2, id);
return stmt.executeUpdate() >= 0; return stmt.executeUpdate() >= 0;
}catch(Exception e){} }
return false; return false;
} }
private void updateCompletion(Completion completionToUpdate) throws SQLException { private void updateCompletion(Completion completionToUpdate) throws SQLException {
PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con); PreparedStatement statement = DatabaseQuery.UPDATE_COMPLETION.prepare(this.con);
statement.setInt(1, completionToUpdate.getTries()); statement.setInt(1, completionToUpdate.getTries());

View file

@ -28,26 +28,31 @@ public class GroupCreate implements Response {
this.locker = locker; 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") @RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dans le groupe / ne peux pas le rejoindre")
@Route(path = "^\\/groupCreate$", type = POST, needLogin = true) @Route(path = "^\\/groupCreate$", type = POST, needLogin = true)
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception{ 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 (this.repo.getPlayerGroup(user.getId(), newGroup.getLinkToChapter(), newGroup.getLinkToPuzzle()) == null) {
if(group.equals(userGroup)){ try {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); this.repo.getGroupId(newGroup);
return; 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: *");
}
}
} }
else {
if (this.repo.insertGroup(group, user)) {
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
locker.setValue(group);
} else {
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *"); HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
} }
} }
} }

View file

@ -24,7 +24,7 @@ public class GroupJoin implements Response{
this.repo = repo; 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") @RouteDoc(responseCode = 403, responseDescription = "L'utilisateur est déjà dedans / ne peux pas le rejoindre")
@Route(path = "^\\/groupJoin$", type = POST, needLogin = true) @Route(path = "^\\/groupJoin$", type = POST, needLogin = true)