코드 최적화

Status
poopki 3 years ago
parent 414c0e1a3e
commit 3d39a8f389
  1. 21
      Account.java
  2. 20
      AccountArray.java
  3. 47
      DB/DBUpdate.java
  4. 18
      UserInfo.java
  5. 28
      UserInfoArray.java
  6. 15
      UserJoin.java
  7. 8
      User_DB.java

@ -1,29 +1,18 @@
package org.poopki.duckdns.user_db; package org.poopki.duckdns.user_db;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class Account { public class Account {
private int m_Balance; private int m_Balance;
public Account(){ public Account(int Balance){ // Account Balance 초기화
m_Balance = 0;
}
public Account(int Balance){
m_Balance = Balance; m_Balance = Balance;
} }
public int getBalance(){ return m_Balance; } public int getBalance(){ return m_Balance; } //잔액 return
public boolean credit(int Amount){ public void credit(int Amount){ // 입금
m_Balance +=Amount; m_Balance +=Amount;
return true;
} }
public boolean withdraw(int Amount){ public boolean withdraw(int Amount){ // 출금
if((m_Balance - Amount) > 0){ if(m_Balance >= Amount){
m_Balance -= Amount; m_Balance -= Amount;
return true; return true;
} }

@ -1,37 +1,27 @@
package org.poopki.duckdns.user_db; package org.poopki.duckdns.user_db;
import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class AccountArray { public class AccountArray {
public static Map<UUID, Account> m_AccountArray = new HashMap<UUID, Account>(); private static Map<UUID, Account> m_AccountArray = new HashMap<UUID, Account>();
public void transfer(UUID Src, UUID Des, int Amount){ public void transfer(UUID Src, UUID Des, int Amount){ // 송금 method
Account m_Src = m_AccountArray.get(Src); Account m_Src = m_AccountArray.get(Src);
Account m_Des = m_AccountArray.get(Des); Account m_Des = m_AccountArray.get(Des);
if(m_Src.getBalance()-Amount>0){ if(m_Src.withdraw(Amount)){ //인출 성공시 입금
m_Src.withdraw(Amount);
m_Des.credit(Amount); m_Des.credit(Amount);
} }
} }
public int getAccountInfo(UUID uuid) { public int getAccountInfo(UUID uuid) { // DB backup시 account 정보 접근 method
Account m_Account = m_AccountArray.get(uuid); Account m_Account = m_AccountArray.get(uuid);
return m_Account.getBalance(); return m_Account.getBalance();
} }
public Account putAccountInfo(UUID uuid) { public void putAccountInfo(UUID uuid, int Amount) { //AccountArray 생성
m_AccountArray.put(uuid, new Account());
Account m_Account = m_AccountArray.get(uuid);
return m_Account;
}
public Account putAccountInfo(UUID uuid, int Amount) {
m_AccountArray.put(uuid, new Account(Amount)); m_AccountArray.put(uuid, new Account(Amount));
Account m_Account = m_AccountArray.get(uuid);
return m_Account;
} }
} }

@ -1,7 +1,6 @@
package org.poopki.duckdns.user_db.DB; package org.poopki.duckdns.user_db.DB;
import org.poopki.duckdns.user_db.AccountArray; import org.poopki.duckdns.user_db.AccountArray;
import org.poopki.duckdns.user_db.UserInfo;
import org.poopki.duckdns.user_db.UserInfoArray; import org.poopki.duckdns.user_db.UserInfoArray;
import java.sql.*; import java.sql.*;
@ -13,12 +12,14 @@ public class DBUpdate {
private static final String DB_USERNAME = "mc_dev"; private static final String DB_USERNAME = "mc_dev";
private static final String DB_PASSWORD = "!Rkdalsrn1027"; private static final String DB_PASSWORD = "!Rkdalsrn1027";
private static UserInfoArray m_InfoArrayInstance; private static UserInfoArray m_InfoArrayInstance;
private static AccountArray m_AccountArray;
public DBUpdate(UserInfoArray m_UIA){ public DBUpdate(UserInfoArray UIA, AccountArray AA){
m_InfoArrayInstance = m_UIA; m_InfoArrayInstance = UIA;
m_AccountArray = AA;
} }
public static void InitDB() throws SQLException { public static void InitDB() throws SQLException { // 플러그인 초기 사용 시 테이블 생성
Connection conn = null; Connection conn = null;
Statement stmt; Statement stmt;
@ -31,6 +32,7 @@ public class DBUpdate {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
String sql = sb.append("create table if not exists UserInfo(") String sql = sb.append("create table if not exists UserInfo(")
.append("UUID varchar(36) PRIMARY KEY,") .append("UUID varchar(36) PRIMARY KEY,")
.append("Name varchar(15), ")
.append("Nation int(8),") //public .append("Nation int(8),") //public
.append("Squad int(8),") //public .append("Squad int(8),") //public
.append("Farm int(8),") //public .append("Farm int(8),") //public
@ -56,7 +58,7 @@ public class DBUpdate {
} }
} }
} }
public static void LoadDB(AccountArray m_AccountArrayInstance) throws SQLException { public static void LoadDB() throws SQLException { //서버 초기 구동 시 DB 로드
Connection conn = null; Connection conn = null;
Statement stmt; Statement stmt;
try { try {
@ -69,14 +71,12 @@ public class DBUpdate {
ResultSet rs = stmt.executeQuery(sql); ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){ while(rs.next()){
m_InfoArrayInstance.m_UserInfoArray.put(UUID.fromString(rs.getString(1)), new UserInfo(UUID.fromString(rs.getString(1)), m_InfoArrayInstance.putUserInfo(UUID.fromString(rs.getString(1)),
Integer.parseInt(rs.getString(2)), rs.getString(2),
Integer.parseInt(rs.getString(3)), Integer.parseInt(rs.getString(3)),
Integer.parseInt(rs.getString(4)), Integer.parseInt(rs.getString(4)),
m_AccountArrayInstance.putAccountInfo( Integer.parseInt(rs.getString(5)));
UUID.fromString( m_AccountArray.putAccountInfo(UUID.fromString(rs.getString(1)),Integer.parseInt(rs.getString(6)));
rs.getString(1)),
Integer.parseInt(rs.getString(5)))));
} }
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -94,7 +94,7 @@ public class DBUpdate {
} }
public void UpdateDB() throws SQLException { public void UpdateDB() throws SQLException { // 서버 종료시 DB 업데이트
Connection conn = null; Connection conn = null;
PreparedStatement pstmt; PreparedStatement pstmt;
@ -102,19 +102,16 @@ public class DBUpdate {
Class.forName("org.mariadb.jdbc.Driver"); Class.forName("org.mariadb.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD); conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
for (UUID key : m_InfoArrayInstance.m_UserInfoArray.keySet()) { for (UUID key : m_InfoArrayInstance.getKeySet()) {
//sql = sb.append("insert into UserInfo values('"+key+"',"+0+","+0+","+0+","+0+","+0+")").toString();
//String sql = sb.append("insert into UserInfo values ('test', 0)").toString(); String sql = "REPLACE INTO UserInfo VALUES (?,?,?,?,?,?)";
//stmt.execute(sql);
String sql = "REPLACE INTO UserInfo VALUES (?,?,?,?,?)";
pstmt = conn.prepareStatement(sql); pstmt = conn.prepareStatement(sql);
//pstmt.setString(1, key.toString());
UserInfo temp = m_InfoArrayInstance.m_UserInfoArray.get(key);
pstmt.setString(1, String.valueOf(key.toString())); pstmt.setString(1, String.valueOf(key.toString()));
pstmt.setString(2, String.valueOf(11)); pstmt.setString(2, String.valueOf(m_InfoArrayInstance.getUserName(key)));
pstmt.setString(3, String.valueOf(23)); pstmt.setString(3, String.valueOf(11));
pstmt.setString(4, String.valueOf(56)); pstmt.setString(4, String.valueOf(23));
pstmt.setString(5, String.valueOf(temp.getBalance())); pstmt.setString(5, String.valueOf(56));
pstmt.setString(6, String.valueOf(m_AccountArray.getAccountInfo(key)));
int cnt = pstmt.executeUpdate(); int cnt = pstmt.executeUpdate();
if( cnt == 0 ){ if( cnt == 0 ){
System.out.println("데이터 입력 실패"); System.out.println("데이터 입력 실패");

@ -1,35 +1,35 @@
package org.poopki.duckdns.user_db; package org.poopki.duckdns.user_db;
import org.bukkit.entity.Player; import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class UserInfo { public class UserInfo {
private UUID m_UUID; private UUID m_UUID;
private String m_Name; private String m_Name;
private static Map<String, Integer> m_status = new HashMap<String, Integer>();
private int m_Nation; private int m_Nation;
private int m_Squad; private int m_Squad;
private int m_Farm; private int m_Farm;
private Account m_Account;
public UserInfo(UUID uuid, int Nation, int Squad, int Farm, Account Instance){ public UserInfo(UUID uuid, String Name, int Nation, int Squad, int Farm){ //UserInfo 초기화
m_UUID = uuid; m_UUID = uuid;
m_Name = Name;
m_Nation = Nation; m_Nation = Nation;
m_Squad = Squad; m_Squad = Squad;
m_Account = Instance; m_Farm = Farm;
} }
public void JoinNation(int Nation) { m_Nation = Nation; } public void JoinNation(int Nation) { m_Nation = Nation; }
public void JoinSquad(int Squad) { m_Squad = Squad; } public void JoinSquad(int Squad) { m_Squad = Squad; }
public void JoinFarm(int Farm) { m_Farm = Farm; } public void JoinFarm(int Farm) { m_Farm = Farm; }
public int getBalance(){ return m_Account.getBalance();}
public String getUserName(){ public String getUserName(){
return m_Name; return m_Name;
} }
public UUID getUserUUID(){ public UUID getUserUUID(){ return m_UUID; }
return m_UUID;
}
} }

@ -1,29 +1,23 @@
package org.poopki.duckdns.user_db; package org.poopki.duckdns.user_db;
import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
public class UserInfoArray { public class UserInfoArray {
private AccountArray m_AccountArrayInstance; private static Map<UUID, UserInfo> m_UserInfoArray = new HashMap<UUID, UserInfo>();
public static Map<UUID, UserInfo> m_UserInfoArray = new HashMap<UUID, UserInfo>();
UserInfoArray(AccountArray Instance){ public String getUserName(UUID uuid){ // 유저 이름 return
m_AccountArrayInstance = Instance; return m_UserInfoArray.get(uuid).getUserName();
} }
public boolean checkNewUser(UUID uuid){ // 유저 목록 check
public void getUserInfo(Player p) { return m_UserInfoArray.containsKey(uuid);
UserInfo m_UserInfo = m_UserInfoArray.get(p.getUniqueId());
p.sendMessage(m_UserInfo.getUserName() + m_UserInfo.getUserUUID() + ("중복 입장"));
p.sendMessage(m_UserInfo.getUserUUID() +("Balance ")+m_AccountArrayInstance.getAccountInfo(p.getUniqueId()));
} }
public Set<UUID> getKeySet(){ // 유저 목록 return
public void NewUserInfo(Player p) { return m_UserInfoArray.keySet();
m_UserInfoArray.put(p.getUniqueId(), new UserInfo(p.getUniqueId(), 0, 0, 0, m_AccountArrayInstance.putAccountInfo(p.getUniqueId()))); }
UserInfo m_UserInfo = m_UserInfoArray.get(p.getUniqueId()); public void putUserInfo(UUID uuid,String Name, int Nation, int Squad, int Farm) { // UserInfoArray entry 입력
p.sendMessage(m_UserInfo.getUserName() + m_UserInfo.getUserUUID() + ("초기 입장")); m_UserInfoArray.put(uuid, new UserInfo(uuid,Name, Nation, Squad, Farm));
p.sendMessage(m_UserInfo.getUserUUID() +("Balance ")+m_AccountArrayInstance.getAccountInfo(p.getUniqueId()));
} }
} }

@ -6,18 +6,17 @@ import org.bukkit.event.player.PlayerJoinEvent;
public class UserJoin implements Listener { public class UserJoin implements Listener {
private UserInfoArray m_InfoArrayInstance; private UserInfoArray m_InfoArrayInstance;
UserJoin(UserInfoArray m_UIA){ private AccountArray m_AccountArray;
m_InfoArrayInstance = m_UIA; UserJoin(UserInfoArray UIA, AccountArray AA){
m_AccountArray = AA;
m_InfoArrayInstance = UIA;
} }
@EventHandler @EventHandler
public void PlayerLogin(PlayerJoinEvent e) { public void PlayerLogin(PlayerJoinEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
if(m_InfoArrayInstance.m_UserInfoArray.containsKey(p.getUniqueId())){ if(!m_InfoArrayInstance.checkNewUser(p.getUniqueId())){ //새로운 user인 경우 array에 등록
m_InfoArrayInstance.getUserInfo(p); m_InfoArrayInstance.putUserInfo(p.getUniqueId(), p.getName(), 0, 0, 0);
m_AccountArray.putAccountInfo(p.getUniqueId(), 0);
}
else{
m_InfoArrayInstance.NewUserInfo(p);
} }
} }
} }

@ -11,21 +11,21 @@ import java.sql.Statement;
public final class User_DB extends JavaPlugin implements Listener { public final class User_DB extends JavaPlugin implements Listener {
AccountArray m_AccountArrayInstance = new AccountArray(); AccountArray m_AccountArrayInstance = new AccountArray();
UserInfoArray m_InfoArrayInstance = new UserInfoArray(m_AccountArrayInstance); // UserInfo저장하는 Array instance 생성 UserInfoArray m_InfoArrayInstance = new UserInfoArray(); // UserInfo저장하는 Array instance 생성
DBUpdate Database_backup = new DBUpdate(m_InfoArrayInstance); DBUpdate Database_backup = new DBUpdate(m_InfoArrayInstance, m_AccountArrayInstance);
@Override @Override
public void onEnable() { public void onEnable() {
// Plugin startup logic // Plugin startup logic
try { try {
Database_backup.InitDB(); Database_backup.InitDB();
Database_backup.LoadDB(m_AccountArrayInstance); Database_backup.LoadDB();
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
getServer().getPluginManager().registerEvents(new UserJoin(m_InfoArrayInstance),this); getServer().getPluginManager().registerEvents(new UserJoin(m_InfoArrayInstance, m_AccountArrayInstance),this);
} }

Loading…
Cancel
Save