#include "receiveChatMessage.h"
#include "player.h"
#include "serialize.h"
#include "message.h"
#include "widget.h"
#include "log.h"
#include "scene.h"
void receiveChatMessage(QByteArray msg, Player* player)
{
QString txt = dataToString(msg.mid(7));
QString author = player->pony.name;
//logMessage("Chat "+author+":"+txt);
if (txt.startsWith("/stuck") || txt.startsWith("unstuck me"))
{
sendLoadSceneRPC(player, player->pony.sceneName);
}
else if (txt == ":anhero")
{
QTimer *anheroTimer = new QTimer();
anheroTimer->setSingleShot(true);
QObject::connect(anheroTimer, &QTimer::timeout, [=]() {
sendSetStatRPC(player, 1, player->pony.health);
Scene* scene = findScene(player->pony.sceneName);
for (Player* other : scene->players)
sendNetviewInstantiate(&player->pony, other);
player->pony.dead = false;
delete anheroTimer;
} );
if (!player->pony.dead)
{
player->pony.dead = true;
sendSetStatRPC(player, 1, 0);
anheroTimer->start(5000);
Scene* scene = findScene(player->pony.sceneName);
for (Player* other : scene->players)
sendNetviewRemove(other, player->pony.netviewId, NetviewRemoveReasonKill);
}
}
else if (txt == ":commands")
{
sendChatMessage(player, "List of Commands:
:roll
Rolls a random number between 00 and 99
:msg player message
Sends a private message to a player
:names
Lists all players on the server
:me action
States your current action
:tp location
Teleports your pony to the specified region", "[Server]", ChatLocal);
}
else if (txt.startsWith(":msg"))
{
if(txt.count(" ") < 2)
sendChatMessage(player, ":msg
Usage:
:msg player message
Player names are case-insensitive, ignore spaces and you do not need to type out their full name.", author, ChatLocal);
else
{
for (int i=0; iinGame>=2 && Player::udpPlayers[i]->pony.name.toLower().remove(" ")
.startsWith(txt.toLower().section(" ", 1, 1)))
{
txt = txt.remove(0, txt.indexOf(" ", 5) + 1);
sendChatMessage(Player::udpPlayers[i], "[PM] " + txt, author, ChatLocal);
sendChatMessage(player, "[PM to "
+ Player::udpPlayers[i]->pony.name + "] " + txt, author, ChatLocal);
}
}
}
}
else if (txt.startsWith(":names"))
{
QString namesmsg = "Players currently in game:";
for (int i=0; iinGame>=2)
namesmsg += "
#b" + Player::udpPlayers[i]->pony.name
+ "#b
- in "
+ Player::udpPlayers[i]->pony.sceneName + "";
sendChatMessage(player, namesmsg, "[Server]", ChatLocal);
}
else if (txt.startsWith(":tp"))
{
if (txt.count(" ") < 1)
{
QString msgtosend = ":tp
Usage:
:tp location
Available locations:";
for (int i=0; i" + Scene::scenes[i].name;
sendChatMessage(player, msgtosend + "", author, ChatLocal);
}
else
sendLoadSceneRPC(player, txt.remove(0, 4));
}
else if (txt == ":me")
{
sendChatMessage(player, ":me
Usage:
:me action", author, ChatLocal);
}
else // Broadcast the message
{
int rollnum = -1;
QString rollstr;
bool actmsg = false;
if (txt == ":roll")
{
if (player->chatRollCooldownEnd < QDateTime::currentDateTime())
{
rollnum = qrand() % 100;
rollstr.sprintf("#b%s#b rolls %02d", author.toLocal8Bit().data(), rollnum);
player->chatRollCooldownEnd = QDateTime::currentDateTime().addSecs(10);
}
}
else if (txt.startsWith(":roll "))
{
if (player->chatRollCooldownEnd < QDateTime::currentDateTime())
{
txt.remove(0, 5);
int sides = -1;
if (txt == "d20")
{
sides = 20;
}
else if (txt == "d12")
{
sides = 12;
}
else if (txt == "d8")
{
sides = 8;
}
else if (txt == "d6")
{
sides = 6;
}
else if (txt == "d4")
{
sides = 4;
}
else if (txt == "d2")
{
sides = 2;
}
if (sides == -1) {
sendChatMessage(player, ":roll
Usage:
:roll (d2|d4|d6|d8|d12|20)", author, ChatLocal);
} else {
rollnum = qrand() % sides;
rollstr.sprintf("#b%s#b rolls %02d out of %d", author.toLocal8Bit().data(), rollnum, sides);
player->chatRollCooldownEnd = QDateTime::currentDateTime().addSecs(10);
}
}
}
if (txt.startsWith(":me "))
{
actmsg = true;
txt.remove(0, 3);
txt = "#b* " + author + "#b" + txt + "";
}
if ((quint8)msg[6] == 8) // Local chat only
{
Scene* scene = findScene(player->pony.sceneName);
if (scene->name.isEmpty())
logMessage(QObject::tr("UDP: Can't find the scene for chat message, aborting"));
else
{
for (int i=0; iplayers.size(); i++)
{
if (scene->players[i]->inGame>=2)
{
if (rollnum > -1)
sendChatMessage(scene->players[i], rollstr, "[Server]", ChatLocal);
else if (actmsg)
sendChatMessage(scene->players[i], txt, "", ChatLocal);
else if (txt.startsWith(">"))
sendChatMessage(scene->players[i], "" + txt + "", author, ChatLocal);
else
sendChatMessage(scene->players[i], txt, author, ChatLocal);
}
}
}
}
else // Send globally
{
for (int i=0; iinGame>=2)
{
if (rollnum > -1)
sendChatMessage(Player::udpPlayers[i], rollstr, "[Server]", ChatGeneral);
else if (actmsg)
sendChatMessage(Player::udpPlayers[i], txt, "", ChatGeneral);
else if (txt.startsWith(">"))
sendChatMessage(Player::udpPlayers[i], "" + txt + "", author, ChatGeneral);
else
sendChatMessage(Player::udpPlayers[i], txt, author, ChatGeneral);
}
}
}
}
}