mirror of
https://e.coding.net/circlecloud/CTZServer.git
synced 2024-11-24 11:58:46 +00:00
complete CTZAuth and fix json read error...
Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
parent
3a1abb4301
commit
db4d35dc05
@ -1,23 +1,43 @@
|
||||
package cn.citycraft.CTZServer;
|
||||
|
||||
import cn.citycraft.sql.KeyValue;
|
||||
import cn.citycraft.sql.SQLHelper;
|
||||
import cn.citycraft.utils.StringUtil;
|
||||
|
||||
public class CTZAuth {
|
||||
static final String TableName = "ctzserver";
|
||||
static final String UserField = "player";
|
||||
static final String PWDField = "password";
|
||||
static ServerThread serverThread;
|
||||
static SQLHelper sql;
|
||||
|
||||
public static void changePassword(String username, String password) {
|
||||
// TODO 处理玩家密码修改事件
|
||||
}
|
||||
|
||||
public static boolean checkPassword(String username, String password) {
|
||||
// TODO 检查玩家密码
|
||||
return false;
|
||||
return sql.isFieldExists(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
|
||||
}
|
||||
|
||||
public static void init(ServerThread serverThread) {
|
||||
CTZAuth.serverThread = serverThread;
|
||||
CTZAuth.sql = serverThread.sql;
|
||||
}
|
||||
|
||||
public static boolean isLogin(String username) {
|
||||
String result = sql.dbSelectFirst(TableName, "islogged", new KeyValue(UserField, username));
|
||||
return (result != null && result.equalsIgnoreCase("1"));
|
||||
}
|
||||
|
||||
public static boolean isRegistered(String username) {
|
||||
// TODO 检查玩家是否注册
|
||||
return false;
|
||||
return sql.isFieldExists(TableName, new KeyValue(UserField, username));
|
||||
}
|
||||
|
||||
public static boolean registerPlayer(String username, String password) {
|
||||
// TODO 注册玩家
|
||||
if (isRegistered(username))
|
||||
return false;
|
||||
else
|
||||
sql.dbInsert(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ public class ServerThread implements Runnable {
|
||||
initCommand();
|
||||
initDatabase();
|
||||
initServerInfo();
|
||||
CTZAuth.init(this);
|
||||
|
||||
server = new CTZLoginServerSocket();
|
||||
server.start();
|
||||
@ -68,24 +69,24 @@ public class ServerThread implements Runnable {
|
||||
CTZServer.warn(ChatColor.RED + "数据库连接失败...");
|
||||
return;
|
||||
}
|
||||
CTZServer.print("数据库连接成功,检查数据表是否存在...");
|
||||
CTZServer.print(ChatColor.GREEN + "数据库连接成功,检查数据表是否存在...");
|
||||
if (!sql.isTableExists(dbtable)) {
|
||||
CTZServer.print("数据表不存在,新建表" + dbtable + "...");
|
||||
CTZServer.print(ChatColor.RED + "数据表不存在,新建表" + dbtable + "...");
|
||||
KeyValue kv = new KeyValue("player", "VARCHAR(16) NOT NULL")
|
||||
.add("password", "VARCHAR(50) NOT NULL")
|
||||
.add("ip", "VARCHAR(40) NOT NULL")
|
||||
.add("lastloginout", "BIGINT(20) NOT NULL")
|
||||
.add("x", "DOUBLE NOT NULL")
|
||||
.add("y", "DOUBLE NOT NULL")
|
||||
.add("z", "DOUBLE NOT NULL")
|
||||
.add("email", "VARCHAR(50)")
|
||||
.add("world", "SMALLINT(6)")
|
||||
.add("islogged", "SMALLINT(6) NOT NULL");
|
||||
.add("ip", "VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'")
|
||||
.add("lastloginout", "BIGINT(20) 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");
|
||||
String Conditions = "UNIQUE (`player`)";
|
||||
if (!sql.createTables(dbtable, kv, Conditions))
|
||||
CTZServer.warn("数据表 " + dbtable + " 创建失败,请尝试手动创建并重启服务器...");
|
||||
CTZServer.warn(ChatColor.RED + "数据表 " + dbtable + " 创建失败,请尝试手动创建并重启服务器...");
|
||||
else
|
||||
CTZServer.print("数据表 " + dbtable + " 创建成功...");
|
||||
CTZServer.print(ChatColor.GREEN + "数据表 " + dbtable + " 创建成功...");
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,8 +95,9 @@ public class ServerThread implements Runnable {
|
||||
*/
|
||||
void initServerInfo() {
|
||||
CTZServer.print(ChatColor.GREEN + "加载服务器信息...");
|
||||
FileUtil.saveResource(this, "/servers.json", false);
|
||||
String json = FileUtil.readFile(new File("servers.json"));
|
||||
File infofile = new File("servers.json");
|
||||
FileUtil.saveResource(this, "/servers.json", infofile, false);
|
||||
String json = FileUtil.readFile(infofile);
|
||||
if (CTZServer.Init(json))
|
||||
CTZServer.print(ChatColor.GREEN + "服务器信息加载成功...");
|
||||
else
|
||||
|
@ -1,38 +0,0 @@
|
||||
package cn.citycraft.CTZServer.modules;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ctzserver")
|
||||
public class AuthModule {
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
@Column
|
||||
private String name;
|
||||
|
||||
@Column
|
||||
private String ip;
|
||||
|
||||
@Column
|
||||
private long x;
|
||||
|
||||
@Column
|
||||
private long y;
|
||||
|
||||
@Column
|
||||
private long z;
|
||||
|
||||
@Column
|
||||
private String world;
|
||||
|
||||
@Column
|
||||
private long lastlogout;
|
||||
|
||||
@Column
|
||||
private boolean islogin;
|
||||
|
||||
}
|
@ -14,6 +14,55 @@ import cn.citycraft.CTZServer.socket.Response.HttpStates;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class CTZLoginServerSocket extends Thread {
|
||||
ServerSocket s = null;
|
||||
|
||||
Socket socket = null;
|
||||
int port = 25580;
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 设定服务端的端口号
|
||||
s = new ServerSocket(port);
|
||||
CTZServer.print(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort());
|
||||
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
|
||||
while (true) {
|
||||
socket = s.accept();
|
||||
new ClientThread(socket).start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CTZServer.print(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage());
|
||||
} finally {
|
||||
ShutDown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭服务器
|
||||
*/
|
||||
public void ShutDown() {
|
||||
try {
|
||||
socket.close();
|
||||
s.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
CTZServer.print(ChatColor.RED + "CTZ服务器已关闭...");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
class ClientThread extends Thread {
|
||||
Socket client = null;
|
||||
BufferedReader br = null;
|
||||
@ -35,6 +84,7 @@ public class CTZLoginServerSocket extends Thread {
|
||||
while (true) {
|
||||
String str = br.readLine();
|
||||
if (str != null) {
|
||||
CTZServer.print(str);
|
||||
Request req = new Request(str);
|
||||
Response res = new Response();
|
||||
String username = "";
|
||||
@ -85,11 +135,11 @@ public class CTZLoginServerSocket extends Thread {
|
||||
res.setState(HttpStates.Bad_Request);
|
||||
break;
|
||||
}
|
||||
// if (Bukkit.getPlayer(username) != null)
|
||||
// res.setHtml("true");
|
||||
// else
|
||||
// res.setHtml("false");
|
||||
// break;
|
||||
if (CTZAuth.isLogin(username))
|
||||
res.setHtml("true");
|
||||
else
|
||||
res.setHtml("false");
|
||||
break;
|
||||
case "changepassword":
|
||||
username = req.Query("username");
|
||||
password = req.Query("password");
|
||||
@ -105,6 +155,7 @@ public class CTZLoginServerSocket extends Thread {
|
||||
break;
|
||||
case "serverlist":
|
||||
res.setState(HttpStates.OK);
|
||||
CTZServer.print(CTZServer.getJson());
|
||||
res.setHtml(CTZServer.getJson());
|
||||
break;
|
||||
default:
|
||||
@ -120,56 +171,9 @@ public class CTZLoginServerSocket extends Thread {
|
||||
pw.close();
|
||||
client.close();
|
||||
} catch (Exception e) {
|
||||
CTZServer.warn(e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ServerSocket s = null;
|
||||
Socket socket = null;
|
||||
|
||||
int port = 25580;
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化服务端口
|
||||
*/
|
||||
public CTZLoginServerSocket(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
// 设定服务端的端口号
|
||||
s = new ServerSocket(port);
|
||||
CTZServer.print(ChatColor.BLUE + "CTZ服务器开始监听 端口:" + s.getLocalPort());
|
||||
// 等待请求,此方法会一直阻塞,直到获得请求才往下走
|
||||
while (true) {
|
||||
socket = s.accept();
|
||||
new ClientThread(socket).start();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
CTZServer.print(ChatColor.RED + "CTZ服务器崩溃: " + e.getMessage());
|
||||
} finally {
|
||||
ShutDown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭服务器
|
||||
*/
|
||||
public void ShutDown() {
|
||||
try {
|
||||
socket.close();
|
||||
s.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
CTZServer.print(ChatColor.RED + "CTZ服务器已关闭...");
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user