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();
|
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();
|
||||||
|
|
|
@ -205,14 +205,16 @@ public class DatabaseRepository {
|
||||||
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();
|
||||||
}
|
}
|
||||||
|
@ -225,7 +227,8 @@ public class DatabaseRepository {
|
||||||
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())
|
||||||
|
return makeCompletion(user, puzzle, result);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -571,22 +574,20 @@ 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);
|
||||||
|
@ -594,12 +595,12 @@ public class DatabaseRepository {
|
||||||
stmt.setObject(3, puzzle);
|
stmt.setObject(3, puzzle);
|
||||||
|
|
||||||
ResultSet result = stmt.executeQuery();
|
ResultSet result = stmt.executeQuery();
|
||||||
if(result.next()) return makeGroup(result);
|
if (result.next())
|
||||||
}catch(Exception e){}
|
return makeGroup(result);
|
||||||
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());
|
||||||
|
@ -607,35 +608,35 @@ public class DatabaseRepository {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 {
|
||||||
|
this.repo.getGroupId(newGroup);
|
||||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
return;
|
return;
|
||||||
}
|
} catch (NullPointerException e) {
|
||||||
|
// if group not exist create it
|
||||||
if (this.repo.insertGroup(group, user)) {
|
if (this.repo.insertGroup(newGroup, user)) {
|
||||||
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 200, "Access-Control-Allow-Origin: *");
|
||||||
|
|
||||||
locker.setValue(group);
|
locker.setValue(newGroup);
|
||||||
} else {
|
} else {
|
||||||
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
HttpUtil.responseHeaders(writer, 403, "Access-Control-Allow-Origin: *");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Add table
Reference in a new issue