-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (187 loc) · 7.25 KB
/
Copy pathmain.cpp
File metadata and controls
225 lines (187 loc) · 7.25 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
#include "include/crow_all.h"
#include <iostream>
#include <string>
using namespace std;
// DEKLARASI KONSTANTA & VARIABEL GLOBAL ASLI
const int n = 128;
string appName = "taskAdventure";
string userName = "0";
string todoList[n];
int todoListReward[n];
bool todoListIsItDone[n];
string rewardList[n];
int rewardListPrice[n];
int coinBalance = 0;
int todoListCount = 0;
int rewardListCount = 0;
int undoneTodoCount = 0;
// FUNGSI SORTING ASLI
void sortTodoList(){
for(int i=0; i<todoListCount-1; i++){
for(int j=0; j<todoListCount-i-1; j++){
if(todoListReward[j]<todoListReward[j+1]){
int tempInt = todoListReward[j];
todoListReward[j] = todoListReward[j+1];
todoListReward[j+1] = tempInt;
string tempString = todoList[j];
todoList[j] = todoList[j+1];
todoList[j+1] = tempString;
bool tempBool = todoListIsItDone[j];
todoListIsItDone[j] = todoListIsItDone[j+1];
todoListIsItDone[j+1] = tempBool;
}
}
}
}
void sortRewardList(){
for(int i=0; i<rewardListCount-1; i++){
for(int j=0; j<rewardListCount-i-1; j++){
if(rewardListPrice[j]<rewardListPrice[j+1]){
int tempInt = rewardListPrice[j];
rewardListPrice[j] = rewardListPrice[j+1];
rewardListPrice[j+1] = tempInt;
string tempString = rewardList[j];
rewardList[j] = rewardList[j+1];
rewardList[j+1] = tempString;
}
}
}
}
int main() {
crow::SimpleApp app;
// API: DASHBOARD
CROW_ROUTE(app, "/api/dashboard")([&](){
sortTodoList();
sortRewardList();
crow::json::wvalue x;
x["userName"] = userName;
x["coinBalance"] = coinBalance;
x["undoneCount"] = undoneTodoCount;
x["todos"] = crow::json::wvalue::list();
for(int i=0; i<todoListCount; i++){
x["todos"][i]["name"] = todoList[i];
x["todos"][i]["reward"] = todoListReward[i];
x["todos"][i]["done"] = todoListIsItDone[i];
}
x["rewards"] = crow::json::wvalue::list();
for(int i=0; i<rewardListCount; i++){
x["rewards"][i]["name"] = rewardList[i];
x["rewards"][i]["price"] = rewardListPrice[i];
}
crow::response res(x);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// API 1: TAMBAH TO-DO
CROW_ROUTE(app, "/api/add-todo").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
todoList[todoListCount] = body["task"].s();
todoListReward[todoListCount] = body["reward"].i();
todoListIsItDone[todoListCount] = false;
todoListCount++;
undoneTodoCount++;
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// API 2: HAPUS TO-DO
CROW_ROUTE(app, "/api/delete-todo").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
int index = body["index"].i();
todoListIsItDone[index] = false;
for(int i=index; i<todoListCount-1; i++){
todoList[i] = todoList[i+1];
todoListReward[i] = todoListReward[i+1];
todoListIsItDone[i] = todoListIsItDone[i+1];
}
todoListCount--;
undoneTodoCount--;
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// API 3: CEKLIS TO-DO
CROW_ROUTE(app, "/api/done-todo").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
int index = body["index"].i();
if(todoListIsItDone[index] == false){
todoListIsItDone[index] = true;
coinBalance += todoListReward[index];
undoneTodoCount--;
}
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// API 4: UNCEKLIS TO-DO
CROW_ROUTE(app, "/api/undone-todo").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
int index = body["index"].i();
if(todoListIsItDone[index] == true){
todoListIsItDone[index] = false;
coinBalance -= todoListReward[index];
undoneTodoCount++;
}
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// API 5: TAMBAH SELF REWARD
CROW_ROUTE(app, "/api/add-reward").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
rewardList[rewardListCount] = body["name"].s();
rewardListPrice[rewardListCount] = body["price"].i();
rewardListCount++;
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// API 6: HAPUS SELF REWARD
CROW_ROUTE(app, "/api/delete-reward").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
int index = body["index"].i();
for(int i=index; i<rewardListCount-1; i++){
rewardList[i] = rewardList[i+1];
rewardListPrice[i] = rewardListPrice[i+1];
}
rewardListCount--;
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// API 7: BELI SELF REWARD
CROW_ROUTE(app, "/api/buy-reward").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
int index = body["index"].i();
if((coinBalance - rewardListPrice[index]) >= 0){
coinBalance -= rewardListPrice[index];
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
} else {
crow::response res(400, "Saldo koin tidak cukup");
res.add_header("Access-Control-Allow-Origin", "*");
return res;
}
});
// API 8: GANTI USERNAME
CROW_ROUTE(app, "/api/change-username").methods("POST"_method)([&](const crow::request& req){
auto body = crow::json::load(req.body);
userName = body["username"].s();
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
return res;
});
// PENYELAMAT CORS (MENGATASI ERROR SAAT TOMBOL DIKLIK)
CROW_CATCHALL_ROUTE(app)([](const crow::request& req) {
crow::response res(200);
res.add_header("Access-Control-Allow-Origin", "*");
res.add_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
res.add_header("Access-Control-Allow-Headers", "Content-Type");
return res;
});
cout << "------------------------------------------\n";
cout << "Task Adventure Backend Ready pada Port 8080\n";
cout << "------------------------------------------\n";
app.port(8080).multithreaded().run();
}