-
Notifications
You must be signed in to change notification settings - Fork 1
/
BetResolver.cs
190 lines (177 loc) · 7.5 KB
/
BetResolver.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
namespace BloodBot
{
public class BetResolver
{
FumbblScraper scraper = new FumbblScraper();
SQL sql = new SQL();
public void ResolveMatch(Match match)
{
Logger.Log("Resolving Match");
Logger.Log("Getting Score");
string score = scraper.GetScore(match.HomeTeam, match.AwayTeam);
string TeamA;
string TeamB;
Logger.Log(score);
// Sort the teams according to the DB
if (sql.IsTeamA(match.HomeTeam))
{
TeamA = match.HomeTeam;
TeamB = match.AwayTeam;
}
else
{
TeamA = match.AwayTeam;
TeamB = match.HomeTeam;
score = score[2] + "-" + score[0];
}
Logger.Log(score);
int ScoreA = Convert.ToInt32(score[0]);
int ScoreB = Convert.ToInt32(score[2]);
char Outcome;
if (ScoreA > ScoreB)
{
Outcome = 'A';
}
else if (ScoreB > ScoreA)
{
Outcome = 'B';
}
else
{
Outcome = 'T';
}
Logger.Log(Outcome);
long MatchID = sql.GetTeamMatch(TeamA);
// check if anyone bet at all, if not end
// check if only losers bet and fuck them in the ass
// check if only winners bet and reward them
Logger.Log("Getting winner pot");
decimal WinnerPot = sql.GetWinnerPot(TeamA, Outcome);
Logger.Log(WinnerPot);
Logger.Log("Getting loser pot");
decimal Loserpot = sql.GetLoserPot(TeamA, Outcome);
Logger.Log(Loserpot);
decimal TotalPot = WinnerPot + Loserpot;
Logger.Log(TotalPot);
List<long> Users = new List<long>();
Logger.Log("Getting Bettors");
Users = sql.GetBettors(MatchID);
Logger.Log("got bettors");
string message = string.Format("Match finished \n **{0}** vs **{1}** \n Score: {2} \n",TeamA,TeamB,score);
if (WinnerPot == 0 && Loserpot == 0)
{
// no one cared
Logger.Log("no one cared");
// TODO send message saying no one cared so nothing happened
}
else if (WinnerPot == 0)
{
// only losers bet
Logger.Log("no one won");
foreach (long User in Users)
{
decimal BetMoney = sql.GetBetMoney(User, MatchID);
sql.DeleteBet(User, MatchID);
sql.SubstractMoneyBet(User, BetMoney);
bool broke = false;
if (sql.GetMoney(User) <= 0 && sql.GetBets(User) == null)
{
sql.AddMoney(User, 50);
broke = true;
}
if (broke)
{
message += string.Format(" {0} :skull_crossbones: {1}★ and broke! Here's 50★ \n", MentionUtils.MentionUser((ulong)User), BetMoney);
}
else
{
message += string.Format(" {0} :skull: {1}★ \n", MentionUtils.MentionUser((ulong)User), BetMoney);
}
}
}
else if (Loserpot == 0)
{
// only winners bet
Logger.Log("no one lost");
foreach (long User in Users)
{
decimal BetMoney = sql.GetBetMoney(User, MatchID);
sql.AddMoney(User, BetMoney);
sql.SubstractMoneyBet(User, BetMoney);
sql.DeleteBet(User, MatchID);
message += string.Format(" {0} Everyone won so no one wins anything, you get your bet back {1}★ \n", MentionUtils.MentionUser((ulong)User), BetMoney);
}
}
else
{
// there were winners and losers
Logger.Log("solving winners and losers");
foreach (long User in Users)
{
Logger.Log(User);
Logger.Log("getting bet money");
decimal BetMoney = sql.GetBetMoney(User, MatchID);
Logger.Log("if betoutcome == outcome");
if (sql.GetBetOutcome(User, MatchID) == Outcome)
{
Logger.Log("calculating percentpot");
Logger.Log("calculating moneywon");
decimal PercentPot = (int)Math.Round(((double)BetMoney / (double)WinnerPot) * 100);
decimal MoneyWon = (int)Math.Round(((double)PercentPot * 0.01) * (double)Loserpot);
Logger.Log("if betscore != null");
if (sql.GetBetScore(User, MatchID) != null && sql.GetBetScore(User, MatchID) == score)
{
sql.AddMoney(User, MoneyWon + BetMoney*2);
sql.SubstractMoneyBet(User, BetMoney);
sql.DeleteBet(User, MatchID);
message += string.Format(" {0} :star2: {1}★*2 + {2}★ → **{3}**★ \n", MentionUtils.MentionUser((ulong)User), BetMoney, MoneyWon, BetMoney * 2 + MoneyWon);
}
else
{
sql.AddMoney(User, MoneyWon + BetMoney);
sql.SubstractMoneyBet(User, BetMoney);
sql.DeleteBet(User, MatchID);
message += string.Format(" {0} :star: {1}★ + {2}★ → **{3}**★ \n", MentionUtils.MentionUser((ulong)User), BetMoney, MoneyWon, BetMoney + MoneyWon);
}
}
else
{
sql.SubstractMoneyBet(User, BetMoney);
sql.DeleteBet(User, MatchID);
bool broke = false;
if (sql.GetMoney(User) <= 0 && sql.GetBets(User) == null)
{
sql.AddMoney(User, 50);
broke = true;
}
if (broke)
{
message += string.Format(" {0} :skull_crossbones: {1}★ and broke! Here's 50★ \n", MentionUtils.MentionUser((ulong)User), BetMoney);
}
else
{
message += string.Format(" {0} :skull: {1}★ \n", MentionUtils.MentionUser((ulong)User), BetMoney);
}
}
}
}
Logger.Log("Finished resolving, deleting match");
sql.DeleteMatch(MatchID);
Logger.Log("match deleted, sending message");
AnnounceResults(message);
}
private async void AnnounceResults(string message)
{
Logger.Log("Announcing bet results");
var channel = Program.client.GetChannel(DiscordConstants.CHANNEL_CASINO) as SocketTextChannel;
await channel.SendMessageAsync(message);
}
}
}