Added points to users

This commit is contained in:
stjn
2021-11-08 20:28:25 +01:00
parent 7066c032cf
commit c9987795bc
5 changed files with 102 additions and 30 deletions
@@ -1,7 +1,10 @@
package com.alttd.commands.database;
import com.alttd.objects.EconUser;
import com.alttd.objects.VillagerType;
import com.alttd.util.Logger;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@@ -9,9 +12,11 @@ import java.sql.SQLException;
import java.util.UUID;
public class Queries {
/** NOTE: run async
/**
* NOTE: run async
* Get the points a user has for a villager type
* @param uuid Uuid for the user you want to get the points for
*
* @param uuid Uuid for the user you want to get the points for
* @param villagerType Type of villager you want to get the points for
* @return The amount of points a user has for the given villager type
*/
@@ -36,14 +41,16 @@ public class Queries {
return (points);
}
/** NOTE: run async
/**
* NOTE: run async
* Add a specified amount of points to the user for the given villager type
* @param uuid Uuid for the user you want to add the points to
*
* @param uuid Uuid for the user you want to add the points to
* @param villagerType Type of villager you want to add the points to
* @param points The amount of points to add
* @param points The amount of points to add
* @return success
*/
public static boolean updateUserPoints(UUID uuid, VillagerType villagerType, int points) {
public static boolean updateUserPoints(UUID uuid, String villagerType, int points) {
String sql = "UPDATE Points = SET user_points = user_points + ? " +
"WHERE UUID = ? " +
"AND villager_type = ?;";
@@ -52,27 +59,29 @@ public class Queries {
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
preparedStatement.setInt(1, points);
preparedStatement.setString(2, uuid.toString());
preparedStatement.setString(3, villagerType.getName());
preparedStatement.setString(3, villagerType);
if (preparedStatement.executeUpdate() == 0)
return createUserPointsEntry(uuid, villagerType, points);
} catch (SQLException e) {
e.printStackTrace();
Logger.warning("Unable to add % points to %" +
" for villager type %", String.valueOf(points), uuid.toString(), villagerType.getName());
" for villager type %", String.valueOf(points), uuid.toString(), villagerType);
return (false);
}
return (true);
}
/** NOTE: run async
/**
* NOTE: run async
* Create a new user entry
* @param uuid Uuid for the user you want to create an entry for
*
* @param uuid Uuid for the user you want to create an entry for
* @param villagerType Type of villager you want to use in that entry
* @param points The amount of points to set the start to
* @param points The amount of points to set the start to
* @return success
*/
public static boolean createUserPointsEntry(UUID uuid, VillagerType villagerType, int points) {
public static boolean createUserPointsEntry(UUID uuid, String villagerType, int points) {
String sql = "INSERT INTO Points " +
"(uuid, villager_type, points) " +
"(?, ?, ?)";
@@ -80,15 +89,37 @@ public class Queries {
try {
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
preparedStatement.setString(1, uuid.toString());
preparedStatement.setString(2, villagerType.getName());
preparedStatement.setString(2, villagerType);
preparedStatement.setInt(3, points);
return (preparedStatement.execute());
} catch (SQLException e) {
e.printStackTrace();
Logger.warning("Unable to create point entry for %" +
" for villager type %", uuid.toString(), villagerType.getName());
" for villager type %", uuid.toString(), villagerType);
return (false);
}
return (true);
}
public static EconUser getEconUser(UUID uuid) {
String sql = "SELECT * FROM POINTS WHERE uuid = ?";
try {
PreparedStatement preparedStatement = Database.connection.prepareStatement(sql);
preparedStatement.setString(1, uuid.toString());
ResultSet resultSet = preparedStatement.executeQuery();
Object2ObjectArrayMap<String, Integer> points = new Object2ObjectArrayMap<>();
while (resultSet.next()) {
points.put(
resultSet.getString("villager_type"),
resultSet.getInt("points"));
}
return (new EconUser(uuid, points));
} catch (SQLException e) {
e.printStackTrace();
}
return (null);
}
}