1
0
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:
502647092 2015-09-14 20:53:31 +08:00
parent 3a1abb4301
commit db4d35dc05
4 changed files with 99 additions and 111 deletions

View File

@ -1,23 +1,43 @@
package cn.citycraft.CTZServer; package cn.citycraft.CTZServer;
import cn.citycraft.sql.KeyValue;
import cn.citycraft.sql.SQLHelper;
import cn.citycraft.utils.StringUtil;
public class CTZAuth { 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) { public static void changePassword(String username, String password) {
// TODO 处理玩家密码修改事件 // TODO 处理玩家密码修改事件
} }
public static boolean checkPassword(String username, String password) { public static boolean checkPassword(String username, String password) {
// TODO 检查玩家密码 return sql.isFieldExists(TableName, new KeyValue(UserField, username).add(PWDField, StringUtil.getMD5Code(password)));
return false; }
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) { public static boolean isRegistered(String username) {
// TODO 检查玩家是否注册 return sql.isFieldExists(TableName, new KeyValue(UserField, username));
return false;
} }
public static boolean registerPlayer(String username, String password) { 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; return false;
} }

View File

@ -30,6 +30,7 @@ public class ServerThread implements Runnable {
initCommand(); initCommand();
initDatabase(); initDatabase();
initServerInfo(); initServerInfo();
CTZAuth.init(this);
server = new CTZLoginServerSocket(); server = new CTZLoginServerSocket();
server.start(); server.start();
@ -68,24 +69,24 @@ public class ServerThread implements Runnable {
CTZServer.warn(ChatColor.RED + "数据库连接失败..."); CTZServer.warn(ChatColor.RED + "数据库连接失败...");
return; return;
} }
CTZServer.print("数据库连接成功,检查数据表是否存在..."); CTZServer.print(ChatColor.GREEN + "数据库连接成功,检查数据表是否存在...");
if (!sql.isTableExists(dbtable)) { if (!sql.isTableExists(dbtable)) {
CTZServer.print("数据表不存在,新建表" + dbtable + "..."); CTZServer.print(ChatColor.RED + "数据表不存在,新建表" + dbtable + "...");
KeyValue kv = new KeyValue("player", "VARCHAR(16) NOT NULL") KeyValue kv = new KeyValue("player", "VARCHAR(16) NOT NULL")
.add("password", "VARCHAR(50) NOT NULL") .add("password", "VARCHAR(50) NOT NULL")
.add("ip", "VARCHAR(40) NOT NULL") .add("ip", "VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'")
.add("lastloginout", "BIGINT(20) NOT NULL") .add("lastloginout", "BIGINT(20) NOT NULL DEFAULT 0")
.add("x", "DOUBLE NOT NULL") .add("x", "DOUBLE NOT NULL DEFAULT 0")
.add("y", "DOUBLE NOT NULL") .add("y", "DOUBLE NOT NULL DEFAULT 0")
.add("z", "DOUBLE NOT NULL") .add("z", "DOUBLE NOT NULL DEFAULT 0")
.add("email", "VARCHAR(50)") .add("email", "VARCHAR(50) NOT NULL DEFAULT 'mc@mc.com'")
.add("world", "SMALLINT(6)") .add("world", "VARCHAR(20) DEFAULT 'world'")
.add("islogged", "SMALLINT(6) NOT NULL"); .add("islogged", "SMALLINT(6) NOT NULL DEFAULT 0");
String Conditions = "UNIQUE (`player`)"; String Conditions = "UNIQUE (`player`)";
if (!sql.createTables(dbtable, kv, Conditions)) if (!sql.createTables(dbtable, kv, Conditions))
CTZServer.warn("数据表 " + dbtable + " 创建失败,请尝试手动创建并重启服务器..."); CTZServer.warn(ChatColor.RED + "数据表 " + dbtable + " 创建失败,请尝试手动创建并重启服务器...");
else else
CTZServer.print("数据表 " + dbtable + " 创建成功..."); CTZServer.print(ChatColor.GREEN + "数据表 " + dbtable + " 创建成功...");
} }
} }
@ -94,8 +95,9 @@ public class ServerThread implements Runnable {
*/ */
void initServerInfo() { void initServerInfo() {
CTZServer.print(ChatColor.GREEN + "加载服务器信息..."); CTZServer.print(ChatColor.GREEN + "加载服务器信息...");
FileUtil.saveResource(this, "/servers.json", false); File infofile = new File("servers.json");
String json = FileUtil.readFile(new File("servers.json")); FileUtil.saveResource(this, "/servers.json", infofile, false);
String json = FileUtil.readFile(infofile);
if (CTZServer.Init(json)) if (CTZServer.Init(json))
CTZServer.print(ChatColor.GREEN + "服务器信息加载成功..."); CTZServer.print(ChatColor.GREEN + "服务器信息加载成功...");
else else

View File

@ -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;
}

View File

@ -14,6 +14,55 @@ import cn.citycraft.CTZServer.socket.Response.HttpStates;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
public class CTZLoginServerSocket extends Thread { 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 { class ClientThread extends Thread {
Socket client = null; Socket client = null;
BufferedReader br = null; BufferedReader br = null;
@ -35,6 +84,7 @@ public class CTZLoginServerSocket extends Thread {
while (true) { while (true) {
String str = br.readLine(); String str = br.readLine();
if (str != null) { if (str != null) {
CTZServer.print(str);
Request req = new Request(str); Request req = new Request(str);
Response res = new Response(); Response res = new Response();
String username = ""; String username = "";
@ -85,11 +135,11 @@ public class CTZLoginServerSocket extends Thread {
res.setState(HttpStates.Bad_Request); res.setState(HttpStates.Bad_Request);
break; break;
} }
// if (Bukkit.getPlayer(username) != null) if (CTZAuth.isLogin(username))
// res.setHtml("true"); res.setHtml("true");
// else else
// res.setHtml("false"); res.setHtml("false");
// break; break;
case "changepassword": case "changepassword":
username = req.Query("username"); username = req.Query("username");
password = req.Query("password"); password = req.Query("password");
@ -105,6 +155,7 @@ public class CTZLoginServerSocket extends Thread {
break; break;
case "serverlist": case "serverlist":
res.setState(HttpStates.OK); res.setState(HttpStates.OK);
CTZServer.print(CTZServer.getJson());
res.setHtml(CTZServer.getJson()); res.setHtml(CTZServer.getJson());
break; break;
default: default:
@ -120,56 +171,9 @@ public class CTZLoginServerSocket extends Thread {
pw.close(); pw.close();
client.close(); client.close();
} catch (Exception e) { } 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);
}
} }