-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerList.cpp
146 lines (120 loc) · 4 KB
/
ServerList.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
#include "ServerList.h"
ServerList::ServerList(std::shared_ptr<Graphic> graphicIn) : BaseWindow(graphicIn)
{
texts.push_back(GuiText(font, fontColor, graphic->getRenderer(), "IP", 100, 20));
texts.push_back(GuiText(font, fontColor, graphic->getRenderer(), "Name", 300, 20));
texts.push_back(GuiText(font, fontColor, graphic->getRenderer(), "Description", 500, 20));
texts.push_back(GuiText(font, fontColor, graphic->getRenderer(), "PlayerCount", 900, 20));
texts.push_back(GuiText(font, fontColor, graphic->getRenderer(), "Ping", 1100, 20));
buttons.push_back(GuiButton(font, fontColor, graphic->getRenderer(), "BACK", 100, 600, 55, 24, STATE::MAINMENU));
buttons.push_back(GuiButton(font, fontColor, graphic->getRenderer(), "Refresh", 600, 600, 70, 24, STATE::SERVERLIST));
yourIp = Utility::doWebRequest("http://icanhazip.com");
yourIp = std::regex_replace(yourIp,std::regex("\\s+"), "");
loadServerList();
}
void ServerList::renderBody(void)
{
BaseWindow::renderBody();
for(int i = 0; i < serverList.size(); i++)
{
serverList[i].render();
}
}
void ServerList::loadServerList(void)
{
std::string jsonString = Utility::doWebRequest("http://hannesf.com/ProjectZ/get.php");
rapidjson::Document doc;
if(!doc.Parse<0>(jsonString.c_str()).HasParseError());
{
const rapidjson::Value& a = doc;
for (rapidjson::SizeType i = 0; i < a.Size(); i++)
{
serverList.push_back(GuiServerList(font, fontColor, graphic->getRenderer(), a[i]["ip"].GetString(), a[i]["name"].GetString(), a[i]["description"].GetString(), 50+50*(i+1)));
}
}
if(enet_initialize () != 0)
{
fprintf (stderr, "An error occurred while initializing ENet.\n");
}
for(int i = 0; i < serverList.size(); i++)
{
ENetHost* client = enet_host_create(NULL, 1, 2, 57600 / 8, 14400 / 8);
if(client == NULL)
{
fprintf (stderr, "An error occurred while trying to create an ENet client host.\n");
}
ENetAddress address;
std::string tmpIp = serverList[i].getIp();
if(yourIp == tmpIp)
{
tmpIp = "localhost";
}
enet_address_set_host(&address, tmpIp.c_str());
address.port = 1234;
ENetPeer* server = enet_host_connect(client, &address, 2, 0);
if(server == NULL)
{
fprintf (stderr, "No available peers for initiating an ENet connection.\n");
}
ENetEvent event;
if(enet_host_service(client, &event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
{
std::cout << "Pinging server..." << std::endl;
char tmp[1400];
sprintf(tmp, "2 %d", SDL_GetTicks());
ENetPacket* packet = enet_packet_create(tmp, strlen(tmp)+1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(server, 0, packet);
enet_host_flush(client);
if(enet_host_service(client, &event, 1000) > 0 && event.type == ENET_EVENT_TYPE_RECEIVE)
{
int time;
sscanf((char*)event.packet->data, "2 %d", &time);
serverList[i].setPing(SDL_GetTicks()-time);
}
std::cout << "Getting playercount..." << std::endl;
packet = enet_packet_create("3", 2, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(server, 0, packet);
enet_host_flush(client);
if(enet_host_service(client, &event, 1000) > 0 && event.type == ENET_EVENT_TYPE_RECEIVE)
{
int playerCount, maxPlayers;
sscanf((char*)event.packet->data, "3 %d %d", &playerCount, &maxPlayers);
serverList[i].setPlayerCount(playerCount, maxPlayers);
}
enet_peer_disconnect(server, 0);
}
else
{
std::cout << "Failed to connect to server" << std::endl;
}
}
}
void ServerList::onEvent(SDL_Event* ev, const Uint8* keyStates)
{
BaseWindow::onEvent(ev, keyStates);
for(int i = 0; i < serverList.size(); i++)
{
serverList[i].onEvent(ev, keyStates);
}
}
void ServerList::update(float delta, const Uint8* keyStates)
{
BaseWindow::update(delta, keyStates);
for(int i = 0; i < serverList.size(); i++)
{
if(serverList[i].hasClicked())
{
newState = true;
state = serverList[i].getOnClick();
std::string tmpIp = serverList[i].getIp();
if(yourIp == tmpIp)
ip = "localhost";
else
ip = tmpIp;
}
}
}
std::string ServerList::getIp(void)
{
return ip;
}