Exemplo n.º 1
0
 public Monster(int health, int damage, int gold, Coords startCoordinates)
 {
     Health = health;
     Damage = damage;
     Gold = gold;
     Coordinates = startCoordinates;
 }
Exemplo n.º 2
0
        public Coords GetRandomFreeCoords(IEnumerable<Client> clients)
        {
            Coords coords = null;
            var tileIsFree = false;
            do
            {
                coords = new Coords(
                    RandomizationFunctions.GetRandomNumber(0, GameMap.GetLength(0) - 1),
                    RandomizationFunctions.GetRandomNumber(0, GameMap.GetLength(1) - 1)
                    );

                if (GameMap[coords.X, coords.Y].HasGold
                    || GameMap[coords.X, coords.Y].IsCaveWall
                    || GameMap[coords.X, coords.Y].HasHealth)
                    continue;

                tileIsFree = true;
                foreach (var client in clients)
                {
                    var player = client.Player;
                    if (player == null)
                        continue;

                    if (player.Coordinates.X == coords.X && player.Coordinates.Y == coords.Y)
                    {
                        tileIsFree = false;
                        break;
                    }
                }

            } while (!tileIsFree);
            return coords;
        }
Exemplo n.º 3
0
 public Player(int id, string name, int health, int attack, int gold, Coords startCoordinates)
 {
     Name = name;
     PlayerId = id;
     Health = health;
     Attack = attack;
     Coordinates = startCoordinates;
     IsDead = false;
 }
Exemplo n.º 4
0
 public Player(int id, Coords startCoordinates)
 {
     PlayerId = id;
     Coordinates = startCoordinates;
 }
Exemplo n.º 5
0
        static void RecieveResponse()
        {
            if (_players == null)
            {
                _players = new List<Player>();
            }
            byte[] bytes = new byte[BUFFERLENGTH];
            _socket.Receive(bytes);
            string fromServer = Encoding.UTF8.GetString(bytes).TrimEnd('\0');
            string[] responses = fromServer.Split(';');
            foreach (string response in responses)
            {
                string[] responseParts = response.Split(' ');
                if (responseParts[0] == "WAP/1.0")
                {
                    if (responseParts[1] == "WELCOME")
                    {
                        _receiveJson = responseParts[2] == "JSON";
                    }

                    if (responseParts[1] == "YOURTURN")
                    {
                        if (!_playerId.HasValue)
                        {
                            _playerId = int.Parse(responseParts[2]);
                        }
                        _responseQueue.Enqueue(ServerResponse.YourTurn);
                    }

                    if (responseParts[1] == "SENDSTATE")
                    {
                        if (_receiveJson)
                        {
                            var sendState = SerializationFunctions.DeserializeObject<SendState>(responseParts[2]);
                            foreach (Player player in sendState.Players)
                            {
                                Player existingPlayer = _players.SingleOrDefault(p => p.PlayerId == player.PlayerId);
                                if (existingPlayer == null)
                                {
                                    _players.Add(new Player(player.PlayerId, player.Name, player.Health, 10, player.Gold, player.Coordinates));
                                }
                                else
                                {
                                    existingPlayer.Gold = player.Gold;
                                    existingPlayer.Health = player.Health;
                                    existingPlayer.Coordinates = player.Coordinates;
                                }
                            }

                            foreach (Tile tile in sendState.Tiles)
                            {
                                gameBoard[tile.X, tile.Y].Gold = tile.Gold;
                                gameBoard[tile.X, tile.Y].Health = tile.Health;
                            }
                        }
                        else
                        {
                            for (int i = 2; i < responseParts.Length; i++)
                            {
                                string[] itemInfos = responseParts[i].Split(',');
                                switch (itemInfos[0])
                                {
                                    case "Pl":
                                        string name = itemInfos[1];
                                        int playerId = int.Parse(itemInfos[2]);
                                        Coords coordinates = new Coords(int.Parse(itemInfos[3]), int.Parse(itemInfos[4]));
                                        int health = int.Parse(itemInfos[5]);
                                        int gold = int.Parse(itemInfos[6]);
                                        Player player = _players.SingleOrDefault(p => p.PlayerId == playerId);
                                        if (player == null)
                                        {
                                            player = new Player(playerId, name, health, 10, gold, coordinates);
                                            _players.Add(player);
                                        }
                                        else
                                        {
                                            player.Health = health;
                                            player.Gold = gold;
                                            player.Coordinates = coordinates;
                                        }
                                        break;
                                    case "P":
                                        coordinates = new Coords(int.Parse(itemInfos[1]), int.Parse(itemInfos[2]));
                                        health = int.Parse(itemInfos[3]);
                                        gameBoard[coordinates.X, coordinates.Y].Health = health;
                                        break;
                                    case "G":
                                        coordinates = new Coords(int.Parse(itemInfos[1]), int.Parse(itemInfos[2]));
                                        gold = int.Parse(itemInfos[3]);
                                        gameBoard[coordinates.X, coordinates.Y].Gold = gold;
                                        break;
                                }
                            }

                        }
                        _responseQueue.Enqueue(ServerResponse.Sendstate);
                    }

                    if (responseParts[1] == "NEWPLAYER")
                    {
                        if (_receiveJson)
                        {
                            var player = SerializationFunctions.DeserializeObject<Player>(responseParts[2]);
                            _players.Add(new Player(player.PlayerId, player.Name, player.Health, 10, player.Gold, player.Coordinates));
                        }
                        else
                        {
                            string[] playerInfos = responseParts[2].Split(',');
                            string name = playerInfos[0];
                            int id = int.Parse(playerInfos[1]);
                            Coords coordinates = new Coords(int.Parse(playerInfos[2]), int.Parse(playerInfos[3]));
                            int health = int.Parse(playerInfos[4]);
                            int gold = int.Parse(playerInfos[5]);
                            var player = new Player(id, name, health, 10, gold, coordinates);
                            player.PlayerId = id;
                            _players.Add(player);
                        }

                        _responseQueue.Enqueue(ServerResponse.NewPlayer);
                    }

                    if (responseParts[1] == "UPDATEPLAYER")
                    {
                        Player playerToUpdate;
                        if (_receiveJson)
                        {
                            var player = SerializationFunctions.DeserializeObject<Player>(responseParts[2]);
                            playerToUpdate = _players.Single(p => p.PlayerId == player.PlayerId);
                            playerToUpdate.Coordinates = player.Coordinates;
                            playerToUpdate.Gold = player.Gold;
                            playerToUpdate.Health = player.Health;
                        }
                        else
                        {
                            string[] playerInfos = responseParts[2].Split(',');
                            int id = int.Parse(playerInfos[0]);
                            Coords coordinates = new Coords(int.Parse(playerInfos[1]), int.Parse(playerInfos[2]));
                            int health = int.Parse(playerInfos[3]);
                            int gold = int.Parse(playerInfos[4]);
                            playerToUpdate = _players.Single(p => p.PlayerId == id);
                            playerToUpdate.Coordinates = coordinates;
                            playerToUpdate.Health = health;
                            playerToUpdate.Gold = gold;
                        }

                        _responseQueue.Enqueue(ServerResponse.UpdatePlayer);
                    }

                    if (responseParts[1] == "REMOVEPLAYER")
                    {
                        int id = int.Parse(responseParts[2]);
                        Player playerToRemove = _players.SingleOrDefault(p => p.PlayerId == id);
                        if (playerToRemove != null)
                        {
                            _players.Remove(playerToRemove);
                        }
                        _responseQueue.Enqueue(ServerResponse.RemovePlayer);
                    }

                    if (responseParts[1] == "UPDATETILE")
                    {
                        if (_receiveJson)
                        {
                            var tile = SerializationFunctions.DeserializeObject<Tile>(responseParts[2]);
                            Tile tileToUpdate = gameBoard[tile.X, tile.Y];
                            tileToUpdate.Gold = tile.Gold;
                            tileToUpdate.Health = tile.Health;
                        }
                        else
                        {
                            string[] tileInfos = responseParts[2].Split(',');
                            Coords coordinates = new Coords(int.Parse(tileInfos[0]), int.Parse(tileInfos[1]));
                            int gold = int.Parse(tileInfos[2]);
                            int health = int.Parse(tileInfos[3]);
                            gameBoard[coordinates.X, coordinates.Y].Gold = gold;
                            gameBoard[coordinates.X, coordinates.Y].Health = health;
                        }
                        _responseQueue.Enqueue(ServerResponse.UpdateTile);
                    }

                    if (responseParts[1] == "MESSAGE")
                    {
                        int id = int.Parse(responseParts[2]);
                        string name = _players.SingleOrDefault(p => p.PlayerId == id)?.Name;
                        if (_chatMessages.Count == 5)
                        {
                            _chatMessages.RemoveAt(0);
                        }
                        var messageBuilder = new StringBuilder();
                        messageBuilder.Append($"{name}: ");
                        for (int i = 3; i < responseParts.Count(); i++)
                        {
                            messageBuilder.Append($"{responseParts[i]} ");
                        }
                        _chatMessages.Add(messageBuilder.ToString());
                        _responseQueue.Enqueue(ServerResponse.Message);
                    }

                    if (responseParts[1] == "DENIED")
                    {
                        if (responseParts[2] == "LOGIN")
                        {
                            _responseQueue.Enqueue(ServerResponse.LoginDenied);
                        }
                        else if (responseParts[2] == "MOVE")
                        {
                            _responseQueue.Enqueue(ServerResponse.MoveDenied);
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
 public HealthPotion(int health, Coords coordinates)
 {
     Health = health;
     Coordinates = coordinates;
 }
Exemplo n.º 7
0
 private static string EncodeTile(WorldMap world, Coords coords, bool json)
 {
     return json ? SerializationFunctions.SerializeObject(world.GameMap[coords.X, coords.Y]) : world.GameMap[coords.X, coords.Y].ToString();
 }
Exemplo n.º 8
0
 private static string EncodeTile(WorldMap world, Coords coords, bool json)
 {
     return(json ? SerializationFunctions.SerializeObject(world.GameMap[coords.X, coords.Y]) : world.GameMap[coords.X, coords.Y].ToString());
 }
Exemplo n.º 9
0
 public void SetCursorPosition(Coords coordinates)
 {
     Console.SetCursorPosition(coordinates.X, coordinates.Y);
 }
Exemplo n.º 10
0
 public void SetCursorPosition(Coords coordinates)
 {
     Console.SetCursorPosition(coordinates.X, coordinates.Y);
 }
Exemplo n.º 11
0
 public Player(int id, Coords startCoordinates)
 {
     PlayerId    = id;
     Coordinates = startCoordinates;
 }