107 lines
4.4 KiB
Java
107 lines
4.4 KiB
Java
package cn.citycraft.CTZServerCommon;
|
|
|
|
import cn.citycraft.PluginHelper.sql.DataBase;
|
|
import cn.citycraft.PluginHelper.sql.KeyValue;
|
|
import cn.citycraft.PluginHelper.sql.MySQLCore;
|
|
import cn.citycraft.PluginHelper.utils.StringUtil;
|
|
import net.md_5.bungee.api.ChatColor;
|
|
|
|
public class CTZAuth {
|
|
static final String TableName = "ctzserver";
|
|
static final String UserField = "player";
|
|
static final String PWDField = "password";
|
|
static final String ISLoginField = "islogin";
|
|
static final String EMAILField = "email";
|
|
static final String ALLOWLoginField = "allowlogin";
|
|
static final String LASTLOGOUT = "lastlogout";
|
|
static final String IP = "ip";
|
|
static DataBase sql;
|
|
|
|
public static boolean allowLogin(final String username) {
|
|
return allowLogin(username, null);
|
|
}
|
|
|
|
public static boolean allowLogin(final String username, final String ip) {
|
|
final KeyValue cdt = new KeyValue(UserField, username);
|
|
if (ip != null) {
|
|
cdt.add(IP, ip);
|
|
}
|
|
final String result = sql.dbSelectFirst(TableName, ALLOWLoginField, cdt);
|
|
return (result != null && result.equalsIgnoreCase("1"));
|
|
}
|
|
|
|
public static void changePassword(final String username, final String password) {
|
|
// TODO 处理玩家密码修改事件
|
|
}
|
|
|
|
public static boolean checkLastLogout(final String username, final long timeout) {
|
|
try {
|
|
final String lastlogout = sql.dbSelectFirst(TableName, LASTLOGOUT, new KeyValue(UserField, username));
|
|
return System.currentTimeMillis() - Integer.parseInt(lastlogout) < timeout * 1000;
|
|
} catch (final Exception e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static boolean checkPassword(final String username, final String password) {
|
|
return sql.isFieldExists(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
|
|
}
|
|
|
|
public static void init(DataBase sql, final String address, final int port, final String datebase, final String username, final String password) {
|
|
CTZAuth.sql = sql;
|
|
CTZServer.print(ChatColor.GREEN + "初始化数据库连接...");
|
|
sql = new DataBase(new MySQLCore(address, port, datebase, username, password));
|
|
if (!sql.testConnect()) {
|
|
CTZServer.warn(ChatColor.RED + "数据库连接失败...");
|
|
return;
|
|
}
|
|
CTZServer.print(ChatColor.GREEN + "数据库连接成功,检查数据表是否存在...");
|
|
if (!sql.isTableExists(TableName)) {
|
|
CTZServer.print(ChatColor.RED + "数据表不存在,新建表" + TableName + "...");
|
|
final KeyValue kv = new KeyValue("player", "VARCHAR(16) NOT NULL")
|
|
.add("password", "VARCHAR(50) NOT NULL")
|
|
.add("ip", "VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'")
|
|
.add("lastlogout", "BIGINT(30) NOT NULL DEFAULT 0")
|
|
.add("x", "DOUBLE NOT NULL DEFAULT 0")
|
|
.add("y", "DOUBLE NOT NULL DEFAULT 0")
|
|
.add("z", "DOUBLE NOT NULL DEFAULT 0")
|
|
.add("email", "VARCHAR(50) NOT NULL DEFAULT 'mc@mc.com'")
|
|
.add("world", "VARCHAR(20) DEFAULT 'world'")
|
|
.add("islogged", "SMALLINT(6) NOT NULL DEFAULT 0")
|
|
.add("allowlogin", "SMALLINT(6) NOT NULL DEFAULT 0");
|
|
final String Conditions = "UNIQUE (`player`)";
|
|
if (!sql.createTables(TableName, kv, Conditions)) {
|
|
CTZServer.warn(ChatColor.RED + "数据表 " + TableName + " 创建失败,请尝试手动创建并重启服务器...");
|
|
} else {
|
|
CTZServer.print(ChatColor.GREEN + "数据表 " + TableName + " 创建成功...");
|
|
}
|
|
}
|
|
}
|
|
|
|
public static boolean isLogin(final String username) {
|
|
final String result = sql.dbSelectFirst(TableName, ISLoginField, new KeyValue(UserField, username));
|
|
return (result != null && result.equalsIgnoreCase("1"));
|
|
}
|
|
|
|
public static boolean isRegistered(final String username) {
|
|
return sql.isFieldExists(TableName, new KeyValue(UserField, username));
|
|
}
|
|
|
|
public static boolean login(final String username, final String ip) {
|
|
return sql.dbUpdate(TableName, new KeyValue(ALLOWLoginField, "1").add(IP, ip == null ? "127.0.0.1" : ip), new KeyValue(UserField, username));
|
|
}
|
|
|
|
public static boolean registerPlayer(final String username, final String password, final String email) {
|
|
if (isRegistered(username)) {
|
|
return false;
|
|
} else {
|
|
return sql.dbInsert(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
|
|
}
|
|
}
|
|
|
|
public static boolean setLastLogout(final String username, final long logouttime) {
|
|
return sql.dbUpdate(TableName, new KeyValue(ALLOWLoginField, "1"), new KeyValue(UserField, username));
|
|
}
|
|
|
|
}
|