-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
569 lines (538 loc) · 18.2 KB
/
Game.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InlineQueryResults;
using Telegram.Bot.Types.ReplyMarkups;
namespace PokerBot
{
class Game
{
public readonly Chat Chat;
public readonly long ChatId;
private readonly TelegramWrapper Telegram;
readonly SemaphoreSlim sem = new SemaphoreSlim(1);
// game config:
int InitialTokens = 5000;
int BigBlind = 20;
int MaxBet = -1; // default: No Limit
// game status:
enum Status { Inactive, CollectParticipants, WaitForBets };
Status status;
readonly List<Player> Players = new List<Player>();
IEnumerable<Player> PlayersInTurn => Players.Where(p => p.Status != Player.PlayerStatus.Folded);
readonly List<Card> Board = new List<Card>();
Queue<Card> Deck;
int ButtonIdx; // index du joueur ayant le bouton
int BigBet; // taille de la relance mini
int StartingBet; // niveau de mise au debut de ce tour d'enchères
int CurrentBet; // niveau de mise actuelle
Player CurrentPlayer;
Player LastPlayerToRaise;
#pragma warning disable IDE0052 // Remove unread private members
Message inscriptionMsg, lastMsg;
string lastMsgText;
Task runNextTurn;
#pragma warning restore IDE0052 // Remove unread private members
private string BB(int montant) => montant % BigBlind == 0 ? $"{montant / BigBlind} BB" : $"{(double)montant / BigBlind:N1} BB";
private Player FindPlayer(User from) => from.Id == CurrentPlayer?.User.Id ? CurrentPlayer : Players.Find(player => player.User.Id == from.Id);
private Player FindPlayer(InlineQuery query) => FindPlayer(query.From);
private Player FindPlayer(CallbackQuery query) => FindPlayer(query.From);
//private Player FindPlayer(Message msg) => FindPlayer(msg.From);
public Game(TelegramWrapper telegram, Chat chat)
{
Telegram = telegram;
Chat = chat;
ChatId = chat.Id;
}
// point d'entrée pour les commandes 'slash'
public async Task OnCommand(Message msg, string cmd, string args)
{
await sem.WaitAsync();
try
{
switch (cmd.ToLower())
{
case "/start": await OnCommandStart(msg, args); return;
case "/stop": await OnCommandStop(msg); return;
case "/b": case "/allin":
case "/relance": await OnCommandRaise(msg, args); return;
case "/check": await OnCommandCheck(msg, args); return;
case "/stacks": await OnCommandStacks(msg); return;
}
}
finally
{
sem.Release();
}
}
private async Task OnCommandStart(Message msg, string arguments)
{
switch (msg.Chat.Type)
{
case ChatType.Channel:
case ChatType.Private:
await Telegram.SendMsg(Chat, "Utilisez /start dans un groupe pour démarrer une partie");
return;
}
switch (status)
{
case Status.Inactive:
inscriptionMsg = null;
break;
case Status.CollectParticipants:
if (DateTime.UtcNow - inscriptionMsg.Date < TimeSpan.FromMinutes(1))
return;
inscriptionMsg = null;
break;
case Status.WaitForBets:
await Telegram.SendMsg(Chat, "Une partie est déjà en cours avec " + string.Join(", ", Players));
return;
}
var args = arguments.Split(' ');
if (args.Length > 0 && int.TryParse(args[0], out int arg)) InitialTokens = arg;
if (args.Length > 1 && int.TryParse(args[1], out arg)) BigBlind = arg;
if (args.Length > 2 && int.TryParse(args[2], out arg))
{
if (arg / BigBlind <= 1 || arg % BigBlind != 0)
{
await Telegram.SendMsg(Chat, "Montant invalide de mise maximale: " + arg);
return;
}
MaxBet = arg;
}
Trace.TraceInformation($"Ouverture de la partie sur {Chat.Title}");
await DoCollectParticipants();
}
private async Task OnCommandStop(Message _)
{
if (status != Status.Inactive)
{
await Telegram.SendMsg(Chat, "Fin de la partie");
status = Status.Inactive;
}
foreach (var player in Players)
Program.gamesByUserId.Remove(player.User.Id);
CurrentPlayer = null;
Players.Clear();
Deck = null;
}
private async Task OnCommandRaise(Message msg, string arguments)
{
if (status != Status.WaitForBets)
await Telegram.SendMsg(Chat, "Commande valide seulement pendant les enchères");
else if (msg.From.Id != CurrentPlayer.User.Id)
await Telegram.SendMsg(Chat, $"Ce n'est pas votre tour, c'est à {CurrentPlayer.MarkDown()} de parler");
else
{
if (!msg.Text.StartsWith("/allin"))
await DoChoice(CallbackWord.allin);
else
{
if (!double.TryParse(arguments, out double relanceBB))
await Telegram.SendMsg(Chat, "Montant de relance invalide");
else
{
int relance = (int)(relanceBB * BigBlind);
if (relance < CurrentBet + BigBet - CurrentPlayer.Bet)
await Telegram.SendMsg(Chat, $"Vous devez relancer de +{BB(CurrentBet + BigBet - CurrentPlayer.Bet)} au minimum");
else if (!CurrentPlayer.CanBet(CurrentPlayer.Bet + relance))
await Telegram.SendMsg(Chat, "Vous n'avez pas assez pour relancer d'autant");
else
{
BigBet = CurrentPlayer.Bet + relance - CurrentBet;
await DoChoice(CallbackWord.raise);
}
}
}
}
}
private async Task OnCommandCheck(Message _, string arguments)
{
var cards = arguments.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(Card.Parse).ToList();
if (cards.IndexOf(Card.Invalid) >= 0)
{
await Telegram.SendMsg(Chat, "Carte non reconnue:\n" + string.Join("\n", cards.Select(c => c.ToFullString())));
return;
}
var (force, descr) = Card.BestHand(cards);
var secondary = Card.CardsForce(cards);
await Telegram.SendMsg(Chat, $"Meilleure main: {descr} (force {force}, {secondary})");
}
private async Task OnCommandStacks(Message _)
{
if (status != Status.WaitForBets)
{
await Telegram.SendMsg(Chat, "Pas de partie en cours");
return;
}
string text = "Stacks des joueurs :";
foreach (var player in Players)
{
text += $"\n{player.MarkDown()}: {BB(player.Stack)}";
if (player.Status == Player.PlayerStatus.Folded)
text += " _(couché)_";
}
text += $"\nPot actuel: {BB(Players.Sum(p => p.Bet))}";
await Telegram.SendMsg(Chat, text);
}
// point d'entrée pour la complétion inline
public async Task OnInline(InlineQuery query)
{
await sem.WaitAsync();
try
{
string expr = query.Query;
var player = FindPlayer(query);
if (player == null)
{
await Telegram.AnswerInline(query);
return;
}
string descr = null;
if (Board.Count >= 3)
descr = "Votre main: " + Card.BestHand(Board.Concat(player.Cards)).descr;
await Telegram.AnswerInline(query,
new[] {
new InlineQueryResultArticle($"Card_"+string.Join("_", player.Cards),
"Vos cartes: " + string.Join(" ", player.Cards),
new InputTextMessageContent("(je regarde mes cartes)"))
{ Description = descr },//, ThumbUrl = "https://raw.githubusercontent.com/hayeah/playing-cards-assets/master/png/2_of_hearts.png", ThumbWidth = 222, ThumbHeight = 323 },
//new InlineQueryResultPhoto("2", $"http://via.placeholder.com/{expr}", $"http://via.placeholder.com/{expr}") { Title = "photo", PhotoWidth = 85, PhotoHeight = 85 },
},
2, isPersonal: true, switchPmText: $"Mise: {BB(player.Bet)} | Stack: {BB(player.Stack)}", switchPmParameter: "cards");
}
finally
{
sem.Release();
}
}
// point d'entrée pour les boutons sous mes messages
enum CallbackWord { join = 0, start = 1, call = 2, raise = 3, allin = 4, fold = 5, check = 6 };
public async Task OnCallback(CallbackQuery query, string[] words)
{
await sem.WaitAsync();
try
{
var callback = Enum.Parse<CallbackWord>(words[0]);
var player = FindPlayer(query);
switch (status)
{
case Status.Inactive:
await Telegram.AnswerCallback(query, "Pas de partie en cours. Tapez /start pour commencer");
return;
case Status.CollectParticipants:
if (callback >= CallbackWord.call)
{
await Telegram.AnswerCallback(query, "Commande invalide");
return;
}
break;
case Status.WaitForBets:
if (callback >= CallbackWord.call) break;
if (player == null)
await Telegram.AnswerCallback(query, "La partie est déjà en cours. Attendez qu'elle finisse...");
else
await Telegram.AnswerCallback(query);
return;
}
switch (callback)
{
case CallbackWord.join: await DoJoin(query); break;
case CallbackWord.start: await DoStart(query); break;
default:
if (player == null)
await Telegram.AnswerCallback(query, "Vous ne jouez pas dans cette partie !");
else if (CurrentPlayer.User.Id != query.From.Id)
await Telegram.AnswerCallback(query, "Ce n'est pas votre tour de jouer !");
else
{
await Telegram.AnswerCallback(query);
await DoChoice(callback);
}
break;
}
}
finally
{
sem.Release();
}
}
private async Task DoJoin(CallbackQuery query)
{
var player = FindPlayer(query);
if (player != null)
{
#if DEBUG
Players.Add(new Player { User = new User { Id = query.From.Id, FirstName = query.From.FirstName + (Players.Count + 1), Username = query.From.Username } });
#else
await Bot.AnswerCallback(query, "Vous êtes déjà inscrit !");
return;
#endif
}
else
Players.Add(new Player { User = query.From });
Program.gamesByUserId[query.From.Id] = this;
await Telegram.AnswerCallback(query, "Vous êtes inscrit");
await DoCollectParticipants();
}
private async Task DoCollectParticipants(bool finalize = false)
{
status = Status.CollectParticipants;
var replyMarkup = new List<InlineKeyboardButton>();
if (!finalize)
{
replyMarkup.Add(InlineKeyboardButton.WithCallbackData("Moi !", "join " + ChatId));
if (Players.Count >= 2)
replyMarkup.Add(InlineKeyboardButton.WithCallbackData("Commencer la partie", "start " + ChatId));
}
var text = $"Jetons: {InitialTokens}, Big Blind: {BigBlind}";
if (MaxBet != -1) text += $" max {MaxBet}";
if (finalize)
text = $"La partie commence! {text}\nParticipants: ";
else
text = $"La partie va commencer... {text}\nQui participe ? ";
text += string.Join(", ", Players.Select(player => player.MarkDown()));
if (inscriptionMsg == null)
inscriptionMsg = await Telegram.SendMsg(Chat, text, new InlineKeyboardMarkup(replyMarkup));
else
await Telegram.EditMsg(inscriptionMsg, text, replyMarkup.ToArray());
}
private async Task DoStart(CallbackQuery query)
{
if (FindPlayer(query) == null)
{
await Telegram.AnswerCallback(query, "Seul un joueur inscrit peut lancer la partie");
return;
}
Trace.TraceInformation($"Lancement de la partie sur {Chat.Title} avec {string.Join(", ", Players)}");
await Telegram.AnswerCallback(query, "C'est parti !");
await DoCollectParticipants(true);
status = Status.WaitForBets;
foreach (var player in Players)
player.Stack = InitialTokens;
ButtonIdx = Players.Count - 1;
await StartTurn();
}
private void ShuffleCards() // Knuth algorithm
{
var rand = new Random();
var cards = Enumerable.Range(0, 52).Select(i => new Card(i)).ToArray();
for (int i = 0; i < 52; i++)
{
int j = rand.Next(i, cards.Length);
Card temp = cards[i]; cards[i] = cards[j]; cards[j] = temp; // swap cards i&j
}
Deck = new Queue<Card>(cards);
}
private async Task StartTurn()
{
ShuffleCards();
Board.Clear();
StartingBet = 0;
foreach (var player in Players)
{
player.Cards.Clear();
player.Bet = 0;
player.Status = Player.PlayerStatus.DidNotTalk;
}
foreach (var player in Players)
player.Cards.Add(Deck.Dequeue());
foreach (var player in Players)
player.Cards.Add(Deck.Dequeue());
int smallIdx = (ButtonIdx + 1) % Players.Count;
int bigIdx = (smallIdx + 1) % Players.Count;
Players[smallIdx].SetBet(BigBlind/2);
Players[bigIdx].SetBet(BigBlind);
CurrentBet = BigBlind;
BigBet = BigBlind;
CurrentPlayer = Players[(bigIdx + 1) % Players.Count];
TraceStatus();
LastPlayerToRaise = CurrentPlayer;
lastMsg = await Telegram.SendMsg(Chat,
lastMsgText = "Nouveau tour, nouvelles cartes!\n" +
$"{Players[smallIdx]}: petite blind. {Players[bigIdx]}: grosse blind.\n" +
$"C'est à {CurrentPlayer.MarkDown()} de parler",
GetChoices());
}
private void TraceStatus()
{
Trace.TraceInformation($"Mises sur {Chat.Title}: " +
string.Join(" ", Players.Select(p => $"{(p == CurrentPlayer ? "»" : "")}{p}:{p.Bet}")) +
$" | mise:{CurrentBet} +{BigBet}");
}
private InlineKeyboardMarkup GetChoices()
{
var choices = new List<InlineKeyboardButton>();
if (CurrentPlayer.Bet == CurrentBet)
choices.Add(InlineKeyboardButton.WithCallbackData("Parler", "check " + ChatId));
else if (CurrentPlayer.CanBet(CurrentBet))
choices.Add(InlineKeyboardButton.WithCallbackData($"Suivre +{BB(CurrentBet - CurrentPlayer.Bet)}", "call " + ChatId));
if (!CurrentPlayer.CanBet(CurrentBet + BigBet + 1))
choices.Add(InlineKeyboardButton.WithCallbackData("All in", "allin " + ChatId));
else if (MaxBet == -1 || CurrentBet + BigBet < MaxBet)
choices.Add(InlineKeyboardButton.WithCallbackData($"Relance +{BB(CurrentBet + BigBet - CurrentPlayer.Bet)}", "raise " + ChatId));
if (CurrentPlayer.Bet < CurrentBet)
choices.Add(InlineKeyboardButton.WithCallbackData("Passer", "fold " + ChatId));
return new InlineKeyboardMarkup(new IEnumerable<InlineKeyboardButton>[] {
choices,
new[] { InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("Voir mes cartes et jetons", "") },
});
}
private async Task DoChoice(CallbackWord choice)
{
string text = CurrentPlayer.ToString();
switch (choice)
{
case CallbackWord.call:
text += " *suit*.";
CurrentPlayer.SetBet(CurrentBet);
break;
case CallbackWord.raise:
LastPlayerToRaise = CurrentPlayer;
CurrentBet += BigBet;
text += $" *relance de {BB(CurrentBet - CurrentPlayer.Bet)}* !";
CurrentPlayer.SetBet(CurrentBet);
break;
case CallbackWord.allin:
int playerBet = CurrentPlayer.Bet + CurrentPlayer.Stack;
//todo
if (playerBet > CurrentBet)
{
LastPlayerToRaise = CurrentPlayer;
CurrentBet = playerBet;
}
text += $" *allin de {BB(CurrentBet - CurrentPlayer.Bet)}* !";
CurrentPlayer.SetBet(playerBet);
break;
case CallbackWord.fold:
text += " *passe*.";
CurrentPlayer.Status = Player.PlayerStatus.Folded;
break;
case CallbackWord.check:
text += " *parle*.";
break;
default:
break;
}
await RemoveLastMessageButtons();
var pot = Players.Sum(p => p.Bet);
if (PlayersInTurn.Count() == 1)
{
var winner = PlayersInTurn.Single();
winner.Stack += pot;
foreach (var player in Players)
player.Bet = 0;
await Telegram.SendMsg(Chat, $"{text}\n{winner.MarkDown()} remporte le pot de {BB(pot)}");
ButtonIdx = (ButtonIdx + 1) % Players.Count;
runNextTurn = Task.Delay(7000).ContinueWith(_ => StartTurn());
}
else
{
var stillBetting = NextPlayer();
#if ABATTAGE_IMMEDIAT
DistributeBoard(); DistributeBoard(); DistributeBoard(); stillBetting = false;
#endif
if (stillBetting)
{
text += $" La mise est à {BB(CurrentBet - StartingBet)}\nC'est au tour de {CurrentPlayer.MarkDown()} de parler";
lastMsg = await Telegram.SendMsg(Chat, lastMsgText = text, GetChoices());
}
else if (Board.Count == 5)
{
text += $" Abattage des cartes !\nBoard: ";
text += string.Join(" ", Board);
var hands = PlayersInTurn.Select(player => (player, hand: Card.BestHand(Board.Concat(player.Cards)))).ToList();
foreach (var (player, hand) in hands)
text += $"\n{string.Join(" ", player.Cards)} : {hand.descr} pour {player}";
var winners = DetermineWinners(hands);
if (winners.Count > 1)
{
text += $"\nEgalité entre {string.Join(" et ", winners)} ! Partage du pot: {BB(pot)}";
foreach (var player in winners)
player.Stack += pot / winners.Count;
}
else
{
text += $"\n{winners[0].MarkDown()} remporte le pot de {BB(pot)} !";
winners[0].Stack += pot;
}
await Telegram.SendMsg(Chat, text);
ButtonIdx = (ButtonIdx + 1) % Players.Count;
runNextTurn = Task.Delay(10000).ContinueWith(_ => StartTurn());
}
else
{
text += $" Le tour d'enchères est terminé. Pot: {BB(pot)}\n{DistributeBoard()}: ";
text += string.Join(" ", Board);
StartingBet = CurrentBet;
BigBet = BigBlind;
LastPlayerToRaise = null;
CurrentPlayer = Players[ButtonIdx];
NextPlayer();
LastPlayerToRaise = CurrentPlayer;
text += $"\nC'est au tour de {CurrentPlayer.MarkDown()} de parler";
lastMsg = await Telegram.SendMsg(Chat, lastMsgText = text, GetChoices());
}
TraceStatus();
}
}
private List<Player> DetermineWinners(List<(Player player, (int force, string descr) hand)> hands)
{
var winners = new List<Player>();
int winForce = 0;
foreach (var (player, hand) in hands)
{
if (hand.force < winForce) continue;
if (hand.force > winForce) winners.Clear();
winForce = hand.force;
winners.Add(player);
}
if (winners.Count == 1)
return winners;
var winners2 = new List<Player>();
winForce = 0;
foreach (var player in winners)
{
var force = Card.CardsForce(Board.Concat(player.Cards));
if (force < winForce) continue;
if (force > winForce) winners2.Clear();
winForce = force;
winners2.Add(player);
}
return winners2;
}
static readonly string[] BoardNames = new[] { "Flop", "Turn", "River" };
private string DistributeBoard()
{
Deck.Dequeue(); // carte brûlée
if (Board.Count == 0)
{
Board.Add(Deck.Dequeue());
Board.Add(Deck.Dequeue());
}
Board.Add(Deck.Dequeue());
return BoardNames[Board.Count - 3];
}
private async Task RemoveLastMessageButtons()
{
if (lastMsg == null) return;
await Telegram.EditMsg(lastMsg, lastMsgText);
}
private bool NextPlayer()
{
int currentIndex = Players.IndexOf(CurrentPlayer);
do
{
currentIndex = (currentIndex + 1) % Players.Count;
CurrentPlayer = Players[currentIndex];
if (CurrentPlayer == LastPlayerToRaise) return false;
} while (CurrentPlayer.Status == Player.PlayerStatus.Folded);
return true;
}
}
}