Example #1
0
        private void RunProbabilityToLoseTest(int N, int[] decisions, double expected, string assertMsg)
        {
            MafiaGame target = new MafiaGame();
            double    actual;

            actual = target.probabilityToLose(N, decisions);
            Assert.AreEqual(expected, actual, assertMsg);
        }
Example #2
0
        public PowerCommand(MafiaGame game, VillageMember member, Power power)
        {
            this.game = game;
            this.member = member;
            this.power = power;

            this.name = power.Name;
            this.allowedInPublic = publicCommands.Contains(power.Name.ToLower());
        }
Example #3
0
        public FactionCommand(MafiaGame game, VillageMember member, Power power)
        {
            this.game = game;
            this.member = member;
            this.power = power;

            this.name = power.Name;
            this.allowedInPublic = false;
        }
Example #4
0
 // END CUT HERE
 // BEGIN CUT HERE
 public static void Main()
 {
     try {
     MafiaGame ___test = new MafiaGame();
     ___test.run_test(-1);
     } catch(Exception e) {
     //Console.WriteLine(e.StackTrace);
     Console.WriteLine(e.ToString());
     }
 }
Example #5
0
// END CUT HERE
// BEGIN CUT HERE
    public static void Main()
    {
        try {
            MafiaGame ___test = new MafiaGame();
            ___test.run_test(-1);
        } catch (Exception e) {
//Console.WriteLine(e.StackTrace);
            Console.WriteLine(e.ToString());
        }
    }
Example #6
0
        public HttpInterface(MafiaGame game)
        {
            secret = (new Random()).Next();
            this.game = game;

            listener = new HttpListener();
            listener.Prefixes.Add("http://*:16981/" + secret + "/");

            listener.Start();
            listener.BeginGetContext(new AsyncCallback(OnRequest), null);
        }
Example #7
0
        public async Task NotifyStartOfGame(MafiaGame game)
        {
            // Send messages to mafia
            foreach (var user in game.Mafia)
            {
                await user.SendMessageAsync("You are in the Mafia!");
            }

            // Send message to channel specifying teams
            await Context.Channel.SendMessageAsync(GetGameAnnouncement(game));
        }
Example #8
0
 public MafiaGameBuilder CreateGame()
 {
     _game = null;
     using (var scope = _serviceScopeFactory.CreateScope())
     {
         _game       = scope.ServiceProvider.GetRequiredService <MafiaGame>();
         _game.Id    = Guid.NewGuid().ToString();
         _game.Name  = $"Huba-buba";
         _game.State = MafiaGameState.Created;
     }
     return(this);
 }
Example #9
0
        public void TestScoreSecnario1()
        {
            var mentions = new List <IUser>();

            var user1 = new Mock <IUser>();

            user1.Setup(u => u.Username).Returns("k");
            user1.Setup(u => u.Id).Returns(1);
            mentions.Add(user1.Object);

            var user2 = new Mock <IUser>();

            user2.Setup(u => u.Username).Returns("t");
            user2.Setup(u => u.Id).Returns(2);
            mentions.Add(user2.Object);

            var g = MafiaGame.CreateGame(mentions, 1).Game;

            var mafia = g.Mafia[0];

            g.Vote(mafia.Id, new List <IUser>()
            {
                mafia
            });

            g.Vote(user1.Object.Id, new List <IUser>()
            {
                mafia
            });
            g.Vote(user2.Object.Id, new List <IUser>()
            {
                mafia
            });

            Dictionary <ulong, int> score = null;

            if (g.Team1.Where(u => u.Id == mafia.Id).Count() > 0)
            {
                score = g.ScoreGame(0, 1);
                Assert.AreEqual(score[g.Team2[0].Id], 1);
            }
            else
            {
                score = g.ScoreGame(1, 0);
                Assert.AreEqual(score[g.Team1[0].Id], 1);
            }

            Assert.AreEqual(score[mafia.Id], 2);
        }
Example #10
0
        public void TestCreateGameGeneratesValidGame()
        {
            for (int j = 0; j < 16; j++)
            {
                var mentions = new List <IUser>();
                for (int i = 0; i < (j % 7) + 2; i++)
                {
                    var user = new Mock <IUser>();
                    user.Setup(u => u.Username).Returns(i.ToString());
                    mentions.Add(user.Object);
                }

                for (int i = 0; i < 100; i++)
                {
                    var numMafia = (i % (mentions.Count - 1)) + 1;
                    var game     = (MafiaGame.CreateGame(mentions, numMafia)).Game;

                    Assert.AreEqual(game.Mafia.Count(), numMafia);                            // validate actual number of mafia was as requested
                    Assert.AreEqual(game.Team1.Count() + game.Team2.Count(), mentions.Count); // validate members of both teams equals total count of mentions

                    var mafia = new Dictionary <string, string>();
                    var t1    = new Dictionary <string, string>();
                    var t2    = new Dictionary <string, string>();

                    foreach (var u in game.Mafia)
                    {
                        Assert.IsTrue(mentions.Contains(u));           // validate each mafia member was part of original mentions
                        Assert.IsFalse(mafia.ContainsKey(u.Username)); // validate users weren't added to mafia twice
                        mafia.Add(u.Username, u.Username);
                    }
                    foreach (var u in game.Team1)
                    {
                        t1.Add(u.Username, u.Username);
                        Assert.IsTrue(mentions.Contains(u)); // validate every team member was part of original mentions
                    }
                    foreach (var u in game.Team2)
                    {
                        t2.Add(u.Username, u.Username);
                        Assert.IsTrue(mentions.Contains(u));        // validate every team member was part of original mentions
                        Assert.IsFalse(t1.ContainsKey(u.Username)); // validate every team2 member is not in team 1
                    }
                    foreach (var u in game.Team1)
                    {
                        Assert.IsFalse(t2.ContainsKey(u.Username)); // validate every team1 member is not in team 2
                    }
                }
            }
        }
Example #11
0
        public async Task Score(int id, int team1Score, int team2Score)
        {
            MafiaGame game = null;

            try
            {
                game = GetGame(id, Context.Guild.GetUser);
            }
            catch (Exception e)
            {
                await Context.Channel.SendMessageAsync($"Could not find Mafia game with Id: {id}");

                return;
            }

            await Context.Channel.SendMessageAsync(GetScoreAnnouncement(game, team1Score, team2Score));
        }
Example #12
0
        private string GetScoreAnnouncement(MafiaGame game, int team1Score, int team2Score)
        {
            var ordered = game.ScoreGame(team1Score, team2Score).OrderByDescending(x => x.Value);

            string output = $"**Mafia Game: {game.Id}**\r\n    Mafia: ";

            foreach (var mafia in game.Mafia)
            {
                output += $"{mafia.Mention } ";
            }

            foreach (var score in ordered)
            {
                output += $"\r\n    {game.Users()[score.Key].Mention} = {score.Value}";
            }

            return(output);
        }
Example #13
0
        private string GetGameAnnouncement(MafiaGame game)
        {
            // Send messages to Team 1
            string output = $"**Mafia Game: {game.Id}**\r\n\r\n    Team1: ";

            foreach (var user in game.Team1)
            {
                output += $"{user.Mention} ";
            }

            // Send messages to team 2
            output += "\r\n    Team2: ";
            foreach (var user in game.Team2)
            {
                output += $"{user.Mention} ";
            }

            return(output);
        }
Example #14
0
        public async Task VoteCommand(int id, [Remainder] string message = "")
        {
            MafiaGame game = null;

            try
            {
                game = GetGame(id, Context.Guild.GetUser);
            }
            catch (Exception e)
            {
                await Context.Channel.SendMessageAsync($"Could not find Mafia game with Id: {id}");

                return;
            }

            game.Vote(Context.User.Id, Context.Message.MentionedUsers.Select(s => (IUser)s).ToList());

            var collection = Context.Database.GetCollection <MafiaGame>();

            collection.Update(game);
        }
Example #15
0
        public async Task NewGameCommand(int numMafias, [Remainder] string message = "")
        {
            var result = MafiaGame.CreateGame(Context.Message.MentionedUsers.Select(s => (IUser)s).ToList(), numMafias);

            if (result == null)
            {
                await Context.Channel.SendMessageAsync(result.ErrorMsg);

                return;
            }
            var game = result.Game;

            var collection = Context.Database.GetCollection <MafiaGame>();

            // Prepare for DB
            game.Id        = collection.Count() + 1;
            game.MessageId = Context.Message.Id;

            // Insert into DB
            collection.Insert(game);
            collection.EnsureIndex(x => x.Id);

            await NotifyStartOfGame(game);
        }
Example #16
0
        public void TestValidateInputs()
        {
            var mentions = new List <IUser>();

            for (int i = 0; i < 3; i++)
            {
                var user = new Mock <IUser>();
                user.Setup(u => u.Username).Returns(i.ToString());
                mentions.Add(user.Object);
            }

            Assert.IsNotNull(MafiaGame.CreateGame(null, 1).ErrorMsg);                      // must have players
            Assert.IsNotNull(MafiaGame.CreateGame(mentions, 0).ErrorMsg);                  // Can not have zero mafia
            Assert.IsNotNull(MafiaGame.CreateGame(mentions, -1).ErrorMsg);                 // Can not have negative mafia
            Assert.IsNotNull(MafiaGame.CreateGame(mentions, mentions.Count).ErrorMsg);     // Can not have same mafia as players
            Assert.IsNotNull(MafiaGame.CreateGame(mentions, mentions.Count + 1).ErrorMsg); // can not have more mafia than players

            // Valid states
            Assert.IsNull(MafiaGame.CreateGame(mentions, 1).ErrorMsg);
            Assert.IsNull(MafiaGame.CreateGame(mentions, 2).ErrorMsg);

            mentions.Clear();
            Assert.IsNotNull(MafiaGame.CreateGame(mentions, 1).ErrorMsg); // Can not have zero players
        }
Example #17
0
 public VoteCountCommand(MafiaGame game)
 {
     this.game = game;
 }
Example #18
0
 public AdminCommand(MafiaGame game)
 {
     this.game = game;
 }
Example #19
0
 public VariantCommand(MafiaGame game)
 {
     this.game = game;
 }
Example #20
0
 public SkipCommand(MafiaGame game)
 {
     this.game = game;
 }
Example #21
0
 public DeadlineCommand(MafiaGame game)
 {
     this.game = game;
 }
Example #22
0
 public RoleCommand(MafiaGame game)
 {
     this.game = game;
 }
Example #23
0
 public ReplaceCommand(MafiaGame game)
 {
     this.game = game;
 }
Example #24
0
 public ModkillCommand(MafiaGame game)
 {
     this.game = game;
 }