Search in sources :

Example 6 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class PlayerInfo method getTotalValue.

/**
 * Get the sum of all values in the 'value' column of each row that meets the given RowRequirements.
 * See the {@link RowRequirement} class for more info about requirements and some examples.
 * @param statType Type of statistics to get data for.
 * @param reqs A list of requirements that need to be met before adding the value to the sum.
 * @return the sum of the values in the rows that meet the given requirement or 0 if results were invalid or non-existent.
 */
public double getTotalValue(PlayerStat statType, RowRequirement... reqs) {
    // Check if we have any requirements - if not, just return double value.
    if (reqs == null || reqs.length == 0) {
        return this.getTotalValue(statType);
    }
    double value = 0;
    List<Query> rows = this.getDataOfPlayerStat(statType);
    if (rows.isEmpty() || !this.isValid())
        return value;
    for (Query row : rows) {
        boolean isValid = true;
        for (RowRequirement req : reqs) {
            // Check if each condition that was given is true.
            if (row.getValue(req.getColumnName()) == null || !row.getValue(req.getColumnName()).toString().equalsIgnoreCase(req.getColumnValue())) {
                isValid = false;
                break;
            }
        }
        // All conditions were met, so we add this value.
        if (isValid) {
            value += row.getDoubleValue("value");
        }
    }
    return value;
}
Also used : Query(me.staartvin.statz.database.datatype.Query) RowRequirement(me.staartvin.statz.database.datatype.RowRequirement)

Example 7 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class StatzUtil method makeQuery.

/**
 * Create a query to retrieve or send data from or to the database.
 * <br><br>To create a query, provide strings as data points.
 * <br>For example, to retrieve the number of cows a player has killed, use this method like so:
 * <br><br><code>makeQuery("uuid", "uuidOfPlayerHere", "mob", "COW")</code>
 *
 * @param strings an array of strings that represents keys and values.
 * @return a {@link me.staartvin.statz.database.datatype.Query} object that represents a query to the database.
 */
public static Query makeQuery(final Object... strings) {
    final LinkedHashMap<String, String> queries = new LinkedHashMap<>();
    for (int i = 0; i < strings.length; i += 2) {
        queries.put(strings[i].toString(), strings[i + 1].toString());
    }
    Query query = new Query(queries);
    return query;
}
Also used : Query(me.staartvin.statz.database.datatype.Query)

Example 8 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class PlayerInfoTest method testResolvePlayerInfo.

// Test two conflicting queries
@Test
public void testResolvePlayerInfo() {
    UUID uuid = UUID.fromString("3657b9cc-2518-4265-ad69-323e11286ce2");
    PlayerStat statType = PlayerStat.ARROWS_SHOT;
    PlayerInfo playerInfo = new PlayerInfo(uuid);
    PlayerInfo playerInfo2 = new PlayerInfo(uuid);
    Query queryA = StatzUtil.makeQuery("UUID", uuid, "World", "worldName1", "value", 5);
    Query queryB = StatzUtil.makeQuery("UUID", uuid, "World", "worldName1", "value", 6);
    playerInfo.addRow(statType, queryA);
    playerInfo2.addRow(statType, queryB);
    PlayerInfo nonConflictingPlayerInfo = playerInfo.resolveConflicts(playerInfo2);
    List<Query> nonConflictingQueries = nonConflictingPlayerInfo.getDataOfPlayerStat(statType);
    // Verify that size is correct
    Assert.assertEquals(1, nonConflictingQueries.size());
    // Verify that there is only 1 row
    Assert.assertEquals(1, nonConflictingPlayerInfo.getNumberOfRows(statType));
    // Verify that there is only 1 statistic stored
    Assert.assertEquals(1, nonConflictingPlayerInfo.getNumberOfStatistics());
    // Verify that value is correct
    Assert.assertEquals(11, nonConflictingPlayerInfo.getTotalValue(statType), 0);
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) UUID(java.util.UUID) Test(org.junit.Test)

Example 9 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class PlayerInfoTest method testResolvePlayerInfo4.

// Test two conflicting (of same statType) and two non conflicting (of another statType, that conflict with each
// other).
@Test
public void testResolvePlayerInfo4() {
    UUID uuid = UUID.fromString("3657b9cc-2518-4265-ad69-323e11286ce2");
    PlayerStat statType = PlayerStat.ARROWS_SHOT;
    PlayerStat statType2 = PlayerStat.KILLS_MOBS;
    PlayerInfo playerInfo = new PlayerInfo(uuid);
    PlayerInfo playerInfo2 = new PlayerInfo(uuid);
    Query queryA = StatzUtil.makeQuery("UUID", uuid, "World", "worldName1", "value", 5);
    Query queryB = StatzUtil.makeQuery("UUID", uuid, "World", "worldName1", "value", 6);
    Query queryC = StatzUtil.makeQuery("UUID", uuid, "World", "worldName2", "value", 8);
    Query queryD = StatzUtil.makeQuery("UUID", uuid, "World", "worldName2", "value", 45);
    playerInfo.addRow(statType, queryA);
    playerInfo2.addRow(statType, queryB);
    playerInfo2.addRow(statType2, queryC);
    playerInfo.addRow(statType2, queryD);
    PlayerInfo nonConflictingPlayerInfo = playerInfo.resolveConflicts(playerInfo2);
    // Verify the number of rows
    Assert.assertEquals(1, nonConflictingPlayerInfo.getNumberOfRows(statType));
    Assert.assertEquals(1, nonConflictingPlayerInfo.getNumberOfRows(statType2));
    // Verify the number of statistics stored.
    Assert.assertEquals(2, nonConflictingPlayerInfo.getNumberOfStatistics());
    // Verify that value is correct
    Assert.assertEquals(11, nonConflictingPlayerInfo.getTotalValue(statType), 0);
    Assert.assertEquals(53, nonConflictingPlayerInfo.getTotalValue(statType2), 0);
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) UUID(java.util.UUID) Test(org.junit.Test)

Example 10 with Query

use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.

the class PlayerInfoTest method testResolvePlayerInfo3.

// Test two conflicting (of same statType) and one non conflicting (of another statType).
@Test
public void testResolvePlayerInfo3() {
    UUID uuid = UUID.fromString("3657b9cc-2518-4265-ad69-323e11286ce2");
    PlayerStat statType = PlayerStat.ARROWS_SHOT;
    PlayerStat statType2 = PlayerStat.KILLS_MOBS;
    PlayerInfo playerInfo = new PlayerInfo(uuid);
    PlayerInfo playerInfo2 = new PlayerInfo(uuid);
    Query queryA = StatzUtil.makeQuery("UUID", uuid, "World", "worldName1", "value", 5);
    Query queryB = StatzUtil.makeQuery("UUID", uuid, "World", "worldName1", "value", 6);
    Query queryC = StatzUtil.makeQuery("UUID", uuid, "World", "worldName2", "value", 8);
    playerInfo.addRow(statType, queryA);
    playerInfo2.addRow(statType, queryB);
    playerInfo2.addRow(statType2, queryC);
    PlayerInfo nonConflictingPlayerInfo = playerInfo.resolveConflicts(playerInfo2);
    // Verify the number of rows
    Assert.assertEquals(1, nonConflictingPlayerInfo.getNumberOfRows(statType));
    Assert.assertEquals(1, nonConflictingPlayerInfo.getNumberOfRows(statType2));
    // Verify the number of statistics stored.
    Assert.assertEquals(2, nonConflictingPlayerInfo.getNumberOfStatistics());
    // Verify that value is correct
    Assert.assertEquals(11, nonConflictingPlayerInfo.getTotalValue(statType), 0);
    Assert.assertEquals(8, nonConflictingPlayerInfo.getTotalValue(statType2), 0);
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

Query (me.staartvin.statz.database.datatype.Query)44 Test (org.junit.Test)25 PlayerInfo (me.staartvin.statz.datamanager.player.PlayerInfo)13 PlayerStat (me.staartvin.statz.datamanager.player.PlayerStat)11 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)6 RowRequirement (me.staartvin.statz.database.datatype.RowRequirement)4 HashMap (java.util.HashMap)2 Material (org.bukkit.Material)2 Player (org.bukkit.entity.Player)2 Inventory (org.bukkit.inventory.Inventory)2 ItemStack (org.bukkit.inventory.ItemStack)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1