forked from SteamWar/SteamWar
@@ -17,57 +17,63 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.sql;
|
||||
package de.steamwar.sql
|
||||
|
||||
import de.steamwar.sql.internal.Field;
|
||||
import de.steamwar.sql.internal.SelectStatement;
|
||||
import de.steamwar.sql.internal.Statement;
|
||||
import de.steamwar.sql.internal.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import de.steamwar.sql.internal.useDb
|
||||
import org.jetbrains.exposed.v1.core.and
|
||||
import org.jetbrains.exposed.v1.core.dao.id.CompositeID
|
||||
import org.jetbrains.exposed.v1.core.dao.id.CompositeIdTable
|
||||
import org.jetbrains.exposed.v1.core.dao.id.EntityID
|
||||
import org.jetbrains.exposed.v1.core.eq
|
||||
import org.jetbrains.exposed.v1.dao.CompositeEntity
|
||||
import org.jetbrains.exposed.v1.dao.CompositeEntityClass
|
||||
import org.jetbrains.exposed.v1.jdbc.upsert
|
||||
import java.util.UUID
|
||||
|
||||
import java.util.UUID;
|
||||
object UserConfigTable: CompositeIdTable("UserConfig") {
|
||||
val userId = reference("userId", SteamwarUserTable)
|
||||
val key = varchar("Key", 32)
|
||||
val value = text("Value")
|
||||
|
||||
@AllArgsConstructor
|
||||
public class UserConfig {
|
||||
|
||||
private static final Table<UserConfig> table = new Table<>(UserConfig.class);
|
||||
private static final SelectStatement<UserConfig> select = table.select(Table.PRIMARY);
|
||||
private static final Statement insert = table.insertAll();
|
||||
private static final Statement delete = table.delete(Table.PRIMARY);
|
||||
|
||||
@Field(keys = {Table.PRIMARY})
|
||||
private final int user;
|
||||
@Field(keys = {Table.PRIMARY})
|
||||
private final String config;
|
||||
@Field
|
||||
private final String value;
|
||||
|
||||
public static String getConfig(UUID player, String config) {
|
||||
return getConfig(SteamwarUser.get(player).getId(), config);
|
||||
}
|
||||
|
||||
public static String getConfig(int player, String config) {
|
||||
UserConfig value = select.select(player, config);
|
||||
return value != null ? value.value : null;
|
||||
}
|
||||
|
||||
public static void updatePlayerConfig(UUID uuid, String config, String value) {
|
||||
updatePlayerConfig(SteamwarUser.get(uuid).getId(), config, value);
|
||||
}
|
||||
|
||||
public static void updatePlayerConfig(int id, String config, String value) {
|
||||
if (value == null) {
|
||||
removePlayerConfig(id, config);
|
||||
return;
|
||||
}
|
||||
insert.update(id, config, value);
|
||||
}
|
||||
|
||||
public static void removePlayerConfig(UUID uuid, String config) {
|
||||
removePlayerConfig(SteamwarUser.get(uuid).getId(), config);
|
||||
}
|
||||
|
||||
public static void removePlayerConfig(int id, String config) {
|
||||
delete.update(id, config);
|
||||
}
|
||||
override val primaryKey = PrimaryKey(userId, key)
|
||||
}
|
||||
|
||||
class UserConfig(id: EntityID<CompositeID>): CompositeEntity(id) {
|
||||
val userId by UserConfigTable.userId
|
||||
val key by UserConfigTable.key
|
||||
var value by UserConfigTable.value
|
||||
|
||||
companion object: CompositeEntityClass<UserConfig>(UserConfigTable) {
|
||||
@JvmStatic
|
||||
fun getConfig(userId: Int, config: String) = useDb {
|
||||
find { (UserConfigTable.userId eq userId) and (UserConfigTable.key eq config) }.firstOrNull()?.value
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getConfig(user: UUID, config: String) = getConfig(SteamwarUser.get(user)!!.id.value, config)
|
||||
|
||||
@JvmStatic
|
||||
fun updatePlayerConfig(id: Int, config: String, value: String?) = useDb {
|
||||
if (value == null) {
|
||||
removePlayerConfig(id, config)
|
||||
} else {
|
||||
UserConfigTable.upsert {
|
||||
it[UserConfigTable.userId] = id
|
||||
it[UserConfigTable.key] = config
|
||||
it[UserConfigTable.value] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun updatePlayerConfig(user: UUID, config: String, value: String?) = updatePlayerConfig(SteamwarUser.get(user)!!.id.value, config, value)
|
||||
|
||||
@JvmStatic
|
||||
fun removePlayerConfig(id: Int, config: String) = useDb {
|
||||
find { (UserConfigTable.userId eq id) and (UserConfigTable.key eq config) }.firstOrNull()?.delete()
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun removePlayerConfig(user: UUID, config: String) = removePlayerConfig(SteamwarUser.get(user)!!.id.value, config)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user