Esempio n. 1
0
 public Player(string nickname, playerColor color, IGameCallback callback)
 {
     this.color = color;
     this.nickname = nickname;
     this.callback = callback;
     this.alive = true;
 }
Esempio n. 2
0
        /// <summary>
        ///  Connects a new user.
        /// </summary>
        /// <returns>Returna a list of players.</returns>
        public List <Player> Connect(String username, int nbOfPoints, int Position, int priority)
        {
            IGameCallback currentCallback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            if (!callbackList.Contains(currentCallback))
            {
                callbackList.Add(currentCallback);
                Player p = (new Player(username, nbOfPoints, Position, priority));
                players.Add(p);
                p.pawns = new List <Pawn> {
                    new Pawn(1, p),
                    new Pawn(2, p),
                    new Pawn(3, p),
                    new Pawn(4, p)
                };
            }

            foreach (var v in callbackList)
            {
                if (v != currentCallback)
                {
                    v.NewPlayerConnected(players);
                }
            }
            return(players);
        }
Esempio n. 3
0
        /// <summary>
        /// Finds and removes a player from the list.
        /// </summary>
        /// <param name="username">THe username of the player.</param>
        public void RemovePlayer(string username)
        {
            Player player = null;
            bool   b      = false;

            foreach (Player p in players)
            {
                if (p.Username == username)
                {
                    player = p;
                    if (currentTurn == p.Priority)
                    {
                        currentTurn++;
                    }

                    b = true;
                }
            }

            if (b)
            {
                players.Remove(player);
            }

            IGameCallback currentCallback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            foreach (var cb in callbackList)
            {
                if (cb != currentCallback)
                {
                    cb.MessageRecieved("Player " + username + " left the game.", "SYSTEM");
                }
            }
            ReasignPriority();
        }
Esempio n. 4
0
 public void Init(IGameCallback gameCallback, ITileControlCallback tileCallback)
 {
     this._gameCallback            = gameCallback;
     _tileCallback                 = tileCallback;
     CurrentGame                   = _games[0];
     _currentHexPanel.GameCallback = this._gameCallback;
     _currentHexPanel.TileCallback = _tileCallback;
 }
Esempio n. 5
0
        public void SubscribeToReplayGameEvents(string userName)
        {
            IGameCallback clientCallbackGame = OperationContext.Current.GetCallbackChannel <IGameCallback>();
            Player        player             = playersOnline.Find(x => x.UserName == userName);

            player.IGameCallback = clientCallbackGame;
            Game.Game game = player.Game;
            game.StartGameReplay(player);
        }
Esempio n. 6
0
        /// <summary>
        /// Notifies other players that there is a new winner.
        /// </summary>
        /// <param name="playerName">The player who has won the game.</param>
        public void NotifyForWinner(string playerName)
        {
            IGameCallback currentCallback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            foreach (var cb in callbackList)
            {
                if (cb != currentCallback)
                {
                    cb.NewWinner(playerName);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Pauses the game for all users.
        /// </summary>
        /// <param name="b"></param>
        public void GamePause(bool b)
        {
            IGameCallback currentCallback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            foreach (var cb in callbackList)
            {
                if (cb != currentCallback)
                {
                    cb.GamePaused(b);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Sends a message to other players.
        /// </summary>
        /// <param name="msg">The message to be sent.</param>
        /// <param name="playerName">The player who is sending the message.</param>
        public void SendMessage(string msg, string playerName)
        {
            IGameCallback currentCallback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            foreach (var cb in callbackList)
            {
                if (cb != currentCallback)
                {
                    cb.MessageRecieved(msg, playerName);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Disconnects the player by removing the current callback from the list.
        /// </summary>
        public void Disconnect()
        {
            IGameCallback currentCallback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            callbackList.Remove(currentCallback);

            foreach (var v in callbackList)
            {
                if (v != currentCallback)
                {
                    v.NewPlayerConnected(players);
                }
            }
        }
Esempio n. 10
0
        public GameManager(IGameCallback gameCallback)
        {
            this.gameCallback = gameCallback;

            // Load Creatures
            this.Friends = new List <FDCreature>();
            this.Enemies = new List <FDCreature>();
            this.Npcs    = new List <FDCreature>();
            this.Deads   = new List <FDCreature>();

            eventManager = new GameEventManager(this);

            dispatcher = new GameStateDispatcher(this);

            enemyAIHandler = new AIHandler(this, CreatureFaction.Enemy);
            npcAIHandler   = new AIHandler(this, CreatureFaction.Npc);
        }
Esempio n. 11
0
        // TODO use password
        public void SubscribeToGameEvents(string userName)
        {
            IGameCallback clientCallbackGame = OperationContext.Current.GetCallbackChannel <IGameCallback>();
            Player        player             = playersOnline.Find(x => x.UserName == userName);

            player.IGameCallback = clientCallbackGame;
            Game.Game game = player.Game;

            foreach (Player otherPlayers in game.Players)
            {
                if (otherPlayers.IGameCallback == null)
                {
                    return;
                }
            }
            game.StartGame();
        }
Esempio n. 12
0
        public JoinGameServiceResponse JoinService(JoinGameServiceRequest joinServiceRequest)
        {
            //checking user authentication:
            Guid    authentication = serviceLogic.GetHeaderAuthentication();
            UserDTO UserGameSender = serviceLogic.TryGetUserByAuthentication(authentication);

            if (UserGameSender == null)//if user exist
            {
                return(new JoinGameServiceResponse {
                    IsSuccess = false, Message = "user failed to authenticate you are not allowed to join this service"
                });
            }
            //if user is authenticated - adding him to the chat service:
            IGameCallback callback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            if (!callbacksByUserName.ContainsKey(UserGameSender.UserName))
            {//from now on, this user will be able to get messages.
                callbacksByUserName.Add(UserGameSender.UserName, callback);
            }
            return(new JoinGameServiceResponse {
                IsSuccess = true, Message = "Joined the game service succesfully"
            });
        }
Esempio n. 13
0
        /// <summary>
        /// Get the player from the game context. Safe to assume this is never null.
        /// </summary>
        /// <returns></returns>
        private Player getPlayerFromGameContext()
        {
            IGameCallback currentPlayerCallback = OperationContext.Current.GetCallbackChannel <IGameCallback>();

            return(playersOnline.Find(x => x.IGameCallback == currentPlayerCallback));
        }
Esempio n. 14
0
 private UserAccount getUserAccount(IGameCallback callback)
 {
     foreach (UserAccount u in subscribers.Keys)
     {
         IGameCallback c  = subscribers[u];
         if (callback.Equals(c))
         {
             return u;
         }
     }
     return null;
 }
Esempio n. 15
0
        /// <summary>
        /// Set a new player to control a territory
        /// use the callback as this is a good way to verify 
        /// that the correct client is accessing service 
        /// and trying to select/control a new territory
        /// </summary>
        /// <param name="territoryId"></param>
        /// <param name="callback">callback reference to client</param>
        /// <returns></returns>
        public bool updateTerritoryInformation(int territoryId, IGameCallback callback, int troops)
        {
            Player tempPlayer = playerExist(callback);
            if (tempPlayer == null)
            {
                return false;
            }

            // check that the player is active
            if (activePlayer.Equals(tempPlayer) == false)
            {
                return false;
            }

            // check that territory id exist
            Territory tempTerritory = getTerritory(territoryId);
            if (tempPlayer == null)
            {
                return false;
            }

            // check what phase game is in
            switch (gameState)
            {
                case State.waiting:
                    break;
                case State.select:

                    //tempTerritory = getTerritory(territoryId);
                    if (tempTerritory.controlledByPlayer != null)
                    {
                        return false;
                    }

                    // set player to control the new territory
                    if (setTerritory(territoryId, tempPlayer, 1))
                    {
                        // set new active player
                        setNextPlayersTurn();
                    }

                    // check if all territories has been selected
                    if (allTerritoriesSelected())
                    {
                        // calculate additional troops
                        int additional = calculateAdditionalTroops(null);
                        foreach (Player p in players)
                        {
                            p.troopsToPlace = additional;
                        }

                        // move to next phase of the game
                        gameState = State.placeOne;
                    }

                    return true;
                case State.placeOne:

                    // check that nobody owns territory already
                    //tempTerritory = getTerritory(territoryId);
                    if (tempTerritory.controlledByPlayer == null)
                    {
                        return false;
                    }

                    // check that player has additonal troops to place
                    if (tempPlayer.troopsToPlace > 0)
                    {
                        // check that player owns territory he clicked on
                        if (tempTerritory.controlledByPlayer.Equals(tempPlayer) == false)
                        {
                            return false;
                        }

                        // set additional troop
                        // set player to control the new territory
                        if (setTerritory(territoryId, tempPlayer, ++tempTerritory.troops))
                        {
                            // decrease amount of troops to place
                            setPlayerTroopsToPlace(tempPlayer, --tempPlayer.troopsToPlace);
                            setNextPlayersTurn();

                            // check the next player's trrops to place
                            // and calculate how many new troops the next player gets
                            if (activePlayer.troopsToPlace == 0)
                            {

                                // move to next state
                                gameState = State.fortify;

                                // caluculate how many new troops the player gets
                                int additionalFortify = calculateAdditionalTroops(activePlayer);
                                setPlayerTroopsToPlace(activePlayer, additionalFortify);

                            }

                            // set new active player

                            return true;
                        }

                    }

                    break;

                case State.reinforce:
                    break;
                case State.attack:
                    break;
                case State.fortify:

                    if (tempPlayer.troopsToPlace > 0)
                    {
                        // check that player control territory he selected
                        if (getTerritory(territoryId).controlledByPlayer != activePlayer)
                        {
                            return false;
                        }

                        if (setTerritory(territoryId, tempPlayer, ++tempTerritory.troops))
                        {
                            // decrease amount of troops to place
                            setPlayerTroopsToPlace(tempPlayer, --tempPlayer.troopsToPlace);

                            // check if there is more troops to place
                            if (tempPlayer.troopsToPlace == 0)
                            {
                                gameState = State.attack;
                            }

                            return true;
                        }
                        else
                        {
                            // setting additional troops failed..
                            return false;
                        }

                    }

                    break;

                case State.end:
                    break;
                default:
                    break;
            }

            return false;
        }
Esempio n. 16
0
        public Player playerExist(IGameCallback callback)
        {
            foreach (Player p in players)
            {
                if (p.callback == callback)
                {
                    return p;
                }
            }

            return null;
        }