1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-05 23:22:47 +00:00

Improves character count checks

This commit is contained in:
2011-12-29 16:04:39 -02:00
parent 1f2b700311
commit 83bf11e1c1
7 changed files with 50 additions and 13 deletions

View File

@@ -1069,7 +1069,7 @@ public abstract class AbstractOrientDatabaseService extends
@Override @Override
public boolean filter(OQueryContextNative record) { public boolean filter(OQueryContextNative record) {
record = query(record); record = query(record, entity);
if (record == null) if (record == null)
return true; return true;
return record.go(); return record.go();
@@ -1084,8 +1084,11 @@ public abstract class AbstractOrientDatabaseService extends
* *
* @param record * @param record
* the document record * the document record
* @param e
* the entity
* @return the record instance or <code>null</code> * @return the record instance or <code>null</code>
*/ */
protected abstract OQueryContextNative query(OQueryContextNative record); protected abstract OQueryContextNative query(
OQueryContextNative record, E e);
} }
} }

View File

@@ -1094,7 +1094,7 @@ public abstract class AbstractSQLDatabaseService extends
SQLQueryFactory<? extends AbstractSQLQuery<?>, ?, ?, ?, ?, ?> factory, SQLQueryFactory<? extends AbstractSQLQuery<?>, ?, ?, ?, ?, ?> factory,
DatabaseService database) { DatabaseService database) {
final AbstractSQLQuery<?> count = factory.query().from(entity); final AbstractSQLQuery<?> count = factory.query().from(entity);
query(count); query(count, entity);
return (int) count.count(); return (int) count.count();
} }
@@ -1103,7 +1103,8 @@ public abstract class AbstractSQLDatabaseService extends
* *
* @param q * @param q
* the query clause * the query clause
* @param e the entity
*/ */
protected abstract void query(AbstractSQLQuery<?> q); protected abstract void query(AbstractSQLQuery<?> q, E e);
} }
} }

View File

@@ -58,4 +58,13 @@ public interface CharacterDAO extends
* characters. * characters.
*/ */
List<L2Character> selectByAccount(AccountID account); List<L2Character> selectByAccount(AccountID account);
/**
* Counts the amount of characters for an given <code>account</code>.
*
* @param account
* the account id
* @return the number of characters for that <code>account</code>
*/
int countByAccount(AccountID account);
} }

View File

@@ -29,6 +29,7 @@ import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.mapper.CharacterMapper; import com.l2jserver.service.database.mapper.CharacterMapper;
import com.l2jserver.service.database.model.QCharacter; import com.l2jserver.service.database.model.QCharacter;
import com.l2jserver.service.database.orientdb.AbstractOrientDBDAO; import com.l2jserver.service.database.orientdb.AbstractOrientDBDAO;
import com.l2jserver.service.database.orientdb.AbstractOrientDatabaseService.CountQuery;
import com.l2jserver.service.database.orientdb.AbstractOrientDatabaseService.DeleteQuery; import com.l2jserver.service.database.orientdb.AbstractOrientDatabaseService.DeleteQuery;
import com.l2jserver.service.database.orientdb.AbstractOrientDatabaseService.InsertQuery; import com.l2jserver.service.database.orientdb.AbstractOrientDatabaseService.InsertQuery;
import com.l2jserver.service.database.orientdb.AbstractOrientDatabaseService.SelectListQuery; import com.l2jserver.service.database.orientdb.AbstractOrientDatabaseService.SelectListQuery;
@@ -117,6 +118,17 @@ public class OrientDBCharacterDAO extends
}); });
} }
@Override
public int countByAccount(final AccountID account) {
return database.query(new CountQuery<QCharacter>(QCharacter.character) {
@Override
protected OQueryContextNative query(OQueryContextNative record,
QCharacter e) {
return record.field(name(e.accountId)).eq(account.getID());
}
});
}
@Override @Override
public List<CharacterID> selectIDs() { public List<CharacterID> selectIDs() {
return database return database
@@ -145,7 +157,8 @@ public class OrientDBCharacterDAO extends
@Override @Override
protected OQueryContextNative query(OQueryContextNative record, protected OQueryContextNative query(OQueryContextNative record,
L2Character o) { L2Character o) {
return record.field(name(entity.characterId)).eq(o.getID().getID()); return record.field(name(entity.characterId)).eq(
o.getID().getID());
} }
}); });
} }
@@ -157,7 +170,8 @@ public class OrientDBCharacterDAO extends
@Override @Override
protected OQueryContextNative query(OQueryContextNative record, protected OQueryContextNative query(OQueryContextNative record,
L2Character o) { L2Character o) {
return record.field(name(entity.characterId)).eq(o.getID().getID()); return record.field(name(entity.characterId)).eq(
o.getID().getID());
} }
}); });
} }

View File

@@ -29,6 +29,7 @@ import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.database.mapper.CharacterMapper; import com.l2jserver.service.database.mapper.CharacterMapper;
import com.l2jserver.service.database.model.QCharacter; import com.l2jserver.service.database.model.QCharacter;
import com.l2jserver.service.database.sql.AbstractSQLDAO; import com.l2jserver.service.database.sql.AbstractSQLDAO;
import com.l2jserver.service.database.sql.AbstractSQLDatabaseService.CountQuery;
import com.l2jserver.service.database.sql.AbstractSQLDatabaseService.DeleteQuery; import com.l2jserver.service.database.sql.AbstractSQLDatabaseService.DeleteQuery;
import com.l2jserver.service.database.sql.AbstractSQLDatabaseService.InsertQuery; import com.l2jserver.service.database.sql.AbstractSQLDatabaseService.InsertQuery;
import com.l2jserver.service.database.sql.AbstractSQLDatabaseService.SelectListQuery; import com.l2jserver.service.database.sql.AbstractSQLDatabaseService.SelectListQuery;
@@ -113,6 +114,16 @@ public class SQLCharacterDAO extends AbstractSQLDAO<L2Character, CharacterID>
}); });
} }
@Override
public int countByAccount(final AccountID account) {
return database.query(new CountQuery<QCharacter>(QCharacter.character) {
@Override
protected void query(AbstractSQLQuery<?> q, QCharacter e) {
q.where(e.accountId.eq(account.getID()));
}
});
}
@Override @Override
public List<CharacterID> selectIDs() { public List<CharacterID> selectIDs() {
return database return database

View File

@@ -161,10 +161,10 @@ public class CharacterServiceImpl extends
*/ */
@Inject @Inject
public CharacterServiceImpl(BroadcastService broadcastService, public CharacterServiceImpl(BroadcastService broadcastService,
WorldEventDispatcherService eventDispatcher, SpawnService spawnService, WorldEventDispatcherService eventDispatcher,
NPCService npcService, GameGuardService ggService, SpawnService spawnService, NPCService npcService,
CharacterDAO characterDao, ItemDAO itemDao, GameGuardService ggService, CharacterDAO characterDao,
CharacterShortcutDAO shortcutDao, ItemDAO itemDao, CharacterShortcutDAO shortcutDao,
CharacterTemplateIDProvider charTemplateIdProvider, CharacterTemplateIDProvider charTemplateIdProvider,
CharacterIDProvider charIdProvider) { CharacterIDProvider charIdProvider) {
super(CharacterServiceConfiguration.class); super(CharacterServiceConfiguration.class);
@@ -184,7 +184,7 @@ public class CharacterServiceImpl extends
public boolean canCreate(AccountID accountID) { public boolean canCreate(AccountID accountID) {
if (!config.isCharacterCreationAllowed()) if (!config.isCharacterCreationAllowed())
return false; return false;
return characterDao.selectByAccount(accountID).size() < config return characterDao.countByAccount(accountID) < config
.getMaxCharactersPerAccount(); .getMaxCharactersPerAccount();
} }
@@ -199,7 +199,7 @@ public class CharacterServiceImpl extends
CharacterInvalidSexException, TooManyCharactersException { CharacterInvalidSexException, TooManyCharactersException {
if (!config.isCharacterCreationAllowed()) if (!config.isCharacterCreationAllowed())
throw new CharacteCreationNotAllowedException(); throw new CharacteCreationNotAllowedException();
if(characterDao.selectByAccount(accountID).size() < config if (characterDao.countByAccount(accountID) < config
.getMaxCharactersPerAccount()) .getMaxCharactersPerAccount())
throw new TooManyCharactersException(); throw new TooManyCharactersException();

View File

@@ -47,7 +47,6 @@ import com.l2jserver.util.factory.CollectionFactory;
import com.sun.org.apache.xml.internal.serialize.OutputFormat; import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer; import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
@SuppressWarnings("restriction")
public class SkillTemplateConverter { public class SkillTemplateConverter {
private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy"; private static final String JDBC_URL = "jdbc:mysql://localhost/l2jlegacy";
private static final String JDBC_USERNAME = "l2j"; private static final String JDBC_USERNAME = "l2j";