Esempio n. 1
0
        public override bool Equals(object obj)
        {
            DeckPlace dp = obj as DeckPlace;

            if (dp == null)
            {
                return(false);
            }
            return(player == dp.player && deckType == dp.deckType);
        }
Esempio n. 2
0
 public ReadOnlyCard(ICard card)
 {
     type = card.Type;
     place = card.Place;
     rank = card.Rank;
     suit = card.Suit;
     owner = card.Owner;
     suitColor = card.SuitColor;
     if (card.Attributes == null)
     {
         attributes = new Dictionary<CardAttribute, int>();
     }
     else
     {
         attributes = new Dictionary<CardAttribute, int>(card.Attributes);
     }
 }
Esempio n. 3
0
        public void AppendCardMoveLog(List<Card> cards, DeckPlace source, DeckPlace dest)
        {
            if (source.Player == null && dest.Player == null || cards.Count == 0)
            {
                return;
            }
            var reasons = (from card in cards select card.Log.GameAction).Distinct();
            Trace.Assert(reasons.Count() == 1);
            var reason = reasons.First();

            var docs = (from pair in Logs
                        where (source.Player == pair.Key || dest.Player == pair.Key)
                        select pair.Value).Concat(new List<FlowDocument>() { GlobalLog });

            foreach (var doc in docs)
            {
                var paragraph = LogFormatter.RichTranslateCardMove(cards, source, dest, reason);

                if (paragraph.Inlines.Count > 0)
                {
                    doc.Blocks.Add(paragraph);
                }
            }
        }
Esempio n. 4
0
 private IDeckContainer _GetMovementDeck(DeckPlace place)
 {
     if (place.Player != null && place.DeckType != DeckType.JudgeResult)
     {
         PlayerViewBase playerView = playersMap[place.Player];
         return playerView;
     }
     else
     {
         return discardDeck;
     }
 }
Esempio n. 5
0
        public void NotifyWuGuStart(Prompt prompt, DeckPlace place)
        {
            Application.Current.Dispatcher.Invoke((ThreadStart)delegate()
            {
                GameModel.WuGuModel = new WuGuChoiceViewModel();
                GameModel.WuGuModel.Prompt = PromptFormatter.Format(prompt);
                bool isFirstRow = true;
                int i = 0;
                int total = Game.CurrentGame.Decks[place].Count;

                foreach (var c in Game.CurrentGame.Decks[place])
                {
                    if (isFirstRow && total > 4 && i >= total / 2) isFirstRow = false;
                    var card = new CardViewModel() { Card = c, IsSelectionMode = true, IsEnabled = true };
                    if (isFirstRow) GameModel.WuGuModel.Cards1.Add(card);
                    else GameModel.WuGuModel.Cards2.Add(card);
                    card.OnSelectedChanged += new EventHandler(card_OnSelectedChanged);
                    i++;
                }

                wuGuWindow.Show();
            });
        }
 public void NotifyWuGuStart(Prompt prompt, DeckPlace place)
 {
 }
Esempio n. 7
0
 public List<Card> this[DeckPlace place]
 {
     get { return this[place.Player, place.DeckType]; }
     set { this[place.Player, place.DeckType] = value; }
 }
Esempio n. 8
0
        public void NotifyTwoSidesCardPickStart(Prompt prompt, DeckPlace place, IDictionary<int, int> groupMap, int group0MaxPick, int group1MaxPick)
        {
            Application.Current.Dispatcher.Invoke((ThreadStart)delegate()
            {
                var twoSidesCardChoiceModel = new TwoSidesCardChoiceViewModel();
                twoSidesCardChoiceModel.Prompt = LogFormatter.Translate(prompt);
                twoSidesCardChoiceModel.GroupOfPlayer = groupMap;

                foreach (var c in Game.CurrentGame.Decks[place])
                {
                    var card = new CardViewModel() { Card = c, IsSelectionMode = true, IsEnabled = true };
                    card.OnSelectedChanged += new EventHandler(card_OnSelectedChanged);
                    twoSidesCardChoiceModel.CardsToPick.Add(card);
                }

                int mainPlayerMaxPick, opponentMaxPick;
                if (groupMap[GameModel.MainPlayerModel.Id] == 0)
                {
                    mainPlayerMaxPick = group0MaxPick;
                    opponentMaxPick = group1MaxPick;
                }
                else
                {
                    mainPlayerMaxPick = group1MaxPick;
                    opponentMaxPick = group0MaxPick;
                }
                for (int i = 0; i < mainPlayerMaxPick; i++)
                {
                    twoSidesCardChoiceModel.CardsPicked1.Add(new CardSlotViewModel());
                }

                for (int i = 0; i < opponentMaxPick; i++)
                {
                    twoSidesCardChoiceModel.CardsPicked2.Add(new CardSlotViewModel());
                }

                GameModel.TwoSidesCardChoiceModel = twoSidesCardChoiceModel;

                if (ViewModelBase.IsDetached) return;

                twoSidesCardChoiceBox.DataContext = twoSidesCardChoiceModel;
                twoSidesCardChoiceWindow.Show();
            });
        }
 public void NotifyTwoSidesCardPickStart(Prompt prompt, DeckPlace place, IDictionary<int, int> groupMap, int group0MaxPick, int group1MaxPick)
 {
 }
Esempio n. 10
0
        public static Paragraph RichTranslateCardMove(List<Card> cards, DeckPlace source, DeckPlace dest, GameAction reason)
        {
            string sourceStr = Translate(source.Player);
            string destStr = Translate(dest.Player);
            var cardsInline = RichTranslate(cards);
            Paragraph paragraph = new Paragraph();
            if (source.Player != null)
            {
                if (reason == GameAction.Discard)
                {
                    paragraph.Inlines.Add(string.Format("{0}弃置了", sourceStr));
                    paragraph.Inlines.AddRange(cardsInline);
                }
                else if (reason == GameAction.PlaceIntoDiscard)
                {
                    paragraph.Inlines.Add(string.Format("{0}将", sourceStr));
                    paragraph.Inlines.AddRange(cardsInline);
                    paragraph.Inlines.Add("置入了弃牌堆");
                }
            }

            if (dest.Player != null)
            {
                bool added = true;
                if (source.DeckType == DeckType.Dealing && dest.DeckType == DeckType.Hand)
                {
                    paragraph.Inlines.Add(string.Format("{0}从牌堆里摸了", destStr));
                }
                else if (source.DeckType == DeckType.Discard)
                {
                    paragraph.Inlines.Add(string.Format("{0}从弃牌堆里回收了", destStr));
                }
                else if (source.Player == dest.Player)
                {
                    if (dest.DeckType == DeckType.Equipment)
                    {
                        paragraph.Inlines.Add(string.Format("{0}装备了", destStr));
                    }
                    else if (source.DeckType == DeckType.Hand && dest.DeckType == DeckType.Hand)
                    {
                        paragraph.Inlines.Add(string.Format("{0}获得了自己的", destStr));
                    }
                    else if (dest.DeckType is PrivateDeckType)
                    {
                        paragraph.Inlines.Add(string.Format("{0}将", destStr));
                        paragraph.Inlines.AddRange(cardsInline);
                        paragraph.Inlines.Add(string.Format("置于武将牌上"));
                        added = false;
                    }
                    else if (dest.DeckType == DeckType.Hand)
                    {
                        paragraph.Inlines.Add(string.Format("{0}获得了", destStr));
                    }
                    else added = false;
                }
                else if (dest.DeckType == DeckType.Hand || dest.DeckType == DeckType.Equipment)
                {
                    var owners = (from card in cards select card.Owner).Distinct();
                    if (owners.Contains(null))
                    {
                        Trace.TraceWarning("Cannot resolve log: reason is {0}, from {1}{2} to {3}{4}", reason, Translate(source.Player), source.DeckType, Translate(dest.Player), dest.DeckType);
                        paragraph.Inlines.Add(string.Format("{0}获得了", destStr));
                    }
                    else
                    {
                        List<Player> players = new List<Player>(owners);
                        paragraph.Inlines.Add(string.Format("{0}获得了{1}的", destStr, Translate(players)));
                        if (dest.DeckType == DeckType.Equipment)
                        {
                            paragraph.Inlines.AddRange(cardsInline);
                            paragraph.Inlines.Add(string.Format("并装备上"));
                            added = false;
                        }
                    }
                }
                else
                {
                    added = false;
                }
                if (added)
                {
                    paragraph.Inlines.AddRange(cardsInline);
                }
            }
            return paragraph;
        }
Esempio n. 11
0
        public void NotifyWuGuStart(Prompt prompt, DeckPlace place)
        {
            Application.Current.Dispatcher.Invoke((ThreadStart)delegate()
            {
                var wuguModel = new WuGuChoiceViewModel();
                wuguModel.Prompt = LogFormatter.Translate(prompt);
                bool isFirstRow = true;
                int i = 0;
                int total = Game.CurrentGame.Decks[place].Count;

                foreach (var c in Game.CurrentGame.Decks[place])
                {
                    if (isFirstRow && total > 5 && i >= (total + 1) / 2) isFirstRow = false;
                    var card = new CardViewModel() { Card = c, IsSelectionMode = true, IsEnabled = true };
                    if (isFirstRow) wuguModel.Cards1.Add(card);
                    else wuguModel.Cards2.Add(card);
                    card.OnSelectedChanged += new EventHandler(card_OnSelectedChanged);
                    i++;
                }
                GameModel.WuGuModel = wuguModel;

                if (ViewModelBase.IsDetached) return;

                wuGuBox.DataContext = wuguModel;
                wuGuWindow.Show();
            });
        }
Esempio n. 12
0
 public DeckPlace ToDeckPlace()
 {
     DeckPlace place = new DeckPlace(PlayerItem == null ? null : PlayerItem.ToPlayer(), DeckType.Register(DeckName));
     return place;
 }
Esempio n. 13
0
 public List <Card> this[DeckPlace place]
 {
     get { return(this[place.Player, place.DeckType]); }
     set { this[place.Player, place.DeckType] = value; }
 }
Esempio n. 14
0
 public static DeckPlaceItem Parse(DeckPlace dp)
 {
     DeckPlaceItem dpi = new DeckPlaceItem();
     dpi.PlayerItem = PlayerItem.Parse(dp.Player);
     dpi.DeckName = dp.DeckType.AbbriviatedName;
     return dpi;
 }
Esempio n. 15
0
            public override void Run(GameEvent gameEvent, GameEventArgs eventArgs)
            {
                Pk1v1Game game = Game.CurrentGame as Pk1v1Game;

                foreach (Player pp in game.Players)
                {
                    game.HandCardVisibility.Add(pp, new List<Player>() { pp });
                }

                // Put the whole deck in the dealing deck

                foreach (Card card in game.CardSet)
                {
                    // We don't want hero cards
                    if (card.Type is HeroCardHandler)
                    {
                        game.Decks[DeckType.Heroes].Add(card);
                        card.Place = new DeckPlace(null, DeckType.Heroes);
                    }
                    else if (card.Type is RoleCardHandler)
                    {
                        card.Place = new DeckPlace(null, RoleDeckType);
                    }
                    else
                    {
                        game.Decks[DeckType.Dealing].Add(card);
                        card.Place = new DeckPlace(null, DeckType.Dealing);
                    }
                }
                if (game.Players.Count == 0)
                {
                    return;
                }
                // Await role decision
                int seed = DateTime.Now.Millisecond;
                game.Seed = seed;
                Trace.TraceError("Seed is {0}", seed);
                if (game.RandomGenerator == null)
                {
                    game.RandomGenerator = new Random(seed);
                    Random random = game.RandomGenerator;
                }
                int selectorId = game.RandomGenerator.Next(2);
                int rulerId = 0;
                bool selectorIs0 = selectorId == 0;
                game.SyncConfirmationStatus(ref selectorIs0);
                if (selectorIs0)
                {
                    selectorId = 0;
                }
                else
                {
                    selectorId = 1;
                }
                int wantToBeRuler = 0;
                game.Players[selectorId].AskForMultipleChoice(new MultipleChoicePrompt("BeRuler"), OptionPrompt.YesNoChoices, out wantToBeRuler);
                rulerId = 1 - (wantToBeRuler ^ selectorId);
                Trace.Assert(rulerId >= 0 && rulerId <= 1);
                Trace.Assert(game.Players.Count == 2);
                if (rulerId == 0)
                {
                    game.AvailableRoles.Add(Role.Ruler);
                    game.AvailableRoles.Add(Role.Defector);
                    game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Ruler));
                    game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Defector));
                }
                else
                {
                    game.AvailableRoles.Add(Role.Defector);
                    game.AvailableRoles.Add(Role.Ruler);
                    game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Defector));
                    game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Ruler));
                }

                List<CardsMovement> moves = new List<CardsMovement>();
                int i = 0;
                foreach (Player p in game.Players)
                {
                    CardsMovement move = new CardsMovement();
                    move.Cards = new List<Card>() { game.Decks[null, RoleDeckType][i] };
                    move.To = new DeckPlace(p, RoleDeckType);
                    moves.Add(move);
                    i++;
                }
                game.MoveCards(moves, null, GameDelays.GameStart);
                GameDelays.Delay(GameDelays.RoleDistribute);
                //hero allocation
                game.Shuffle(game.Decks[DeckType.Heroes]);

                List<Card> heroPool = new List<Card>();
                int toDraw = 12;
                for (int rc = 0; rc < toDraw; rc++)
                {
                    game.SyncImmutableCardAll(game.Decks[DeckType.Heroes][rc]);
                    heroPool.Add(game.Decks[DeckType.Heroes][rc]);
                }
                game.SyncImmutableCards(game.Players[rulerId], heroPool);
                DeckType tempHero = DeckType.Register("TempHero");
                game.Decks[null, tempHero].AddRange(heroPool);
                Trace.TraceInformation("Ruler is {0}", rulerId);
                game.Players[rulerId].Role = Role.Ruler;
                game.Players[1 - rulerId].Role = Role.Defector;

                List<int> heroSelectCount = new List<int>() { 1, 2, 2, 2, 2, 2, 1 };
                int seq = 0;
                int turn = rulerId;
                Dictionary<int, int> map = new Dictionary<int,int>();
                map.Add(0, 0);
                map.Add(1, 1);
                var deckPlace = new DeckPlace(null, tempHero);
                game.NotificationProxy.NotifyTwoSidesCardPickStart(new CardChoicePrompt("Pk1v1.InitHeroPick.Init"), deckPlace, map, 6, 6);
                while (heroSelectCount.Count > seq)
                {
                    List<DeckPlace> sourceDecks = new List<DeckPlace>();
                    sourceDecks.Add(new DeckPlace(null, tempHero));
                    List<string> resultDeckNames = new List<string>();
                    resultDeckNames.Add("HeroChoice");
                    List<int> resultDeckMaximums = new List<int>();
                    int numHeroes = heroSelectCount[seq];
                    resultDeckMaximums.Add(numHeroes);
                    List<List<Card>> answer;
                    var newVer = new Pk1v1HeroChoiceVerifier(1, seq + 1 == heroSelectCount.Count ? -(Game.CurrentGame.Settings.TimeOutSeconds - 2) : 0);
                    for (int j = 0; j < numHeroes; j++)
                    {
                        var option = new AdditionalCardChoiceOptions();
                        option.IsTwoSidesCardChoice = true;
                        if (!game.UiProxies[game.Players[turn]].AskForCardChoice(new CardChoicePrompt("Pk1v1.InitHeroPick", numHeroes), sourceDecks, resultDeckNames, resultDeckMaximums, newVer, out answer, option))
                        {
                            answer = new List<List<Card>>();
                            answer.Add(new List<Card>());
                            answer[0].Add(game.Decks[null, tempHero].First(h => !answer[0].Contains(h) && !game.Decks[game.Players[turn], SelectedHero].Contains(h) && !game.Decks[game.Players[1 - turn], SelectedHero].Contains(h)));
                        }
                        game.Decks[game.Players[turn], SelectedHero].AddRange(answer[0]);
                        game.NotificationProxy.NotifyTwoSidesCardPicked(turn == 0, game.Decks[deckPlace].IndexOf(answer[0][0]));
                    }
                    seq++;
                    turn = 1 - turn;
                }
                GameDelays.Delay(GameDelays.Pk1v1EndOfSelection);
                game.NotificationProxy.NotifyTwoSidesCardPickEnd();
                game.Shuffle(game.Decks[null, DeckType.Dealing]);

                Player current = game.CurrentPlayer = game.Players[1 - rulerId];

                Dictionary<Player, List<Card>> restDraw = new Dictionary<Player, List<Card>>();
                List<Player> players = new List<Player>(game.Players);
                foreach (Player p in players)
                {
                    restDraw.Add(p, new List<Card>(game.Decks[p, SelectedHero]));
                }

                var heroSelection = new Dictionary<Player, List<Card>>();
                int numberOfHeroes = Game.CurrentGame.Settings.DualHeroMode ? 2 : 1;
                game.GlobalProxy.AskForHeroChoice(restDraw, heroSelection, numberOfHeroes, new RequireCardsChoiceVerifier(numberOfHeroes, false, true));

                bool notUsed = true;
                game.SyncConfirmationStatus(ref notUsed);

                foreach (var pxy in game.UiProxies)
                {
                    pxy.Value.Freeze();
                }

                for (int repeat = 0; repeat < numberOfHeroes; repeat++)
                {
                    foreach (Player p in players)
                    {
                        Card c;
                        int idx;
                        //only server has the result
                        if (!game.IsClient)
                        {
                            idx = repeat;
                            if (heroSelection.ContainsKey(p))
                            {
                                c = heroSelection[p][repeat];
                                idx = restDraw[p].IndexOf(c);
                            }
                            else
                            {
                                c = restDraw[p][idx];
                            }
                            if (game.GameServer != null)
                            {
                                foreach (Player player in game.Players)
                                {
                                    game.GameServer.SendPacket(player.Id, new StatusSync() { Status = idx });
                                }
                                game.GameServer.SendPacket(game.Players.Count, new StatusSync() { Status = idx });
                            }
                        }
                        // you are client
                        else
                        {
                            idx = (int)game.GameClient.Receive();
                            c = restDraw[p][idx];
                        }
                        game.Decks[p, SelectedHero].Remove(c);
                        var h = (HeroCardHandler)c.Type;
                        Trace.TraceInformation("Assign {0} to player {1}", h.Hero.Name, p.Id);
                        var hero = h.Hero.Clone() as Hero;
                        foreach (var skill in new List<ISkill>(hero.Skills))
                        {
                            if (skill.IsRulerOnly)
                            {
                                hero.Skills.Remove(skill);
                            }
                        }

                        if (repeat == 0)
                        {
                            p.Hero = hero;
                            p.Allegiance = hero.Allegiance;
                            p.IsMale = hero.IsMale;
                            p.IsFemale = !hero.IsMale;
                            if (numberOfHeroes == 1)
                            {
                                p.MaxHealth = hero.MaxHealth;
                                p.Health = hero.MaxHealth;
                            }
                        }
                        else if (repeat == 1)
                        {
                            p.Hero2 = hero;
                            int aveHp = (p.Hero2.MaxHealth + p.Hero.MaxHealth) / 2;
                            p.MaxHealth = aveHp;
                            p.Health = aveHp;
                        }
                    }
                }

                foreach (var rm in heroPool)
                {
                    game.Decks[DeckType.Heroes].Remove(rm);
                }
                foreach (var st in game.Decks[game.Players[0], SelectedHero])
                {
                    st.Place = new DeckPlace(game.Players[0], SelectedHero);
                }
                foreach (var st in game.Decks[game.Players[1], SelectedHero])
                {
                    st.Place = new DeckPlace(game.Players[1], SelectedHero);
                }
                game.Shuffle(game.Decks[DeckType.Heroes]);
                if (game.IsClient)
                {
                    foreach (var card in game.Decks[DeckType.Heroes])
                    {
                        card.Type = new UnknownHeroCardHandler();
                        card.Id = Card.UnknownHeroId;
                    }
                }

                GameDelays.Delay(GameDelays.GameBeforeStart);

                Game.CurrentGame.NotificationProxy.NotifyGameStart();
                GameDelays.Delay(GameDelays.GameStart);
                GameDelays.Delay(GameDelays.GameStart);

                foreach (var pl in game.Players)
                {
                    StartGameDeal(game, pl);
                }
                foreach (var pl in game.Players)
                {
                    try
                    {
                        game.Emit(GameEvent.HeroDebut, new GameEventArgs() { Source = pl });
                    }
                    catch (EndOfTurnException)
                    {
                        game.CurrentPlayer = game.Players[1 - game.CurrentPlayer.Id];
                    }
                }

                foreach (var act in game.AlivePlayers)
                {
                    game.Emit(GameEvent.PlayerGameStartAction, new GameEventArgs() { Source = act });
                }

                //redo this: current player might change
                current = game.CurrentPlayer;
                while (true)
                {
                    GameEventArgs args = new GameEventArgs();
                    args.Source = current;
                    game.CurrentPhaseEventIndex = 0;
                    game.CurrentPhase = TurnPhase.BeforeStart;
                    game.CurrentPlayer = current;
                    game.Emit(GameEvent.DoPlayer, args);
                    current = game.NextAlivePlayer(current);
                }
            }
Esempio n. 16
0
 public static string Translate(DeckPlace deckPlace)
 {
     if (deckPlace == null) return string.Empty;
     if (deckPlace.Player == null) return Translate(deckPlace.DeckType);
     string formatter = Application.Current.TryFindResource("Deck.PrivateDeck.Name") as string;
     Trace.Assert(formatter != null);
     if (formatter == null) return string.Empty;
     return string.Format(formatter, Translate(deckPlace.Player), Translate(deckPlace.DeckType));
 }
Esempio n. 17
0
 public void NotifyWuGuStart(DeckPlace place)
 {
 }
Esempio n. 18
0
            public override void Run(GameEvent gameEvent, GameEventArgs eventArgs)
            {
                Game game = Game.CurrentGame;

                foreach (Player pp in game.Players)
                {
                    game.HandCardVisibility.Add(pp, new List<Player>() { pp });
                }

                int numberOfDefectors = 1;

                // Put the whole deck in the dealing deck
                game.Decks[DeckType.Dealing] = game.CardSet.GetRange(0, game.CardSet.Count);
                var dealdeck = new List<Card>(game.Decks[DeckType.Dealing]);
                foreach (Card card in dealdeck)
                {
                    // We don't want hero cards
                    if (card.Type is HeroCardHandler)
                    {
                        game.Decks[DeckType.Dealing].Remove(card);
                        bool isSPHero = false;
                        if (!game.IsClient) isSPHero = (card.Type as HeroCardHandler).Hero.IsSpecialHero;
                        else isSPHero = card.Id == Card.UnknownSPHeroId;
                        if (isSPHero)
                        {
                            game.Decks[DeckType.SpecialHeroes].Add(card);
                            card.Place = new DeckPlace(null, DeckType.SpecialHeroes);
                        }
                        else
                        {
                            game.Decks[DeckType.Heroes].Add(card);
                            card.Place = new DeckPlace(null, DeckType.Heroes);
                        }

                    }
                    else if (card.Type is RoleCardHandler)
                    {
                        game.Decks[DeckType.Dealing].Remove(card);
                        card.Place = new DeckPlace(null, RoleDeckType);
                    }
                    else
                    {
                        card.Place = new DeckPlace(null, DeckType.Dealing);
                    }
                }
                if (game.Players.Count == 0)
                {
                    return;
                }
                // Await role decision
                int seed = DateTime.Now.Millisecond;
                Trace.TraceError("Seed is {0}", seed);
                Random random = new Random(seed);
                int rulerId = 0;

                game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Ruler));
                Trace.Assert(game.Players.Count > 1);
                Trace.Assert(numberOfDefectors + 1 <= game.Players.Count);
                int t = numberOfDefectors;
                while (t-- > 0)
                {
                    game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Defector));
                }
                int remaining = game.Players.Count - numberOfDefectors;
                int rebel;
                int loyalist;
                if (remaining <= 1)
                {
                    rebel = 0;
                    loyalist = 0;
                }
                else
                {
                    rebel = (int)Math.Ceiling(((double)remaining) / 2);
                    loyalist = remaining - rebel - 1;
                }

                Trace.Assert(rebel + loyalist + numberOfDefectors + 1 == game.Players.Count);
                (game as RoleGame).NumberOfDefectors = numberOfDefectors;
                (game as RoleGame).NumberOfRebels = rebel;

                while (rebel-- > 0)
                {
                    game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Rebel));
                }
                while (loyalist-- > 0)
                {
                    game.Decks[null, RoleDeckType].Add(_FindARoleCard(Role.Loyalist));
                }

                Shuffle(game.Decks[null, RoleDeckType]);

                if (game.IsClient)
                {
                    int count = game.Decks[null, RoleDeckType].Count;
                    game.Decks[null, RoleDeckType].Clear();
                    while (count-- > 0)
                    {
                        Card c = new Card(SuitType.None, 0, new UnknownRoleCardHandler());
                        c.Id = Card.UnknownRoleId;
                        c.Place = new DeckPlace(null, RoleDeckType);
                        game.Decks[null, RoleDeckType].Add(c);
                    }
                    game.SyncImmutableCardAll(game.Decks[null, RoleDeckType][0]);
                }
                else
                {
                    foreach (Card c in game.Decks[null, RoleDeckType])
                    {
                        c.Place = new DeckPlace(null, RoleDeckType);
                        if ((c.Type as RoleCardHandler).Role == Role.Ruler)
                        {
                            game.SyncImmutableCardAll(c);
                        }
                    }
                }

                int i = 0;
                for (i = 0; i < game.Players.Count; i++)
                {
                    game.SyncImmutableCard(game.Players[i], game.Decks[null, RoleDeckType][i]);
                }

                List<CardsMovement> moves = new List<CardsMovement>();
                i = 0;
                foreach (Player p in game.Players)
                {
                    CardsMovement move = new CardsMovement();
                    move.Cards = new List<Card>() { game.Decks[null, RoleDeckType][i] };
                    move.To = new DeckPlace(p, RoleDeckType);
                    moves.Add(move);
                    i++;
                }
                game.MoveCards(moves, null, GameDelayTypes.GameStart);

                i = 0;
                foreach (Player player in game.Players)
                {
                    Card card = game.Decks[player, RoleDeckType][0];
                    var role = card.Type as RoleCardHandler;
                    if (role != null)
                    {
                        if (role.Role == Role.Ruler)
                        {
                            rulerId = i;
                        }
                        player.Role = role.Role;
                    }
                    i++;
                }

                GameDelays.Delay(GameDelayTypes.RoleDistribute);

                game.NotificationProxy.NotifyLogEvent(new LogEvent("HerosInitialization"), new List<Player>());
                if (!game.IsClient) GameDelays.Delay(GameDelayTypes.ServerSideCompensation);

                //hero allocation
                Shuffle(game.Decks[DeckType.Heroes]);
                if (!game.IsClient)
                {
                    foreach (var hero in new List<Card>(game.Decks[DeckType.Heroes]))
                    {
                        foreach (var s in (hero.Type as HeroCardHandler).Hero.Skills)
                        {
                            if (s.IsRulerOnly)
                            {
                                game.Decks[DeckType.Heroes].Remove(hero);
                                game.Decks[DeckType.Heroes].Insert(0, hero);
                            }
                        }
                    }
                }
                int numHeroes = Game.CurrentGame.Settings.DualHeroMode ? 2 : 1;
                List<Card> rulerDraw = new List<Card>();
                int toDraw = 12 + (Game.CurrentGame.Settings.DualHeroMode ? 3 : 0);
                for (int rc = 0; rc < toDraw; rc++)
                {
                    game.SyncImmutableCardAll(game.Decks[DeckType.Heroes][rc]);
                    rulerDraw.Add(game.Decks[DeckType.Heroes][rc]);
                }
                game.SyncImmutableCards(game.Players[rulerId], rulerDraw);
                DeckType tempHero = new DeckType("TempHero");
                game.Decks[null, tempHero].AddRange(rulerDraw);
                Trace.TraceInformation("Ruler is {0}", rulerId);
                game.Players[rulerId].Role = Role.Ruler;
                List<DeckPlace> sourceDecks = new List<DeckPlace>();
                sourceDecks.Add(new DeckPlace(null, tempHero));
                List<string> resultDeckNames = new List<string>();
                resultDeckNames.Add("HeroChoice");
                List<int> resultDeckMaximums = new List<int>();
                resultDeckMaximums.Add(numHeroes);
                List<List<Card>> answer;
                if (!game.UiProxies[game.Players[rulerId]].AskForCardChoice(new CardChoicePrompt("RulerHeroChoice"), sourceDecks, resultDeckNames, resultDeckMaximums, new RequireCardsChoiceVerifier(numHeroes, false, true), out answer))
                {
                    answer = new List<List<Card>>();
                    answer.Add(new List<Card>());
                    answer[0].Add(game.Decks[DeckType.Heroes][0]);
                    if (Game.CurrentGame.Settings.DualHeroMode)
                    {
                        answer[0].Add(game.Decks[DeckType.Heroes][1]);
                    }
                }
                game.SyncImmutableCardAll(answer[0][0]);
                if (Game.CurrentGame.Settings.DualHeroMode)
                {
                    game.SyncImmutableCardAll(answer[0][1]);
                }
                game.Decks[DeckType.Heroes].Remove(answer[0][0]);
                if (Game.CurrentGame.Settings.DualHeroMode)
                {
                    game.Decks[DeckType.Heroes].Remove(answer[0][1]);
                }

                HeroCardHandler h = (HeroCardHandler)answer[0][0].Type;
                Trace.TraceInformation("Assign {0} to player {1}", h.Hero.Name, rulerId);
                Game.CurrentGame.Players[rulerId].Hero = h.Hero;
                Game.CurrentGame.Players[rulerId].Allegiance = h.Hero.Allegiance;
                Game.CurrentGame.Players[rulerId].IsMale = h.Hero.IsMale ? true : false;
                Game.CurrentGame.Players[rulerId].IsFemale = h.Hero.IsMale ? false : true;
                if (Game.CurrentGame.Settings.DualHeroMode)
                {
                    h = (HeroCardHandler)answer[0][1].Type;
                    var hero2 = h.Hero.Clone() as Hero;
                    Trace.TraceInformation("Assign {0} to player {1}", hero2.Name, rulerId);
                    Game.CurrentGame.Players[rulerId].Hero2 = hero2;
                }
                Game.CurrentGame.Players[rulerId].MaxHealth = Game.CurrentGame.Players[rulerId].Health = (game as RoleGame).GetMaxHealth(Game.CurrentGame.Players[rulerId]);

                int optionalHeros = game.Settings.NumHeroPicks;
                toDraw = optionalHeros + (Game.CurrentGame.Settings.DualHeroMode ? Math.Max(6 - optionalHeros, 0) : 0);
                Shuffle(game.Decks[DeckType.Heroes]);
                Dictionary<Player, List<Card>> restDraw = new Dictionary<Player, List<Card>>();
                List<Player> players = new List<Player>(game.Players);
                players.Remove(game.Players[rulerId]);
                int idx = 0;
                foreach (Player p in players)
                {
                    restDraw.Add(p, new List<Card>());
                    for (int n = 0; n < toDraw; n++)
                    {
                        game.SyncImmutableCard(p, game.Decks[DeckType.Heroes][idx]);
                        restDraw[p].Add(game.Decks[DeckType.Heroes][idx]);
                        idx++;
                    }
                }

                var heroSelection = new Dictionary<Player, List<Card>>();
                game.GlobalProxy.AskForHeroChoice(restDraw, heroSelection, numHeroes, new RequireCardsChoiceVerifier(numHeroes));

                bool notUsed = true;
                game.SyncConfirmationStatus(ref notUsed);

                List<Card> toRemove = new List<Card>();
                for (int repeat = 0; repeat < 2; repeat++)
                {
                    if (repeat == 1 && !Game.CurrentGame.Settings.DualHeroMode) break;
                    foreach (Player p in players)
                    {
                        Card c;
                        //only server has the result
                        if (!game.IsClient)
                        {
                            idx = repeat;
                            if (heroSelection.ContainsKey(p))
                            {
                                c = heroSelection[p][repeat];
                                idx = restDraw[p].IndexOf(c);
                            }
                            else
                            {
                                c = restDraw[p][idx];
                            }
                            foreach (Player player in game.Players)
                            {
                                game.GameServer.SendObject(player.Id, idx);
                            }
                            game.GameServer.SendObject(game.Players.Count, idx);
                        }
                        // you are client
                        else
                        {
                            idx = (int)game.GameClient.Receive();
                            c = restDraw[p][idx];
                        }
                        game.SyncImmutableCardAll(c);
                        toRemove.Add(c);
                        h = (HeroCardHandler)c.Type;
                        Trace.TraceInformation("Assign {0} to player {1}", h.Hero.Name, p.Id);
                        var hero = h.Hero.Clone() as Hero;
                        foreach (var skill in new List<ISkill>(hero.Skills))
                        {
                            if (skill.IsRulerOnly)
                            {
                                hero.Skills.Remove(skill);
                            }
                        }
                        if (repeat == 1) p.Hero2 = hero;
                        else p.Hero = hero;
                        if (repeat == 0) p.Allegiance = hero.Allegiance;
                        if (repeat == 0)
                        {
                            p.MaxHealth = p.Health = hero.MaxHealth;
                            p.IsMale = hero.IsMale ? true : false;
                            p.IsFemale = hero.IsMale ? false : true;
                        }
                        if (repeat == 1)
                        {
                            int aveHp = (p.Hero2.MaxHealth + p.Hero.MaxHealth) / 2;
                            p.MaxHealth = p.Health = aveHp;
                        }

                    }
                }
                foreach (var card in toRemove)
                {
                    game.Decks[DeckType.Heroes].Remove(card);
                }

                if (game.IsClient)
                {
                    foreach (var card in game.Decks[DeckType.Heroes])
                    {
                        card.Type = new UnknownHeroCardHandler();
                        card.Id = Card.UnknownHeroId;
                    }
                }
                Shuffle(game.Decks[DeckType.Heroes]);

                foreach (var pxy in game.UiProxies)
                {
                    pxy.Value.Freeze();
                }

                //Heroes Convert and handle god hero
                var toCheck = new List<Player>(game.Players);
                game.SortByOrderOfComputation(game.Players[rulerId], toCheck);
                Dictionary<string, List<Card>> convertibleHeroes = new Dictionary<string, List<Card>>();
                game.SyncImmutableCardsAll(game.Decks[DeckType.SpecialHeroes]);
                foreach (Card card in game.Decks[DeckType.SpecialHeroes])
                {
                    var hero = (card.Type as HeroCardHandler).Hero.HeroConvertFrom;
                    if (!convertibleHeroes.Keys.Contains(hero)) convertibleHeroes[hero] = new List<Card>();
                    convertibleHeroes[hero].Add(card);
                }
                foreach (var p in toCheck)
                {
                    bool changeHero = false;
                    for (int heroIndex = 0; heroIndex < 2; heroIndex++)
                    {
                        if (heroIndex == 1 && !Game.CurrentGame.Settings.DualHeroMode) break;
                        Hero playerHero = heroIndex == 0 ? p.Hero : p.Hero2;
                        if (convertibleHeroes.Keys.Contains(playerHero.Name))
                        {
                            DeckType tempSpHeroes = new DeckType("tempSpHeroes");
                            DeckPlace heroesConvert = new DeckPlace(p, tempSpHeroes);
                            game.Decks[heroesConvert].AddRange(convertibleHeroes[playerHero.Name]);
                            List<List<Card>> choice;
                            AdditionalCardChoiceOptions options = new AdditionalCardChoiceOptions();
                            options.IsCancellable = true;
                            if (p.AskForCardChoice(new CardChoicePrompt("HeroesConvert", p),
                                new List<DeckPlace>() { heroesConvert },
                                new List<string>() { "convert" },
                                new List<int>() { 1 },
                                new RequireOneCardChoiceVerifier(false, true),
                                out choice,
                                options))
                            {
                                foreach (var sk in playerHero.Skills)
                                {
                                    sk.Owner = null;
                                }
                                Hero hero = ((choice[0][0].Type as HeroCardHandler).Hero.Clone()) as Hero;
                                foreach (var skill in new List<ISkill>(hero.Skills))
                                {
                                    if (skill.IsRulerOnly && (p.Role != Role.Ruler || heroIndex == 1))
                                    {
                                        hero.Skills.Remove(skill);
                                    }
                                }
                                if (heroIndex == 0)
                                {
                                    p.Hero = hero;
                                    p.Allegiance = hero.Allegiance;
                                    p.IsMale = hero.IsMale ? true : false;
                                    p.IsFemale = hero.IsMale ? false : true;
                                }
                                else p.Hero2 = hero;
                                changeHero = true;
                                game.Emit(GameEvent.PlayerChangedHero, new GameEventArgs() { Source = p });
                            }
                            game.Decks[heroesConvert].Clear();
                        }
                    }
                    if (changeHero) p.MaxHealth = p.Health = (game as RoleGame).GetMaxHealth(p);
                    Game.CurrentGame.HandleGodHero(p);
                }

                Shuffle(game.Decks[null, DeckType.Dealing]);

                Player current = game.CurrentPlayer = game.Players[rulerId];

                GameDelays.Delay(GameDelayTypes.GameBeforeStart);
                StartGameDeal(game);

                Game.CurrentGame.NotificationProxy.NotifyGameStart();
                GameDelays.Delay(GameDelayTypes.GameStart);
                GameDelays.Delay(GameDelayTypes.GameStart);

                foreach (var act in game.AlivePlayers)
                {
                    game.Emit(GameEvent.PlayerGameStartAction, new GameEventArgs() { Source = act });
                }

                while (true)
                {
                    GameEventArgs args = new GameEventArgs();
                    args.Source = current;
                    game.CurrentPhaseEventIndex = 0;
                    game.CurrentPhase = TurnPhase.BeforeStart;
                    game.CurrentPlayer = current;
                    game.Emit(GameEvent.DoPlayer, args);
                    current = game.NextAlivePlayer(current);
                }
            }