-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMarketHandler.cs
More file actions
300 lines (269 loc) · 11.2 KB
/
MarketHandler.cs
File metadata and controls
300 lines (269 loc) · 11.2 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using MelonLoader;
using System.Linq;
namespace GladiatorManagerAccess
{
public class MarketHandler : IAccessibleHandler
{
private bool _wasOpen = false;
private int _selectedIndex = 0;
private List<string> _marketItems = new List<string>();
private bool _viewingProfile = false;
private int _selectedGladId = -1;
private List<int> _hirableIndices = new List<int>();
public string GetHelpText()
{
if (_viewingProfile)
return "Market Recruitment: Use Up and Down arrows to review the gladiator's contract and stats. Press Enter on 'Recruit' to hire them. Press Escape to return to the list.";
return "Market List: Use Up and Down arrows to browse available recruits. Press Enter to view a gladiator's profile and contract details. Press Escape to return home.";
}
public void Update()
{
var pop = Object.FindObjectOfType<Populator>();
bool isOpen = pop != null && pop.barracks && AccessStateManager.IsIn(AccessStateManager.State.Shop);
if (isOpen && !_wasOpen)
{
_wasOpen = true;
_viewingProfile = false;
_selectedIndex = 0;
RefreshHirableList();
BuildMarketItems();
AnnounceCurrent();
}
else if (!isOpen && _wasOpen)
{
_wasOpen = false;
}
if (isOpen)
{
ProcessInput();
}
}
private void RefreshHirableList()
{
_hirableIndices.Clear();
// In this game, hirable gladiators are often in the allThePGladiators list but with Recruited = false
var all = DataManager.allThePGladiators;
for (int i = 0; i < all.Count; i++)
{
if (!all[i].Recruited && all[i].Alive)
{
_hirableIndices.Add(all[i].ID);
}
}
}
private void BuildMarketItems()
{
_marketItems.Clear();
if (_viewingProfile)
{
BuildProfileItems();
return;
}
var team = DataManager.allTheTeams.Find(x => x.PlayerTeam);
_marketItems.Add($"--- Market (Money: {team.Money} gold) ---");
RefreshHirableList();
if (_hirableIndices.Count == 0)
{
_marketItems.Add("No recruits available this week.");
}
else
{
foreach (int id in _hirableIndices)
{
var g = DataManager.allThePGladiators.Find(x => x.ID == id);
if (g != null)
{
_marketItems.Add($"{g.FirstName} {g.Surname} ({g.Class}) - Fee: {g.SigningFee} gold");
}
}
}
_marketItems.Add("Back to Market List");
}
private void BuildProfileItems()
{
var g = DataManager.allThePGladiators.Find(x => x.ID == _selectedGladId);
if (g == null)
{
_viewingProfile = false;
BuildMarketItems();
return;
}
_marketItems.Add($"--- {g.FirstName} {g.Surname} Profile ---");
_marketItems.Add($"Origin: {g.Origin}");
_marketItems.Add($"Class: {g.Class}");
_marketItems.Add($"Age: {g.AgeYears} years");
_marketItems.Add($"Gender: {g.Gender}");
_marketItems.Add($"Appearance: {AppearanceUtilities.GetAppearance(g.ID, true)}");
// For hirable, stats are often hidden or estimated
_marketItems.Add($"Contract: {g.ContractType}, {g.ContractLength} weeks");
_marketItems.Add($"Salary: {g.Salary} gold/week");
_marketItems.Add($"Signing Fee: {g.SigningFee} gold");
_marketItems.Add("--- Attributes ---");
var v = GetVisibility(g.ID);
_marketItems.Add(FormatStat("Initiative", g.Initiative, false, v?.Initiative ?? true));
_marketItems.Add(FormatStat("Strength", g.Strength, false, v?.Strength ?? true));
_marketItems.Add(FormatStat("Agility", g.Agility, false, v?.Agility ?? true));
_marketItems.Add(FormatStat("Toughness", g.Toughness, false, v?.Toughness ?? true));
_marketItems.Add(FormatStat("Discipline", g.Discipline, false, v?.Discipline ?? true));
_marketItems.Add(FormatStat("Weapon Skill", g.Sword, false, v?.Sword ?? true));
_marketItems.Add(FormatStat("Bravery", g.Bravery, false, v?.Bravery ?? true));
_marketItems.Add(FormatStat("Recovery", g.Recovery, false, v?.Recovery ?? true));
_marketItems.Add(FormatStat("Speed", g.Speed, false, v?.Speed ?? true));
_marketItems.Add(FormatStat("Stamina", g.Stamina, false, v?.Stamina ?? true));
_marketItems.Add(FormatStat("Leadership", g.Leadership, false, v?.Leadership ?? true));
_marketItems.Add(FormatStat("Current Ability", g.CurrentAbility, false, v?.CurrentAbility ?? true));
_marketItems.Add(FormatStat("Potential", g.PotAbility, false, v?.PotentialAbility ?? true));
_marketItems.Add($"Recruit {g.FirstName} for {g.SigningFee} gold");
_marketItems.Add("Back to Market List");
}
private string FormatStat(string label, int value, bool isOwned, bool visible)
{
if (isOwned) return $"{label}: {value}";
bool isMental = label.Contains("Intelligence") || label.Contains("Work Ethic");
string desc = isMental ?
GeneralUtilities.AttributeIntToDescriptonWorkEthicIntelligence(value) :
GeneralUtilities.AttributeIntToDescripton(value);
if (label == "Current Ability" || label == "Potential")
{
int stars = Mathf.CeilToInt(value / 10f);
if (stars < 1) stars = 1;
if (stars > 20) stars = 20;
if (visible) return $"{label}: {desc} ({stars} stars, value {value})";
return $"{label}: {desc} ({stars} stars)";
}
if (visible) return $"{label}: {desc} ({value})";
return $"{label}: {desc}";
}
private VisibileStats GetVisibility(int id)
{
try { var list = DataManager.dbManager.Query<VisibileStats>("SELECT * FROM RecruitmentVisibility WHERE ID = ?", id); return list != null && list.Count > 0 ? list[0] : null; }
catch { return null; }
}
private void ProcessInput()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
_selectedIndex--;
if (_selectedIndex < 0) _selectedIndex = _marketItems.Count - 1;
AnnounceCurrent();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
_selectedIndex++;
if (_selectedIndex >= _marketItems.Count) _selectedIndex = 0;
AnnounceCurrent();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
ActivateCurrent();
}
else if (Input.GetKeyDown(KeyCode.Escape))
{
if (_viewingProfile)
{
_viewingProfile = false;
_selectedIndex = 0;
BuildMarketItems();
AnnounceCurrent();
}
else
{
ExitToHome();
}
}
}
private void ExitToHome()
{
var lm = Object.FindObjectOfType<LevelManager>();
if (lm != null)
{
lm.LoadLevel("Home");
}
else
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Home");
}
}
private void SyncGameSelectionWithModSelection(Populator pop)
{
if (_selectedGladId == -1) return;
for (int i = 1; i <= 24; i++)
{
if (Populator.rearrangeGrid[i] == _selectedGladId)
{
if (i <= 12)
{
Populator.bottomHalfDisplay = false;
Populator.displayAdjustment = 0;
pop.changeDisplayID(i);
}
else
{
Populator.bottomHalfDisplay = true;
Populator.displayAdjustment = 12;
pop.changeDisplayID(i - 12);
}
break;
}
}
}
private void AnnounceCurrent()
{
if (_selectedIndex >= 0 && _selectedIndex < _marketItems.Count)
{
string itemText = _marketItems[_selectedIndex];
ScreenReader.Say($"{itemText}, {_selectedIndex + 1} of {_marketItems.Count}.");
}
}
private void ActivateCurrent()
{
string item = _marketItems[_selectedIndex];
if (item == "Back to Market List")
{
_viewingProfile = false;
_selectedIndex = 0;
BuildMarketItems();
AnnounceCurrent();
}
else if (item.Contains("Recruit") && item.Contains(" for "))
{
var pop = Object.FindObjectOfType<Populator>();
if (pop != null && pop.recruitButton != null)
{
SyncGameSelectionWithModSelection(pop);
pop.recruitButton.onClick.Invoke();
// After recruiting, the list changes
MelonLoader.MelonCoroutines.Start(DelayedRefreshAfterRecruit());
}
}
else if (!_viewingProfile && item.Contains("Fee:"))
{
// Select gladiator from list
int listIndex = _selectedIndex - 1; // Subtract header
if (listIndex >= 0 && listIndex < _hirableIndices.Count)
{
_selectedGladId = _hirableIndices[listIndex];
_viewingProfile = true;
_selectedIndex = 0;
var pop = Object.FindObjectOfType<Populator>();
if (pop != null) SyncGameSelectionWithModSelection(pop);
BuildMarketItems();
AnnounceCurrent();
}
}
}
private System.Collections.IEnumerator DelayedRefreshAfterRecruit()
{
yield return new UnityEngine.WaitForSeconds(0.5f);
_viewingProfile = false;
_selectedIndex = 0;
RefreshHirableList();
BuildMarketItems();
ScreenReader.Say("Recruitment successful. Returning to market list.");
AnnounceCurrent();
}
}
}