Limit group size
This commit is contained in:
parent
f722a4db22
commit
f23904d860
2 changed files with 16 additions and 1 deletions
|
@ -24,6 +24,7 @@ public enum DatabaseQuery {
|
|||
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_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_PLAYER_IN_GROUP("INSERT INTO containsGroups (fk_player, fk_group) VALUES (?,?)"),
|
||||
LEAVE_GROUP("DELETE FROM containsGroups WHERE fk_player = ? AND fk_group = ?"),
|
||||
|
|
|
@ -677,6 +677,11 @@ public class DatabaseRepository {
|
|||
|
||||
public boolean insertUserInGroup(Group group, PeerAtUser user) throws SQLException{
|
||||
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());
|
||||
if (id != null && alreadyInGroup == null) {
|
||||
PreparedStatement stmt = DatabaseQuery.INSERT_PLAYER_IN_GROUP.prepare(this.con);
|
||||
|
@ -689,6 +694,15 @@ public class DatabaseRepository {
|
|||
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 {
|
||||
Integer id = getGroupId(group);
|
||||
if (id != null) {
|
||||
|
|
Loading…
Add table
Reference in a new issue