-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameSetup.xaml.cs
More file actions
202 lines (166 loc) · 6.47 KB
/
GameSetup.xaml.cs
File metadata and controls
202 lines (166 loc) · 6.47 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
using Newtonsoft.Json;
using CrossPlatformProject2.Models; //implement models to gamepage
namespace CrossPlatformProject2;
public partial class GameSetup : ContentPage
{
private List<Entry> playerNameEntriesList = new List<Entry>();//list to store player names
private static readonly string FilePath = Path.Combine(FileSystem.AppDataDirectory, "SavedGame.json");//file path
private Dictionary<string, int> Categories = new();
//dictionary to map category names to IDs
public GameSetup()
{
InitializeComponent();
//add options for player selection
playerPicker.Items.Add("1 Player");
playerPicker.Items.Add("2 Player");
playerPicker.Items.Add("3 Player");
playerPicker.Items.Add("4 Player");
//add difficulty options
difficultyPicker.Items.Add("Easy");
difficultyPicker.Items.Add("Medium");
difficultyPicker.Items.Add("Hard");
//Question Picker
totalQuestionsPicker.Items.Add("1");
totalQuestionsPicker.Items.Add("2");
totalQuestionsPicker.Items.Add("3");
totalQuestionsPicker.Items.Add("4");
totalQuestionsPicker.Items.Add("5");
//Default Question Picker Index
//totalQuestionsPicker.SelectedIndex = 3;
//populate category picker
LoadCategories();
}
private async void LoadCategories()
{
string apiUrl = "https://opentdb.com/api_category.php";
using HttpClient client = new HttpClient();
try
{
var response = await client.GetStringAsync(apiUrl);
//deserialize JSON into a dictionary
var categoryResponse = JsonConvert.DeserializeObject<CategoryRoot>(response);
//validate category response and populate picker
if (categoryResponse != null && categoryResponse.TriviaCategories != null)
{
foreach (var category in categoryResponse.TriviaCategories)
{
categoryPicker.Items.Add(category.name); //add category to picker
Categories[category.name] = category.id; //map category name to ID
}
}
else
{
await DisplayAlert("Error", "Failed to load categories from the API.", "OK");
}
}
catch (Exception ex)
{
//handle exceptions during category loading
await DisplayAlert("Error", $"Failed to load categories: {ex.Message}", "OK");
}
}
private void OnPlayerCountChanged(object sender, EventArgs e)
{
//clear prvious player names entered
playerNameEntries.Clear();
playerNameEntriesList.Clear();
if (playerPicker.SelectedIndex < 0)
{
return;
}
//calculate number of players
int numberOfPlayers = playerPicker.SelectedIndex + 1;
//for loop to add player names entries based on selection
for (int i = 1; i <= numberOfPlayers; i++)
{
var entry = new Entry
{
Placeholder = $"Enter player {i} Name",
BackgroundColor = Colors.White,
TextColor = Colors.Black,
FontSize = 16,
};
//add player entry
playerNameEntriesList.Add(entry);
playerNameEntries.Children.Add(entry);
}
}
private async void OnStartButtonClicked_Clicked(object sender, EventArgs e)
{
if (playerPicker.SelectedItem == null)
{
await DisplayAlert("Error", "Please select the number of players.", "OK");
return;
}
if (difficultyPicker.SelectedItem == null)
{
await DisplayAlert("Error", "Please select the difficulty.", "OK");
return;
}
if (totalQuestionsPicker.SelectedItem == null)
{
await DisplayAlert("Error", "Please select the total number of questions.", "OK");
return;
}
var playerNames = playerNameEntriesList
.Where(entry => !string.IsNullOrWhiteSpace(entry.Text))
.Select(entry => entry.Text)
.ToList();
if (playerNames.Count != playerPicker.SelectedIndex + 1)
{
await DisplayAlert("Error", "Please fill in all player names.", "OK");
return;
}
string selectedPlayers = playerPicker.SelectedItem.ToString();
string selectedDifficulty = difficultyPicker.SelectedItem.ToString();
string selectedCategory = categoryPicker.SelectedItem?.ToString();
//Question
string totalQuestions = totalQuestionsPicker.SelectedItem?.ToString();
if (string.IsNullOrEmpty(selectedCategory) || !Categories.TryGetValue(selectedCategory, out int selectedCategoryId))
{
await DisplayAlert("Error", "Invalid category selection.", "OK");
return;
}
if (int.TryParse(totalQuestionsPicker.SelectedItem?.ToString(), out int totalQuestionsParsed))
{
await Navigation.PushAsync(new GamePage(selectedPlayers, selectedDifficulty, selectedCategoryId, playerNames, totalQuestionsParsed));
}
// Push these values to the GamePage for use
}
private async Task LoadGameFromFile()
{
if (File.Exists(GamePage.FilePath))
{
string json = await File.ReadAllTextAsync(GamePage.FilePath);
var gameState = JsonConvert.DeserializeObject<GameState>(json);
if (gameState != null)
{
//navigate to the GamePage with the loaded data
await Navigation.PushAsync(new GamePage(
gameState.PlayerNames.Count.ToString() + " Players", // Use number of players
gameState.SelectedDifficulty,
gameState.selectedCategoryId,
gameState.PlayerNames,
gameState.TotalQuestions
//gameState.TriviaQuestions
));
}
else
{
await DisplayAlert("Error", "Failed to load the saved game.", "OK");
}
}
else
{
await DisplayAlert("No Saved Game", "No saved game data found.", "OK");
}
}
private async void OnLoadGameClicked(object sender, EventArgs e)
{
await LoadGameFromFile();
}
private async void homeButton_Clicked(object sender, EventArgs e)
{
await Navigation.PopAsync();
}
}