Esempio n. 1
0
        public String Update(int MapId)
        {
            IList<Field> fields = new FieldDAO().GetByMapId(MapId);
            Epoch epoch = new EpochDAO().GetCurrentByMapId(MapId);

            // wypuszczenie wojsk z pól
            IList<Order> orders = new OrderDAO().GetByEpoch(epoch.EpochId);
            foreach (Order o in orders)
            {
                if (o.OrderTypeName.Equals("Move") || o.OrderTypeName.Equals("Defend"))
                {
                    Field f = new FieldDAO().GetById(o.FieldId);

                    Dictionary<int, int> unitModifier = new Dictionary<int, int>();
                    unitModifier[o.UnitTypeId] = -o.Count;
                    UnitUpdate(f.KingdomId, f, unitModifier);
                }
            }
            
            Dictionary<int,UnitType> unitTypes = new Dictionary<int,UnitType>();
            
            foreach(UnitType ut in new UnitTypeDAO().GetAll())
            {
                unitTypes.Add(ut.UnitTypeId,ut);
            }

            // rozpatrywanie rozkazów
            foreach (Field field in fields)
            {
                IList<Order> ordersAll = new OrderDAO().GetByFieldEpoch(epoch.EpochId, field.FieldId);
                if (ordersAll.Count > 0)
                {
                    IList<Order> orderMove = new List<Order>();
                    IList<Order> orderDefend = new List<Order>();
                    IList<Order> orderBuy = new List<Order>();
                    IList<Order> orderGather = new List<Order>();


                    // Podział rozkazów według rodzaju
                    foreach (Order order in ordersAll)
                    {
                        if (order.OrderTypeName.Equals("Move"))
                        {
                            orderMove.Add(order);
                        }
                        else if (order.OrderTypeName.Equals("Defend"))
                        {
                            orderDefend.Add(order);
                        }
                        else if (order.OrderTypeName.Equals("Buy"))
                        {
                            orderBuy.Add(order);
                        }
                        else if (order.OrderTypeName.Equals("Gather"))
                        {
                            orderGather.Add(order);
                        }
                    }

                    // królestwo do którego naley pole na początku tury
                    Kingdom startKingdom = new KingdomDAO().GetInfoById(field.KingdomId);

                    if (startKingdom.KingdomId!=0)
                    {

                        // zbieranie ryżu
                        int collectedRice = 0;
                        foreach (Order o in orderGather)
                        {
                            collectedRice += o.Count * RICE_BY_ONE_MAN;
                        }
                        startKingdom.KingdomResources += collectedRice;
                        



                        // rekrutacja 
                        Dictionary<int, int> unitModifier = new Dictionary<int, int>();
                        int toPay = 0;
                        foreach (Order o in orderBuy)
                        {
                            if (unitModifier.ContainsKey(o.UnitTypeId))
                            {
                                unitModifier[o.UnitTypeId] += o.Count;
                            }
                            else
                            {
                                unitModifier[o.UnitTypeId] = o.Count;
                            }
                            toPay += o.Count * unitTypes[o.UnitTypeId].UnitTypeCost;
                        }
                        UnitUpdate(startKingdom.KingdomId, field, unitModifier);
                        
                        startKingdom.KingdomResources -= toPay;
                        new KingdomDAO().Update(startKingdom);
                    }

                    // af[kingdom][unitType] = count
                    Dictionary<int, Dictionary<int, int>> attackForces = new Dictionary<int, Dictionary<int, int>>();
                    foreach (Order o in orderDefend.Concat(orderMove))
                    {
                        if (!attackForces.ContainsKey(o.KingdomId))
                        {
                            attackForces[o.KingdomId] = new Dictionary<int, int>();
                        }

                        if (attackForces[o.KingdomId].ContainsKey(o.UnitTypeId))
                        {
                            attackForces[o.KingdomId][o.UnitTypeId] += o.Count;
                        }
                        else
                        {
                            attackForces[o.KingdomId][o.UnitTypeId] = o.Count;
                        }
                    }
                     

                    int winner = Battle(attackForces);

                    if (winner == 0)
                    {
                        // nic sie nie dzieje
                        // wszyscy bioracy udzial w walkach zostali wycieci w pień, więc nie ma kto wrócić na pole
                    }
                    else if (winner == startKingdom.KingdomId)
                    {
                        UnitUpdate(startKingdom.KingdomId, field, attackForces[startKingdom.KingdomId]);
                    }
                    else
                    {
                        field.KingdomId = winner;
                        new FieldDAO().Update(field);
                        UnitUpdate(winner, field, attackForces[winner]);
                    }

                }


                
            }
            new EpochDAO().Add(MapId);

            this.DiplomacyStatusUpdate(MapId,fields);
            return "Hello world";

            // 1: wyciągnąc z bazy wszystkie pola danej mapy
            // 2: Dla każdego pola wyciągnąc jego rozkazy
            // 3: Rozdzielić rozkazy według typu
            // 4: Rozpatrzyć zbieranie ryżu
            // 5: Rozpatrzyc rekrutacje
            // 6: Rozpatrzec ataki i obrone
        }
Esempio n. 2
0
        public String Update(int MapId)
        {
            IList <Field> fields = new FieldDAO().GetByMapId(MapId);
            Epoch         epoch  = new EpochDAO().GetCurrentByMapId(MapId);

            // wypuszczenie wojsk z pól
            IList <Order> orders = new OrderDAO().GetByEpoch(epoch.EpochId);

            foreach (Order o in orders)
            {
                if (o.OrderTypeName.Equals("Move") || o.OrderTypeName.Equals("Defend"))
                {
                    Field f = new FieldDAO().GetById(o.FieldId);

                    Dictionary <int, int> unitModifier = new Dictionary <int, int>();
                    unitModifier[o.UnitTypeId] = -o.Count;
                    UnitUpdate(f.KingdomId, f, unitModifier);
                }
            }

            Dictionary <int, UnitType> unitTypes = new Dictionary <int, UnitType>();

            foreach (UnitType ut in new UnitTypeDAO().GetAll())
            {
                unitTypes.Add(ut.UnitTypeId, ut);
            }

            // rozpatrywanie rozkazów
            foreach (Field field in fields)
            {
                IList <Order> ordersAll = new OrderDAO().GetByFieldEpoch(epoch.EpochId, field.FieldId);
                if (ordersAll.Count > 0)
                {
                    IList <Order> orderMove   = new List <Order>();
                    IList <Order> orderDefend = new List <Order>();
                    IList <Order> orderBuy    = new List <Order>();
                    IList <Order> orderGather = new List <Order>();


                    // Podział rozkazów według rodzaju
                    foreach (Order order in ordersAll)
                    {
                        if (order.OrderTypeName.Equals("Move"))
                        {
                            orderMove.Add(order);
                        }
                        else if (order.OrderTypeName.Equals("Defend"))
                        {
                            orderDefend.Add(order);
                        }
                        else if (order.OrderTypeName.Equals("Buy"))
                        {
                            orderBuy.Add(order);
                        }
                        else if (order.OrderTypeName.Equals("Gather"))
                        {
                            orderGather.Add(order);
                        }
                    }

                    // królestwo do którego naley pole na początku tury
                    Kingdom startKingdom = new KingdomDAO().GetInfoById(field.KingdomId);

                    if (startKingdom.KingdomId != 0)
                    {
                        // zbieranie ryżu
                        int collectedRice = 0;
                        foreach (Order o in orderGather)
                        {
                            collectedRice += o.Count * RICE_BY_ONE_MAN;
                        }
                        startKingdom.KingdomResources += collectedRice;



                        // rekrutacja
                        Dictionary <int, int> unitModifier = new Dictionary <int, int>();
                        int toPay = 0;
                        foreach (Order o in orderBuy)
                        {
                            if (unitModifier.ContainsKey(o.UnitTypeId))
                            {
                                unitModifier[o.UnitTypeId] += o.Count;
                            }
                            else
                            {
                                unitModifier[o.UnitTypeId] = o.Count;
                            }
                            toPay += o.Count * unitTypes[o.UnitTypeId].UnitTypeCost;
                        }
                        UnitUpdate(startKingdom.KingdomId, field, unitModifier);

                        startKingdom.KingdomResources -= toPay;
                        new KingdomDAO().Update(startKingdom);
                    }

                    // af[kingdom][unitType] = count
                    Dictionary <int, Dictionary <int, int> > attackForces = new Dictionary <int, Dictionary <int, int> >();
                    foreach (Order o in orderDefend.Concat(orderMove))
                    {
                        if (!attackForces.ContainsKey(o.KingdomId))
                        {
                            attackForces[o.KingdomId] = new Dictionary <int, int>();
                        }

                        if (attackForces[o.KingdomId].ContainsKey(o.UnitTypeId))
                        {
                            attackForces[o.KingdomId][o.UnitTypeId] += o.Count;
                        }
                        else
                        {
                            attackForces[o.KingdomId][o.UnitTypeId] = o.Count;
                        }
                    }


                    int winner = Battle(attackForces);

                    if (winner == 0)
                    {
                        // nic sie nie dzieje
                        // wszyscy bioracy udzial w walkach zostali wycieci w pień, więc nie ma kto wrócić na pole
                    }
                    else if (winner == startKingdom.KingdomId)
                    {
                        UnitUpdate(startKingdom.KingdomId, field, attackForces[startKingdom.KingdomId]);
                    }
                    else
                    {
                        field.KingdomId = winner;
                        new FieldDAO().Update(field);
                        UnitUpdate(winner, field, attackForces[winner]);
                    }
                }
            }
            new EpochDAO().Add(MapId);

            this.DiplomacyStatusUpdate(MapId, fields);
            return("Hello world");

            // 1: wyciągnąc z bazy wszystkie pola danej mapy
            // 2: Dla każdego pola wyciągnąc jego rozkazy
            // 3: Rozdzielić rozkazy według typu
            // 4: Rozpatrzyć zbieranie ryżu
            // 5: Rozpatrzyc rekrutacje
            // 6: Rozpatrzec ataki i obrone
        }