Esempio n. 1
0
        private async Task ImportTrainerCardsFromSet(object arg)
        {
            using (var client = new WebClient())
            {
                var json = await client.DownloadStringTaskAsync($"https://api.pokemontcg.io/v1/cards?setCode={SelectedSet.SetCode}&supertype=Trainer").ConfigureAwait(false);

                var headers = client.ResponseHeaders;

                int cardsFound = int.Parse(headers.Get("Count"));
                int pageSize   = int.Parse(headers.Get("Page-Size"));

                if (cardsFound > pageSize)
                {
                    //TODO
                }

                foreach (var card in JsonConvert.DeserializeObject <PokemonTcgSdk.Trainer>(json).Cards)
                {
                    TrainerCards.Add(new TrainerCard
                    {
                        Name      = card.Name,
                        ImageUrl  = card.ImageUrlHiRes,
                        Completed = false,
                        SetCode   = card.SetCode,
                        CardId    = NetworkId.Generate()
                    });
                }

                SelectedTrainerCard = TrainerCards.First();
            }
        }
Esempio n. 2
0
        internal async Task Load()
        {
            if (!File.Exists("Data/formats.json"))
            {
                return;
            }

            var json = await File.ReadAllTextAsync("Data/formats.json");

            Formats = new ObservableCollection <Format>(Serializer.Deserialize <List <Format> >(json));

            foreach (var format in Formats)
            {
                if (format.Id == null)
                {
                    format.Id = NetworkId.Generate();
                }
            }

            var cards = new ObservableCollection <Card>(mainView.PokemonsViewModel.PokemonCards.Select(x => x.Card));

            foreach (var card in mainView.TrainerCardViewModel.TrainerCards)
            {
                cards.Add(card);
            }

            AllCards = new ObservableCollection <Card>(cards.OrderBy(x => x.SetCode).ThenBy(x => x.Name).ToList());
        }
Esempio n. 3
0
        public void Start(int port)
        {
            Instance = this;
            Clients  = new ConcurrentDictionary <NetworkId, NetworkPlayer>();

            Services = new Dictionary <string, IService>
            {
                { typeof(GameService).Name, new GameService() },
                { typeof(ImageService).Name, new ImageService() },
                { typeof(CardService).Name, new CardService() },
                { typeof(PlayerService).Name, new PlayerService() },
                { typeof(InfoService).Name, new InfoService() },
            };

            foreach (var service in Services.Values)
            {
                service.InitTypes();
            }

            Id       = NetworkId.Generate();
            listener = new TcpListener(IPAddress.Parse("0.0.0.0"), port);
            listener.Start();

            Console.WriteLine("Listening for connections...");

            defaultConsoleTop = Console.CursorTop;

            serverThread = new Thread(Run);
            serverThread.Start();
        }
Esempio n. 4
0
 public override NetworkMessage ToNetworkMessage(NetworkId senderId)
 {
     return(new NetworkMessage(MessageType, this, senderId, NetworkId.Generate())
     {
         RequiresResponse = false
     });
 }
Esempio n. 5
0
        public FormatsViewModel()
        {
            Formats       = new ObservableCollection <Format>();
            AddNewCommand = new RelayCommand((x) => true, (x) =>
            {
                Formats.Add(new Format()
                {
                    Name = "New format", Id = NetworkId.Generate()
                });
                SelectedFormat = Formats.Last();
            });
            DeleteRestriction = new RelayCommand((x) => SelectedRestriction != null, (x) =>
            {
                SelectedFormat.Restrictions.Remove((Restriction)x);

                if (SelectedFormat.Restrictions.Count > 0)
                {
                    SelectedRestriction = SelectedFormat.Restrictions.First();
                }
                else
                {
                    SelectedRestriction = null;
                }
            });
            AddRestriction = new RelayCommand((x) => SelectedFormat != null, (x) =>
            {
                SelectedFormat.Restrictions.Add(new Restriction()
                {
                    Name = "New restriction"
                });
                SelectedRestriction = SelectedFormat.Restrictions.Last();
            });
        }
Esempio n. 6
0
        private void ImportPokemon(object obj)
        {
            var dialog = new ImportPokemon();

            if (dialog.ShowDialog().Value)
            {
                using (var client = new WebClient())
                {
                    try
                    {
                        var json       = client.DownloadString(dialog.Url);
                        var pokemonSdk = JsonConvert.DeserializeObject <JsonPokemon>(json);

                        var pokemon = PokemonCreator.CreateCardFromSdkCard(pokemonSdk);

                        PokemonCards.Add(new PokemonViewModel(pokemon));
                        SelectedCard             = PokemonCards.Last();
                        SelectedCard.Card.CardId = NetworkId.Generate();
                    }
                    catch
                    {
                    }
                }
            }
        }
Esempio n. 7
0
        private async Task ImportPokemonSet(object arg)
        {
            using (var client = new WebClient())
            {
                var json = await client.DownloadStringTaskAsync($"https://api.pokemontcg.io/v1/cards?setCode={SelectedSet.SetCode}&supertype=Pok%C3%A9mon").ConfigureAwait(false);

                var headers = client.ResponseHeaders;

                int cardsFound = int.Parse(headers.Get("Count"));
                int pageSize   = int.Parse(headers.Get("Page-Size"));

                if (cardsFound > pageSize)
                {
                    //TODO
                    MessageBox.Show("There are more cards!!!");
                }
                var pokemons = new List <PokemonViewModel>();
                foreach (var card in JsonConvert.DeserializeObject <JsonPokemonList>(json).Cards)
                {
                    var pokemon = PokemonCreator.CreateCardFromSdkCard(card);

                    var model = new PokemonViewModel(pokemon);
                    model.Card.CardId = NetworkId.Generate();
                    pokemons.Add(model);
                }

                foreach (var pokemon in pokemons.OrderBy(x => x.Card.Name))
                {
                    PokemonCards.Add(pokemon);
                }

                SelectedCard = PokemonCards.First();
            }
        }
Esempio n. 8
0
        private async Task ImportEnergyCardsFromSet(object arg)
        {
            using (var client = new WebClient())
            {
                var json = await client.DownloadStringTaskAsync($"https://api.pokemontcg.io/v1/cards?setCode={SelectedSet.SetCode}&supertype=Energy").ConfigureAwait(false);

                var headers = client.ResponseHeaders;

                int cardsFound = int.Parse(headers.Get("Count"));
                int pageSize   = int.Parse(headers.Get("Page-Size"));

                if (cardsFound > pageSize)
                {
                    //TODO
                }

                foreach (var card in JsonConvert.DeserializeObject <PokemonTcgSdk.Energy>(json).Cards)
                {
                    EnergyCards.Add(new EnergyCard()
                    {
                        Name       = card.Name,
                        ImageUrl   = card.ImageUrlHiRes,
                        SetCode    = card.SetCode,
                        IsBasic    = card.SubType.ToLower() == "basic",
                        Amount     = 1,
                        EnergyType = EnergyTypes.Colorless,
                        CardId     = NetworkId.Generate()
                    });
                }

                SelectedEnergyCard = EnergyCards.Last();
            }
        }
Esempio n. 9
0
        public void SelectPrizeCard(int amount, GameField game)
        {
            if (amount <= 0)
            {
                return;
            }

            var message = new SelectPrizeCardsMessage(amount).ToNetworkMessage(NetworkId.Generate());

            var response   = NetworkPlayer.SendAndWaitForResponse <CardListMessage>(message);
            var cardsDrawn = new List <Card>();

            foreach (var cardId in response.Cards)
            {
                var card = game.Cards[cardId];
                PrizeCards.Remove(card);
                Hand.Add(card);
                cardsDrawn.Add(card);
            }

            game.SendEventToPlayers(new DrawCardsEvent()
            {
                Cards = new List <Card>(cardsDrawn), Player = Id
            });
        }
Esempio n. 10
0
 private void AddTrainerCard(object obj)
 {
     TrainerCards.Add(new TrainerCard()
     {
         Name = "New Trainer", CardId = NetworkId.Generate()
     });
     SelectedTrainerCard = TrainerCards.Last();
 }
Esempio n. 11
0
 private void AddEnergyCard(object obj)
 {
     EnergyCards.Add(new EnergyCard()
     {
         Name = "New Energy", CardId = NetworkId.Generate()
     });
     SelectedEnergyCard = EnergyCards.Last();
 }
Esempio n. 12
0
        public void CardListMessage()
        {
            var message = new CardListMessage(new List <NetworkId>()
            {
                NetworkId.Generate(), NetworkId.Generate(), NetworkId.Generate()
            });

            Assert.NotNull(SerializeAndBack(message));
        }
Esempio n. 13
0
        public static T Clone <T>(this T source) where T : IEntity
        {
            var serialized = JsonConvert.SerializeObject(source, JsonSettings);
            T   entity     = JsonConvert.DeserializeObject <T>(serialized, JsonSettings);

            entity.Id = NetworkId.Generate();

            return(entity);
        }
Esempio n. 14
0
 private void Update()
 {
     if (Input.GetKeyDown(KeyCode.H))
     {
         Trigger(new PokemonBecameActiveEvent {
             ReplacedPokemonId = NetworkId.Generate()
         });
     }
 }
        public void Bounce_EvolvedPokemon_OnlyBasic()
        {
            var effect = new BouncePokemonEffect()
            {
                OnlyBasic            = true,
                ShuffleIntoDeck      = false,
                ReturnAttachedToHand = true,
                TargetingMode        = TargetingMode.OpponentActive
            };

            var opponent = new Player();

            var pokemon = new PokemonCard(opponent)
            {
                Name = "Pokemon1", Stage = 0, Hp = 100
            };
            var evolution = new PokemonCard(opponent)
            {
                Name = "Evo", Stage = 1, EvolvesFrom = "Pokemon1", Hp = 100
            };

            opponent.ActivePokemonCard = evolution;

            pokemon.Evolve(evolution);
            var other = new PokemonCard(opponent);

            opponent.BenchedPokemon.Add(other);

            var sub = Substitute.For <INetworkPlayer>();

            sub.SendAndWaitForResponse <CardListMessage>(Arg.Any <NetworkMessage>()).ReturnsForAnyArgs(new CardListMessage
            {
                Cards = new List <NetworkId> {
                    other.Id
                }
            });
            opponent.SetNetworkPlayer(sub);

            var game = new GameField();

            game.AddPlayer(new Player()
            {
                Id = NetworkId.Generate()
            });
            game.AddPlayer(opponent);
            opponent.Id = NetworkId.Generate();

            effect.Process(game, game.Players[0], opponent, null);

            Assert.Single(opponent.Hand);
            Assert.Equal(pokemon.Id, opponent.Hand[0].Id);

            Assert.Single(opponent.DiscardPile);
            Assert.Equal(evolution.Id, opponent.DiscardPile[0].Id);
        }
Esempio n. 16
0
 public GameField()
 {
     Id = NetworkId.Generate();
     playersSetStartBench      = new HashSet <NetworkId>();
     DamageStoppers            = new List <DamageStopper>();
     TemporaryPassiveAbilities = new List <Ability>();
     GameState   = GameFieldState.WaitingForConnection;
     GameLog     = new GameLog();
     forcedFlips = new Queue <bool>();
     EnergyRule  = new StandardEnergyRule();
     Players     = new Player[2];
 }
Esempio n. 17
0
 public void Setup()
 {
     game.AddPlayer(new Player()
     {
         Id = NetworkId.Generate()
     });
     game.AddPlayer(new Player()
     {
         Id = NetworkId.Generate()
     });
     player1 = game.Players[0];
 }
Esempio n. 18
0
 public GameServerInstance()
 {
     ServerId = NetworkId.Generate();
     Players  = new List <ServerPlayer>(2);
     Actions  = new Dictionary <MessageTypes, Action <NetworkMessage> >
     {
         { MessageTypes.RegisterForGame, OnRegister },
         { MessageTypes.Attack, OnAttack },
         { MessageTypes.SelectedActive, OnActiveSelected },
         { MessageTypes.SelectedBench, OnBenchedSelected }
     };
 }
Esempio n. 19
0
        public GameField JoinTheActiveGame(NetworkId playerToJoin, NetworkId gameToJoin, Deck deckInfo)
        {
            RemoveCompletedGames();
            GameField game;

            if (!ActiveGames.TryGetValue(gameToJoin, out game))
            {
                return(null);
            }

            var player = new Player(MasterServer.Instance.Clients[playerToJoin]);

            var cardService = (CardService)MasterServer.Instance.Services[typeof(CardService).Name];

            foreach (var id in deckInfo.Cards.Select(x => x.CardId))
            {
                var card = cardService.CreateCardById(id);
                card.Owner = player;
                var pokemonCard = card as PokemonCard;
                if (pokemonCard != null && pokemonCard.Ability != null)
                {
                    pokemonCard.Ability.Id           = NetworkId.Generate();
                    pokemonCard.Ability.PokemonOwner = pokemonCard;
                }
                if (pokemonCard != null)
                {
                    foreach (var attack in pokemonCard.Attacks)
                    {
                        attack.Id = NetworkId.Generate();
                    }
                    pokemonCard.ReInitLists();
                }
                if (card is TrainerCard)
                {
                    var trainerCard = (TrainerCard)card;

                    if (trainerCard.Ability != null)
                    {
                        trainerCard.Ability.Id = NetworkId.Generate();
                    }
                }

                player.Deck.Cards.Push(card);
            }

            game.AddPlayer(player);
            game.StartGame();

            SendUpdateToPlayers(game.Players, game);

            return(game);
        }
Esempio n. 20
0
        public void ProcessEffectsTest()
        {
            var attack = new DamageManyAttack
            {
                Damage           = 20,
                EnemyBenchDamage = 10,
                SelfDamage       = 30,
                TeamBenchDamage  = 40
            };

            var player = new Player()
            {
                Id = NetworkId.Generate()
            };

            player.ActivePokemonCard = new PokemonCard()
            {
                Hp = 100, Owner = player
            };
            player.BenchedPokemon.Add(new PokemonCard()
            {
                Hp = 100, Owner = player
            });

            var opponent = new Player()
            {
                Id = NetworkId.Generate()
            };

            opponent.ActivePokemonCard = new PokemonCard()
            {
                Hp = 100, Owner = opponent
            };
            opponent.BenchedPokemon.Add(new PokemonCard()
            {
                Hp = 100, Owner = opponent
            });

            var game = new GameField();

            game.AddPlayer(player);
            game.AddPlayer(opponent);
            game.ActivePlayerIndex = 0;

            attack.ProcessEffects(game, player, opponent);

            Assert.Equal(10, opponent.BenchedPokemon.GetFirst().DamageCounters);
            Assert.Equal(40, player.BenchedPokemon.GetFirst().DamageCounters);
            Assert.Equal(30, player.ActivePokemonCard.DamageCounters);
            Assert.Equal(20, attack.GetDamage(null, null, new GameField()).NormalDamage);
        }
Esempio n. 21
0
        protected override void Activate(Player owner, Player opponent, int damageTaken, GameField game)
        {
            var message  = new SelectPrizeCardsMessage(1).ToNetworkMessage(NetworkId.Generate());
            var response = owner.NetworkPlayer.SendAndWaitForResponse <CardListMessage>(message);

            var selectedCard = game.Cards[response.Cards.First()];

            owner.PrizeCards.Remove(selectedCard);

            var topOfDeck = owner.Deck.Cards.Pop();

            owner.PrizeCards.Add(topOfDeck);

            owner.Deck.Cards.Push(selectedCard);
        }
        public void CardShuffledIntoDeck()
        {
            var effect = new BouncePokemonEffect()
            {
                ShuffleIntoDeck = true,
                TargetingMode   = TargetingMode.OpponentActive
            };

            var opponent = new Player();
            var pokemon  = new PokemonCard(opponent)
            {
                AttachedEnergy = new List <EnergyCard>
                {
                    new EnergyCard()
                },
                Hp = 100
            };

            opponent.ActivePokemonCard = pokemon;
            var other = new PokemonCard(opponent);

            opponent.BenchedPokemon.Add(other);

            var sub = Substitute.For <INetworkPlayer>();

            sub.SendAndWaitForResponse <CardListMessage>(Arg.Any <NetworkMessage>()).ReturnsForAnyArgs(new CardListMessage
            {
                Cards = new List <NetworkId> {
                    other.Id
                }
            });
            opponent.SetNetworkPlayer(sub);

            var game = new GameField();

            game.AddPlayer(new Player()
            {
                Id = NetworkId.Generate()
            });
            game.AddPlayer(opponent);
            opponent.Id = NetworkId.Generate();


            effect.Process(game, game.Players[0], opponent, null);

            Assert.Equal(2, opponent.Deck.Cards.Count);
            Assert.Equal(other, opponent.ActivePokemonCard);
        }
Esempio n. 23
0
 public FossilePokemon(TrainerCard source)
 {
     Id            = NetworkId.Generate();
     Name          = source.Name;
     PokemonName   = Name;
     Owner         = source.Owner;
     SetCode       = source.SetCode;
     RevealedToAll = true;
     ImageUrl      = source.ImageUrl;
     Type          = EnergyTypes.Colorless;
     Hp            = 10;
     PrizeCards    = 0;
     Attacks       = new ObservableCollection <Attack>();
     Ability       = new DiscardSelfAbility(this);
     Source        = source;
 }
Esempio n. 24
0
        public void InitTest()
        {
            Players[0] = new Player {
                Id = NetworkId.Generate(), MyGameIndex = 0
            };
            Players[1] = new Player {
                Id = NetworkId.Generate(), MyGameIndex = 1
            };
            ActivePlayerIndex = 0;

            for (var i = 0; i < 20; i++)
            {
                ActivePlayer.Deck.Cards.Push(new PokemonCard(ActivePlayer));
                NonActivePlayer.Deck.Cards.Push(new PokemonCard(NonActivePlayer));
            }
        }
Esempio n. 25
0
        public GameField HostGame(NetworkId hostPlayer, Deck deckInfo)
        {
            RemoveCompletedGames();
            var player = new Player(MasterServer.Instance.Clients[hostPlayer]);
            var game   = new GameField();

            game.Format = deckInfo.FormatId;

            var cardService = (CardService)MasterServer.Instance.Services[typeof(CardService).Name];

            foreach (var id in deckInfo.Cards.Select(x => x.CardId))
            {
                var card = cardService.CreateCardById(id);
                card.Owner = player;

                var pokemonCard = card as PokemonCard;
                if (pokemonCard != null && pokemonCard.Ability != null)
                {
                    pokemonCard.Ability.Id           = NetworkId.Generate();
                    pokemonCard.Ability.PokemonOwner = pokemonCard;
                }
                if (pokemonCard != null)
                {
                    foreach (var attack in pokemonCard.Attacks)
                    {
                        attack.Id = NetworkId.Generate();
                    }
                    pokemonCard.ReInitLists();
                }
                if (card is TrainerCard)
                {
                    var trainerCard = (TrainerCard)card;

                    if (trainerCard.Ability != null)
                    {
                        trainerCard.Ability.Id = NetworkId.Generate();
                    }
                }

                player.Deck.Cards.Push(card);
            }

            game.AddPlayer(player);
            ActiveGames.TryAdd(game.Id, game);

            return(game);
        }
Esempio n. 26
0
        public void DealDamage_Redirect_Higher_Than_Taken()
        {
            var game = new GameField();

            var player = new Player()
            {
                Id = NetworkId.Generate()
            };

            var active = new PokemonCard()
            {
                Hp    = 100,
                Owner = player
            };

            game.AddPlayer(player);
            game.AddPlayer(new Player()
            {
                ActivePokemonCard = new PokemonCard()
                {
                    Hp = 100
                }, Id = NetworkId.Generate()
            });
            game.ActivePlayerIndex = 0;
            game.NonActivePlayer.ActivePokemonCard.Owner = game.NonActivePlayer;

            player.ActivePokemonCard = active;

            var benched = new PokemonCard()
            {
                Hp    = 100,
                Owner = player
            };

            benched.Ability = new DamageRedirectorAbility(benched)
            {
                AskYesNo         = false,
                AmountToRedirect = 50
            };
            player.BenchedPokemon.Add(benched);

            game.ActivePlayer.ActivePokemonCard.DealDamage(20, game, game.NonActivePlayer.ActivePokemonCard, true);

            Assert.Equal(0, active.DamageCounters);
            Assert.Equal(20, benched.DamageCounters);
        }
Esempio n. 27
0
        private void Run()
        {
            while (true)
            {
                var client = listener.AcceptTcpClient();
                var player = new NetworkPlayer(client);

                player.Id = NetworkId.Generate();

                Logger.Instance.Info("Player connected with id: " + player.Id);

                player.Send(new NetworkMessage(MessageTypes.Connected, player.Id, Id, NetworkId.Generate()));
                player.DataReceived   += Player_DataReceived;
                player.OnDisconnected += Player_OnDisconnected;
                Clients.TryAdd(player.Id, player);
            }
        }
Esempio n. 28
0
        public void TestWithValues(float modifier, int damage, int expected, bool roundDown = false)
        {
            var game = new GameField();

            var player = new Player()
            {
                Id = NetworkId.Generate()
            };
            var opponent = new Player()
            {
                Id = NetworkId.Generate()
            };

            game.AddPlayer(player);
            game.AddPlayer(opponent);

            opponent.ActivePokemonCard = new PokemonCard()
            {
                Owner = opponent
            };

            game.ActivePlayerIndex = 0;

            var pokemon = new PokemonCard()
            {
                Owner = player
            };

            var effect = new AttachmentEffect()
            {
                Ability = new DamageTakenModifier()
                {
                    Modifer   = modifier,
                    RoundDown = roundDown
                },
                TargetingMode = TargetingMode.YourActive
            };

            player.ActivePokemonCard = pokemon;

            effect.Process(game, player, opponent, player.ActivePokemonCard);

            pokemon.DealDamage(damage, game, opponent.ActivePokemonCard, true);

            Assert.Equal(expected, pokemon.DamageCounters);
        }
Esempio n. 29
0
        private void WaitForConnections()
        {
            while (Players.Count < 2)
            {
                Console.WriteLine("listening...");
                var client = listener.AcceptTcpClient();

                var player = new ServerPlayer(client);
                player.DataReceived += Player_DataReceived;
                Players.Add(player);
                Console.WriteLine("Player connected...");

                player.Id = NetworkId.Generate();
                player.Send(new NetworkMessage(MessageTypes.Connected, player.Id, ServerId, NetworkId.Generate()));
                SendGameToPlayers();
            }

            InitGame();
            Console.WriteLine("Both players connected");
        }
Esempio n. 30
0
        public TCGCards.Core.Deck LoadDeckSelectedDeck()
        {
            var fileName = decks[deckDropDown.value].Name;

            foreach (var character in Path.GetInvalidFileNameChars())
            {
                fileName = fileName.Replace(character, '\0');
            }

            var deckFile = Path.Combine(Application.streamingAssetsPath, "Decks", fileName + Deck.deckExtension);

            var deck = Serializer.Deserialize <TCGCards.Core.Deck>(File.ReadAllText(deckFile));

            foreach (var card in deck.Cards)
            {
                card.Id = NetworkId.Generate();
            }

            return(deck);
        }