-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSG_WarBot.py
More file actions
223 lines (178 loc) · 8.54 KB
/
SG_WarBot.py
File metadata and controls
223 lines (178 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import math
import praw
import SG_Repository
import SG_Messages
import pprint
import time
import deuces
import ConfigParser
import SG_Utils
config = ConfigParser.ConfigParser()
config.read("settings.config")
config_header = "CasinoWar"
username = config.get("General", "username")
version = config.get("General", "version")
starting_balance = int(config.get("General", "starting_balance"))
small_bet = int(config.get(config_header, "small_bet"))
mid_bet = int(config.get(config_header, "mid_bet"))
big_bet = int(config.get(config_header, "big_bet"))
game_type = 'Casino War'
def format_wager_reply(username, wager_amount, player_card, player_war_card,
dealer_card, dealer_war_card, outcome, winnings, new_balance):
reply_template = SG_Messages.ReplyMessages.CASINO_WAR_REPLY_WRAPPER_TEMPLATE_MSG
reply_body = ""
first_draw_results = SG_Messages.ReplyMessages.CASINO_WAR_BODY_TEMPLATE.format(player_card, dealer_card)
reply_body += first_draw_results
if player_war_card != '':
# if we went to war, include the war results
reply_body += """
***Going to war!***
"""
war_results = SG_Messages.ReplyMessages.CASINO_WAR_BODY_TEMPLATE.format(player_war_card, dealer_war_card)
reply_body += war_results
return reply_template.format(username, wager_amount, reply_body, outcome, winnings, winnings - wager_amount, new_balance)
def parse_post_for_wager(post_body, player_balance):
body_tokens = post_body.strip().split(' ')
if str(body_tokens[0]) == 'wager':
if body_tokens[1] == 'big':
return big_bet
elif body_tokens[1] == 'mid':
return mid_bet
elif body_tokens[1] == 'small':
return small_bet
return 0
def evaluate_win(player_card, dealer_card):
return card.get_rank_int(player_card) - card.get_rank_int(dealer_card)
def calculate_winnings(result_diff, wager_amount, war = False):
if war:
if result_diff > 0:
value = math.trunc(wager_amount * 1.5)
return value
elif result_diff < 0:
return 0
else:
return wager_amount * 3
else:
if result_diff > 0:
return wager_amount * 2
else:
return 0
def play_war(wager_amount):
deck = deuces.Deck()
player_card = deck.draw(1)
dealer_card = deck.draw(1)
print "Player: {} {}".format(card.int_to_pretty_str(player_card), card.get_rank_int(player_card))
print "Dealer: {} {}".format(card.int_to_pretty_str(dealer_card), card.get_rank_int(dealer_card))
player_war_card = ''
dealer_war_card = ''
# evaluate the difference of the player and dealer cards
result = evaluate_win(player_card, dealer_card)
if result > 0:
winnings = calculate_winnings(result, wager_amount, False)
print "Player wins - winnings = {}!".format(winnings)
outcome = SG_Repository.WagerOutcome.WIN
elif result < 0:
winnings = calculate_winnings(result, wager_amount, False)
print "Player loses - winnings = {}!".format(winnings)
outcome = SG_Repository.WagerOutcome.LOSE
else:
print "Tie! Going to war...\n\n"
player_war_card = deck.draw(1)
dealer_war_card = deck.draw(1)
print "Player: {} {}".format(card.int_to_pretty_str(player_war_card), card.get_rank_int(player_war_card))
print "Dealer: {} {}".format(card.int_to_pretty_str(dealer_war_card), card.get_rank_int(dealer_war_card))
war_result = evaluate_win(player_war_card, dealer_war_card)
if war_result > 0:
winnings = calculate_winnings(war_result, wager_amount, True)
print "Player wins - winnings = {}!".format(winnings)
outcome = SG_Repository.WagerOutcome.WIN
elif war_result < 0:
winnings = calculate_winnings(war_result, wager_amount, True)
print "Player loses - winnings = {}!".format(winnings)
outcome = SG_Repository.WagerOutcome.LOSE
else:
winnings = calculate_winnings(war_result, wager_amount, True)
print "Player wins war tie - winnings = {}!".format(winnings)
outcome = SG_Repository.WagerOutcome.WIN
player_war_card = card.int_to_pretty_str(player_war_card)
dealer_war_card = card.int_to_pretty_str(dealer_war_card)
wager_result = {'player_card' : card.int_to_pretty_str(player_card), 'dealer_card' : card.int_to_pretty_str(dealer_card), 'outcome' : outcome,
'winnings' : winnings, 'player_war_card' : player_war_card, 'dealer_war_card' : dealer_war_card}
print("War wager result:")
pprint.pprint(wager_result)
return wager_result
# create our Reddit instance
c_id = config.get("General", "client_id")
c_secret = config.get("General", "client_secret")
user = config.get("General", "plain_username")
pw = config.get("General", "password")
reddit = praw.Reddit(
client_id = c_id,
client_secret = c_secret,
username = user,
password = pw,
user_agent = 'Dealer bot v{} by /u/eganwall'.format(version)
)
# initialize our repository
sg_repo = SG_Repository.Repository()
# get our messaging classes
error_messages = SG_Messages.ErrorMessages
reply_messages = SG_Messages.ReplyMessages
constants = SG_Messages.MiscConstants
card = deuces.Card()
# set our subreddit so that we can do mod stuff like edit flairs
subreddit = reddit.subreddit('solutiongambling')
def bot_loop():
# get the Submission object for our war thread
submission = reddit.submission('6m5yhm')
submission.comment_sort = 'new'
# submission.comments.replace_more(limit=0)
for comment in list(submission.comments):
# if we haven't processed this comment yet, make a new record for it and
# process it
if sg_repo.GET_COMMENT_BY_ID(comment.id) is None:
sg_repo.INSERT_COMMENT_ID(comment.id)
# if this player hasn't commented on the sub yet, make sure we
# create a record of them, update their flair, and send them the
# welcome PM
if sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name) is None:
sg_repo.INSERT_PLAYER(comment.author.name, starting_balance)
SG_Utils.update_player_flair(comment.author.name, starting_balance, '')
reddit.redditor(comment.author.name).message('Welcome!',
reply_messages.NEW_PLAYER_WELCOME_MESSAGE.format(
comment.author.name),
from_subreddit='/r/SolutionGambling')
# get the player from the DB so we can validate their wager
# and update their balance
player = sg_repo.GET_PLAYER_BY_USERNAME(comment.author.name)
# now process the comment - first we convert it to lower
post_body_lower = comment.body.lower()
print("Processing post body: ".format(post_body_lower))
wager_amount = parse_post_for_wager(post_body_lower, player['balance'])
print("Wager amount from post: {}".format(wager_amount))
if wager_amount <= 0:
print("Wager amount not valid")
comment.reply(error_messages.WAR_ERROR_MSG.format(player['username']))
continue
if wager_amount > player['balance']:
print("Player wagered more than their balance")
comment.reply(error_messages.INSUFFICIENT_BALANCE_ERROR_MSG)
continue
wager_result = play_war(wager_amount)
new_player_balance = player['balance'] - wager_amount + wager_result['winnings']
sg_repo.INSERT_WAGER(player['username'], wager_result['outcome'],
wager_amount, wager_result['winnings'], new_player_balance, game_type)
SG_Utils.update_player_after_wager(player['username'], new_player_balance, player['flair_css_class'])
reply = format_wager_reply(player['username'], wager_amount, wager_result['player_card'],
wager_result['player_war_card'], wager_result['dealer_card'],
wager_result['dealer_war_card'], wager_result['outcome'],
wager_result['winnings'], new_player_balance)
print("Reply formatted:\n{}".format(reply))
comment.reply(reply)
else:
break
while True:
bot_loop()
print("---------------------- Processing finished - sleeping for 10 seconds...")
time.sleep(10)