4
4
import com .jagrosh .jdautilities .command .SlashCommandEvent ;
5
5
import net .dv8tion .jda .api .EmbedBuilder ;
6
6
import net .dv8tion .jda .api .entities .MessageEmbed ;
7
+ import net .dv8tion .jda .api .events .interaction .command .CommandAutoCompleteInteractionEvent ;
8
+ import net .dv8tion .jda .api .interactions .commands .OptionType ;
9
+ import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
7
10
import net .dv8tion .jda .api .utils .TimeFormat ;
8
11
import org .hibernate .Transaction ;
9
12
import pw .chew .chewbotcca .util .DatabaseHelper ;
10
13
import pw .chew .mlb .models .Bet ;
14
+ import pw .chew .mlb .models .BetKind ;
11
15
import pw .chew .mlb .models .Profile ;
16
+ import pw .chew .mlb .objects .GameBlurb ;
17
+ import pw .chew .mlb .objects .GameState ;
12
18
13
19
import java .util .ArrayList ;
14
20
import java .util .List ;
@@ -18,7 +24,8 @@ public BettingCommand() {
18
24
this .name = "betting" ;
19
25
this .help = "Bet on a team" ;
20
26
this .children = new SlashCommand []{
21
- new BettingProfileSubCommand ()
27
+ new BettingProfileSubCommand (),
28
+ new BettingBetSubCommand ()
22
29
};
23
30
}
24
31
@@ -40,7 +47,7 @@ protected void execute(SlashCommandEvent event) {
40
47
List <Bet > bets = retrieveBets (event .getUser ().getIdLong ());
41
48
42
49
// build embed
43
- event .replyEmbeds (buildProfileEmbed (event , profile , bets )).queue ();
50
+ event .replyEmbeds (buildProfileEmbed (event , profile , bets )).setEphemeral ( true ). queue ();
44
51
}
45
52
46
53
private MessageEmbed buildProfileEmbed (SlashCommandEvent event , Profile profile , List <Bet > bets ) {
@@ -58,8 +65,8 @@ private MessageEmbed buildProfileEmbed(SlashCommandEvent event, Profile profile,
58
65
Bet bet = bets .get (i );
59
66
betString .add ("%s | %s%s - %s" .formatted (
60
67
TimeFormat .DATE_SHORT .format (bet .getCreatedAt ()),
61
- bet .getSuccessful () ? "+" : "- " ,
62
- bet .getAmount (),
68
+ bet .amount () > 0 ? "+" : "" ,
69
+ bet .amount (),
63
70
bet .getReason ()
64
71
));
65
72
}
@@ -70,6 +77,122 @@ private MessageEmbed buildProfileEmbed(SlashCommandEvent event, Profile profile,
70
77
}
71
78
}
72
79
80
+ public static class BettingBetSubCommand extends SlashCommand {
81
+ public BettingBetSubCommand () {
82
+ this .name = "bet" ;
83
+ this .help = "Bet on a team" ;
84
+ this .options = List .of (
85
+ 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 ),
87
+ new OptionData (OptionType .INTEGER , "amount" , "How much to bet. 0 to remove bet." , true , false )
88
+ .setMinValue (0 )
89
+ );
90
+ }
91
+
92
+ @ Override
93
+ protected void execute (SlashCommandEvent event ) {
94
+ // options
95
+ int teamId = (int ) event .optLong ("team" , 0 );
96
+ int gamePk = (int ) event .optLong ("game" , 0 );
97
+ int amount = (int ) event .optLong ("amount" , 0 );
98
+
99
+ // Check the game status
100
+ GameState state = GameState .fromPk (String .valueOf (gamePk ));
101
+ if (state .isFinal ()) {
102
+ event .reply ("This game is already over!" ).setEphemeral (true ).queue ();
103
+ return ;
104
+ }
105
+ 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 ();
107
+ return ;
108
+ }
109
+
110
+ // start session
111
+ var session = DatabaseHelper .getSessionFactory ().openSession ();
112
+
113
+ // check for a bet, check by user_id, game_pk, and team_id
114
+ Bet bet = session .createQuery ("from Bet where userId = :userId and gamePk = :gamePk and teamId = :teamId" , Bet .class )
115
+ .setParameter ("userId" , event .getUser ().getIdLong ())
116
+ .setParameter ("gamePk" , gamePk )
117
+ .setParameter ("teamId" , teamId )
118
+ .uniqueResult ();
119
+
120
+ // Ensure we have enough credit to bet
121
+ Profile user = retrieveProfile (event .getUser ().getIdLong ());
122
+
123
+ // Get game blurb
124
+ GameBlurb blurb = new GameBlurb (gamePk + "" );
125
+
126
+ // if bet exists, update it
127
+ if (bet == null ) {
128
+ if (amount == 0 ) {
129
+ event .reply ("You must bet at least 1 credit!" ).setEphemeral (true ).queue ();
130
+ return ;
131
+ }
132
+
133
+ if (user .getCredits () < amount ) {
134
+ event .reply ("You don't have enough credits to bet that much! You only have " + user .getCredits () + " credits!" ).setEphemeral (true ).queue ();
135
+ return ;
136
+ }
137
+
138
+ String team = blurb .away ().id () == teamId ? blurb .away ().name () : blurb .home ().name ();
139
+
140
+ Bet newBet = new Bet ();
141
+ newBet .setUserId (event .getUser ().getIdLong ());
142
+ newBet .setGamePk (gamePk );
143
+ newBet .setTeamId (teamId );
144
+ newBet .setBet (amount );
145
+ newBet .setKind (BetKind .PENDING );
146
+ newBet .setReason ("Bet on " + team + " for " + blurb .name ());
147
+
148
+ user .setCredits (user .getCredits () - amount );
149
+
150
+ Transaction trans = session .beginTransaction ();
151
+ session .save (newBet );
152
+ session .update (user );
153
+ trans .commit ();
154
+
155
+ event .reply ("Bet placed!" ).setEphemeral (true ).queue ();
156
+ } else if (amount == 0 ) {
157
+ int currentBet = bet .getBet ();
158
+
159
+ Transaction trans = session .beginTransaction ();
160
+ session .delete (bet );
161
+
162
+ user .setCredits (user .getCredits () + currentBet );
163
+ session .update (user );
164
+
165
+ trans .commit ();
166
+
167
+ event .reply ("Bet removed!" ).setEphemeral (true ).queue ();
168
+ } else {
169
+ int currentBet = bet .getBet ();
170
+ int creditsToDeduct = amount - currentBet ;
171
+ if (amount > currentBet && creditsToDeduct > user .getCredits ()) {
172
+ event .reply ("You don't have enough credits to bet that much! You only have " + user .getCredits () + " credits!" ).queue ();
173
+ return ;
174
+ }
175
+
176
+ user .setCredits (user .getCredits () - creditsToDeduct );
177
+ bet .setBet (amount );
178
+
179
+ Transaction trans = session .beginTransaction ();
180
+ session .update (bet );
181
+ session .update (user );
182
+ trans .commit ();
183
+
184
+ event .reply ("Bet updated!" ).setEphemeral (true ).queue ();
185
+ }
186
+
187
+ session .close ();
188
+ }
189
+
190
+ @ Override
191
+ public void onAutoComplete (CommandAutoCompleteInteractionEvent event ) {
192
+ event .replyChoices (PlanGameCommand .handleAutoComplete (event )).queue ();
193
+ }
194
+ }
195
+
73
196
public static Profile retrieveProfile (long userId ) {
74
197
var session = DatabaseHelper .getSessionFactory ().openSession ();
75
198
Profile profile = session .find (Profile .class , userId );
@@ -81,9 +204,9 @@ public static Profile retrieveProfile(long userId) {
81
204
82
205
// add initial betting credits
83
206
Bet bet = new Bet ();
84
- bet .setAmount ( 100 );
85
- bet .setAutomated ( true );
86
- bet .setSuccessful ( true );
207
+ bet .setKind ( BetKind . AUTOMATED );
208
+ bet .setBet ( 0 );
209
+ bet .setPayout ( 100 );
87
210
bet .setReason ("Initial betting credits" );
88
211
bet .setUserId (userId );
89
212
session .save (bet );
0 commit comments