public void Options_Test_NewUserShowStatistics() { RpsPlayData player = CreatePlayer("r"); Embed e = _rps.Options(CreatePlayer("s")); var fields = e.Fields; RpsUserData actualUser = (RpsUserData)_fileSystem.Load().ElementAt(0); RpsUserData expectedUser = new RpsUserData { Plays = int.Parse(fields.First(x => x.Name == "Plays").Value), TotalPlays = int.Parse(fields.First(x => x.Name == "Total Plays").Value), Wins = int.Parse(fields.First(x => x.Name == "Wins").Value), Losses = int.Parse(fields.First(x => x.Name == "Losses").Value), Draws = int.Parse(fields.First(x => x.Name == "Draws").Value), Ratio = int.Parse(fields.First(x => x.Name == "Win Ratio").Value), CurrentStreak = int.Parse(fields.First(x => x.Name == "Current Streak").Value), BestStreak = int.Parse(fields.First(x => x.Name == "Best Streak").Value), Resets = int.Parse(fields.First(x => x.Name == "Resets").Value), RockChosen = int.Parse(fields.First(x => x.Name == ":rock:").Value), PaperChosen = int.Parse(fields.First(x => x.Name == ":roll_of_paper:").Value), ScissorsChosen = int.Parse(fields.First(x => x.Name == ":scissors:").Value), Coins = int.Parse(fields.First(x => x.Name == "Rings").Value) }; Equal(actualUser, 345678912, expectedUser.Plays, expectedUser.TotalPlays, expectedUser.Wins, expectedUser.Losses, expectedUser.Draws, expectedUser.Ratio, expectedUser.CurrentStreak, expectedUser.BestStreak, expectedUser.Resets, expectedUser.RockChosen, expectedUser.PaperChosen, expectedUser.ScissorsChosen, expectedUser.Coins); DeleteFile(player.GuildId + ".xml"); }
public void PlayVsBot_Test_BonusStreak() { RpsPlayData player = CreatePlayer("p"); const int playsTimes = 11; int coins = 0; for (int i = 0; i < playsTimes; i++) { _rps.Play(player, null, RpsActors.Rock); } RpsUserData user = (RpsUserData)_fileSystem.Load().ElementAt(0); for (int i = 1; i < playsTimes + 1; i++) { int bonus = 1; if (i % 10 == 0) { bonus = i * 2; } coins += (int)Math.Ceiling(bonus * 0.5f); } Equal(user, 345678912, playsTimes, playsTimes, playsTimes, ratio: 100, currentStreak: playsTimes, paperChosen: playsTimes, coins: coins); DeleteFile(player.GuildId + ".xml"); }
private Embed Responding(RpsActors authorActor, RpsUserData authorUd, RpsUserData mentionUd, RpsPlayData authorPlayData, RpsPlayData mentionPlayData) { authorUd.Actor = authorActor; int mentionActor = (int)mentionUd.Actor; string authorMention = authorPlayData.Mention; string mentionedMention = mentionPlayData.Mention; string result = $"{mentionedMention} chose {ConvertActorToEmoji(mentionUd.Actor)}\n" + $"{authorMention} chose {ConvertActorToEmoji(authorUd.Actor)}\n\n"; GameState mentionState, authorState; string thumbnailWinner = string.Empty; //Responding wins if ((mentionActor + 1) % (int)RpsActors.Max == (int)authorActor) { result += $"{authorMention} wins! {CoinsInKeyText}"; authorState = GameState.Win; mentionState = GameState.Lose; thumbnailWinner = authorPlayData.ThumbnailIconUrl; } //Instigator wins else if (((int)authorActor + 1) % (int)RpsActors.Max == mentionActor) { result += $"{mentionedMention} wins! {CoinsInKeyText}"; mentionState = GameState.Win; authorState = GameState.Lose; thumbnailWinner = mentionPlayData.ThumbnailIconUrl; } //Draw else { result += "Draw game."; mentionState = authorState = GameState.Draw; } ProcessResults(mentionUd, mentionState, result, mentionedMention, out result); ProcessResults(authorUd, authorState, result, authorMention, out result); _usersActiveVs.Remove(authorUd); _usersActiveVs.Remove(mentionUd); if (_usersActiveVs.Count == 0) { _timerVs.Stop(); } return(new EmbedBuilder() .WithAuthor(TitleBuilder(authorPlayData)) .WithTitle("*GAME*") .WithThumbnailUrl(thumbnailWinner) .WithColor(Color.DarkOrange) .WithDescription(result).Build()); }
public void PlayVsBot_Test_Draw() { RpsPlayData player = CreatePlayer("r"); _rps.Play(player, null, RpsActors.Rock); RpsUserData user = (RpsUserData)_fileSystem.Load().ElementAt(0); Equal(user, 345678912, 1, 1, draws: 1, rockChosen: 1); DeleteFile(player.GuildId + ".xml"); }
public void PlayVsBot_Test_Win() { RpsPlayData player = CreatePlayer("r"); _rps.Play(player, null, RpsActors.Scissors); RpsUserData user = (RpsUserData)_fileSystem.Load().ElementAt(0); Equal(user, 345678912, 1, 1, 1, ratio: 100, currentStreak: 1, rockChosen: 1, coins: 1); DeleteFile(player.GuildId + ".xml"); }
private void CheckVsTimers(object sender, ElapsedEventArgs e) { foreach (RpsUserData ud in _usersActiveVs.Where(data => DateTime.Now > data.DateVs)) { RpsUserData temp = ud; temp.DateVs = DateTime.Now; temp.UserIdVs = 0; int i = FindIndex(temp); _users[i] = temp; _fileSystem.UpdateFile(temp); } }
public void Save_Test_Fail() { var tuple = SetUpSaveFileTests(); var fileSystem = tuple.Item1; var file = tuple.Item2; RpsUserData user = new RpsUserData(); bool ok = fileSystem.Save(user); Assert.False(ok); Assert.False(File.Exists(file)); }
public void PlayVsBot_Test_WinRatio() { RpsPlayData player = CreatePlayer("p"); _rps.Play(player, null, RpsActors.Rock); _rps.Play(player, null, RpsActors.Scissors); RpsUserData user = (RpsUserData)_fileSystem.Load().ElementAt(0); Equal(user, 345678912, user.Plays, user.TotalPlays, user.Wins, user.Losses, ratio: 50, bestStreak: user.BestStreak, paperChosen: user.PaperChosen, coins: user.Coins); DeleteFile(player.GuildId + ".xml"); }
public void PlayVsBot_Test_PlayWithEachActor() { RpsPlayData player = CreatePlayer("r"); _rps.Play(player, null); _rps.Play(CreatePlayer("p"), null); _rps.Play(CreatePlayer("s"), null); RpsUserData user = (RpsUserData)_fileSystem.Load().ElementAt(0); Equal(user, 345678912, 3, 3, user.Wins, user.Losses, user.Draws, user.Ratio, user.CurrentStreak, rockChosen: 1, paperChosen: 1, scissorsChosen: 1, coins: user.Coins); DeleteFile(player.GuildId + ".xml"); }
private bool Exists(ulong id, out RpsUserData userData) { int i = FindIndex(id); bool exists = i > -1; if (exists) { userData = _users[i]; return(true); } userData = null; return(false); }
public void PlayVsPlayer_Test_InstigatorWins() { RpsPlayData player1 = CreatePlayer("s"); RpsPlayData player2 = CreatePlayer("p", "Testa", "Test321", 147258369); _rps.Play(player1, player2); _rps.Play(player2, player1); var users = (List <RpsUserData>)_fileSystem.Load(); RpsUserData user1 = users.Find(x => x.UserId == 345678912); RpsUserData user2 = users.Find(x => x.UserId == 147258369); Equal(user2, 147258369, 1, 1, losses: 1, paperChosen: 1); Equal(user1, 345678912, 1, 1, 1, ratio: 100, currentStreak: 1, scissorsChosen: 1, coins: 1); DeleteFile(player1.GuildId + ".xml"); }
public void PlayVsPlayer_Test_Draw() { RpsPlayData player1 = CreatePlayer("r"); RpsPlayData player2 = CreatePlayer("r", "Testa", "Test321", 147258369); _rps.Play(player1, player2); _rps.Play(player2, player1); var users = (List <RpsUserData>)_fileSystem.Load(); RpsUserData user1 = users.Find(x => x.UserId == 345678912); RpsUserData user2 = users.Find(x => x.UserId == 147258369); Equal(user1, 345678912, 1, 1, draws: 1, rockChosen: 1); Equal(user2, 147258369, 1, 1, draws: 1, rockChosen: 1); DeleteFile(player1.GuildId + ".xml"); }
public void PlayVsBot_Test_BestStreak() { RpsPlayData player = CreatePlayer("p"); const int playsTimes = 11; for (int i = 0; i < playsTimes; i++) { _rps.Play(player, null, RpsActors.Rock); } _rps.Play(player, null, RpsActors.Scissors); int totalPlayTimes = playsTimes + 1; RpsUserData user = (RpsUserData)_fileSystem.Load().ElementAt(0); Equal(user, 345678912, totalPlayTimes, totalPlayTimes, playsTimes, 1, ratio: user.Ratio, bestStreak: playsTimes, paperChosen: totalPlayTimes, coins: user.Coins); DeleteFile(player.GuildId + ".xml"); }
public void Options_Test_ResetAndTotalPlays() { RpsPlayData player = CreatePlayer("p"); int totalPlays = 3; for (int i = 0; i < totalPlays; i++) { _rps.Play(player, null, RpsActors.Rock); } _rps.Options(CreatePlayer("r")); _rps.Play(CreatePlayer("p"), null, RpsActors.Rock); RpsUserData user = (RpsUserData)_fileSystem.Load().ElementAt(0); Equal(user, 345678912, 1, totalPlays + 1, user.Wins, ratio: user.Ratio, resets: 1, currentStreak: user.CurrentStreak, bestStreak: user.BestStreak, paperChosen: user.PaperChosen, coins: user.Coins); DeleteFile(player.GuildId + ".xml"); }
private void Equal(RpsUserData ud, ulong userId = 0, int plays = 0, int totalPlays = 0, int wins = 0, int losses = 0, int draws = 0, int ratio = 0, int currentStreak = 0, int bestStreak = 0, int resets = 0, int rockChosen = 0, int paperChosen = 0, int scissorsChosen = 0, int coins = 0) { Assert.Equal((double)userId, ud.UserId); Assert.Equal(plays, ud.Plays); Assert.Equal(totalPlays, ud.TotalPlays); Assert.Equal(wins, ud.Wins); Assert.Equal(losses, ud.Losses); Assert.Equal(draws, ud.Draws); Assert.Equal(ratio, ud.Ratio); Assert.Equal(currentStreak, ud.CurrentStreak); Assert.Equal(bestStreak, ud.BestStreak); Assert.Equal(resets, ud.Resets); Assert.Equal(rockChosen, ud.RockChosen); Assert.Equal(paperChosen, ud.PaperChosen); Assert.Equal(scissorsChosen, ud.ScissorsChosen); Assert.Equal(coins, ud.Coins); }
private RpsUserData CreateUser(RpsPlayData playData) { RpsUserData newData = new RpsUserData { Name = playData.Username, UserId = playData.UserId, GuildId = playData.GuildId, ChannelId = playData.ChannelId, Actor = ConvertInputIntoActor(playData.Input), DateVs = DateTime.Now }; _users.Add(newData); _fileSystem.Save(newData); return(newData); //LogToFile(LogSeverity.Info, $"Saved user: RockPaperScissors {temp.UserId} [{socketMessage.Author.Username}] {temp.GuildId} {temp.ChannelId}"); }
private Embed Challenging(RpsActors playerActor, RpsUserData authorUd, RpsUserData mentionUd, RpsPlayData authorPlayData, RpsPlayData mentionPlayData) { authorUd.UserIdVs = mentionUd.UserId; authorUd.Actor = playerActor; authorUd.DateVs = DateTime.Now.AddDays(1); int i = FindIndex(authorUd.UserId); _users[i] = authorUd; _fileSystem.UpdateFile(authorUd); mentionUd.UserIdVs = authorUd.UserId; mentionUd.Actor = RpsActors.Max; mentionUd.DateVs = authorUd.DateVs; i = FindIndex(mentionUd.UserId); _users[i] = mentionUd; _fileSystem.UpdateFile(mentionUd); _usersActiveVs.Add(authorUd); _usersActiveVs.Add(mentionUd); if (!_timerVs.Enabled) { _timerVs.Start(); } string rps = $"{ConvertActorToEmoji(RpsActors.Rock)}{ConvertActorToEmoji(RpsActors.Paper)}{ConvertActorToEmoji(RpsActors.Scissors)}"; return(new EmbedBuilder() .WithTitle($"{rps}*GET READY FOR THE NEXT BATTLE*{rps}") .WithAuthor(TitleBuilder(authorPlayData)) .WithDescription($"{authorPlayData.Mention} " + "**VS** " + $"{mentionPlayData.Mention}\n\n" + $"{authorPlayData.Username} has already made a move.") .WithFields(new EmbedFieldBuilder { IsInline = true, Name = "Ends", Value = authorUd.DateVs }) .WithColor(Color.DarkOrange).Build()); }
private IEnumerable <RpsUserData> CollectUserData(Dictionary <XDocument, ulong> xmls) { List <RpsUserData> ud = new List <RpsUserData>(); foreach (KeyValuePair <XDocument, ulong> xml in xmls) { foreach (XElement e in xml.Key.Descendants("Service").Descendants(ElementRoot).Descendants("User")) { RpsUserData user = new RpsUserData(); if (xml.Key.Document != null) { user.GuildId = xml.Value; } int i = 0; user.UserId = GetAttributeValueLong(_attributeNames.ElementAt(i++), e); user.UserIdVs = GetAttributeValueLong(_attributeNames.ElementAt(i++), e); user.ChannelId = GetAttributeValueLong(_attributeNames.ElementAt(i++), e); user.Plays = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.TotalPlays = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.Wins = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.Losses = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.Draws = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.Ratio = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.CurrentStreak = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.BestStreak = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.Resets = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.RockChosen = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.PaperChosen = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.ScissorsChosen = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.Coins = GetAttributeValueInt(_attributeNames.ElementAt(i++), e); user.Actor = GetAttributeValueActor(_attributeNames.ElementAt(i++), e); user.DateVs = GetAttributeValueDateTime(_attributeNames.ElementAt(i) ?? DateTime.Now.ToString(CultureInfo.InvariantCulture), e); ud.Add(user); } } return(ud); }
private void EqualUser2(RpsUserData ud, ulong userId = 420, ulong channelId = 4, ulong guildId = 123456789, int plays = 100, int totalPlays = 15, int wins = 55, int losses = 44, int draws = 11, int ratio = 10, int resets = 2, int rockChosen = 22, int paperChosen = 55, int scissorsChosen = 33, int currentStreak = 44, int bestStreak = 25, int coins = 15, ulong userIdVs = 69, string actor = "Paper") { Assert.Equal((double)userId, ud.UserId); Assert.Equal((double)channelId, ud.ChannelId); Assert.Equal((double)guildId, ud.GuildId); Assert.Equal((double)userIdVs, ud.UserIdVs); Assert.Equal(actor, ud.Actor.ToString()); Assert.Equal(plays, ud.Plays); Assert.Equal(totalPlays, ud.TotalPlays); Assert.Equal(wins, ud.Wins); Assert.Equal(losses, ud.Losses); Assert.Equal(draws, ud.Draws); Assert.Equal(ratio, ud.Ratio); Assert.Equal(currentStreak, ud.CurrentStreak); Assert.Equal(bestStreak, ud.BestStreak); Assert.Equal(resets, ud.Resets); Assert.Equal(rockChosen, ud.RockChosen); Assert.Equal(paperChosen, ud.PaperChosen); Assert.Equal(scissorsChosen, ud.ScissorsChosen); Assert.Equal(coins, ud.Coins); }
private void UpdateUsers(RpsUserData ud) { Exists(ud.UserId, out int i); _users[i] = ud; }
private int FindIndex(RpsUserData ud) { return(_users.FindIndex(x => x.UserId == ud.UserId)); }
private void ProcessResults(RpsUserData ud, GameState state, string result, string mentionUser, out string resultText) { resultText = result; ud.Plays++; ud.TotalPlays++; switch (ud.Actor) { case RpsActors.Rock: ud.RockChosen++; break; case RpsActors.Paper: ud.PaperChosen++; break; case RpsActors.Scissors: ud.ScissorsChosen++; break; case RpsActors.Max: LogToFile(LogSeverity.Error, "Wrong actor was given."); break; default: LogToFile(LogSeverity.Error, "No actor was given."); break; } switch (state) { case GameState.Win: ud.Wins++; ud.CurrentStreak++; int bonus = CalculateStreakBonus(ud.CurrentStreak, ud.Plays); ud.Coins += bonus; string newRecord = ud.CurrentStreak > ud.BestStreak ? "New streak record!!!" : string.Empty; string streak = ud.CurrentStreak > 1 ? ud.CurrentStreak + $" win streak! {newRecord}" : string.Empty; string plural = bonus == 1 ? string.Empty : "s"; resultText = resultText.Replace(CoinsInKeyText, $"+{bonus} Ring{plural}. {streak}\n"); break; case GameState.Lose: ud.Losses++; ud.Coins--; bool emptyWallet = ud.Coins <= 0; if (emptyWallet) { ud.Coins = 0; resultText += $"{mentionUser} -0 Rings (empty wallet)"; } else { resultText += $"{mentionUser} -1 Ring"; } resultText += "\n"; if (ud.CurrentStreak > ud.BestStreak) { ud.BestStreak = ud.CurrentStreak; } ud.CurrentStreak = 0; break; case GameState.Draw: ud.Draws++; break; case GameState.None: LogToFile(LogSeverity.Error, "Wrong game state was given."); break; default: LogToFile(LogSeverity.Error, "No game state was given."); break; } ud.Actor = RpsActors.Max; ud.UserIdVs = 0; float ratio = (float)ud.Wins / ud.Plays; ud.Ratio = (int)(ratio * 100); int i = FindIndex(ud); _users[i] = ud; _fileSystem.UpdateFile(ud); }