-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
208 lines (150 loc) Β· 5.57 KB
/
Copy pathmain.cpp
File metadata and controls
208 lines (150 loc) Β· 5.57 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
#include <QuickSDL/GameManager.h>
#include <headers/JoinScreen.h>
#include <headers/PlayScreen.h>
#include <mutex>
#include <socket/Client.hpp>
#include <socket/ThreadPool.hpp>
#include <sstream>
#include <thread>
using namespace TSS;
using namespace std;
void *auto_recv_message();
int main(int argc, char const *argv[]) {
if (argc < 3) {
std::cout << "Usage: " << argv[0] << " <interface> <port>" << std::endl;
exit(EXIT_FAILURE);
}
TSS::Client *client = TSS::Client::Instance(atoi(argv[2]), (char *)argv[1]);
// TSS::Client *client = TSS::Client::Instance(5500, "127.0.0.1");
client->auth_menu();
QuickSDL::GameManager *game = QuickSDL::GameManager::Instance();
thread recv_thread(auto_recv_message);
recv_thread.detach();
game->Run();
QuickSDL::GameManager::Release();
game = NULL;
TSS::Client::Release();
client = NULL;
printf("Client destroyed\n");
return 0;
}
void *auto_recv_message() {
Client *client = Client::Instance();
char buffer[1024];
while (true) {
memset(buffer, 0, sizeof(buffer));
size_t bytes_received = recv(client->get_sock(), buffer, sizeof(buffer), 0);
if (bytes_received <= 0) {
cout << "Connection closed on socket " << socket << endl;
close(client->get_sock());
break;
}
buffer[bytes_received] = '\0';
cout << "Received message: " << buffer << endl;
string bufferStr(buffer);
istringstream iss(bufferStr);
string event_name, data;
getline(iss, event_name, '\n');
getline(iss, data, '\n');
if (event_name == "room:get_rooms:success") {
cout << endl << "Rooms data" << data << endl;
TSS::LinkedList *rooms = new TSS::LinkedList();
json_to_rooms_list(data, rooms);
client->rooms = rooms;
cout << "Rooms list: " << client->rooms->length << endl;
continue;
}
if (event_name == "leaderboard:get:success") {
cout << endl << "Leaderboard data" << data << endl;
TSS::LinkedList *leaderboard = new TSS::LinkedList();
json_to_leaderboard_list(data, leaderboard);
client->leaderboard = leaderboard;
cout << "Leaderboard list: " << client->leaderboard->length << endl;
continue;
}
if (event_name == "room:create_room:success" ||
event_name == "room:join_room:success" ||
event_name ==
"room:leave_room:success" || // found other user leave room
event_name == "game:ready:success" ||
event_name == "game:unready:success" ||
event_name == "game:start:success" ||
event_name == "game:pause:success" || // found leader pause game
event_name == "game:resume:success" || // found leader resume game
event_name == "game:game_end:success") {
Room *room = json_to_room(string_to_json(data));
client->set_current_room(room);
client->is_game_paused = room->status == 3;
int alive_count = 0;
for (int i = 0; i < room->players.size(); i++)
if (room->players[i].status != 3) alive_count++;
client->alive_players = alive_count;
cout << "Update room status: " << client->get_current_room()->room_id
<< endl;
continue;
}
// found other user shoot bullet
if (event_name == "game:shoot") {
string dataStr(data);
istringstream iss(dataStr);
string username, pos_x, pos_y, direction;
getline(iss, username, ':');
getline(iss, pos_x, ':');
getline(iss, pos_y, ':');
getline(iss, direction, ':');
PlayScreen *playScreen = PlayScreen::Instance();
playScreen->Shoot(username, Vector2(stof(pos_x), stof(pos_y)),
static_cast<GameEntity::DIRECTION>(stoi(direction)));
cout << "Bullet start shoot: " << username << " " << pos_x << endl;
continue;
}
// found other user start move
if (event_name == "game:move:start") {
string dataStr(data);
istringstream iss(dataStr);
string username, pos_x, pos_y, direction;
getline(iss, username, ':');
getline(iss, pos_x, ':');
getline(iss, pos_y, ':');
getline(iss, direction, ':');
PlayScreen *playScreen = PlayScreen::Instance();
cout << "Move start" << username << " " << pos_x << endl;
playScreen->SetPlayerPosition(
username, Vector2(stof(pos_x), stof(pos_y)), true,
static_cast<GameEntity::DIRECTION>(stoi(direction)));
continue;
}
// found other user stop move
if (event_name == "game:move:stop") {
string dataStr(data);
istringstream iss(dataStr);
string username, pos_x, pos_y;
getline(iss, username, ':');
getline(iss, pos_x, ':');
getline(iss, pos_y, ':');
PlayScreen *playScreen = PlayScreen::Instance();
playScreen->SetPlayerPosition(username, Vector2(stof(pos_x), stof(pos_y)),
false, GameEntity::DIRECTION::none);
cout << "Move stop" << username << " " << pos_x << endl;
continue;
}
if (event_name == "auth:logout:success") {
cout << "Logout success" << endl;
break;
}
if (event_name == "game:player_dead:success") {
string dead_user;
getline(iss, dead_user, '\n');
Room *room = json_to_room(string_to_json(data));
client->set_current_room(room);
int alive_count = 0;
for (int i = 0; i < room->players.size(); i++)
if (room->players[i].status == 1) alive_count++;
client->alive_players = alive_count;
PlayScreen *playScreen = PlayScreen::Instance();
playScreen->PlayerDead(dead_user);
printf("Player dead: %s\n", dead_user.c_str());
}
}
return NULL;
}