Rewrite MonsterBook, touch up chr loading
Temporarily disabled loading monster cards from db
This commit is contained in:
@@ -1,190 +1,106 @@
|
||||
/*
|
||||
This file is part of the OdinMS Maple Story Server
|
||||
Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
|
||||
Matthias Butz <matze@odinms.de>
|
||||
Jan Christian Meyer <vimes@odinms.de>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation version 3 as published by
|
||||
the Free Software Foundation. You may not use, modify or distribute
|
||||
this program under any other version of the GNU Affero General Public
|
||||
License.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package client;
|
||||
|
||||
import tools.DatabaseConnection;
|
||||
import database.monsterbook.MonsterCard;
|
||||
import net.jcip.annotations.ThreadSafe;
|
||||
import tools.PacketCreator;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public final class MonsterBook {
|
||||
private int specialCard = 0;
|
||||
private int normalCard = 0;
|
||||
private int bookLevel = 1;
|
||||
private final Map<Integer, Integer> cards = new LinkedHashMap<>();
|
||||
private final Lock lock = new ReentrantLock();
|
||||
// TODO: add tests
|
||||
@ThreadSafe
|
||||
public class MonsterBook {
|
||||
private final Map<Integer, MonsterCard> cards;
|
||||
private int bookLevel;
|
||||
|
||||
public void addCard(final Client c, final int cardid) {
|
||||
c.getPlayer().getMap().broadcastMessage(c.getPlayer(), PacketCreator.showForeignCardEffect(c.getPlayer().getId()), false);
|
||||
public MonsterBook(List<MonsterCard> monsterCards) {
|
||||
this.cards = monsterCards.stream()
|
||||
.collect(Collectors.toMap(MonsterCard::cardId, Function.identity()));
|
||||
}
|
||||
|
||||
Integer qty;
|
||||
lock.lock();
|
||||
try {
|
||||
qty = cards.get(cardid);
|
||||
public synchronized List<MonsterCard> getCards() {
|
||||
return new ArrayList<>(cards.values());
|
||||
}
|
||||
|
||||
if (qty != null) {
|
||||
if (qty < 5) {
|
||||
cards.put(cardid, qty + 1);
|
||||
}
|
||||
} else {
|
||||
cards.put(cardid, 1);
|
||||
qty = 0;
|
||||
|
||||
if (cardid / 1000 >= 2388) {
|
||||
specialCard++;
|
||||
} else {
|
||||
normalCard++;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
public synchronized void addCard(int cardId, Client client) {
|
||||
var monsterCard = cards.get(cardId);
|
||||
if (monsterCard != null && monsterCard.isMaxLevel()) {
|
||||
client.sendPacket(PacketCreator.addMonsterCardAlreadyFull());
|
||||
return;
|
||||
}
|
||||
|
||||
if (qty < 5) {
|
||||
if (qty == 0) { // leveling system only accounts unique cards
|
||||
calculateLevel();
|
||||
}
|
||||
|
||||
c.sendPacket(PacketCreator.addCard(false, cardid, qty + 1));
|
||||
c.sendPacket(PacketCreator.showGainCard());
|
||||
boolean isNewCard = monsterCard == null;
|
||||
final MonsterCard card;
|
||||
if (isNewCard) {
|
||||
card = new MonsterCard(cardId, (byte) 1);
|
||||
cards.put(cardId, card);
|
||||
calculateAndSetLevel();
|
||||
} else {
|
||||
c.sendPacket(PacketCreator.addCard(true, cardid, 5));
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateLevel() {
|
||||
lock.lock();
|
||||
try {
|
||||
int collectionExp = (normalCard + specialCard);
|
||||
|
||||
int level = 0, expToNextlevel = 1;
|
||||
do {
|
||||
level++;
|
||||
expToNextlevel += level * 10;
|
||||
} while (collectionExp >= expToNextlevel);
|
||||
|
||||
bookLevel = level; // thanks IxianMace for noticing book level differing between book UI and character info UI
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int getBookLevel() {
|
||||
lock.lock();
|
||||
try {
|
||||
return bookLevel;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Integer, Integer> getCards() {
|
||||
lock.lock();
|
||||
try {
|
||||
return Collections.unmodifiableMap(cards);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int getTotalCards() {
|
||||
lock.lock();
|
||||
try {
|
||||
return specialCard + normalCard;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int getNormalCard() {
|
||||
lock.lock();
|
||||
try {
|
||||
return normalCard;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int getSpecialCard() {
|
||||
lock.lock();
|
||||
try {
|
||||
return specialCard;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void loadCards(final int charid) throws SQLException {
|
||||
lock.lock();
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT cardid, level FROM monsterbook WHERE charid = ? ORDER BY cardid ASC")) {
|
||||
ps.setInt(1, charid);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
int cardid;
|
||||
int level;
|
||||
while (rs.next()) {
|
||||
cardid = rs.getInt("cardid");
|
||||
level = rs.getInt("level");
|
||||
if (cardid / 1000 >= 2388) {
|
||||
specialCard++;
|
||||
} else {
|
||||
normalCard++;
|
||||
}
|
||||
cards.put(cardid, level);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
card = new MonsterCard(cardId, (byte) (monsterCard.level() + 1));
|
||||
cards.put(cardId, card);
|
||||
}
|
||||
|
||||
calculateLevel();
|
||||
var chr = client.getPlayer();
|
||||
chr.sendPacket(PacketCreator.addMonsterCard(card));
|
||||
chr.sendPacket(PacketCreator.showMonsterCardEffect());
|
||||
chr.getMap().broadcastMessage(chr, PacketCreator.showForeignMonsterCardEffect(chr.getId()), false);
|
||||
}
|
||||
|
||||
public void saveCards(Connection con, int chrId) throws SQLException {
|
||||
private synchronized void calculateAndSetLevel() {
|
||||
int collectionExp = getTotalCards();
|
||||
|
||||
int level = 0;
|
||||
int expToNextLevel = 1;
|
||||
do {
|
||||
level++;
|
||||
expToNextLevel += level * 10;
|
||||
} while (collectionExp >= expToNextLevel);
|
||||
|
||||
this.bookLevel = level;
|
||||
}
|
||||
|
||||
public synchronized int getBookLevel() {
|
||||
return bookLevel;
|
||||
}
|
||||
|
||||
public synchronized int getNormalCards() {
|
||||
return (int) cards.values().stream()
|
||||
.filter(Predicate.not(MonsterCard::isSpecial))
|
||||
.count();
|
||||
}
|
||||
|
||||
public synchronized int getSpecialCards() {
|
||||
return (int) cards.values().stream()
|
||||
.filter(MonsterCard::isSpecial)
|
||||
.count();
|
||||
}
|
||||
|
||||
public synchronized int getTotalCards() {
|
||||
return cards.size();
|
||||
}
|
||||
|
||||
public synchronized void saveCards(Connection con, int chrId) throws SQLException {
|
||||
final String query = """
|
||||
INSERT INTO monsterbook (charid, cardid, level)
|
||||
VALUES (?, ?, ?)
|
||||
ON DUPLICATE KEY UPDATE level = ?;
|
||||
""";
|
||||
try (final PreparedStatement ps = con.prepareStatement(query)) {
|
||||
for (Map.Entry<Integer, Integer> cardAndLevel : cards.entrySet()) {
|
||||
final int card = cardAndLevel.getKey();
|
||||
final int level = cardAndLevel.getValue();
|
||||
for (MonsterCard card : cards.values()) {
|
||||
// insert
|
||||
ps.setInt(1, chrId);
|
||||
ps.setInt(2, card);
|
||||
ps.setInt(3, level);
|
||||
ps.setInt(2, card.cardId());
|
||||
ps.setInt(3, card.level());
|
||||
|
||||
// update
|
||||
ps.setInt(4, level);
|
||||
ps.setInt(4, card.level());
|
||||
|
||||
ps.addBatch();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user