1
0
mirror of https://e.coding.net/circlecloud/CTZServerCommon.git synced 2024-12-26 18:28:46 +00:00

update...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092 2015-10-11 23:06:33 +08:00
parent 8f050484d4
commit 08890c82ed

View File

@ -1,8 +1,8 @@
package cn.citycraft.CTZServerCommon;
import cn.citycraft.PluginHelper.sql.DataBase;
import cn.citycraft.PluginHelper.sql.KeyValue;
import cn.citycraft.PluginHelper.sql.MySQLHelper;
import cn.citycraft.PluginHelper.sql.DataBaseCore;
import cn.citycraft.PluginHelper.sql.MySQLCore;
import cn.citycraft.PluginHelper.utils.StringUtil;
import net.md_5.bungee.api.ChatColor;
@ -15,49 +15,50 @@ public class CTZAuth {
static final String ALLOWLoginField = "allowlogin";
static final String LASTLOGOUT = "lastlogout";
static final String IP = "ip";
static DataBaseCore sql;
static DataBase sql;
public static boolean allowLogin(String username) {
public static boolean allowLogin(final String username) {
return allowLogin(username, null);
}
public static boolean allowLogin(String username, String ip) {
KeyValue cdt = new KeyValue(UserField, username);
if (ip != 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);
String result = sql.dbSelectFirst(TableName, ALLOWLoginField, cdt);
}
final String result = sql.dbSelectFirst(TableName, ALLOWLoginField, cdt);
return (result != null && result.equalsIgnoreCase("1"));
}
public static void changePassword(String username, String password) {
public static void changePassword(final String username, final String password) {
// TODO 处理玩家密码修改事件
}
public static boolean checkLastLogout(String username, long timeout) {
public static boolean checkLastLogout(final String username, final long timeout) {
try {
String lastlogout = sql.dbSelectFirst(TableName, LASTLOGOUT, new KeyValue(UserField, username));
final String lastlogout = sql.dbSelectFirst(TableName, LASTLOGOUT, new KeyValue(UserField, username));
return System.currentTimeMillis() - Integer.parseInt(lastlogout) < timeout * 1000;
} catch (Exception e) {
} catch (final Exception e) {
return false;
}
}
public static boolean checkPassword(String username, String password) {
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(DataBaseCore sql, String address, int port, String datebase, String username, String 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 MySQLHelper(address, port, datebase, username, password);
if (!sql.dbConnection()) {
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 + "...");
KeyValue kv = new KeyValue("player", "VARCHAR(16) NOT NULL")
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")
@ -68,35 +69,37 @@ public class CTZAuth {
.add("world", "VARCHAR(20) DEFAULT 'world'")
.add("islogged", "SMALLINT(6) NOT NULL DEFAULT 0")
.add("allowlogin", "SMALLINT(6) NOT NULL DEFAULT 0");
String Conditions = "UNIQUE (`player`)";
if (!sql.createTables(TableName, kv, Conditions))
final String Conditions = "UNIQUE (`player`)";
if (!sql.createTables(TableName, kv, Conditions)) {
CTZServer.warn(ChatColor.RED + "数据表 " + TableName + " 创建失败,请尝试手动创建并重启服务器...");
else
} else {
CTZServer.print(ChatColor.GREEN + "数据表 " + TableName + " 创建成功...");
}
}
}
public static boolean isLogin(String username) {
String result = sql.dbSelectFirst(TableName, ISLoginField, new KeyValue(UserField, username));
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(String username) {
public static boolean isRegistered(final String username) {
return sql.isFieldExists(TableName, new KeyValue(UserField, username));
}
public static boolean login(String username, String ip) {
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(String username, String password, String email) {
if (isRegistered(username))
public static boolean registerPlayer(final String username, final String password, final String email) {
if (isRegistered(username)) {
return false;
else
} else {
return sql.dbInsert(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
}
}
public static boolean setLastLogout(String username, long logouttime) {
public static boolean setLastLogout(final String username, final long logouttime) {
return sql.dbUpdate(TableName, new KeyValue(ALLOWLoginField, "1"), new KeyValue(UserField, username));
}