Awarding weekly allowance and storing currency.
This commit is contained in:
@@ -4,8 +4,9 @@ public class Player {
|
||||
private int _id;
|
||||
private String _name;
|
||||
private String _type;
|
||||
private Integer _lastLoginReward;
|
||||
|
||||
public Player(int id, String name, String type) {
|
||||
public Player(int id, String name, String type, Integer lastLoginReward) {
|
||||
_id = id;
|
||||
_name = name;
|
||||
_type = type;
|
||||
@@ -23,6 +24,14 @@ public class Player {
|
||||
return _type;
|
||||
}
|
||||
|
||||
public Integer getLastLoginReward() {
|
||||
return _lastLoginReward;
|
||||
}
|
||||
|
||||
public void setLastLoginReward(int lastLoginReward) {
|
||||
_lastLoginReward = lastLoginReward;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.gempukku.lotro;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateUtils {
|
||||
public static int getCurrentDate() {
|
||||
Calendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
|
||||
return date.get(Calendar.YEAR) * 10000 + (date.get(Calendar.MONTH) + 1) * 100 + date.get(Calendar.DAY_OF_MONTH);
|
||||
}
|
||||
|
||||
public static int offsetDate(int start, int dayOffset) {
|
||||
try {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
||||
Date date = format.parse(String.valueOf(start));
|
||||
date.setDate(date.getDate() + dayOffset);
|
||||
return Integer.parseInt(format.format(date));
|
||||
} catch (ParseException exp) {
|
||||
throw new RuntimeException("Can't parse date", exp);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getMondayBeforeOrOn(int date) {
|
||||
try {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
||||
Date current = format.parse(String.valueOf(date));
|
||||
if (current.getDay() == 0)
|
||||
return offsetDate(date, -6);
|
||||
else
|
||||
return offsetDate(date, 1 - current.getDay());
|
||||
} catch (ParseException exp) {
|
||||
throw new RuntimeException("Can't parse date", exp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,21 @@ public class CollectionsManager {
|
||||
addItemsToPlayerCollection(_playerDAO.getPlayer(player), collectionType, items);
|
||||
}
|
||||
|
||||
public void addCurrencyToPlayerCollection(Player player, CollectionType collectionType, int currency) {
|
||||
_readWriteLock.writeLock().lock();
|
||||
try {
|
||||
final CardCollection playerCollection = getPlayerCollection(player, collectionType.getCode());
|
||||
if (playerCollection != null) {
|
||||
MutableCardCollection mutableCardCollection = new DefaultCardCollection(playerCollection);
|
||||
mutableCardCollection.addCurrency(currency);
|
||||
|
||||
_collectionDAO.setCollectionForPlayer(player.getId(), collectionType.getCode(), mutableCardCollection);
|
||||
}
|
||||
} finally {
|
||||
_readWriteLock.writeLock().unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public void moveCollectionToCollection(String player, CollectionType collectionFrom, CollectionType collectionTo) {
|
||||
moveCollectionToCollection(_playerDAO.getPlayer(player), collectionFrom, collectionTo);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public class PlayerDAO {
|
||||
try {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type from player where id=?");
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type, last_login_reward from player where id=?");
|
||||
try {
|
||||
statement.setInt(1, id);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
@@ -36,8 +36,11 @@ public class PlayerDAO {
|
||||
if (rs.next()) {
|
||||
String name = rs.getString(2);
|
||||
String type = rs.getString(3);
|
||||
Integer lastLoginReward = rs.getInt(4);
|
||||
if (rs.wasNull())
|
||||
lastLoginReward = null;
|
||||
|
||||
return new Player(id, name, type);
|
||||
return new Player(id, name, type, lastLoginReward);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -75,7 +78,7 @@ public class PlayerDAO {
|
||||
public Player loginUser(String login, String password) throws SQLException {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type from player where name=? and password=?");
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type, last_login_reward from player where name=? and password=?");
|
||||
try {
|
||||
statement.setString(1, login);
|
||||
statement.setString(2, encodePassword(password));
|
||||
@@ -85,8 +88,11 @@ public class PlayerDAO {
|
||||
int id = rs.getInt(1);
|
||||
String name = rs.getString(2);
|
||||
String type = rs.getString(3);
|
||||
Integer lastLoginReward = rs.getInt(4);
|
||||
if (rs.wasNull())
|
||||
lastLoginReward = null;
|
||||
|
||||
return new Player(id, name, type);
|
||||
return new Player(id, name, type, lastLoginReward);
|
||||
} else
|
||||
return null;
|
||||
} finally {
|
||||
@@ -100,6 +106,44 @@ public class PlayerDAO {
|
||||
}
|
||||
}
|
||||
|
||||
public void setLastReward(Player player, int currentReward) throws SQLException {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("update player set last_login_reward =? where id=?");
|
||||
try {
|
||||
statement.setInt(1, currentReward);
|
||||
statement.setInt(2, player.getId());
|
||||
statement.execute();
|
||||
player.setLastLoginReward(currentReward);
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean updateLastReward(Player player, int previousReward, int currentReward) throws SQLException {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("update player set last_login_reward =? where id=? and last_login_reward=?");
|
||||
try {
|
||||
statement.setInt(1, currentReward);
|
||||
statement.setInt(2, player.getId());
|
||||
statement.setInt(3, previousReward);
|
||||
if (statement.executeUpdate() == 1) {
|
||||
player.setLastLoginReward(currentReward);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} finally {
|
||||
statement.close();
|
||||
}
|
||||
} finally {
|
||||
conn.close();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean registerUser(String login, String password) throws SQLException {
|
||||
boolean result = validateLogin(login);
|
||||
if (!result)
|
||||
@@ -181,7 +225,7 @@ public class PlayerDAO {
|
||||
private Player getPlayerFromDB(String playerName) throws SQLException {
|
||||
Connection conn = _dbAccess.getDataSource().getConnection();
|
||||
try {
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type from player where name=?");
|
||||
PreparedStatement statement = conn.prepareStatement("select id, name, type, last_login_reward from player where name=?");
|
||||
try {
|
||||
statement.setString(1, playerName);
|
||||
ResultSet rs = statement.executeQuery();
|
||||
@@ -190,8 +234,11 @@ public class PlayerDAO {
|
||||
int id = rs.getInt(1);
|
||||
String name = rs.getString(2);
|
||||
String type = rs.getString(3);
|
||||
Integer lastLoginReward = rs.getInt(4);
|
||||
if (rs.wasNull())
|
||||
lastLoginReward = null;
|
||||
|
||||
return new Player(id, name, type);
|
||||
return new Player(id, name, type, lastLoginReward);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -7,5 +7,9 @@ public interface MutableCardCollection extends CardCollection {
|
||||
|
||||
public boolean removeItem(String itemId, int count);
|
||||
|
||||
public void addCurrency(int currency);
|
||||
|
||||
public boolean removeCurrency(int currency);
|
||||
|
||||
public CardCollection openPack(String packId, String selection, PacksStorage packBox);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class ConstructedLeagueData implements LeagueData {
|
||||
@@ -36,22 +34,13 @@ public class ConstructedLeagueData implements LeagueData {
|
||||
String format = params[8 + i * 2];
|
||||
String seriePrizePool = params[9 + i * 2];
|
||||
|
||||
DefaultLeagueSerieData data = new DefaultLeagueSerieData(_leaguePrizes, false, "Week " + (i + 1), getDate(start, i * days), getDate(start, ((i + 1) * days) - 1), matchCount, format, seriePrizePool, collectionType);
|
||||
DefaultLeagueSerieData data = new DefaultLeagueSerieData(_leaguePrizes, false, "Week " + (i + 1),
|
||||
DateUtils.offsetDate(start, i * days), DateUtils.offsetDate(start, ((i + 1) * days) - 1),
|
||||
matchCount, format, seriePrizePool, collectionType);
|
||||
_series.add(data);
|
||||
}
|
||||
}
|
||||
|
||||
private int getDate(int start, int dayOffset) {
|
||||
try {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
||||
Date date = format.parse(String.valueOf(start));
|
||||
date.setDate(date.getDate() + dayOffset);
|
||||
return Integer.parseInt(format.format(date));
|
||||
} catch (ParseException exp) {
|
||||
throw new RuntimeException("Can't parse date", exp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LeagueSerieData> getSeries() {
|
||||
return _series;
|
||||
@@ -67,7 +56,7 @@ public class ConstructedLeagueData implements LeagueData {
|
||||
int status = oldStatus;
|
||||
if (status == 0) {
|
||||
LeagueSerieData lastSerie = _series.get(_series.size() - 1);
|
||||
if (currentTime > getDate(lastSerie.getEnd(), 1)) {
|
||||
if (currentTime > DateUtils.offsetDate(lastSerie.getEnd(), 1)) {
|
||||
for (LeagueStanding leagueStanding : leagueStandings) {
|
||||
CardCollection leaguePrize = _leaguePrizes.getPrizeForLeague(leagueStanding.getStanding(), leagueStandings.size(), _prizeMultiplier, _leaguePrizePool);
|
||||
collectionsManager.addItemsToPlayerCollection(leagueStanding.getPlayerName(), _prizeCollectionType, leaguePrize.getAll());
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.LeagueDAO;
|
||||
import com.gempukku.lotro.db.LeagueMatchDAO;
|
||||
@@ -47,13 +48,8 @@ public class LeagueService {
|
||||
_activeLeaguesLoadedDate = 0;
|
||||
}
|
||||
|
||||
private int getCurrentDate() {
|
||||
Calendar date = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
|
||||
return date.get(Calendar.YEAR) * 10000 + (date.get(Calendar.MONTH) + 1) * 100 + date.get(Calendar.DAY_OF_MONTH);
|
||||
}
|
||||
|
||||
private synchronized void ensureLoadedCurrentLeagues() {
|
||||
int currentDate = getCurrentDate();
|
||||
int currentDate = DateUtils.getCurrentDate();
|
||||
if (currentDate != _activeLeaguesLoadedDate) {
|
||||
try {
|
||||
_activeLeagues = _leagueDao.loadActiveLeagues(currentDate);
|
||||
@@ -68,7 +64,7 @@ public class LeagueService {
|
||||
}
|
||||
|
||||
public Set<League> getActiveLeagues() {
|
||||
if (getCurrentDate() == _activeLeaguesLoadedDate)
|
||||
if (DateUtils.getCurrentDate() == _activeLeaguesLoadedDate)
|
||||
return Collections.unmodifiableSet(_activeLeagues);
|
||||
else {
|
||||
ensureLoadedCurrentLeagues();
|
||||
@@ -91,7 +87,7 @@ public class LeagueService {
|
||||
for (LeagueSerieData leagueSerieData : leagueData.getSeries()) {
|
||||
CollectionType serieCollectionType = leagueSerieData.getCollectionType();
|
||||
if (serieCollectionType != null && serieCollectionType.getCode().equals(collectionType)) {
|
||||
return leagueData.joinLeague(_collectionsManager, player, getCurrentDate());
|
||||
return leagueData.joinLeague(_collectionsManager, player, DateUtils.getCurrentDate());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +114,7 @@ public class LeagueService {
|
||||
}
|
||||
|
||||
public LeagueSerieData getCurrentLeagueSerie(League league) {
|
||||
final int currentDate = getCurrentDate();
|
||||
final int currentDate = DateUtils.getCurrentDate();
|
||||
|
||||
for (LeagueSerieData leagueSerieData : league.getLeagueData().getSeries()) {
|
||||
if (currentDate >= leagueSerieData.getStart() && currentDate <= leagueSerieData.getEnd())
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.gempukku.lotro.league;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.game.CardCollection;
|
||||
@@ -7,9 +8,10 @@ import com.gempukku.lotro.game.DefaultCardCollection;
|
||||
import com.gempukku.lotro.game.MutableCardCollection;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SealedLeagueData implements LeagueData {
|
||||
private String _format;
|
||||
@@ -35,22 +37,11 @@ public class SealedLeagueData implements LeagueData {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
_series.add(
|
||||
new DefaultLeagueSerieData(_leaguePrizes, true, "Week " + (i + 1),
|
||||
getDate(start, i * serieDuration), getDate(start, (i + 1) * serieDuration - 1), maxMatches,
|
||||
DateUtils.offsetDate(start, i * serieDuration), DateUtils.offsetDate(start, (i + 1) * serieDuration - 1), maxMatches,
|
||||
_format, _format, _collectionType));
|
||||
}
|
||||
}
|
||||
|
||||
private int getDate(int start, int dayOffset) {
|
||||
try {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
||||
Date date = format.parse(String.valueOf(start));
|
||||
date.setDate(date.getDate() + dayOffset);
|
||||
return Integer.parseInt(format.format(date));
|
||||
} catch (ParseException exp) {
|
||||
throw new RuntimeException("Can't parse date", exp);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LeagueSerieData> getSeries() {
|
||||
return Collections.unmodifiableList(_series);
|
||||
@@ -90,7 +81,7 @@ public class SealedLeagueData implements LeagueData {
|
||||
|
||||
if (status == _series.size()) {
|
||||
LeagueSerieData lastSerie = _series.get(_series.size() - 1);
|
||||
if (currentTime > getDate(lastSerie.getEnd(), 1)) {
|
||||
if (currentTime > DateUtils.offsetDate(lastSerie.getEnd(), 1)) {
|
||||
for (LeagueStanding leagueStanding : leagueStandings) {
|
||||
CardCollection leaguePrize = _leaguePrizes.getPrizeForLeague(leagueStanding.getStanding(), leagueStandings.size(), 1f, _format);
|
||||
collectionsManager.addItemsToPlayerCollection(leagueStanding.getPlayerName(), _prizeCollectionType, leaguePrize.getAll());
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.gempukku.lotro.server;
|
||||
|
||||
import com.gempukku.lotro.DateUtils;
|
||||
import com.gempukku.lotro.collection.CollectionsManager;
|
||||
import com.gempukku.lotro.collection.DeliveryService;
|
||||
import com.gempukku.lotro.db.PlayerDAO;
|
||||
import com.gempukku.lotro.db.vo.CollectionType;
|
||||
import com.gempukku.lotro.game.Player;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -15,10 +18,33 @@ public abstract class AbstractResource {
|
||||
|
||||
@Context
|
||||
protected PlayerDAO _playerDao;
|
||||
@Context
|
||||
protected CollectionsManager _collectionManager;
|
||||
|
||||
@Context
|
||||
protected DeliveryService _deliveryService;
|
||||
|
||||
protected final void processLoginReward(HttpServletRequest request) throws Exception {
|
||||
String logged = getLoggedUser(request);
|
||||
if (logged != null) {
|
||||
Player player = _playerDao.getPlayer(logged);
|
||||
|
||||
int currentDate = DateUtils.getCurrentDate();
|
||||
int latestMonday = DateUtils.getMondayBeforeOrOn(currentDate);
|
||||
|
||||
Integer lastReward = player.getLastLoginReward();
|
||||
if (lastReward == null) {
|
||||
_playerDao.setLastReward(player, latestMonday);
|
||||
_collectionManager.addCurrencyToPlayerCollection(player, new CollectionType("permanent", "My cards"), 5000);
|
||||
} else {
|
||||
if (latestMonday == lastReward) {
|
||||
if (_playerDao.updateLastReward(player, lastReward, latestMonday))
|
||||
_collectionManager.addCurrencyToPlayerCollection(player, new CollectionType("permanent", "My cards"), 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected final void processDeliveryServiceNotification(HttpServletRequest request, HttpServletResponse response) {
|
||||
String logged = getLoggedUser(request);
|
||||
if (logged != null && _deliveryService.hasUndeliveredPackages(logged))
|
||||
|
||||
@@ -36,7 +36,7 @@ public class HallResource extends AbstractResource {
|
||||
public Document getHall(
|
||||
@QueryParam("participantId") String participantId,
|
||||
@Context HttpServletRequest request,
|
||||
@Context HttpServletResponse response) throws ParserConfigurationException {
|
||||
@Context HttpServletResponse response) throws ParserConfigurationException, Exception {
|
||||
Player resourceOwner = getResourceOwnerSafely(request, participantId);
|
||||
|
||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
@@ -68,6 +68,7 @@ public class HallResource extends AbstractResource {
|
||||
|
||||
doc.appendChild(hall);
|
||||
|
||||
processLoginReward(request);
|
||||
processDeliveryServiceNotification(request, response);
|
||||
|
||||
return doc;
|
||||
|
||||
Reference in New Issue
Block a user