Ejemplo n.º 1
0
        public Game(IActionQueue actionQueue, IPieceProvider pieceProvider, string name, int maxPlayers, int maxSpectators, GameRules rule, GameOptions options, string password = null)
        {
            if (actionQueue == null)
                throw new ArgumentNullException("actionQueue");
            if (pieceProvider == null)
                throw new ArgumentNullException("pieceProvider");
            if (name == null)
                throw new ArgumentNullException("name");
            if (maxPlayers <= 0)
                throw new ArgumentOutOfRangeException("maxPlayers", "maxPlayers must be strictly positive");
            if (maxSpectators <= 0)
                throw new ArgumentOutOfRangeException("maxSpectators", "maxSpectators must be strictly positive");
            if (options == null)
                throw new ArgumentNullException("options");

            Id = Guid.NewGuid();
            _actionQueue = actionQueue;
            _pieceProvider = pieceProvider;
            _pieceProvider.Occurancies = () => options.PieceOccurancies;
            Name = name;
            CreationTime = DateTime.Now;
            MaxPlayers = maxPlayers;
            MaxSpectators = maxSpectators;
            Rule = rule;
            Options = options;
            Password = password;
            State = GameStates.Created;

            _specialId = 0;
            _isSuddenDeathActive = false;
            _gameStatistics = new Dictionary<string, GameStatisticsByPlayer>();
            _winList = new List<WinEntry>();
            _suddenDeathTimer = new Timer(SuddenDeathCallback, null, Timeout.Infinite, 0);
            _voteKickTimer = new Timer(VoteKickCallback, null, Timeout.Infinite, 0);
        }
Ejemplo n.º 2
0
 public void ClientChangeOptions(GameOptions options)
 {
     ExceptionFreeAction(() => _proxy.ClientChangeOptions(options));
 }
Ejemplo n.º 3
0
        public void OnGameOptionsChanged(GameOptions gameOptions)
        {
            ResetTimeout();

            throw new NotImplementedException();
        }
Ejemplo n.º 4
0
        public void TestChangeOptionsPieceProviderOccuranciesModified()
        {
            GameOptions originalOptions = new GameOptions();
            originalOptions.Initialize(GameRules.Custom);
            IGame game = CreateGame("game1", 5, 10, GameRules.Custom, originalOptions);
            game.Start(new CancellationTokenSource());
            IClient client1 = CreateClient("client1", new CountCallTetriNETCallback());
            game.Join(client1, false);

            GameOptions newOptions = new GameOptions();
            newOptions.Initialize(GameRules.Standard);
            game.ChangeOptions(client1, newOptions);

            Assert.IsNotNull(PieceProvider.Occurancies);
            Assert.AreEqual(newOptions.PieceOccurancies, PieceProvider.Occurancies());
        }
Ejemplo n.º 5
0
 public void ClientChangeOptions(GameOptions options)
 {
     IClient client = ClientManager[ClientCallback];
     if (client != null && HostClientChangeOptions != null)
         HostClientChangeOptions(client, options);
 }
Ejemplo n.º 6
0
 public void ClientChangeOptions(GameOptions options)
 {
     SetCallbackAndAddress();
     Host.ClientChangeOptions(options);
 }
Ejemplo n.º 7
0
        public void TestChangeOptionsOkWhenWaitingGameStart()
        {
            GameOptions originalOptions = new GameOptions();
            originalOptions.Initialize(GameRules.Custom);
            IGame game = CreateGame("game1", 5, 10, GameRules.Custom, originalOptions);
            game.Start(new CancellationTokenSource());
            IClient client1 = CreateClient("client1", new CountCallTetriNETCallback());
            game.Join(client1, false);

            GameOptions newOptions = new GameOptions();
            newOptions.Initialize(GameRules.Custom);
            game.ChangeOptions(client1, newOptions);

            Assert.AreEqual(newOptions, game.Options);
        }
Ejemplo n.º 8
0
 protected abstract IGame CreateGame(string name, int maxPlayers, int maxSpectators, GameRules rule, GameOptions options, string password);
Ejemplo n.º 9
0
 private void OnGameOptionsChanged(GameOptions gameOptions)
 {
     Console.WriteLine("OnGameOptionsChanged");
 }
Ejemplo n.º 10
0
        public void TestChangeOptionsFailedIfNotGameMaster()
        {
            GameOptions originalOptions = new GameOptions();
            originalOptions.Initialize(GameRules.Custom);
            IGame game = CreateGame("game1", 5, 10, GameRules.Custom, originalOptions);
            game.Start(new CancellationTokenSource());
            IClient client1 = CreateClient("client1", new CountCallTetriNETCallback());
            game.Join(client1, false);
            IClient client2 = CreateClient("client2", new CountCallTetriNETCallback());
            game.Join(client2, false);

            GameOptions newOptions = new GameOptions();
            newOptions.Initialize(GameRules.Custom);
            bool succeed = game.ChangeOptions(client2, newOptions);

            Assert.AreEqual(originalOptions, game.Options);
            Assert.IsFalse(succeed);
        }
Ejemplo n.º 11
0
 public void OnGameOptionsChanged(GameOptions gameOptions)
 {
     ExceptionFreeAction(() => Callback.OnGameOptionsChanged(gameOptions));
 }
Ejemplo n.º 12
0
 protected override IGame CreateGame(string name)
 {
     GameOptions options = new GameOptions();
     options.Initialize(GameRules.Standard);
     return new Game(new ActionQueueMock(), new PieceProviderMock(), name, 10, 10, GameRules.Standard, options);
 }
Ejemplo n.º 13
0
 public bool ChangeOptions(GameOptions options)
 {
     _proxy.Do(x => x.ClientChangeOptions(options));
     return true;
 }
Ejemplo n.º 14
0
        public bool ChangeOptions(IClient client, GameOptions options)
        {
            if (State != GameStates.WaitStartGame)
            {
                Log.Default.WriteLine(LogLevels.Warning, "Cannot change options, game {0} is started", Name);
                return false;
            }
            if (client != null)
            {
                if (!client.IsPlayer)
                {
                    Log.Default.WriteLine(LogLevels.Warning, "Cannot change options, {0} is not flagged as player", client.Name);
                    return false;
                }
                if (client.Game != this)
                {
                    Log.Default.WriteLine(LogLevels.Warning, "Cannot change options, {0} is not in game {1}", client.Name, Name);
                    return false;
                }
                if (!client.IsGameMaster)
                {
                    Log.Default.WriteLine(LogLevels.Warning, "Cannot change options, client {0} is not game master", client.Name);
                    return false;
                }
            }
            if (!options.IsValid)
            {
                Log.Default.WriteLine(LogLevels.Warning, "Cannot change options, options are not valid");
                return false;
            }
            //
            Options = options;
            _pieceProvider.Occurancies = () => Options.PieceOccurancies;
            // Inform clients
            foreach (IClient target in Clients)
                target.OnGameOptionsChanged(options);

            Log.Default.WriteLine(LogLevels.Info, "Game {0}: game options changed by {1}", Name, client == null ? "[SERVER]" : client.Name);
            return true;
        }
Ejemplo n.º 15
0
        public void TestChangeOptionsClientsInformed()
        {
            GameOptions originalOptions = new GameOptions();
            originalOptions.Initialize(GameRules.Custom);
            IGame game = CreateGame("game1", 5, 10, GameRules.Custom, originalOptions);
            game.Start(new CancellationTokenSource());
            CountCallTetriNETCallback callback1 = new CountCallTetriNETCallback();
            IClient client1 = CreateClient("client1", callback1);
            game.Join(client1, false);
            CountCallTetriNETCallback callback2 = new CountCallTetriNETCallback();
            IClient client2 = CreateClient("client2", callback2);
            game.Join(client2, true);
            CountCallTetriNETCallback callback3 = new CountCallTetriNETCallback();
            IClient client3 = CreateClient("client3", callback3);
            game.Join(client3, false);

            GameOptions newOptions = new GameOptions();
            newOptions.Initialize(GameRules.Custom);
            game.ChangeOptions(client1, newOptions);

            Assert.AreEqual(1, callback1.GetCallCount("OnGameOptionsChanged"));
            Assert.AreEqual(1, callback2.GetCallCount("OnGameOptionsChanged"));
            Assert.AreEqual(1, callback3.GetCallCount("OnGameOptionsChanged"));
        }
 public void OnGameOptionsChanged(GameOptions gameOptions)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 17
0
 protected override IGame CreateGame(string name, int maxPlayers, int maxSpectators)
 {
     GameOptions options = new GameOptions();
     options.Initialize(GameRules.Standard);
     return CreateGame(name, maxPlayers, maxSpectators, GameRules.Classic, options);
 }
Ejemplo n.º 18
0
 protected override IGame CreateGame(string name, int maxPlayers, int maxSpectators, GameRules rule, GameOptions options, string password)
 {
     return new Game(new ActionQueueMock(), new PieceProviderMock(), name, maxPlayers, maxSpectators, rule, options, password);
 }
Ejemplo n.º 19
0
        protected IGame CreateGame(IActionQueue actionQueue, IPieceProvider pieceProvider, string name, int maxPlayers, int maxSpectators, GameRules rule, GameOptions options, string password = null)
        {
            ActionQueue = actionQueue as ActionQueueMock;
            PieceProvider = pieceProvider as PieceProviderMock;

            return new Game(actionQueue, pieceProvider, name, maxPlayers, maxSpectators, rule, options, password);
        }
Ejemplo n.º 20
0
 public void ClientChangeOptions(GameOptions options)
 {
     IClient client = ClientManager[ClientCallback];
     if (client != null)
         HostClientChangeOptions.Do(x => x(client, options));
     else
         Log.Default.WriteLine(LogLevels.Warning, "ClientChangeOptions from unknown client");
 }
Ejemplo n.º 21
0
        public void TestConstructorSetProperties()
        {
            const string name = "game1";
            const int maxPlayers = 5;
            const int maxSpectators = 10;
            const GameRules rule = GameRules.Extended;
            const string password = "******";
            GameOptions options = new GameOptions();

            IGame game = new Game(new ActionQueueMock(), new PieceProviderMock(), name, maxPlayers, maxSpectators, rule, options, password);

            Assert.IsNotNull(game);
            Assert.AreEqual(name, game.Name);
            Assert.AreEqual(maxPlayers, game.MaxPlayers);
            Assert.AreEqual(maxSpectators, game.MaxSpectators);
            Assert.AreEqual(rule, game.Rule);
            Assert.AreEqual(options, game.Options);
            Assert.AreEqual(password, game.Password);
            Assert.AreNotEqual(Guid.Empty, game.Id);
            Assert.AreEqual(GameStates.Created, game.State);
        }
Ejemplo n.º 22
0
 public void OnGameOptionsChanged(GameOptions gameOptions)
 {
     UpdateCallInfo(System.Reflection.MethodBase.GetCurrentMethod().Name, gameOptions);
 }
Ejemplo n.º 23
0
 public IGame CreateGame(string name, int maxPlayers, int maxSpectators, GameRules rule, GameOptions options, string password)
 {
     return new Game(new BlockingActionQueue(), new PieceBag(RangeRandom.Random, 4), name, maxPlayers, maxSpectators, rule, options, password);
 }