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