Beispiel #1
0
 /// <summary>
 /// Make sure player is a part of the game
 /// or if not, call him spectator for example
 /// </summary>
 /// <param name="p"></param>
 /// <param name="m"></param>
 public void newMessage(Player p, String m)
 {
     messages.Add(new ChatMessage(p, m));
 }
Beispiel #2
0
 private void updateAllClientsOfNewChatMessage(int gameId, Player exceptThisPlayer)
 {
     foreach (Player p in games[gameId].players)
     {
         if (p.Equals(exceptThisPlayer) == false)
         {
             p.callback.newChatMessage();
         }
     }
 }
Beispiel #3
0
 // TODO
 // if a client disconnect the callback will give a error!
 private void updateAllClientsThatANewPlayerHasBeenAdded(int gameId, Player exceptThisPlayer)
 {
     // all player object has a client reference
     foreach (Player p in games[gameId].players)
     {
         // alert all clients except yourself, notifying yourself
         // of something you just did does not make sense
         if (p.Equals(exceptThisPlayer) == false)
         {
             // update player is a part of the callback and will be executed
             // on the client side
             p.callback.updatePlayers();
         }
     }
 }
 public ChatMessage(Player p, String message)
 {
     this.sentBy = p;
     this.message = message;
     this.time = DateTime.Now;
 }
Beispiel #5
0
        public bool signIntoGame(int gameId, string nickname, Player.playerColor color)
        {
            if (gameIdExist(gameId) == false)
            {
                return false;
            }

            // if game is full, return false
            if (games[gameId].players.Count == games[gameId].maxPlayers)
            {
                return false;
            }

            // check if nickname is already taken
            if (games[gameId].playerExist(nickname) != null)
            {
                return false;
            }

            // also check if nickname is to short or to long
            // between 3 and 20 characters?
            if (nickname.Length < 3 || nickname.Length > 20)
            {
                return false;
            }

            // get callback
            IGameCallback callback = OperationContext.Current.GetCallbackChannel<IGameCallback>();
            Player tempPlayer = null;

            // TODO:
            // only allow certain characters like the english alphabet?

            // check if color is already taken
            // for now just support blue, red, green and yellow
            switch (color)
            {
                case Player.playerColor.blue:

                    // check if blue already exist
                    if (games[gameId].playerExist(Player.playerColor.blue) != null)
                    {
                        return false;
                    }

                    // one client per player
                    tempPlayer = new Player(nickname, Player.playerColor.blue, callback);
                    games[gameId].addPlayer(tempPlayer);
                    break;

                case Player.playerColor.yellow:

                    if (games[gameId].playerExist(Player.playerColor.yellow) != null)
                    {
                        return false;
                    }

                    tempPlayer = new Player(nickname, Player.playerColor.yellow, callback);
                    games[gameId].addPlayer(tempPlayer);
                    break;

                case Player.playerColor.red:
                    if (games[gameId].playerExist(Player.playerColor.red) != null)
                    {
                        return false;
                    }

                    tempPlayer = new Player(nickname, Player.playerColor.red, callback);
                    games[gameId].addPlayer(tempPlayer);
                    break;

                case Player.playerColor.green:
                    if (games[gameId].playerExist(Player.playerColor.green) != null)
                    {
                        return false;
                    }

                    tempPlayer = new Player(nickname, Player.playerColor.green, callback);
                    games[gameId].addPlayer(tempPlayer);
                    break;

                default:
                    return false;
            }

            // callback to all clients
            updateAllClientsThatANewPlayerHasBeenAdded(gameId, tempPlayer);
            return true;
        }
Beispiel #6
0
        /// <summary>
        /// Public for testing purposes
        /// </summary>
        /// <param name="territoryId"></param>
        /// <param name="p">Player</param>
        /// <param name="troops">amount of troops to place at given territory</param>
        /// <returns>true if territory exist and is updated</returns>
        public bool setTerritory(int territoryId, Player p, int troops)
        {
            foreach (Continent c in Continents)
            {
                foreach (Territory t in c.Territories)
                {
                    if (t.id == territoryId)
                    {
                        t.controlledByPlayer = p;
                        t.troops = troops;
                        return true;
                    }
                }
            }

            return false;
        }
Beispiel #7
0
 private void setPlayerTroopsToPlace(Player player, int troops)
 {
     foreach (Player p in players)
     {
         if (p.Equals(player))
         {
             p.troopsToPlace = troops;
         }
     }
 }
Beispiel #8
0
        public Player playerExist(Player.playerColor color)
        {
            foreach (Player p in players)
            {
                if (p.color == color)
                {
                    return p;
                }
            }

            return null;
        }
Beispiel #9
0
        // pass by reference
        public bool setPlayerStatus(Player player, bool alive)
        {
            foreach (Player p in players)
            {
                if (p.Equals(player))
                {
                    p.alive = alive;
                    return true;
                }

            }

            return false;
        }
Beispiel #10
0
        public bool finishTurn(Player p)
        {
            if (activePlayer != p)
            {
                return false;
            }

            // move directly to next player and game state fortify
            if (gameState == State.attack)
            {
                setNextPlayersTurn();
                gameState = State.fortify;

                int additionalFortify = calculateAdditionalTroops(activePlayer);
                setPlayerTroopsToPlace(activePlayer, additionalFortify);

                return true;
            }

            return false;
        }
Beispiel #11
0
        public bool checkIfPlayerControlContinent(Player p, Continent c)
        {
            if (p == null || c == null)
            {
                return false;
            }

            foreach (Territory t in c.Territories)
            {
                if (t.controlledByPlayer != p)
                {
                    return false;
                }
            }

            return true;
        }
Beispiel #12
0
        public virtual int calculateAdditionalTroops(Player p)
        {
            // note: p can be null

            // just for testing give each player just 3 troops
            // to place
            return 3;

            // check game state
            switch (gameState)
            {
                case State.waiting:
                    break;
                case State.select:

                    // start of game
                    // return amount according to the traditional risk rules
                    switch (maxPlayers)
                    {
                        case 2: return 40;
                        case 3: return 35;
                        case 4: return 30;
                        case 5: return 25;
                        case 6: return 20;
                        case 7: return 15;
                        default:
                            return 0;
                    }

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

                    // TODO!
                    return 2;

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

            return 0;
        }
Beispiel #13
0
        public bool attack(Player p, int territoryOne, int territoryTwo)
        {
            // check if Player p is the active player
            if (p != activePlayer)
            {
                return false;
            }

            Territory tempOne = getTerritory(territoryOne);
            Territory tempTwo = getTerritory(territoryTwo);
            if (tempOne == null || tempTwo == null)
            {
                return false;
            }

            bool oneIsAttacking = true;

            // check that one is owned by the player and one owned by
            // another player
            if (tempOne.controlledByPlayer == p)
            {
                oneIsAttacking = true;
            }
            else
            {
                oneIsAttacking = false;
            }

            if (oneIsAttacking)
            {
                if (tempTwo.controlledByPlayer == p)
                {
                    // both territories are controlled by player
                    return false;
                }
            }
            else
            {
                if (tempOne.controlledByPlayer == p)
                {
                    return false;
                }
            }

            // sjekk that the territories border to each other
            if (territoryBorderToEachOther(tempOne, tempTwo) == false)
            {
                return false;
            }

            Territory attackingTerritory;
            Territory defendingTerritory;
            // start attack...
            if (oneIsAttacking)
            {
                attackingTerritory = tempOne;
                defendingTerritory = tempTwo;
            }
            else
            {
                attackingTerritory = tempTwo;
                defendingTerritory = tempOne;
            }

            tempOne = null;
            tempTwo = null;

            if (attackingTerritory.troops > defendingTerritory.troops)
            {
                // set attacker as winner
                // only one troop in new territory
                setTerritory(defendingTerritory.id, activePlayer, 1);
                checkIfPlayerHasLost();

                // check if game is won
                if (checkIfActivePlayerIsHasWon())
                {
                    gameState = State.end;
                }

            }
            else
            {
                // defender only has one troop if looses
                setTerritory(defendingTerritory.id, defendingTerritory.controlledByPlayer, 1);
            }

            return true;
        }
Beispiel #14
0
        /// <summary>
        /// add new player to list of players
        /// also check if the game should move to select phase
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public bool addPlayer(Player p, bool testing = false)
        {
            // skip these tests if testing is set to true ;)
            if (testing == false)
            {
                // don't think this will happen, but just
                // to be safe and avoid any annoying null point reference
                if (p == null)
                {
                    return false;
                }

                // check if callback reference is uniqe
                // one player per client
                if (playerExist(p.callback) != null)
                {
                    return false;
                }
            }

            // check if all 4 players are signed in
            // then the game can begin!
            if (maxPlayers > players.Count)
            {
                players.Add(p);

                // if this is the first player to be added, make player
                // the "activePlayer"
                if (players.Count == 1)
                {
                    activePlayer = p;
                }
            }
            else
            {
                return false;
            }

            if (maxPlayers == players.Count)
            {
                gameState = Game.State.select;
            }

            return true;
        }