Account 모델 구현

Status
poopki 3 years ago
parent d110918f9e
commit 414c0e1a3e
  1. 32
      Account.java
  2. 37
      AccountArray.java
  3. 28
      DB/DBUpdate.java
  4. 14
      UserInfo.java
  5. 11
      UserInfoArray.java
  6. 3
      UserJoin.java
  7. 9
      User_DB.java

@ -0,0 +1,32 @@
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){
m_Balance = Balance;
}
public int getBalance(){ return m_Balance; }
public boolean credit(int Amount){
m_Balance +=Amount;
return true;
}
public boolean withdraw(int Amount){
if((m_Balance - Amount) > 0){
m_Balance -= Amount;
return true;
}
return false;
}
}

@ -0,0 +1,37 @@
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>();
public void transfer(UUID Src, UUID Des, int Amount){
Account m_Src = m_AccountArray.get(Src);
Account m_Des = m_AccountArray.get(Des);
if(m_Src.getBalance()-Amount>0){
m_Src.withdraw(Amount);
m_Des.credit(Amount);
}
}
public int getAccountInfo(UUID uuid) {
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) {
m_AccountArray.put(uuid, new Account(Amount));
Account m_Account = m_AccountArray.get(uuid);
return m_Account;
}
}

@ -1,12 +1,10 @@
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.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class DBUpdate {
@ -22,7 +20,7 @@ public class DBUpdate {
public static void InitDB() throws SQLException {
Connection conn = null;
Statement stmt = null;
Statement stmt;
try {
Class.forName("org.mariadb.jdbc.Driver");
@ -35,7 +33,8 @@ public class DBUpdate {
.append("UUID varchar(36) PRIMARY KEY,")
.append("Nation int(8),") //public
.append("Squad int(8),") //public
.append("Farm int(8)") //public
.append("Farm int(8),") //public
.append("Account int(10)") //public
/*
.append("Occupation int(8),") //private
.append("Account int(8),") //private
@ -57,9 +56,9 @@ public class DBUpdate {
}
}
}
public static void LoadDB() throws SQLException {
public static void LoadDB(AccountArray m_AccountArrayInstance) throws SQLException {
Connection conn = null;
Statement stmt = null;
Statement stmt;
try {
Class.forName("org.mariadb.jdbc.Driver");
@ -73,7 +72,11 @@ public class DBUpdate {
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))));
Integer.parseInt(rs.getString(4)),
m_AccountArrayInstance.putAccountInfo(
UUID.fromString(
rs.getString(1)),
Integer.parseInt(rs.getString(5)))));
}
} catch (ClassNotFoundException e) {
@ -93,7 +96,7 @@ public class DBUpdate {
public void UpdateDB() throws SQLException {
Connection conn = null;
PreparedStatement pstmt = null;
PreparedStatement pstmt;
try{
Class.forName("org.mariadb.jdbc.Driver");
@ -103,14 +106,15 @@ public class DBUpdate {
//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 (?,?,?,?)";
String sql = "REPLACE INTO UserInfo VALUES (?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
//pstmt.setString(1, key.toString());
pstmt.setString(1, String.valueOf(m_InfoArrayInstance.m_UserInfoArray.get(key).getUserUUID()));
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()));
int cnt = pstmt.executeUpdate();
if( cnt == 0 ){
System.out.println("데이터 입력 실패");

@ -10,29 +10,25 @@ public class UserInfo {
private int m_Nation;
private int m_Squad;
private int m_Farm;
private Account m_Account;
UserInfo(Player p){
m_UUID = p.getUniqueId();
m_Nation = 0;
m_Squad = 0;
m_Farm = 0;
}
public UserInfo(UUID uuid, int Nation, int Squad, int Farm){
public UserInfo(UUID uuid, int Nation, int Squad, int Farm, Account Instance){
m_UUID = uuid;
m_Nation = Nation;
m_Squad = Squad;
m_Farm = Farm;
m_Account = Instance;
}
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;
return m_UUID;
}

@ -7,16 +7,23 @@ import java.util.Map;
import java.util.UUID;
public class UserInfoArray {
private AccountArray m_AccountArrayInstance;
public static Map<UUID, UserInfo> m_UserInfoArray = new HashMap<UUID, UserInfo>();
UserInfoArray(AccountArray Instance){
m_AccountArrayInstance = Instance;
}
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 void putUserInfo(Player p) {
m_UserInfoArray.put(p.getUniqueId(), new UserInfo(p));
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()));
}
}

@ -14,9 +14,10 @@ public class UserJoin implements Listener {
Player p = e.getPlayer();
if(m_InfoArrayInstance.m_UserInfoArray.containsKey(p.getUniqueId())){
m_InfoArrayInstance.getUserInfo(p);
}
else{
m_InfoArrayInstance.putUserInfo(p);
m_InfoArrayInstance.NewUserInfo(p);
}
}
}

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

Loading…
Cancel
Save