cleaned useless or buggy modules

This commit is contained in:
Anselme 2015-10-05 17:00:26 +02:00
parent a2038cc981
commit 2794f21093
11 changed files with 9 additions and 443 deletions

View File

@ -16,10 +16,7 @@ SOURCES = main.cpp \
poilaumodule.cpp \ poilaumodule.cpp \
fourasmodule.cpp \ fourasmodule.cpp \
riddles.cpp \ riddles.cpp \
todomodule.cpp \ todomodule.cpp
server.cpp \
servermodule.cpp \
simulation.cpp
HEADERS += \ HEADERS += \
regismodule.h \ regismodule.h \
@ -28,8 +25,4 @@ HEADERS += \
poilaumodule.h \ poilaumodule.h \
fourasmodule.h \ fourasmodule.h \
riddles.h \ riddles.h \
todomodule.h \ todomodule.h
packet.h \
server.h \
servermodule.h \
simulation.h

View File

@ -7,7 +7,6 @@
#include "poilaumodule.h" #include "poilaumodule.h"
#include "fourasmodule.h" #include "fourasmodule.h"
#include "todomodule.h" #include "todomodule.h"
#include "servermodule.h"
class HelloWorldModule : public Module class HelloWorldModule : public Module
{ {
@ -33,12 +32,11 @@ public :
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
BotApp app = BotApp(argc, argv); BotApp app = BotApp(argc, argv);
app.addModule(new HelloWorldModule()); //app.addModule(new HelloWorldModule());
app.addModule(new RegisModule()); app.addModule(new RegisModule());
app.addModule(new SparrowModule()); app.addModule(new SparrowModule());
app.addModule(new PoilAuModule()); app.addModule(new PoilAuModule());
app.addModule(new FourasModule()); app.addModule(new FourasModule());
app.addModule(new TodoModule()); //app.addModule(new TodoModule());
app.addModule(new ServerModule());
return app.exec(); return app.exec();
} }

View File

@ -1,35 +0,0 @@
#ifndef PACKET_H
#define PACKET_H
enum PacketType
{
INIT_CLIENT,
DELTA_CLIENT,
INIT_SERVER
};
struct InitClientPacket
{
float x;
float y;
float angle;
float vforward;
float vangle;
};
struct DeltaClientPacket
{
float acceleration;
float turning;
};
struct InitServerPacket
{
unsigned int nbClients;
unsigned int idClient;
// InitClientPacket list = sizeof(InitClientPacket)*nbClients
// map ?
};
#endif // PACKET_H

View File

@ -1,11 +1,14 @@
#include "regismodule.h" #include "regismodule.h"
#include "message.h" #include "message.h"
RegisModule::RegisModule() : regisEnable(false) {} RegisModule::RegisModule() : regisEnable(false)
{
voc.load("../../res/vocab.txt");
}
bool RegisModule::messageHandler(Message msg) bool RegisModule::messageHandler(Message msg)
{ {
if(msg.args.contains("régis ?") && regisEnable) if(msg.args.contains("régis"))
{ {
answer = say(voc.getRandNom() + " " answer = say(voc.getRandNom() + " "
+ voc.getRandAdjectif() + " " + voc.getRandAdjectif() + " "
@ -13,14 +16,6 @@ bool RegisModule::messageHandler(Message msg)
+ voc.getRandNom() + "."); + voc.getRandNom() + ".");
return true; return true;
} }
else if(msg.args.compare("!initrégis") == 0)
{
regisEnable = true;
voc.load("../../res/vocab.txt");
setNick("il_est_con_Regis");
answer = QString("NICK %1\r\n").arg(getNick());
return true;
}
return false; return false;
} }

View File

@ -8,7 +8,6 @@
class RegisModule : public Module class RegisModule : public Module
{ {
bool regisEnable;
Vocab voc; Vocab voc;
public: public:
RegisModule(); RegisModule();

View File

@ -1,147 +0,0 @@
#include "server.h"
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
Server::Server(unsigned short port, QObject *parent) : QObject(parent)
{
worldData = simulation.getWorldData();
server = new QTcpServer();
timer = new QTimer();
connect(server, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
if(server->listen(QHostAddress::Any, port))
{
emit outMessage(QString("Listening on port %1").arg(port));
timer->start(25);
}
else
emit errMessage(QString("Can't listen on port %1 : %2").arg(port).arg(server->errorString()));
}
void Server::update()
{
// check if some clients are disconnected or invalid
for(unsigned int i=0; i<clients.size(); ++i)
{
if(!clients[i]->isOk())
{
delete clients[i];
clients[i] = clients.back();
clients.pop_back();
simulation.removeClient(i--);
}
}
// update the simulation
simulation.update();
// send wold data to clients
for(unsigned int i=0; i<clients.size(); ++i)
clients[i]->sendFull(*worldData, i);
}
Server::~Server()
{
timer->stop();
delete timer;
for(ServerClient *c : clients)
delete c;
if(server->isListening())
server->close();
server->deleteLater();
}
void Server::on_newConnection()
{
QTcpSocket* socket = server->nextPendingConnection();
ServerClient* client = new ServerClient(clients.size(), socket);
if(client->isOk())
{
simulation.addClient();
clients.push_back(client);
connect(client, SIGNAL(receivedDelta(int,DeltaClientPacket*)), this, SLOT(onClientDelta(int,DeltaClientPacket*)));
connect(client, SIGNAL(outMessage(QString)), this, SIGNAL(outMessage(QString)));
connect(client, SIGNAL(errMessage(QString)), this, SIGNAL(errMessage(QString)));
}
else
delete client;
}
void Server::onClientDelta(int id, DeltaClientPacket* delta)
{
simulation.clientInput(id, *delta);
}
ServerClient::ServerClient(int id, QTcpSocket* mySocket) :
ok(false),
myId(id),
socket(mySocket)
{
if(socket->state() == QTcpSocket::ConnectedState)
{
ok = true;
emit outMessage(QString("New player connected : %1").arg(socket->peerAddress().toString()));
}
connect(socket, SIGNAL(disconnected()), this, SLOT(on_disconnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(on_readyRead()));
}
ServerClient::~ServerClient()
{
if(socket != NULL)
{
if(socket->isOpen())
socket->close();
socket->deleteLater();
}
}
void ServerClient::on_readyRead()
{
while(socket->bytesAvailable())
{
int type;
socket->read((char*)&type, sizeof(int));
switch(type)
{
case DELTA_CLIENT:
{
DeltaClientPacket* delta = new DeltaClientPacket();
socket->read((char*)delta, sizeof(DeltaClientPacket));
emit receivedDelta(myId, delta);
}
break;
default:
emit errMessage(QString("Incorrect packet received"));
socket->close();
break;
}
}
}
void ServerClient::on_disconnected()
{
emit outMessage(QString("Player disconnected"));
disconnect(socket, SIGNAL(disconnected()));
disconnect(socket, SIGNAL(readyRead()));
ok = false;
}
void ServerClient::sendDelta()
{
// TODO
}
void ServerClient::sendFull(const std::vector<InitClientPacket> &worldData, int id)
{
myId = id;
InitServerPacket packet;
packet.idClient = id;
packet.nbClients = worldData.size();
int type = INIT_SERVER;
socket->write((char*)&type, sizeof(int));
socket->write((char*)&packet, sizeof(InitServerPacket));
for(const InitClientPacket &info : worldData)
socket->write((const char*)&info, sizeof(InitClientPacket));
socket->flush();
}

View File

@ -1,63 +0,0 @@
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <vector>
#include "packet.h"
#include "simulation.h"
class QTimer;
class QTcpServer;
class QTcpSocket;
class ServerClient;
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(unsigned short port = 20202, QObject *parent = 0);
~Server();
QTcpServer* server;
QTimer* timer;
Simulation simulation;
std::vector<InitClientPacket>* worldData;
std::vector<ServerClient*> clients;
int currentClient;
signals:
void outMessage(QString);
void errMessage(QString);
public slots:
void update();
void on_newConnection();
void onClientDelta(int, DeltaClientPacket*);
};
class ServerClient : public QObject
{
Q_OBJECT
public:
ServerClient(int id, QTcpSocket* mySocket);
~ServerClient();
bool isOk() {return ok;}
void sendDelta();
void sendFull(const std::vector<InitClientPacket> &clients, int id);
private:
bool ok;
int myId;
QTcpSocket* socket;
public slots:
void on_disconnected();
void on_readyRead();
signals:
void receivedDelta(int, DeltaClientPacket*);
void outMessage(QString);
void errMessage(QString);
};
#endif // SERVER_H

View File

@ -1,49 +0,0 @@
#include "servermodule.h"
#include "message.h"
#include "server.h"
ServerModule::ServerModule() :
server(NULL)
{
}
bool ServerModule::messageHandler(Message msg)
{
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) == 0)
{
if(msg.args.compare("!server start") == 0)
{
if(server == NULL)
{
server = new Server();
answer = QString("server started");
return true;
}
}
else if(msg.args.compare("!server stop") == 0)
{
if(server != NULL)
{
delete server;
server = NULL;
answer = QString("server stopped");
return true;
}
}
else if(msg.args.compare("!server") == 0)
{
if(server == NULL)
answer = QString("server is not running");
else
answer = QString("server is running");
return true;
}
}
return false;
}
QString ServerModule::getName()
{
return "server";
}

View File

@ -1,18 +0,0 @@
#ifndef SERVERMODULE_H
#define SERVERMODULE_H
#include "module.h"
class Server;
class ServerModule : public Module
{
Server* server;
public:
ServerModule();
virtual bool messageHandler(Message msg);
virtual QString getName();
};
#endif // SERVERMODULE_H

View File

@ -1,87 +0,0 @@
#include "simulation.h"
#include <cmath>
#include <ctime>
#include <cstdlib>
#define PI 3.1416f
#define MAX_ANGULAR_SPEED 0.05f
#define MAX_FORWARD_SPEED 1.f
#define MAX_REVERSE_SPEED 0.2f
#define MAX_ACCELERATION 0.02f
#define MAX_DECELERATION (MAX_ACCELERATION*2)
Simulation::Simulation()
{
std::srand(std::time(NULL));
}
std::vector<InitClientPacket>* Simulation::getWorldData()
{
return &worldData;
}
/**
* @brief Simulation::addClient is only called server-side
*/
void Simulation::addClient()
{
InitClientPacket info;
info.x = std::rand()%30;
info.y = std::rand()%30;
info.angle = ((float)(std::rand()%360))*PI/180.f;
info.vforward = 0;
info.vangle = 0;
worldData.push_back(info);
}
/**
* @brief Simulation::removeClient is only called server-side
*/
void Simulation::removeClient(int i)
{
worldData[i] = worldData.back();
worldData.pop_back();
}
void Simulation::clientInput(int i, const DeltaClientPacket &delta)
{
InitClientPacket *info = &(worldData[i]);
float dvforward;
// handling speed
if(fabs(delta.acceleration) > 0.01f) // if accelerating
{
float max = delta.acceleration * info->vforward > 0 ? MAX_ACCELERATION : MAX_DECELERATION;
if(fabs(delta.acceleration) < 1)
dvforward = delta.acceleration*max;
else
dvforward = delta.acceleration > 0 ? max : -max;
}
else // not accelerating
dvforward = -info->vforward/8;
info->vforward += dvforward;
if(info->vforward>MAX_FORWARD_SPEED)
info->vforward = MAX_FORWARD_SPEED;
if(info->vforward<-MAX_REVERSE_SPEED)
info->vforward = -MAX_REVERSE_SPEED;
// handling angle
info->vangle = delta.turning;
if(abs(info->vangle) > 0.01f)
{
float x = 2*PI*fabs(info->vforward)/MAX_FORWARD_SPEED;
float maxAngularSpeed = (x/5 + 1 + cos(PI + x))*MAX_ANGULAR_SPEED;
if(info->vangle > maxAngularSpeed)
info->vangle = maxAngularSpeed;
if(info->vangle < -maxAngularSpeed)
info->vangle = -maxAngularSpeed;
}
}
void Simulation::update()
{
for(InitClientPacket &info : worldData)
{
info.angle += info.vangle;
info.x += std::cos(info.angle)*info.vforward;
info.y += std::sin(info.angle)*info.vforward;
}
}

View File

@ -1,20 +0,0 @@
#ifndef SIMULATION_H
#define SIMULATION_H
#include "packet.h"
#include <vector>
#include <cstddef>
class Simulation
{
std::vector<InitClientPacket> worldData;
public:
Simulation();
std::vector<InitClientPacket>* getWorldData();
void addClient();
void removeClient(int i);
void clientInput(int i, const DeltaClientPacket &delta);
void update();
};
#endif // SIMULATION_H