Advertisement
yeah568

Untitled

Sep 21st, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.98 KB | None | 0 0
  1. #include "receiveChatMessage.h"
  2. #include "player.h"
  3. #include "serialize.h"
  4. #include "message.h"
  5. #include "widget.h"
  6. #include "log.h"
  7. #include "scene.h"
  8.  
  9. void receiveChatMessage(QByteArray msg, Player* player)
  10. {
  11.     QString txt = dataToString(msg.mid(7));
  12.     QString author = player->pony.name;
  13.     //logMessage("Chat "+author+":"+txt);
  14.  
  15.     if (txt.startsWith("/stuck") || txt.startsWith("unstuck me"))
  16.     {
  17.         sendLoadSceneRPC(player, player->pony.sceneName);
  18.     }
  19.     else if (txt == ":anhero")
  20.     {
  21.         QTimer *anheroTimer = new QTimer();
  22.         anheroTimer->setSingleShot(true);
  23.  
  24.         QObject::connect(anheroTimer, &QTimer::timeout, [=]() {
  25.             sendSetStatRPC(player, 1, player->pony.health);
  26.             Scene* scene = findScene(player->pony.sceneName);
  27.             for (Player* other : scene->players)
  28.                 sendNetviewInstantiate(&player->pony, other);
  29.             player->pony.dead = false;
  30.             delete anheroTimer;
  31.           } );
  32.         if (!player->pony.dead)
  33.         {
  34.             player->pony.dead = true;
  35.             sendSetStatRPC(player, 1, 0);
  36.             anheroTimer->start(5000);
  37.             Scene* scene = findScene(player->pony.sceneName);
  38.             for (Player* other : scene->players)
  39.                 sendNetviewRemove(other, player->pony.netviewId, NetviewRemoveReasonKill);
  40.         }
  41.     }
  42.     else if (txt == ":commands")
  43.     {
  44.         sendChatMessage(player, "<span color=\"yellow\">List of Commands:</span><br /><em>:roll</em><br /><span color=\"yellow\">Rolls a random number between 00 and 99</span><br /><em>:msg player message</em><br /><span color=\"yellow\">Sends a private message to a player</span><br /><em>:names</em><br /><span color=\"yellow\">Lists all players on the server</span><br /><em>:me action</em><br /><span color=\"yellow\">States your current action</span><br /><em>:tp location</em><br /><span color=\"yellow\">Teleports your pony to the specified region</span>", "[Server]", ChatLocal);
  45.     }
  46.     else if (txt.startsWith(":msg"))
  47.     {
  48.         if(txt.count(" ") < 2)
  49.             sendChatMessage(player, ":msg<br /><span color=\"yellow\">Usage:</span><br /><em>:msg player message</em><br /><span color=\"yellow\">Player names are case-insensitive, ignore spaces and you do not need to type out their full name.</span>", author, ChatLocal);
  50.         else
  51.         {
  52.             for (int i=0; i<Player::udpPlayers.size(); i++)
  53.             {
  54.                 if (Player::udpPlayers[i]->inGame>=2 && Player::udpPlayers[i]->pony.name.toLower().remove(" ")
  55.                         .startsWith(txt.toLower().section(" ", 1, 1)))
  56.                 {
  57.                     txt = txt.remove(0, txt.indexOf(" ", 5) + 1);
  58.                     sendChatMessage(Player::udpPlayers[i], "<span color=\"yellow\">[PM] </span>" + txt, author, ChatLocal);
  59.                     sendChatMessage(player, "<span color=\"yellow\">[PM to "
  60.                                     + Player::udpPlayers[i]->pony.name + "] </span>" + txt, author, ChatLocal);
  61.                 }
  62.             }
  63.         }
  64.     }
  65.     else if (txt.startsWith(":names"))
  66.     {
  67.         QString namesmsg = "<span color=\"yellow\">Players currently in game:</span>";
  68.  
  69.         for (int i=0; i<Player::udpPlayers.size(); i++)
  70.             if (Player::udpPlayers[i]->inGame>=2)
  71.                 namesmsg += "<br />#b" + Player::udpPlayers[i]->pony.name
  72.                         + "#b<br /><span color=\"yellow\"> - in "
  73.                         + Player::udpPlayers[i]->pony.sceneName + "</span>";
  74.  
  75.         sendChatMessage(player, namesmsg, "[Server]", ChatLocal);
  76.     }
  77.     else if (txt.startsWith(":tp"))
  78.     {
  79.         if (txt.count(" ") < 1)
  80.         {
  81.           QString msgtosend = ":tp<br /><span color=\"yellow\">Usage:</span><br /><em>:tp location</em><br /><span color=\"yellow\">Available locations:</span><em>";
  82.  
  83.             for (int i=0; i<Scene::scenes.size(); i++)
  84.                 msgtosend += "<br />" + Scene::scenes[i].name;
  85.  
  86.             sendChatMessage(player, msgtosend + "</em>", author, ChatLocal);
  87.         }
  88.  
  89.         else
  90.             sendLoadSceneRPC(player, txt.remove(0, 4));
  91.     }
  92.     else if (txt == ":me")
  93.     {
  94.         sendChatMessage(player, ":me<br /><span color=\"yellow\">Usage:</span><br /><em>:me action</em>", author, ChatLocal);
  95.     }
  96.     else // Broadcast the message
  97.     {
  98.         int rollnum = -1;
  99.         QString rollstr;
  100.         bool actmsg = false;
  101.  
  102.         if (txt == ":roll")
  103.         {
  104.             if (player->chatRollCooldownEnd < QDateTime::currentDateTime())
  105.             {
  106.                 rollnum = qrand() % 100;
  107.                 rollstr.sprintf("<span color=\"yellow\">#b%s#b rolls %02d</span>", author.toLocal8Bit().data(), rollnum);
  108.                 player->chatRollCooldownEnd = QDateTime::currentDateTime().addSecs(10);
  109.             }
  110.         }
  111.         else if (txt.startsWith(":roll "))
  112.         {
  113.             if (player->chatRollCooldownEnd < QDateTime::currentDateTime())
  114.             {
  115.                 txt.remove(0, 5);
  116.  
  117.                 int sides;
  118.                 if (txt == "d20")
  119.                 {
  120.                     sides = 20;
  121.                 }
  122.                 else if (txt == "d12")
  123.                 {
  124.                     sides = 12;
  125.                 }
  126.                 else if (txt == "d8")
  127.                 {
  128.                     sides = 8;
  129.                 }
  130.                 else if (txt == "d6")
  131.                 {
  132.                     sides = 6;
  133.                 }
  134.                 else if (txt == "d4")
  135.                 {
  136.                     sides = 4;
  137.                 }
  138.                 else if (txt == "d2")
  139.                 {
  140.                     sides = 2;
  141.                 }
  142.  
  143.                 rollnum = qrand() % sides;
  144.                 rollstr.sprintf("<span color=\"yellow\">#b%s#b rolls %02d out of %d</span>", author.toLocal8Bit().data(), rollnu, sides);
  145.                 player->chatRollCooldownEnd = QDateTime::currentDateTime().addSecs(10);
  146.             }
  147.         }
  148.         if (txt.startsWith(":me "))
  149.         {
  150.             actmsg = true;
  151.             txt.remove(0, 3);
  152.             txt = "<em>#b* " + author + "#b" + txt + "</em>";
  153.         }
  154.         if ((quint8)msg[6] == 8) // Local chat only
  155.         {
  156.             Scene* scene = findScene(player->pony.sceneName);
  157.             if (scene->name.isEmpty())
  158.                 logMessage(QObject::tr("UDP: Can't find the scene for chat message, aborting"));
  159.             else
  160.             {
  161.                 for (int i=0; i<scene->players.size(); i++)
  162.                 {
  163.                     if (scene->players[i]->inGame>=2)
  164.                     {
  165.                         if (rollnum > -1)
  166.                             sendChatMessage(scene->players[i], rollstr, "[Server]", ChatLocal);
  167.                         else if (actmsg)
  168.                             sendChatMessage(scene->players[i], txt, "", ChatLocal);
  169.                         else if (txt.startsWith(">"))
  170.                             sendChatMessage(scene->players[i], "<span color=\"green\">" + txt + "</span>", author, ChatLocal);
  171.                         else
  172.                             sendChatMessage(scene->players[i], txt, author, ChatLocal);
  173.                     }
  174.                 }
  175.             }
  176.         }
  177.         else // Send globally
  178.         {
  179.             for (int i=0; i<Player::udpPlayers.size(); i++)
  180.             {
  181.                 if (Player::udpPlayers[i]->inGame>=2)
  182.                 {
  183.                     if (rollnum > -1)
  184.                         sendChatMessage(Player::udpPlayers[i], rollstr, "[Server]", ChatGeneral);
  185.                     else if (actmsg)
  186.                         sendChatMessage(Player::udpPlayers[i], txt, "", ChatGeneral);
  187.                     else if (txt.startsWith(">"))
  188.                         sendChatMessage(Player::udpPlayers[i], "<span color=\"green\">" + txt + "</span>", author, ChatGeneral);
  189.                     else
  190.                         sendChatMessage(Player::udpPlayers[i], txt, author, ChatGeneral);
  191.  
  192.                 }
  193.             }
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement