-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintroduction.cpp
369 lines (294 loc) · 10.2 KB
/
introduction.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <iostream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <fstream>
#include <chrono>
#include <thread>
#include "introduction.h"
#include "gameroom.h"
#include "revive.h"
#include <map>
#include <vector>
std::vector<int> encryptionKey = {862442, 421612, 916945, 148275}; // Longer key for added complexity
// constructor
Introduction::Introduction(){
};
// runs the introduction
void Introduction::run_introduction()
{
welcome();
register_or_login();
}
// reads & returns the number of tokens the player has
int Introduction::get_tokens()
{
std::ifstream read_file("data.txt");
std::string current_line;
std::string tmp;
std::stringstream string_stream;
int tokens;
int current_position = 0;
int token_position_in_file = 2;
std::string ciphertext;
while (getline(read_file, current_line))
{
if (current_position == token_position_in_file)
{
tmp = current_line;
}
current_position = current_position + 1;
}
ciphertext = tmp;
// decrypt ciphertext to get token amount
tokens = decrypt(ciphertext);
return tokens;
}
// adjust and set new token amount in the file
void Introduction::set_tokens(int tokens)
{
std::ifstream data_file;
data_file.open("data.txt");
std::fstream tmp("test.txt", std::ios::out);
std::string current_line;
std::string line;
int current_position = 0;
int token_position_in_file = 2;
while (getline(data_file, current_line))
{
tmp << current_line << std::endl;
}
tmp.close();
data_file.close();
std::ifstream test_file("test.txt");
std::fstream data;
data.open("data.txt", std::ios::out);
// encrypt tokens before writing to file
std::string ciphertext = encrypt(tokens);
while (getline(test_file, line))
{
if (current_position != token_position_in_file)
{
data << line << std::endl;
}
else if (current_position == token_position_in_file)
{
data << ciphertext << std::endl;
}
current_position = current_position + 1;
}
test_file.close();
remove("test.txt");
data.close();
}
std::string Introduction::encrypt(int tokens){
int encryptedValue = tokens;
for (size_t i = 0; i < encryptionKey.size(); ++i) {
encryptedValue ^= encryptionKey[i]; // XOR with each byte of the key
}
std::stringstream stream;
stream << std::hex << encryptedValue;
return (stream).str();
}
int Introduction::decrypt(std::string cipher){
int decryptedValue = std::stoi(cipher, 0, 16);
for (size_t i = 0; i < encryptionKey.size(); ++i) {
decryptedValue ^= encryptionKey[i]; // XOR with each byte of the key to reverse encryption
}
return decryptedValue;
}
// displays a welcome banner to the terminal window
void Introduction::welcome()
{
// clear console
system("clear");
const int time = 400;
// welcome banner
std::this_thread::sleep_for(std::chrono::milliseconds(time));
std::cout << "\n\n";
std::cout << " *** *** ******* *** *** ********** ************" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
std::cout << " *** *** *** *** *** *** *** *** ****" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
std::cout << " *** *** *** *** *** *** ***** ****" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
std::cout << " ************* *** *** *** *** **** **********" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
std::cout << " *** *** *** *** *** *** ***** ****" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
std::cout << " *** *** *** *** **** **** *** *** ****" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(time));
std::cout << " *** *** ******* *********** ********** ************" << std::endl;
}
// validates the user's input in this panel
bool Introduction::validate_input(std::string users_input)
{
// clear console
system("clear");
const int time = 3000;
// Check if digit is one of the available options
if (users_input == ("1") || users_input == ("2"))
{
return true;
}
else
{
std::cout << "\n\n\n\n\n\n\n\n\n\n\n";
std::cout << " PLEASE ENTER A VALID RESPONSE";
std::cout << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
return false;
}
}
// prompts user to login if this option is selected
void Introduction::login()
{
system("clear");
std::string username;
std::string password;
std::string current_line;
std::string next_line;
std::string spacer = "";
std::cout << "\n"; //31
const int time = 5000;
bool user_info_found = false;
std::cout << "\n\n\n\n\n\n\n\n";
std::cout << " LOGIN MENU";
std::cout << "\n\n";
std::cout << " ENTER YOUR USERNAME: ";
std::cin >> username;
std::cout << "\n";
std::cout << " ENTER YOUR PASSWORD: ";
std::cin >> password;
std::ifstream read_file("data.txt");
getline(read_file, current_line); //username is line 1
getline(read_file, next_line); // password is line 2
for(int i = 0; i <= 32 - std::to_string(get_tokens()).length(); i++){
spacer = spacer + " ";
}
if ((username == current_line) && (password == next_line) && get_tokens() > 0)
{
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n";
std::cout << " LOGIN SUCCESSFUL" << std::endl;
std::cout << spacer << "YOU HAVE " << (get_tokens()) << token_or_tokens(get_tokens()) << std::endl;
std::cout << " PLEASE WAIT WHILE WE BRING YOU TO THE GAME FLOOR" << std::endl;
std::cout << "";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
user_info_found = true;
}
// user stopped playing after reaching 0 tokens and logged back in
else if((username == current_line) && (password == next_line) && get_tokens() <= 0){
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n";
std::cout << " LOGIN SUCCESSFUL" << std::endl;
std::cout << "";
std::this_thread::sleep_for(std::chrono::milliseconds(time));
user_info_found = true;
// revive the player
Revive revive_obj;
revive_obj.Explanation();
}
std::cout << "\n";
read_file.close();
if (!user_info_found)
{
system("clear");
int needed_spaces = 6;
spacing_buffer(needed_spaces);
std::cout << " COULD NOT FIND AN ACCOUNT WITH THE GIVEN USERNAME AND PASSWORD" << std::endl;
std::cout << " PLEASE REGISTER OR TRY TO LOGIN AGAIN"
<< std::endl;
std::cin.clear(); // maybe keep this, keep testing
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clear the input buffer
register_or_login();
}
// clear input before moving to next object
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Clear the input buffer
Gameroom game_floor_obj;
game_floor_obj.run_gameroom();
}
// prompts user to register if that option is selected
void Introduction::reg()
{
system("clear");
const int time = 4000;
const int initial_tokens = 10;
std::string username;
std::string password;
// encrypt token amount
std::string ciphertext = encrypt(initial_tokens);
int temp = decrypt(ciphertext);
std::fstream data_file;
data_file.open("data.txt", std::ios::out);
std::cout << "\n\n\n\n\n\n\n\n";
std::cout << " REGISTRATION MENU";
std::cout << "\n\n";
std::cout << " CREATE A USERNAME: ";
std::cin >> username;
std::cout << "\n";
std::cout << " CREATE A PASSWORD: ";
std::cin >> password;
system("clear");
std::cout << "\n\n\n\n\n\n\n\n\n\n";
std::cout << " REGISTRATION COMPLETE" << std::endl;
std::cout << " LOGIN TO CONTINUE";
std::cout << "\n\n";
data_file << username << std::endl;
data_file << password << std::endl;
data_file << ciphertext << std::endl;
data_file.close();
std::this_thread::sleep_for(std::chrono::milliseconds(time));
login();
}
// direct programs control to either register or login method
void Introduction::direct_control_flow(std::string users_input)
{
if (users_input == ("1"))
{
login();
}
else if (users_input == ("2"))
{
reg();
}
}
// prompts user to register or login during menu screen
void Introduction::register_or_login()
{
bool is_valid = false;
std::string users_input;
int time = 3000;
do
{
std::cout << "\n\n\n\n\n";
std::cout << " 1: LOGIN";
std::cout << "\n";
std::cout << " 2: REGISTER";
std::cout << "\n\n";
std::cout << " SELECT AN OPTION: ";
std::getline(std::cin, users_input);
is_valid = validate_input(users_input);
system("clear");
std::cout << "\n\n\n\n\n";
} while (!is_valid);
direct_control_flow(users_input);
}
// this will take in the token amount and return the string "TOKEN" or "TOKENS" depending on how many
std::string Introduction::token_or_tokens(int token_amount){
if((token_amount) == 1){
return " TOKEN";
}
else{
return " TOKENS";
}
}
// added white space to make program more visually appealing
void Introduction::spacing_buffer(int spaces)
{
for (int i = 0; i < spaces; i++)
{
std::cout << "\n";
};
}