16
16
import pw .chew .mlb .objects .GameBlurb ;
17
17
import pw .chew .mlb .objects .GameState ;
18
18
19
+ import java .time .OffsetDateTime ;
19
20
import java .util .ArrayList ;
20
21
import java .util .List ;
21
22
@@ -25,7 +26,8 @@ public BettingCommand() {
25
26
this .help = "Bet on a team" ;
26
27
this .children = new SlashCommand []{
27
28
new BettingProfileSubCommand (),
28
- new BettingBetSubCommand ()
29
+ new BettingBetSubCommand (),
30
+ new BettingClaimSubCommand ()
29
31
};
30
32
}
31
33
@@ -77,13 +79,84 @@ private MessageEmbed buildProfileEmbed(SlashCommandEvent event, Profile profile,
77
79
}
78
80
}
79
81
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
+
80
153
public static class BettingBetSubCommand extends SlashCommand {
81
154
public BettingBetSubCommand () {
82
155
this .name = "bet" ;
83
156
this .help = "Bet on a team" ;
84
157
this .options = List .of (
85
158
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 ),
87
160
new OptionData (OptionType .INTEGER , "amount" , "How much to bet. 0 to remove bet." , true , false )
88
161
.setMinValue (0 )
89
162
);
@@ -93,7 +166,7 @@ public BettingBetSubCommand() {
93
166
protected void execute (SlashCommandEvent event ) {
94
167
// options
95
168
int teamId = (int ) event .optLong ("team" , 0 );
96
- int gamePk = (int ) event .optLong ("game " , 0 );
169
+ int gamePk = (int ) event .optLong ("date " , 0 );
97
170
int amount = (int ) event .optLong ("amount" , 0 );
98
171
99
172
// Check the game status
@@ -103,7 +176,7 @@ protected void execute(SlashCommandEvent event) {
103
176
return ;
104
177
}
105
178
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 ();
107
180
return ;
108
181
}
109
182
@@ -193,6 +266,18 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
193
266
}
194
267
}
195
268
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
+
196
281
public static Profile retrieveProfile (long userId ) {
197
282
var session = DatabaseHelper .getSessionFactory ().openSession ();
198
283
Profile profile = session .find (Profile .class , userId );
0 commit comments