Skip to content

Commit adc3e4a

Browse files
committed
/betting claim and some more work
1 parent e2bc319 commit adc3e4a

File tree

3 files changed

+101
-12
lines changed

3 files changed

+101
-12
lines changed

src/main/java/pw/chew/mlb/commands/BettingCommand.java

Lines changed: 89 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import pw.chew.mlb.objects.GameBlurb;
1717
import pw.chew.mlb.objects.GameState;
1818

19+
import java.time.OffsetDateTime;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -25,7 +26,8 @@ public BettingCommand() {
2526
this.help = "Bet on a team";
2627
this.children = new SlashCommand[]{
2728
new BettingProfileSubCommand(),
28-
new BettingBetSubCommand()
29+
new BettingBetSubCommand(),
30+
new BettingClaimSubCommand()
2931
};
3032
}
3133

@@ -77,13 +79,84 @@ private MessageEmbed buildProfileEmbed(SlashCommandEvent event, Profile profile,
7779
}
7880
}
7981

82+
public static class BettingClaimSubCommand extends SlashCommand {
83+
public BettingClaimSubCommand() {
84+
this.name = "claim";
85+
this.help = "Claim your free daily credits";
86+
}
87+
88+
@Override
89+
protected void execute(SlashCommandEvent event) {
90+
Bet recentDailyCredit = getRecentDailyCredit(event.getUser().getIdLong());
91+
long lastRecentDaily = 0;
92+
93+
if (recentDailyCredit != null) {
94+
lastRecentDaily = recentDailyCredit.getCreatedAt().getEpochSecond();
95+
}
96+
97+
long now = OffsetDateTime.now().toEpochSecond();
98+
long diff = now - lastRecentDaily;
99+
100+
// must be less than 24 hours
101+
if (diff < 86400) {
102+
long canClaimAt = lastRecentDaily + 86400;
103+
104+
event.reply("You already claimed your daily credits! You can claim again <t:" + canClaimAt + ":R>").setEphemeral(true).queue();
105+
return;
106+
}
107+
108+
// add daily credit
109+
addDailyCredit(event.getUser().getIdLong());
110+
111+
event.reply("You have claimed your daily credits!").setEphemeral(true).queue();
112+
}
113+
114+
public Bet getRecentDailyCredit(long userId) {
115+
var session = DatabaseHelper.getSessionFactory().openSession();
116+
117+
// get Bets where user_id == userId
118+
List<Bet> bets = session.createQuery("from Bet where userId = :userId and kind = :kind and reason = :reason order by createdAt desc", Bet.class)
119+
.setParameter("userId", userId)
120+
.setParameter("reason", "Daily Credits")
121+
.setParameter("kind", BetKind.AUTOMATED)
122+
.getResultList();
123+
124+
session.close();
125+
126+
if (bets.isEmpty()) {
127+
return null;
128+
}
129+
130+
return bets.get(0);
131+
}
132+
133+
public void addDailyCredit(long userId) {
134+
var session = DatabaseHelper.getSessionFactory().openSession();
135+
136+
Bet bet = new Bet();
137+
bet.setKind(BetKind.AUTOMATED);
138+
bet.setBet(0);
139+
bet.setPayout(10);
140+
bet.setReason("Daily Credits");
141+
bet.setUserId(userId);
142+
143+
Profile profile = retrieveProfile(userId);
144+
profile.setCredits(profile.getCredits() + 10);
145+
146+
Transaction trans = session.beginTransaction();
147+
session.update(profile);
148+
session.save(bet);
149+
trans.commit();
150+
}
151+
}
152+
80153
public static class BettingBetSubCommand extends SlashCommand {
81154
public BettingBetSubCommand() {
82155
this.name = "bet";
83156
this.help = "Bet on a team";
84157
this.options = List.of(
85158
new OptionData(OptionType.INTEGER, "team", "Which team to bet on", true, true),
86-
new OptionData(OptionType.INTEGER, "game", "Which game to bet on", true, true),
159+
new OptionData(OptionType.INTEGER, "date", "Which game to bet on", true, true),
87160
new OptionData(OptionType.INTEGER, "amount", "How much to bet. 0 to remove bet.", true, false)
88161
.setMinValue(0)
89162
);
@@ -93,7 +166,7 @@ public BettingBetSubCommand() {
93166
protected void execute(SlashCommandEvent event) {
94167
// options
95168
int teamId = (int) event.optLong("team", 0);
96-
int gamePk = (int) event.optLong("game", 0);
169+
int gamePk = (int) event.optLong("date", 0);
97170
int amount = (int) event.optLong("amount", 0);
98171

99172
// Check the game status
@@ -103,7 +176,7 @@ protected void execute(SlashCommandEvent event) {
103176
return;
104177
}
105178
if (state.inning() > 4) {
106-
event.reply("Bets cannot be placed or changed past the 4th inning! To see your bet, run /betting placed").setEphemeral(true).queue();
179+
event.reply("Bets cannot be placed or changed past the 4th inning! To see your bet, run `/betting placed`").setEphemeral(true).queue();
107180
return;
108181
}
109182

@@ -193,6 +266,18 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
193266
}
194267
}
195268

269+
public static class BettingPlacedSubCommand extends SlashCommand {
270+
public BettingPlacedSubCommand() {
271+
this.name = "placed";
272+
this.help = "View your placed bets";
273+
}
274+
275+
@Override
276+
protected void execute(SlashCommandEvent event) {
277+
278+
}
279+
}
280+
196281
public static Profile retrieveProfile(long userId) {
197282
var session = DatabaseHelper.getSessionFactory().openSession();
198283
Profile profile = session.find(Profile.class, userId);

src/main/java/pw/chew/mlb/listeners/GameFeedHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ public static void endGame(String gamePk, GameState currentState, String scoreca
520520
removeThread(gamePk);
521521

522522
// Handle bets
523-
List<Bet> bets = BetHelper.betsForGame(gamePk);
524-
BetHelper.awardWinners(bets, currentState.winningTeam());
523+
LoggerFactory.getLogger(GameFeedHandler.class).debug("Awarding bets for game " + gamePk);
524+
BetHelper.awardWinners(gamePk, currentState.winningTeam());
525525
}
526526

527527
/**

src/main/java/pw/chew/mlb/objects/BetHelper.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
package pw.chew.mlb.objects;
22

33
import org.hibernate.Transaction;
4+
import org.slf4j.LoggerFactory;
45
import pw.chew.chewbotcca.util.DatabaseHelper;
6+
import pw.chew.mlb.listeners.GameFeedHandler;
57
import pw.chew.mlb.models.Bet;
68
import pw.chew.mlb.models.BetKind;
79

810
import java.util.ArrayList;
911
import java.util.List;
1012

1113
public class BetHelper {
12-
public static List<Bet> betsForGame(String gamePk) {
14+
public static void awardWinners(String gamePk, int winningTeam) {
1315
var session = DatabaseHelper.getSessionFactory().openSession();
1416

1517
// get all sessions where bet's gamePk is the gamePk
16-
return session.createQuery("from Bet where gamePk = :gamePk", Bet.class)
17-
.setParameter("gamePk", gamePk)
18+
var bets = session.createQuery("from Bet where gamePk = :gamePk", Bet.class)
19+
.setParameter("gamePk", Integer.parseInt(gamePk))
1820
.getResultList();
19-
}
2021

21-
public static void awardWinners(List<Bet> bets, int winningTeam) {
22+
LoggerFactory.getLogger(BetHelper.class).debug("Bets for game {}: {}", gamePk, bets.size());
23+
24+
LoggerFactory.getLogger(BetHelper.class).debug("Winning team: {}", winningTeam);
2225
int totalPoints = 0;
2326
int winningPoints = 0;
2427

@@ -40,7 +43,6 @@ public static void awardWinners(List<Bet> bets, int winningTeam) {
4043
}
4144
}
4245

43-
var session = DatabaseHelper.getSessionFactory().openSession();
4446
Transaction trans = session.beginTransaction();
4547

4648
// first let's set all the losers and make sure they lose their points
@@ -55,6 +57,8 @@ public static void awardWinners(List<Bet> bets, int winningTeam) {
5557
bet.setKind(BetKind.WIN);
5658
}
5759

60+
LoggerFactory.getLogger(GameFeedHandler.class).debug("Awarding winners: {}", winningBets);
61+
5862
// save all the bets
5963
trans.commit();
6064
}

0 commit comments

Comments
 (0)