/
server.cpp
153 lines (134 loc) · 4.62 KB
/
server.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
#include "server.h"
#include <iostream>
Server::Server(int argc, char **argv) : QCoreApplication(argc, argv) {
state = new ServerState();
state->init();
counter = new QElapsedTimer();
loop = new QTimer();
connect(loop, SIGNAL(timeout()), this, SLOT(onLoop()));
TICKS_PER_SECOND = 20;
std::cout << "TICKS PER SECOND = " << TICKS_PER_SECOND << std::endl;
SKIP_TICKS = 1000 / TICKS_PER_SECOND;
MAX_FRAMESKIP = 10;
counter->start();
next_game_tick = counter->elapsed();
loop->start(0);
//QVariantMap map;
//map.insert("login", "kabanov");
//map.insert("password", "");
//map.insert("uids", "1,2");
//API::gi().request("users.getInfo", map);
}
Server::~Server() {
state->release();
}
/*void Server::init()
{
port = 560;
p_server = new QTcpServer(this);
if (!p_server->listen(QHostAddress::Any, port))
{
Console::print("Connection failed");
p_server->close();
}
else
{
Console::print(QString("Server has started at port: ") + QVariant(port).toString());
}
connect(p_server, SIGNAL(newConnection()), this, SLOT(onClientConnected()));
game_world = new GameWorld();
physics_world = new PhysicsWorld();
next_player_id = 1;
next_object_id = 1;
/*car = new Vehicle(1, new Rectangle2D(Point2D(0,0), 1.962, 4.710), 1887);
double r[] = {3.59, 2.19, 1.41, 1, 0.83};
std::vector<double> ratios(r, r + sizeof(r) / sizeof(double));
car->setEngine(VehicleEngine(637, 6000, ratios, 3.06));
CarTrack back(-1.62724, 1.604, 1000 / (2 * M_PI * 456), 0.466, true, true, RotationReaction::NoRotation, 0.9, 0.025, 1.5, 1.5, M_PI / 6);
CarTrack front(1.74552, 1.603, 1000 / (2 * M_PI * 456), 0.544, false, true, RotationReaction::StraightRot, 0.9, 0.025, 1.5, 1.5, M_PI / 6);
car->setWheels(back, front, 1.45);
car->setVehicleBody(VehicleBody(0.356, 5.0, 1.923, 1.45, car->chassis.getMassCenter()));
car->setCoordinates(Vector2D(-10, 0));
car->setAngle(-asin(1));
car->setTorquePercent(0.1);
car->setAccelerationState(AccelerationState::ForwardAcc);
Turret* t = new Turret(2, new Rectangle2D(Point2D(0, -0.5), 1.5, 1.5), 100, 7, 0.1, 2 * asin(1), Bullet::BULLET, 0);
car->addTurret(t);
// game_world->add(car);
// physics_world->add(car);
}*/
/*QByteArray Server::prepareEvent(const GameObjectEvent& event)
{
QByteArray buffer;
buffer.append(event.type + ";");
buffer.append(QVariant(event.id_player).toString() + ";");
buffer.append(QVariant(event.id_object).toString() + ";");
buffer.append(QVariant(event.id_parent).toString() + ";");
buffer.append(QVariant(event.class_object).toString() + ";");
buffer.append(QVariant(event.type_object).toString() + ";");
buffer.append(QVariant(event.position.x).toString() + ";");
buffer.append(QVariant(event.position.y).toString() + ";");
buffer.append(QVariant(event.rotation).toString());
return buffer;
}*/
/*void Server::Invoke(const Event &event)
{
Console::print(QString("Invoke: ")+event.type);
if (event.type == "tryauth")
{
Console::print("tryauth");
GameObjectEvent e(0, GameObjectEvent::AUTHORIZED);
e.id_player = next_player_id;
next_player_id++;
broadcast(prepareEvent(e));
GameObjectEvent e2(0, GameObjectEvent::ADDED_OBJECT);
e2.id_player = e.id_player;
e2.id_object = next_object_id;
e2.class_object = GameObjectType::VEHICLE;
e2.type_object = 1;
e2.position.x = 0;
e2.position.y = 0;
e2.rotation = 1;
next_object_id++;
broadcast(prepareEvent(e2));
}
}*/
/*void Server::onClientConnected()
{
Console::print("Client connected");
QTcpSocket* socket = p_server->nextPendingConnection();
NetworkClient* player = new NetworkClient(socket);
Console::print("add listener");
player->addEventListener(this);
players.push_back(player);
}
void Server::onClientDisconnected()
{
Console::print("Client disconnected");
}
void Server::broadcast(const QByteArray& buffer)
{
//Console::print(QString("Broadcast: "+message));
for (int i = 0; i < players.size(); ++i)
{
if (players[i]->isValid())
{
players[i]->send(buffer);
}
else
{
delete players[i];
players[i] = players[players.size() - 1];
players.pop_back();
--i;
}
}
}*/
void Server::onLoop() {
int loops = 0;
while (counter->elapsed() > next_game_tick && loops < MAX_FRAMESKIP) {
state->tick(1.0 / TICKS_PER_SECOND);
next_game_tick += SKIP_TICKS;
loops++;
}
}