Limit group size

This commit is contained in:
jeffcheasey88 2024-03-29 15:26:42 +01:00
parent f722a4db22
commit f23904d860
2 changed files with 16 additions and 1 deletions

View file

@ -24,6 +24,7 @@ public enum DatabaseQuery {
ALL_GROUPS_BY_CHAPTER("SELECT * FROM groups WHERE fk_chapter = ?"), ALL_GROUPS_BY_CHAPTER("SELECT * FROM groups WHERE fk_chapter = ?"),
GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ? GET_GROUP_FOR_PLAYER("SELECT g.* FROM groups g JOIN containsGroups cg ON cg.fk_group = g.id_group WHERE cg.fk_player = ? AND g.fk_chapter = ?"), // AND g.fk_puzzle = ?
GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ? GET_GROUP_ID_BY_DATA("SELECT id_group FROM groups WHERE name = ? AND (fk_chapter = ?)"), // OR fk_puzzle = ?
GET_GROUP_USERS_COUNT("SELECT count(*) as howmany FROM containsGroups WHERE fk_group = ?"),
INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"), INSERT_GROUP("INSERT INTO groups (name, fk_chapter) VALUES (?,?)"),
INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"), INSERT_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"), LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"),

View file

@ -677,6 +677,11 @@ public class DatabaseRepository {
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException{ public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException{
Integer id = getGroupId(group); Integer id = getGroupId(group);
if(id != null){
int howmany = numberInGroup(id);
System.out.println("join group, already have "+howmany);
if(howmany > 3) return false;
}
Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter()); Group alreadyInGroup = getPlayerGroup(user.getId(), group.getLinkToChapter());
if (id != null && alreadyInGroup == null) { 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);
@ -689,6 +694,15 @@ public class DatabaseRepository {
return false; return false;
} }
public int numberInGroup(int group) throws SQLException{
PreparedStatement stmt = DatabaseQuery.GET_GROUP_USERS_COUNT.prepare(this.con);
stmt.setInt(1, group);
ResultSet result = stmt.executeQuery();
if(result.next()) return result.getInt("howmany");
return 0;
}
public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException { public boolean leaveGroup(Group group, PeerAtUser user) throws SQLException {
Integer id = getGroupId(group); Integer id = getGroupId(group);
if (id != null) { if (id != null) {