added mmokobanmodule

This commit is contained in:
Anselme 2017-09-25 18:06:16 +02:00
parent 9a94283309
commit 831b76c77f
4 changed files with 109 additions and 2 deletions

View File

@ -2,3 +2,4 @@ bin_dir=$$PWD/bin
lib_dir=$$PWD/lib
mmokoban_lib_dir=$$PWD/../mmokoban/lib
include_dir=$$PWD/../mmokoban/src
include_dir=$$PWD/../mmokoban/include

View File

@ -31,6 +31,7 @@ unix {
INCLUDEPATH += ../ircbot
INCLUDEPATH += $$include_dir
INCLUDEPATH += $$sfml_dir
SOURCES = main.cpp \
regismodule.cpp \
@ -43,7 +44,8 @@ SOURCES = main.cpp \
jankenmodule.cpp \
janken.cpp \
rpgmodule.cpp \
punishermodule.cpp
punishermodule.cpp \
mmokobanmodule.cpp
HEADERS += \
regismodule.h \
@ -56,4 +58,5 @@ HEADERS += \
jankenmodule.h \
janken.h \
rpgmodule.h \
punishermodule.h
punishermodule.h \
mmokobanmodule.h

78
app/mmokobanmodule.cpp Normal file
View File

@ -0,0 +1,78 @@
#include "mmokobanmodule.h"
#include "message.h"
#include "user.h"
#include <QStringList>
#include <common/mapkoban.h>
#include <common/defines.h>
#include <serverlib/server.h>
#include <thread>
MMOkobanModule::MMOkobanModule() :
m_running(false),
m_mmokoban_server(nullptr)
{
}
bool MMOkobanModule::messageHandler(Message msg)
{
if(!getUsers()->getFromNick(msg.nick)->isOp())
return false;
if(msg.args.startsWith("!mmokoban"))
{
if(msg.args.compare("!mmokoban start") == 0)
{
if(m_running)
answer = say("MMOkoban server is already running.");
else
{
start();
answer = say("MMOkoban server starting...");
}
return true;
}
else if(msg.args.compare("!mmokoban stop") == 0)
{
if(!m_running)
answer = say("MMOkoban server is already stopped.");
else
{
stop();
answer = say("MMOkoban server stopping...");
}
return true;
}
}
return false;
}
QString MMOkobanModule::getName()
{
return "mmokoban";
}
void MMOkobanModule::start()
{
m_running = true;
// launching server
m_mmokoban_server = new Server(TCP_PORT);
m_mmokoban_server->initMap();
new std::thread(startUpdateLoop, m_mmokoban_server, m_running);
}
void MMOkobanModule::startUpdateLoop(Server* server, bool &running)
{
while(running)
server->update();
delete server;
}
void MMOkobanModule::stop()
{
m_running = false;
m_mmokoban_server = nullptr;
}

25
app/mmokobanmodule.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef MMOKOBANMODULE_H
#define MMOKOBANMODULE_H
#include <module.h>
class Server;
class MMOkobanModule : public Module
{
bool m_running;
Server* m_mmokoban_server;
void start();
void stop();
static void startUpdateLoop(Server* server, bool &running);
public:
MMOkobanModule();
bool messageHandler(Message msg);
QString getName();
};
#endif // MMOKOBANMODULE_H