Adding product to leagues.

This commit is contained in:
marcins78@gmail.com
2011-12-12 22:57:48 +00:00
parent efb1ada9ff
commit 85383463a2
6 changed files with 153 additions and 7 deletions

View File

@@ -10,6 +10,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -49,6 +50,47 @@ public class CollectionDAO {
return null;
}
public Map<Integer, MutableCardCollection> getPlayerCollectionsByType(String type) {
try {
Connection connection = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = connection.prepareStatement("select player_id, collection from collection where type=?");
try {
statement.setString(1, type);
ResultSet rs = statement.executeQuery();
try {
Map<Integer, MutableCardCollection> playerCollections = new HashMap<Integer, MutableCardCollection>();
while (rs.next()) {
int playerId = rs.getInt(1);
Blob blob = rs.getBlob(2);
try {
InputStream inputStream = blob.getBinaryStream();
try {
playerCollections.put(playerId, _collectionSerializer.deserializeCollection(inputStream));
} finally {
inputStream.close();
}
} finally {
blob.free();
}
}
return playerCollections;
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
connection.close();
}
} catch (SQLException exp) {
throw new RuntimeException("Unable to get player collection from DB", exp);
} catch (IOException exp) {
throw new RuntimeException("Unable to get player collection from DB", exp);
}
}
private MutableCardCollection getCollectionFromDB(Player player, String type) {
try {
Connection connection = _dbAccess.getDataSource().getConnection();

View File

@@ -53,6 +53,24 @@ public class LeagueDAO {
}
}
public void setBaseCollectionForLeague(League league, CardCollection collection) throws SQLException, IOException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("update league set collection=? where type=?");
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
_serializer.serializeCollection(collection, outputStream);
statement.setBlob(1, new ByteArrayInputStream(outputStream.toByteArray()));
statement.setString(2, league.getType());
statement.execute();
} finally {
statement.close();
}
} finally {
conn.close();
}
}
private void loadActiveLeagues(int currentTime) throws SQLException, IOException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {

View File

@@ -24,6 +24,33 @@ public class PlayerDAO {
_players.clear();
}
public Player getPlayer(int id) throws SQLException {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
PreparedStatement statement = conn.prepareStatement("select id, name, type from player where id=?");
try {
statement.setInt(1, id);
ResultSet rs = statement.executeQuery();
try {
if (rs.next()) {
String name = rs.getString(2);
String type = rs.getString(3);
return new Player(id, name, type);
} else {
return null;
}
} finally {
rs.close();
}
} finally {
statement.close();
}
} finally {
conn.close();
}
}
public Player getPlayer(String playerName) {
if (_players.containsKey(playerName))
return _players.get(playerName);

View File

@@ -9,7 +9,7 @@ import java.util.*;
import java.util.concurrent.CountDownLatch;
public class DefaultCardCollection implements MutableCardCollection {
private Map<String, Integer> _counts = new TreeMap<String, Integer>(new CardBlueprintIdComparator());
private Map<String, Integer> _counts = new HashMap<String, Integer>();
private CountDownLatch _collectionReadyLatch = new CountDownLatch(1);
@@ -153,7 +153,10 @@ public class DefaultCardCollection implements MutableCardCollection {
@Override
public void addPacks(String packId, int count) {
_counts.put(packId, count);
Integer oldCount = _counts.get(packId);
if (oldCount == null)
oldCount = 0;
_counts.put(packId, oldCount + count);
}
@Override

View File

@@ -4,19 +4,21 @@ import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.LeagueSeasonDAO;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.game.DefaultCardCollection;
import com.gempukku.lotro.game.LotroCardBlueprintLibrary;
import com.gempukku.lotro.game.MutableCardCollection;
import com.gempukku.lotro.game.Player;
import com.gempukku.lotro.hall.HallServer;
import com.sun.jersey.spi.resource.Singleton;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Singleton
@Path("/admin")
@@ -71,7 +73,7 @@ public class AdminResource extends AbstractResource {
validateAdmin(request);
DefaultCardCollection collection = new DefaultCardCollection();
String[] packs = packCollection.split("\n");
List<String> packs = getPacks(packCollection);
for (String pack : packs)
collection.addPacks(pack, 1);
collection.finishedReading();
@@ -98,6 +100,51 @@ public class AdminResource extends AbstractResource {
return "OK";
}
@Path("/addLeagueProduct")
@POST
public String addLeagueProduct(
@FormParam("leagueType") String leagueType,
@FormParam("packProduct") String packProduct,
@Context HttpServletRequest request) throws Exception {
validateAdmin(request);
League league = getLeagueByType(leagueType);
if (league == null)
throw new WebApplicationException(Response.Status.NOT_FOUND);
MutableCardCollection baseCollection = league.getBaseCollection();
List<String> packs = getPacks(packProduct);
for (String pack : packs)
baseCollection.addPacks(pack, 1);
_leagueDao.setBaseCollectionForLeague(league, baseCollection);
Map<Integer, MutableCardCollection> playerCollections = _collectionDao.getPlayerCollectionsByType(leagueType);
for (Map.Entry<Integer, MutableCardCollection> playerCollection : playerCollections.entrySet()) {
int playerId = playerCollection.getKey();
MutableCardCollection collection = playerCollection.getValue();
for (String pack : packs)
collection.addPacks(pack, 1);
Player player = _playerDao.getPlayer(playerId);
_collectionDao.setCollectionForPlayer(player, leagueType, collection);
}
return "OK";
}
private List<String> getPacks(String packProduct) {
List<String> result = new LinkedList<String>();
for (String pack : packProduct.split("\n"))
result.add(pack.trim());
return result;
}
private League getLeagueByType(String leagueType) {
for (League league : _leagueDao.getActiveLeagues())
if (league.getType().equals(leagueType))
return league;
return null;
}
private void validateAdmin(HttpServletRequest request) {
Player player = getResourceOwnerSafely(request, null);

View File

@@ -33,5 +33,14 @@
Max matches: <input type="text" name="maxMatches"><br/>
<input type="submit" value="Create">
</form>
<h2>Add league product</h2>
<form method="POST" action="server/admin/addLeagueProduct">
Type: <input type="text" name="leagueType"><br/>
Packs: <textarea rows="5" cols="20" name="packProduct"></textarea><br/>
<input type="submit" value="Create">
</form>
</body>
</html>