You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

527 lines
20 KiB

package org.poopki.duckdns.user_db.DB;
import org.poopki.duckdns.user_db.*;
import org.poopki.duckdns.user_db.Array.AccountArray;
import org.poopki.duckdns.user_db.Array.FarmArray;
import org.poopki.duckdns.user_db.Array.NationArray;
import org.poopki.duckdns.user_db.Array.SquadArray;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class DBUpdate {
private final String DB_DRIVER_CLASS = "org.mariadb.jdbc.Driver";
private final String DB_URL = "jdbc:mariadb://192.168.2.4:3307/mc_dev";
private final String DB_USERNAME = "mc_dev";
private final String DB_PASSWORD = "!Rkdalsrn1027";
private UserInfoArray m_InfoArrayInstance;
private AccountArray m_AccountArray;
private SquadArray m_SquadArray;
private FarmArray m_FarmArray;
private NationArray m_NationArray;
UUID ZERO_UUID = new UUID(0,0);
static int MAX_FARM_NUM = 13;
public DBUpdate(UserInfoArray UIA, FarmArray FA, NationArray NA, SquadArray SA ,AccountArray AA){
m_InfoArrayInstance = UIA;
m_AccountArray = AA;
m_FarmArray = FA;
m_NationArray = NA;
m_SquadArray = SA;
}
public void InitDB() throws SQLException { // 플러그인 초기 사용 시 테이블 생성
Connection conn = null;
Statement stmt;
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connection success");
stmt = conn.createStatement();
StringBuilder sb = new StringBuilder();
String sql = sb.append("create table if not exists TechTree(")
.append("UUID varchar(36) PRIMARY KEY")
.append(")").toString();
stmt.execute(sql);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
finally {
if( conn != null && !conn.isClosed()){
conn.close();
}
}
}
public void LoadDB() throws SQLException { //서버 초기 구동 시 DB 로드
Connection conn = null;
Statement stmt;
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connection success");
stmt = conn.createStatement();
String sql = "SELECT * FROM UserInfo";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
m_InfoArrayInstance.putUserInfo(UUID.fromString(rs.getString(1)),
rs.getString(2),
UUID.fromString(rs.getString(3)),
UUID.fromString(rs.getString(4)),
UUID.fromString(rs.getString(5)),
new UUID(0,0));
m_AccountArray.putAccountInfo(UUID.fromString(rs.getString(1)),Integer.parseInt(rs.getString(6)));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}finally{
if(conn != null && !conn.isClosed()){
conn.close();
}
}
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connection success");
stmt = conn.createStatement();
String sql = "SELECT * FROM Farm";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
List<UUID> m_MemberList = new ArrayList<>();
for(int index = 4; index<MAX_FARM_NUM; index++){
if(rs.getString(index) != null){
m_MemberList.add(index-4,UUID.fromString(rs.getString(index)));
}
}
m_FarmArray.PutGroup(UUID.fromString(rs.getString(1)),
rs.getString(2),
m_MemberList);
m_AccountArray.putAccountInfo(UUID.fromString(rs.getString(1)),Integer.parseInt(rs.getString(3)));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}finally{
if(conn != null && !conn.isClosed()){
conn.close();
}
}
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connection success");
stmt = conn.createStatement();
String sql = "SELECT * FROM Nation";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
List<UUID> m_MemberList = new ArrayList<>();
for(int index = 4; index<MAX_FARM_NUM; index++){
if(rs.getString(index) != null){
m_MemberList.add(index-4,UUID.fromString(rs.getString(index)));
}
}
m_NationArray.PutGroup(UUID.fromString(rs.getString(1)),
rs.getString(2),
m_MemberList);
m_AccountArray.putAccountInfo(UUID.fromString(rs.getString(1)),Integer.parseInt(rs.getString(3)));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}finally{
if(conn != null && !conn.isClosed()){
conn.close();
}
}
//
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connection success");
stmt = conn.createStatement();
String sql = "SELECT * FROM TechTree";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
Integer index=2;
if(rs.getString(index) != null){
for(String TechName: m_NationArray.m_NationArray.get(UUID.fromString(rs.getString(1))).m_TechStat.keySet()){
m_NationArray.SetGroupTechTree(UUID.fromString(rs.getString(1)),TechName, Integer.parseInt(rs.getString(index)));
index++;
}
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}finally{
if(conn != null && !conn.isClosed()){
conn.close();
}
}
//
try {
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("Connection success");
stmt = conn.createStatement();
String sql = "SELECT * FROM Squad";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
List<UUID> m_MemberList = new ArrayList<>();
for(int index = 4; index<11; index++){
if(rs.getString(index) != null){
m_MemberList.add(index-4,UUID.fromString(rs.getString(index)));
}
}
m_SquadArray.PutGroup(UUID.fromString(rs.getString(1)),
rs.getString(2),
m_MemberList);
m_AccountArray.putAccountInfo(UUID.fromString(rs.getString(1)),Integer.parseInt(rs.getString(3)));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}finally{
if(conn != null && !conn.isClosed()){
conn.close();
}
}
}
public void UpdateDB() throws SQLException { // 서버 종료시 DB 업데이트
Connection conn = null;
PreparedStatement pstmt;
// Update UserINFO
try{
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key : m_InfoArrayInstance.getKeySet()) {
String sql = "REPLACE INTO UserInfo VALUES (?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, String.valueOf(key.toString()));
pstmt.setString(2, String.valueOf(m_InfoArrayInstance.getUserName(key)));
pstmt.setString(3, String.valueOf(m_InfoArrayInstance.getUserGroupUUID(key, "Nation")));
pstmt.setString(4, String.valueOf(m_InfoArrayInstance.getUserGroupUUID(key, "Squad")));
pstmt.setString(5, String.valueOf(m_InfoArrayInstance.getUserGroupUUID(key, "Farm")));
pstmt.setString(6, String.valueOf(m_AccountArray.getAccountInfo(key)));
int cnt = pstmt.executeUpdate();
if( cnt == 0 ) {
System.out.println("[DB] 유저 데이터 입력 실패");
}
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
finally {
if( conn != null && !conn.isClosed()){
conn.close();
}
}
UpdateFarmInfo();
UpdateNationInfo();
UpdateSquadInfo();
try{
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key: m_NationArray.m_DeleteQueue){
String sql = "DELETE FROM TechTree WHERE UUID=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, String.valueOf(key.toString()));
int cnt = pstmt.executeUpdate();
m_NationArray.m_DeleteQueue.remove(key);
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
finally {
if( conn != null && !conn.isClosed()){
conn.close();
}
}
try{
Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key : m_NationArray.getKeySet()) {
String sql = "REPLACE INTO TechTree VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
int index = 2;
pstmt.setString(1, String.valueOf(key.toString()));
for (String Name : m_NationArray.m_NationArray.get(key).m_TechStat.keySet()){
pstmt.setString(index, String.valueOf(m_NationArray.m_NationArray.get(key).GetTechTree(Name)));
index++;
}
int cnt = pstmt.executeUpdate();
if( cnt == 0 ){
System.out.println("[DB] 국가 데이터 입력 실패");
}
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
finally {
if( conn != null && !conn.isClosed()){
conn.close();
}
}
}
public void UpdateFarmInfo(){
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key: m_FarmArray.m_DeleteQueue){
String sql = "DELETE FROM Farm WHERE Farm_UUID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, String.valueOf(key.toString()));
int cnt = pstmt.executeUpdate();
m_FarmArray.m_DeleteQueue.remove(key);
}
if( conn != null && !conn.isClosed()){
conn.close();
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key : m_FarmArray.getKeySet()) {
String sql = "REPLACE INTO Farm SET ";
String sql_A = "FARM_UUID='"+String.valueOf(key.toString())+"', NAME='"+String.valueOf(m_FarmArray.GetGroupName(key))+"', Account='"+String.valueOf(m_AccountArray.getAccountInfo(key))+"',";
String sql_B = "";
int index=1;
for (UUID MemUUID : m_FarmArray.GetGroupMembersUUID(key)){
sql_B = sql_B + "MEMBER"+String.valueOf(index)+"_UUID='"+String.valueOf(MemUUID.toString())+"',";
index++;
}
sql_B = sql_B.substring(0,sql_B.length()-1);
sql = sql+sql_A+sql_B;
PreparedStatement pstmt = conn.prepareStatement(sql);
int cnt = pstmt.executeUpdate();
if( cnt == 0 ){
System.out.println("[DB] 분대 데이터 입력 실패");
}
}
if( conn != null && !conn.isClosed()){
conn.close();
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
}
public void UpdateNationInfo(){
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key: m_NationArray.m_DeleteQueue){
String sql = "DELETE FROM Nation WHERE Nation_UUID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, String.valueOf(key.toString()));
int cnt = pstmt.executeUpdate();
m_NationArray.m_DeleteQueue.remove(key);
}
if( conn != null && !conn.isClosed()){
conn.close();
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key : m_NationArray.getKeySet()) {
String sql = "REPLACE INTO Nation SET ";
String sql_A = "NATION_UUID='"+String.valueOf(key.toString())+"', NAME='"+String.valueOf(m_NationArray.GetGroupName(key))+"', Account='"+String.valueOf(m_AccountArray.getAccountInfo(key))+"',";
String sql_B = "";
int index=1;
for (UUID MemUUID : m_NationArray.GetGroupMembersUUID(key)){
sql_B = sql_B + "MEMBER"+String.valueOf(index)+"_UUID='"+String.valueOf(MemUUID.toString())+"',";
index++;
}
sql_B = sql_B.substring(0,sql_B.length()-1);
sql = sql+sql_A+sql_B;
PreparedStatement pstmt = conn.prepareStatement(sql);
int cnt = pstmt.executeUpdate();
if( cnt == 0 ){
System.out.println("[DB] 분대 데이터 입력 실패");
}
else{
System.out.println("[DB] 분대 데이터 입력 성공");
}
}
if( conn != null && !conn.isClosed()){
conn.close();
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
}
public void UpdateSquadInfo(){
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key: m_SquadArray.m_DeleteQueue){
String sql = "DELETE FROM Squad WHERE Squad_UUID=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, String.valueOf(key.toString()));
int cnt = pstmt.executeUpdate();
m_SquadArray.m_DeleteQueue.remove(key);
}
if( conn != null && !conn.isClosed()){
conn.close();
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key : m_SquadArray.getKeySet()) {
String sql = "REPLACE INTO Squad SET ";
String sql_A = "Squad_UUID='"+String.valueOf(key.toString())+"', NAME='"+String.valueOf(m_SquadArray.GetGroupName(key))+"', Account='"+String.valueOf(m_AccountArray.getAccountInfo(key))+"',";
String sql_B = "";
int index=1;
for (UUID MemUUID : m_SquadArray.GetGroupMembersUUID(key)){
sql_B = sql_B + "MEMBER"+String.valueOf(index)+"_UUID='"+String.valueOf(MemUUID.toString())+"',";
index++;
}
sql_B = sql_B.substring(0,sql_B.length()-1);
sql = sql+sql_A+sql_B;
PreparedStatement pstmt = conn.prepareStatement(sql);
int cnt = pstmt.executeUpdate();
if( cnt == 0 ){
System.out.println("[DB] 분대 데이터 입력 실패");
}
else{
System.out.println("[DB] 분대 데이터 입력 성공");
}
}
if( conn != null && !conn.isClosed()){
conn.close();
}
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
catch(SQLException e){
System.err.println("에러 내용 :" + e.getMessage());
System.out.println("DB Connection fail");
}
}
}