코드 최적화

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;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class Account {
private int m_Balance;
public Account(){
m_Balance = 0;
}
public Account(int Balance){
public Account(int Balance){ // Account Balance 초기화
m_Balance = Balance;
}
public int getBalance(){ return m_Balance; }
public boolean credit(int Amount){
public int getBalance(){ return m_Balance; } //잔액 return
public void credit(int Amount){ // 입금
m_Balance +=Amount;
return true;
}
public boolean withdraw(int Amount){
if((m_Balance - Amount) > 0){
public boolean withdraw(int Amount){ // 출금
if(m_Balance >= Amount){
m_Balance -= Amount;
return true;
}

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

@ -1,35 +1,35 @@
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 UserInfo {
private UUID m_UUID;
private String m_Name;
private static Map<String, Integer> m_status = new HashMap<String, Integer>();
private int m_Nation;
private int m_Squad;
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_Name = Name;
m_Nation = Nation;
m_Squad = Squad;
m_Account = Instance;
m_Farm = Farm;
}
public void JoinNation(int Nation) { m_Nation = Nation; }
public void JoinSquad(int Squad) { m_Squad = Squad; }
public void JoinFarm(int Farm) { m_Farm = Farm; }
public int getBalance(){ return m_Account.getBalance();}
public String getUserName(){
return m_Name;
}
public UUID getUserUUID(){
return m_UUID;
}
public UUID getUserUUID(){ return m_UUID; }
}

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

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

@ -11,21 +11,21 @@ import java.sql.Statement;
public final class User_DB extends JavaPlugin implements Listener {
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
public void onEnable() {
// Plugin startup logic
try {
Database_backup.InitDB();
Database_backup.LoadDB(m_AccountArrayInstance);
Database_backup.LoadDB();
} catch (SQLException 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