Пример #1
0
        internal void UpdateSelectSum(bool spreading)
        {
            sumAssist.Clear();
            sumAssist.Add(0);
            int count, p1, p2, maxParam = -1, minParam = int.MaxValue, selectedCount = SelectedCards.Count - SelectSumFixedCount;
            HashSet <ClientCard> available = new HashSet <ClientCard>();
            var remains = new List <ClientCard>(SelectableCards);

            remains.RemoveAll((c) => c.Selected);
            for (int i = 0; i < SelectedCards.Count; ++i)
            {
                count = sumAssist.Count;
                p1    = SelectedCards[i].SelectionParam & 0xffff;
                p2    = SelectedCards[i].SelectionParam >> 16;
                int pmin = (p2 > 0 && p2 < p1) ? p2 : p1;
                minParam = minParam < pmin ? minParam : pmin;
                maxParam = maxParam > p1 ? maxParam : p1;
                maxParam = maxParam > p2 ? maxParam : p2;
                for (int j = 0; j < count; ++j)
                {
                    if (p2 > 0)
                    {
                        sumAssist.Add(sumAssist[j] + p2);
                    }
                    sumAssist[j] += p1;
                }
            }
            if (Overflowable)
            {
                sumAssist.Sort();
                int sumMax = sumAssist[sumAssist.Count - 1];
                int sumMin = sumAssist[0];
                if (sumMin >= SelectSumVal)
                {
                    SendSelectResponse();
                    return;
                }
                else if (sumMax >= SelectSumVal && sumMax - maxParam < SelectSumVal)
                {
                    Finishable = true;
                    MainGame.Instance.DuelWindow.SetCancelOrFinish(2);
                }
                int sum;
                for (int i = 0; i < remains.Count; ++i)
                {
                    remains.Swap(0, i);
                    p1       = remains[0].SelectionParam & 0xffff;
                    p2       = remains[0].SelectionParam >> 16;
                    sum      = sumMin + p1;
                    minParam = minParam < p1 ? minParam : p1;
                    if (sum >= SelectSumVal)
                    {
                        if (sum - minParam < SelectSumVal)
                        {
                            available.Add(remains[0]);
                        }
                    }
                    else if (CheckMin(1, remains, SelectSumVal - sum, SelectSumVal - sum + minParam - 1))
                    {
                        available.Add(remains[0]);
                    }
                    if (p2 == 0)
                    {
                        continue;
                    }
                    sum      = sumMin + p2;
                    minParam = minParam < p2 ? minParam : p2;
                    if (sum >= SelectSumVal)
                    {
                        if (sum - minParam < SelectSumVal)
                        {
                            available.Add(remains[0]);
                        }
                    }
                    else if (CheckMin(1, remains, SelectSumVal - sum, SelectSumVal - sum + minParam - 1))
                    {
                        available.Add(remains[0]);
                    }
                }
            }
            else
            {
                if (sumAssist.Contains(SelectSumVal) && selectedCount >= SelectMin && selectedCount <= SelectMax)
                {
                    SendSelectResponse();
                    return;
                }
                HashSet <int> noduplicate = new HashSet <int>(sumAssist);
                foreach (int val in noduplicate)
                {
                    int tarValue = SelectSumVal - val;
                    for (int i = 0; i < remains.Count; ++i)
                    {
                        if (available.Contains(remains[i]))
                        {
                            continue;
                        }
                        remains.Swap(0, i);
                        p1 = remains[0].SelectionParam & 0xffff;
                        p2 = remains[0].SelectionParam >> 16;
                        if (CheckSum(1, remains, tarValue - p1, selectedCount + 1) ||
                            (p2 > 0 && CheckSum(1, remains, tarValue - p2, selectedCount + 1)))
                        {
                            available.Add(remains[0]);
                        }
                    }
                }
            }
            if (!Finishable && available.Count == 1)
            {
                SelectedCards.AddRange(available);
                SendSelectResponse();
                return;
            }
            if (spreading)
            {
                DetachAll();
                Spread(available);
            }
            else
            {
                SelectableCards.ForEach((c) => c.Selectable = false);
                foreach (var c in available)
                {
                    c.Selectable = true;
                }
            }
        }