1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Cysharp.Threading.Tasks;
using UnityEngine;
 
public class GuildAtkDefBatTreasureWin : UIBase
{
    [SerializeField] ImageEx bgImage;
    [SerializeField] RectTransform noOpenRect;
    [SerializeField] TextEx titleNameText;
    [SerializeField] TextEx openCntText;
    [SerializeField] ButtonEx awardButton;
    [SerializeField] ButtonEx lastLineButton;
    [SerializeField] ButtonEx nextLineButton;
    [SerializeField] ImageEx timeImage;
    [SerializeField] TextEx timeText;
    [SerializeField] ScrollerController scroller;
 
    private const int COLS = 5;
    private const int MAX_ROWS = 8;
    private int maxGridCount;
    private int currentTopRow;
    private bool isWin;
    private int cachedDisplayState;  // 缓存上一次的 displayState,变化时关窗
 
    GuildAtkDefBatManager manager => GuildAtkDefBatManager.Instance;
 
    private int pendingAnimGridIndex = 0;
    private int pendingAnimAwardID = 0;
 
    protected override void InitComponent()
    {
        base.InitComponent();
 
        awardButton.AddListener(() =>
        {
            UIManager.Instance.OpenWindow<GuildAtkDefBatAwardPreviewWin>();
        });
 
        lastLineButton.AddListener(() =>
        {
            if (currentTopRow > 0)
            {
                currentTopRow--;
                RefreshScroller();
            }
        });
 
        nextLineButton.AddListener(() =>
        {
            int totalRows = Mathf.CeilToInt((float)maxGridCount / COLS);
            if (currentTopRow < totalRows - MAX_ROWS)
            {
                currentTopRow++;
                RefreshScroller();
            }
        });
    }
 
    protected override void OnPreOpen()
    {
        base.OnPreOpen();
        scroller.OnRefreshCell += OnRefreshCell;
        manager.OnUpdateAtkDefBatInfoEvent += OnUpdateAtkDefBatInfo;
        manager.OnTreasureDataUpdateEvent += OnTreasureDataUpdate;
        manager.OnTreasureGridOpenEvent += OnTreasureGridOpen;
        GlobalTimeEvent.Instance.secondEvent += OnSecondEvent;
        manager.isTreasureWinOpen = true;
        cachedDisplayState = manager.GetGuildBattleDisplayState();
        Display();
    }
 
    protected override void OnPreClose()
    {
        base.OnPreClose();
        scroller.OnRefreshCell -= OnRefreshCell;
        manager.OnUpdateAtkDefBatInfoEvent -= OnUpdateAtkDefBatInfo;
        manager.OnTreasureDataUpdateEvent -= OnTreasureDataUpdate;
        manager.OnTreasureGridOpenEvent -= OnTreasureGridOpen;
        GlobalTimeEvent.Instance.secondEvent -= OnSecondEvent;
        manager.isTreasureWinOpen = false;
    }
 
    private void OnUpdateAtkDefBatInfo()
    {
        // A525到达时,displayState变化则关窗(此时翻牌事件尚未触发)
        int curDisplayState = manager.GetGuildBattleDisplayState();
        if (curDisplayState != cachedDisplayState)
            CloseWindow();
    }
 
    private void OnTreasureDataUpdate()
    {
        openCntText.text = Language.Get("GuildAtkDefBat16", manager.GetMyOpenRemainCount(), manager.GetMyMaxOpenCount());
    }
 
    private void OnTreasureGridOpen(int gridIndex, int awardId)
    {
        pendingAnimGridIndex = gridIndex;
        pendingAnimAwardID = awardId;
        RefreshScroller();
        pendingAnimGridIndex = 0;
        pendingAnimAwardID = 0;
    }
 
    private void OnSecondEvent()
    {
        DisplayTime();
    }
 
    public void Display()
    {
        int displayState = manager.GetGuildBattleDisplayState();
        isWin = displayState == 4;
        bool isTrucePeriod = displayState == 4 || displayState == 5;
 
        if (isTrucePeriod)
        {
            titleNameText.text = Language.Get(isWin ? "GuildAtkDefBat18" : "GuildAtkDefBat19");
        }
        else
        {
            titleNameText.text = Language.Get("GuildAtkDefBat17");
        }
 
        bgImage.SetSprite(isWin ? "GuildAtkDefBatTreasureWin" : "GuildAtkDefBatTreasureFail");
 
        // 不等于休战期就隐藏noOpenRect
        noOpenRect.SetActive(!isTrucePeriod);
 
        // 开启次数
        openCntText.text = Language.Get("GuildAtkDefBat16", manager.GetMyOpenRemainCount(), manager.GetMyMaxOpenCount());
 
        // 剩余时间(仅休战期)
        DisplayTime();
 
        // 计算最大格子数
        maxGridCount = manager.CalculateMaxGridCount();
 
        // 重置滚动位置
        currentTopRow = 0;
 
        // 刷新Scroller
        RefreshScroller();
    }
 
    void DisplayTime()
    {
        int displayState = manager.GetGuildBattleDisplayState();
        bool isTrucePeriod = displayState == 4 || displayState == 5;
        timeImage.SetActive(isTrucePeriod);
        timeText.SetActive(isTrucePeriod);
        if (isTrucePeriod)
        {
            int cd = manager.GetCountdownToCurrentPeriodEnd();
            timeText.text = Language.Get("GuildAtkDefBat49", TimeUtility.SecondsToShortDHMS(cd));
        }
    }
 
    void RefreshScroller()
    {
        scroller.Refresh();
        int totalRows = Mathf.CeilToInt((float)maxGridCount / COLS);
        int visibleRows = Mathf.Min(MAX_ROWS, totalRows - currentTopRow);
 
        for (int i = 0; i < visibleRows; i++)
        {
            scroller.AddCell(ScrollerDataType.Header, currentTopRow + i);
        }
        scroller.Restart();
 
        lastLineButton.SetActive(currentTopRow > 0);
        nextLineButton.SetActive(currentTopRow < totalRows - MAX_ROWS);
    }
 
    void OnRefreshCell(ScrollerDataType type, CellView cell)
    {
        var tc = cell as GuildAtkDefBatTreasureCell;
        if (tc != null)
        {
            int rowIdx = cell.index;
            int startIdx = rowIdx * COLS + 1;
            tc.SetData(startIdx, maxGridCount, isWin, manager.treasureGridRecords, pendingAnimGridIndex);
        }
    }
 
}