Browse Source

Added tags to RenX.Announcements; Plugin now signals an unload when init() fails during OnRehash() or load().

pull/3/head
JustinAJ 10 years ago
parent
commit
35bd641e0a
  1. 65
      RenX.Announcements/RenX_Announcements.cpp
  2. 24
      RenX.Announcements/RenX_Announcements.h

65
RenX.Announcements/RenX_Announcements.cpp

@ -23,42 +23,70 @@
RenX_AnnouncementsPlugin pluginInstance; RenX_AnnouncementsPlugin pluginInstance;
void announce(unsigned int) void announce_(unsigned int x)
{ {
if (pluginInstance.random == false) pluginInstance.announce(x);
}
void RenX_AnnouncementsPlugin::announce(unsigned int)
{
if (RenX_AnnouncementsPlugin::random == false)
{ {
pluginInstance.lastLine++; RenX_AnnouncementsPlugin::lastLine++;
if (pluginInstance.lastLine == pluginInstance.announcementsFile.getLineCount()) pluginInstance.lastLine = 0; if (RenX_AnnouncementsPlugin::lastLine == RenX_AnnouncementsPlugin::announcementsFile.getLineCount()) RenX_AnnouncementsPlugin::lastLine = 0;
} }
else else
{ {
unsigned int trand; unsigned int trand;
do trand = rand() % pluginInstance.announcementsFile.getLineCount(); do trand = rand() % RenX_AnnouncementsPlugin::announcementsFile.getLineCount();
while (trand == pluginInstance.lastLine); while (trand == RenX_AnnouncementsPlugin::lastLine);
pluginInstance.lastLine = trand; RenX_AnnouncementsPlugin::lastLine = trand;
} }
const Jupiter::ReadableString &announcement = pluginInstance.announcementsFile.getLine(pluginInstance.lastLine); Jupiter::StringS announcement = RenX_AnnouncementsPlugin::announcementsFile.getLine(RenX_AnnouncementsPlugin::lastLine);
announcement.replace(RenX_AnnouncementsPlugin::dateTag, Jupiter::ReferenceString(getTimeFormat(RenX_AnnouncementsPlugin::dateFmt.c_str())));
announcement.replace(RenX_AnnouncementsPlugin::timeTag, Jupiter::ReferenceString(getTimeFormat(RenX_AnnouncementsPlugin::timeFmt.c_str())));
Jupiter::String msg;
RenX::Core *core = RenX::getCore(); RenX::Core *core = RenX::getCore();
RenX::Server *server; RenX::Server *server;
for (unsigned int i = 0; i != RenX::getCore()->getServerCount(); i++) for (unsigned int i = 0; i != RenX::getCore()->getServerCount(); i++)
{ {
server = core->getServer(i); server = core->getServer(i);
if (server->players.size() != 0) if (server->players.size() != 0)
server->sendMessage(announcement); {
msg = announcement;
msg.replace(RenX_AnnouncementsPlugin::rulesTag, server->getRules());
server->sendMessage(msg);
} }
}
}
int RenX_AnnouncementsPlugin::OnRehash()
{
RenX_AnnouncementsPlugin::announcementsFile.unload();
return RenX_AnnouncementsPlugin::init();
} }
RenX_AnnouncementsPlugin::RenX_AnnouncementsPlugin() int RenX_AnnouncementsPlugin::init()
{ {
RenX_AnnouncementsPlugin::dateTag = Jupiter::IRC::Client::Config->get(this->getName(), STRING_LITERAL_AS_REFERENCE("DateTag"), STRING_LITERAL_AS_REFERENCE("{DATE}"));
RenX_AnnouncementsPlugin::timeTag = Jupiter::IRC::Client::Config->get(this->getName(), STRING_LITERAL_AS_REFERENCE("TimeTag"), STRING_LITERAL_AS_REFERENCE("{TIME}"));
RenX_AnnouncementsPlugin::rulesTag = Jupiter::IRC::Client::Config->get(this->getName(), STRING_LITERAL_AS_REFERENCE("RulesTag"), STRING_LITERAL_AS_REFERENCE("{RULES}"));;
RenX_AnnouncementsPlugin::dateFmt = Jupiter::IRC::Client::Config->get(this->getName(), STRING_LITERAL_AS_REFERENCE("DateFormat"), STRING_LITERAL_AS_REFERENCE("%A, %B %d, %Y"));
RenX_AnnouncementsPlugin::timeFmt = Jupiter::IRC::Client::Config->get(this->getName(), STRING_LITERAL_AS_REFERENCE("TimeFormat"), STRING_LITERAL_AS_REFERENCE("%H:%M:%S"));
RenX_AnnouncementsPlugin::random = Jupiter::IRC::Client::Config->getBool(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Random"));
RenX_AnnouncementsPlugin::announcementsFile.load(Jupiter::IRC::Client::Config->get(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("File"), STRING_LITERAL_AS_REFERENCE("Announcements.txt"))); RenX_AnnouncementsPlugin::announcementsFile.load(Jupiter::IRC::Client::Config->get(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("File"), STRING_LITERAL_AS_REFERENCE("Announcements.txt")));
if (RenX_AnnouncementsPlugin::announcementsFile.getLineCount() != 0) if (RenX_AnnouncementsPlugin::announcementsFile.getLineCount() == 0)
{ {
RenX_AnnouncementsPlugin::random = Jupiter::IRC::Client::Config->getBool(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Random")); fputs("[RenX.Announcements] ERROR: No announcements loaded." ENDL, stderr);
time_t delay = Jupiter::IRC::Client::Config->getInt(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Delay"), 60); return -1;
RenX_AnnouncementsPlugin::timer = new Jupiter::Timer(0, delay, announce);
if (RenX_AnnouncementsPlugin::random == false) RenX_AnnouncementsPlugin::lastLine = RenX_AnnouncementsPlugin::announcementsFile.getLineCount() - 1;
} }
else fputs("[RenX.Announcements] ERROR: No announcements loaded." ENDL, stderr); time_t delay = Jupiter::IRC::Client::Config->getInt(STRING_LITERAL_AS_REFERENCE("RenX.Announcements"), STRING_LITERAL_AS_REFERENCE("Delay"), 60);
RenX_AnnouncementsPlugin::timer = new Jupiter::Timer(0, delay, announce_);
if (RenX_AnnouncementsPlugin::random == false)
RenX_AnnouncementsPlugin::lastLine = RenX_AnnouncementsPlugin::announcementsFile.getLineCount() - 1;
return 0;
} }
RenX_AnnouncementsPlugin::~RenX_AnnouncementsPlugin() RenX_AnnouncementsPlugin::~RenX_AnnouncementsPlugin()
@ -67,6 +95,11 @@ RenX_AnnouncementsPlugin::~RenX_AnnouncementsPlugin()
RenX_AnnouncementsPlugin::announcementsFile.unload(); RenX_AnnouncementsPlugin::announcementsFile.unload();
} }
extern "C" __declspec(dllexport) bool load()
{
return pluginInstance.init() == 0;
}
extern "C" __declspec(dllexport) Jupiter::Plugin *getPlugin() extern "C" __declspec(dllexport) Jupiter::Plugin *getPlugin()
{ {
return &pluginInstance; return &pluginInstance;

24
RenX.Announcements/RenX_Announcements.h

@ -21,23 +21,35 @@
#include "Jupiter/Plugin.h" #include "Jupiter/Plugin.h"
#include "Jupiter/Timer.h" #include "Jupiter/Timer.h"
#include "Jupiter/File.h" #include "Jupiter/File.h"
#include "Jupiter/String.h"
#include "Jupiter/CString.h"
#include "Jupiter/Reference_String.h"
#include "RenX_Plugin.h" #include "RenX_Plugin.h"
class RenX_AnnouncementsPlugin : public RenX::Plugin class RenX_AnnouncementsPlugin : public RenX::Plugin
{ {
public: public:
void announce(unsigned int);
int init();
public: // Jupiter::Plugin
int OnRehash();
//int think();
const Jupiter::ReadableString &getName() override { return name; } const Jupiter::ReadableString &getName() override { return name; }
RenX_AnnouncementsPlugin();
~RenX_AnnouncementsPlugin(); ~RenX_AnnouncementsPlugin();
public:
Jupiter::File announcementsFile;
bool random;
unsigned int lastLine;
private: private:
STRING_LITERAL_AS_NAMED_REFERENCE(name, "RenX.Announcements"); STRING_LITERAL_AS_NAMED_REFERENCE(name, "RenX.Announcements");
bool random;
unsigned int lastLine;
Jupiter::Timer *timer; Jupiter::Timer *timer;
Jupiter::StringS dateTag;
Jupiter::StringS timeTag;
Jupiter::StringS rulesTag;
//Jupiter::StringS modsTag;
Jupiter::CStringS dateFmt;
Jupiter::CStringS timeFmt;
Jupiter::File announcementsFile;
}; };
#endif // _RENX_ANNOUNCEMENTS_H_HEADER #endif // _RENX_ANNOUNCEMENTS_H_HEADER
Loading…
Cancel
Save