public void Setup() { var rpsRule = new RpsRule(); var rpsGame = new RpsGame(rpsRule); rpsTournament = new RpsTournament(rpsGame); }
public async Task RpsCommand(SocketMessage message, SocketUser user, int bet, [Optional] string rpsls) { if (!(message.Channel is SocketTextChannel channel)) { return; } if (message.Author.Id == user.Id) { await channel.SendMessageAsync("You can't challenge yourself, baka!"); return; } if (bet <= 0) { await channel.SendMessageAsync($"You have to bet more than 0 {config.Currency.CurrencyName}."); return; } var guildData = dataStore.GetGuildData(channel.Guild); if (guildData.GetCoins(message.Author) < bet) { await channel.SendMessageAsync($"You don't have enough {config.Currency.CurrencyName}."); return; } if (guildData.GetCoins(user) < bet) { await channel.SendMessageAsync($"Your opponent doesn't have enough {config.Currency.CurrencyName}."); return; } if (runningGames.Any(g => g.Player1.Id == message.Author.Id || g.Player2.Id == message.Author.Id)) { await channel.SendMessageAsync("You are already in a game of rock-paper-scissors."); return; } if (runningGames.Any(g => g.Player1.Id == user.Id || g.Player2.Id == user.Id)) { await channel.SendMessageAsync("Your opponent is already in a game of rock-paper-scissors."); return; } var isRpsls = (rpsls == null) ? false : (rpsls == "rpsls"); var game = new RpsGame { Player1 = message.Author, Player2 = user, Channel = channel }; runningGames.Add(game); await channel.SendMessageAsync($"{user.Mention}, you have been challenged to a game of rock-paper-scissors. You can accept in the next {config.Commands.RPS.AcceptTimeout} seconds with `{config.Commands.Tag}rps a`"); try { await Task.Delay(TimeSpan.FromSeconds(config.Commands.RPS.AcceptTimeout), game.TimeoutCancellation.Token); await channel.SendMessageAsync($"{user.Mention} didn't accept {message.Author.Mention}'s challenge in time."); } catch (TaskCanceledException) { int player1Coins, player2Coins; if ((player1Coins = guildData.GetCoins(message.Author)) < bet) { await channel.SendMessageAsync($"You don't have enough {config.Currency.CurrencyName} anymore."); return; } if ((player2Coins = guildData.GetCoins(user)) < bet) { await channel.SendMessageAsync($"Your opponent doesn't have enough {config.Currency.CurrencyName} anymore."); return; } guildData.SetCoins(message.Author, player1Coins - bet); guildData.SetCoins(user, player2Coins - bet); var validChoices = new List <string> { "rock", "paper", "scissors" }; if (isRpsls) { validChoices.Add("lizard"); validChoices.Add("spock"); } var rpsWin = new Dictionary <int, int[]> { { 0, new[] { 2, 3 } }, { 1, new[] { 0, 4 } }, { 2, new[] { 1, 3 } }, { 3, new[] { 1, 4 } }, { 4, new[] { 0, 2 } } }; var choiceString = String.Join(", ", validChoices.Take(validChoices.Count - 1)) + " or " + validChoices.Last(); await channel.SendMessageAsync($"{user.Mention} has accepted {message.Author}'s challenge. Both players, please send me your choice ({choiceString}) via DM within the next {config.Commands.RPS.ChoiceTimeout} seconds."); var player1Task = client.WaitForMessageAsync(TimeSpan.FromSeconds(config.Commands.RPS.ChoiceTimeout), CreateMessageFilter(message.Author)); var player2Task = client.WaitForMessageAsync(TimeSpan.FromSeconds(config.Commands.RPS.ChoiceTimeout), CreateMessageFilter(user)); var player1Msg = await player1Task; var player2Msg = await player2Task; if (player1Msg == null || player2Msg == null) { await channel.SendMessageAsync("I didn't receive your choices in time. The game was canceled."); guildData.SetCoins(message.Author, guildData.GetCoins(message.Author) + bet); guildData.SetCoins(user, guildData.GetCoins(user) + bet); return; } var p1Choice = validChoices.IndexOf(player1Msg.Content.ToLowerInvariant()); var p2Choice = validChoices.IndexOf(player2Msg.Content.ToLowerInvariant()); var text = $"{message.Author.Mention} chose {validChoices[p1Choice]}, {user.Mention} chose {validChoices[p2Choice]}. "; if (rpsWin[p1Choice].Contains(p2Choice)) { text += $"That's a win for {message.Author.Mention}!"; guildData.SetCoins(message.Author, guildData.GetCoins(message.Author) + bet * 2); } else if (rpsWin[p2Choice].Contains(p1Choice)) { text += $"That's a win for {user.Mention}!"; guildData.SetCoins(user, guildData.GetCoins(user) + bet * 2); } else { text += "That's a draw!"; guildData.SetCoins(message.Author, guildData.GetCoins(message.Author) + bet); guildData.SetCoins(user, guildData.GetCoins(user) + bet); } await channel.SendMessageAsync(text); Func <SocketMessage, bool> CreateMessageFilter(SocketUser targetUser) { return((msg) => MessageFilter(msg).GetAwaiter().GetResult()); async Task <bool> MessageFilter(SocketMessage msg) { if (!(msg.Channel is SocketDMChannel && targetUser.Id == msg.Author.Id)) { return(false); } if (validChoices.Contains(msg.Content.ToLowerInvariant())) { await msg.Channel.SendMessageAsync($"You chose {msg.Content.ToLowerInvariant()}."); return(true); } await msg.Channel.SendMessageAsync($"Please choose one of {choiceString}."); return(false); } } } finally { runningGames.Remove(game); } }
public RpsGameTest() { _rpsGame = new RpsGame(); }
public void StartUp() { _game = new RpsGame(); }