QTfrontend/net/newnetclient.cpp
changeset 10695 e295995348f9
parent 10401 c31276023295
child 11046 47a8c19ecb60
equal deleted inserted replaced
10693:9819e69bc6db 10695:e295995348f9
    30 #include "roomslistmodel.h"
    30 #include "roomslistmodel.h"
    31 #include "playerslistmodel.h"
    31 #include "playerslistmodel.h"
    32 #include "servermessages.h"
    32 #include "servermessages.h"
    33 #include "HWApplication.h"
    33 #include "HWApplication.h"
    34 
    34 
    35 char delimeter='\n';
    35 char delimiter='\n';
    36 
    36 
    37 HWNewNet::HWNewNet() :
    37 HWNewNet::HWNewNet() :
    38     isChief(false),
    38     isChief(false),
    39     m_game_connected(false),
    39     m_game_connected(false),
    40     netClientState(Disconnected)
    40     netClientState(Disconnected)
    72 
    72 
    73 HWNewNet::~HWNewNet()
    73 HWNewNet::~HWNewNet()
    74 {
    74 {
    75     if (m_game_connected)
    75     if (m_game_connected)
    76     {
    76     {
    77         RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("User quit"));
    77         RawSendNet(QString("QUIT%1%2").arg(delimiter).arg("User quit"));
    78         emit disconnected(tr("User quit"));
    78         emit disconnected(tr("User quit"));
    79     }
    79     }
    80     NetSocket.flush();
    80     NetSocket.flush();
    81 }
    81 }
    82 
    82 
    89 }
    89 }
    90 
    90 
    91 void HWNewNet::Disconnect()
    91 void HWNewNet::Disconnect()
    92 {
    92 {
    93     if (m_game_connected)
    93     if (m_game_connected)
    94         RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("User quit"));
    94         RawSendNet(QString("QUIT%1%2").arg(delimiter).arg("User quit"));
    95     m_game_connected = false;
    95     m_game_connected = false;
    96 
    96 
    97     NetSocket.disconnectFromHost();
    97     NetSocket.disconnectFromHost();
    98 }
    98 }
    99 
    99 
   106     }
   106     }
   107 
   107 
   108     myroom = room;
   108     myroom = room;
   109 
   109 
   110     if(password.isEmpty())
   110     if(password.isEmpty())
   111         RawSendNet(QString("CREATE_ROOM%1%2").arg(delimeter).arg(room));
   111         RawSendNet(QString("CREATE_ROOM%1%2").arg(delimiter).arg(room));
   112     else
   112     else
   113         RawSendNet(QString("CREATE_ROOM%1%2%1%3").arg(delimeter).arg(room).arg(password));
   113         RawSendNet(QString("CREATE_ROOM%1%2%1%3").arg(delimiter).arg(room).arg(password));
   114 
   114 
   115     isChief = true;
   115     isChief = true;
   116 }
   116 }
   117 
   117 
   118 void HWNewNet::JoinRoom(const QString & room, const QString &password)
   118 void HWNewNet::JoinRoom(const QString & room, const QString &password)
   124     }
   124     }
   125 
   125 
   126     myroom = room;
   126     myroom = room;
   127 
   127 
   128     if(password.isEmpty())
   128     if(password.isEmpty())
   129         RawSendNet(QString("JOIN_ROOM%1%2").arg(delimeter).arg(room));
   129         RawSendNet(QString("JOIN_ROOM%1%2").arg(delimiter).arg(room));
   130     else
   130     else
   131         RawSendNet(QString("JOIN_ROOM%1%2%1%3").arg(delimeter).arg(room).arg(password));
   131         RawSendNet(QString("JOIN_ROOM%1%2%1%3").arg(delimiter).arg(room).arg(password));
   132 
   132 
   133     isChief = false;
   133     isChief = false;
   134 }
   134 }
   135 
   135 
   136 void HWNewNet::AddTeam(const HWTeam & team)
   136 void HWNewNet::AddTeam(const HWTeam & team)
   137 {
   137 {
   138     QString cmd = QString("ADD_TEAM") + delimeter +
   138     QString cmd = QString("ADD_TEAM") + delimiter +
   139                   team.name() + delimeter +
   139                   team.name() + delimiter +
   140                   QString::number(team.color()) + delimeter +
   140                   QString::number(team.color()) + delimiter +
   141                   team.grave() + delimeter +
   141                   team.grave() + delimiter +
   142                   team.fort() + delimeter +
   142                   team.fort() + delimiter +
   143                   team.voicepack() + delimeter +
   143                   team.voicepack() + delimiter +
   144                   team.flag() + delimeter +
   144                   team.flag() + delimiter +
   145                   QString::number(team.difficulty());
   145                   QString::number(team.difficulty());
   146 
   146 
   147     for(int i = 0; i < HEDGEHOGS_PER_TEAM; ++i)
   147     for(int i = 0; i < HEDGEHOGS_PER_TEAM; ++i)
   148     {
   148     {
   149         cmd.append(delimeter);
   149         cmd.append(delimiter);
   150         cmd.append(team.hedgehog(i).Name);
   150         cmd.append(team.hedgehog(i).Name);
   151         cmd.append(delimeter);
   151         cmd.append(delimiter);
   152         cmd.append(team.hedgehog(i).Hat);
   152         cmd.append(team.hedgehog(i).Hat);
   153     }
   153     }
   154     RawSendNet(cmd);
   154     RawSendNet(cmd);
   155 }
   155 }
   156 
   156 
   157 void HWNewNet::RemoveTeam(const HWTeam & team)
   157 void HWNewNet::RemoveTeam(const HWTeam & team)
   158 {
   158 {
   159     RawSendNet(QString("REMOVE_TEAM") + delimeter + team.name());
   159     RawSendNet(QString("REMOVE_TEAM") + delimiter + team.name());
   160 }
   160 }
   161 
   161 
   162 void HWNewNet::NewNick(const QString & nick)
   162 void HWNewNet::NewNick(const QString & nick)
   163 {
   163 {
   164     RawSendNet(QString("NICK%1%2").arg(delimeter).arg(nick));
   164     RawSendNet(QString("NICK%1%2").arg(delimiter).arg(nick));
   165 }
   165 }
   166 
   166 
   167 void HWNewNet::ToggleReady()
   167 void HWNewNet::ToggleReady()
   168 {
   168 {
   169     RawSendNet(QString("TOGGLE_READY"));
   169     RawSendNet(QString("TOGGLE_READY"));
   171 
   171 
   172 void HWNewNet::SendNet(const QByteArray & buf)
   172 void HWNewNet::SendNet(const QByteArray & buf)
   173 {
   173 {
   174     QString msg = QString(buf.toBase64());
   174     QString msg = QString(buf.toBase64());
   175 
   175 
   176     RawSendNet(QString("EM%1%2").arg(delimeter).arg(msg));
   176     RawSendNet(QString("EM%1%2").arg(delimiter).arg(msg));
   177 }
   177 }
   178 
   178 
   179 void HWNewNet::RawSendNet(const QString & str)
   179 void HWNewNet::RawSendNet(const QString & str)
   180 {
   180 {
   181     RawSendNet(str.toUtf8());
   181     RawSendNet(str.toUtf8());
   290     {
   290     {
   291         if(lst.size() < 3 || lst[2].toInt() < cMinServerVersion)
   291         if(lst.size() < 3 || lst[2].toInt() < cMinServerVersion)
   292         {
   292         {
   293             // TODO: Warn user, disconnect
   293             // TODO: Warn user, disconnect
   294             qWarning() << "Server too old";
   294             qWarning() << "Server too old";
   295             RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("Server too old"));
   295             RawSendNet(QString("QUIT%1%2").arg(delimiter).arg("Server too old"));
   296             Disconnect();
   296             Disconnect();
   297             emit disconnected(tr("The server is too old. Disconnecting now."));
   297             emit disconnected(tr("The server is too old. Disconnecting now."));
   298             return;
   298             return;
   299         }
   299         }
   300 
   300 
   301         RawSendNet(QString("NICK%1%2").arg(delimeter).arg(mynick));
   301         RawSendNet(QString("NICK%1%2").arg(delimiter).arg(mynick));
   302         RawSendNet(QString("PROTO%1%2").arg(delimeter).arg(*cProtoVer));
   302         RawSendNet(QString("PROTO%1%2").arg(delimiter).arg(*cProtoVer));
   303         netClientState = Connected;
   303         netClientState = Connected;
   304         m_game_connected = true;
   304         m_game_connected = true;
   305         emit adminAccess(false);
   305         emit adminAccess(false);
   306         return;
   306         return;
   307     }
   307     }
   329     }
   329     }
   330 
   330 
   331     if (lst[0] == "PING")
   331     if (lst[0] == "PING")
   332     {
   332     {
   333         if (lst.size() > 1)
   333         if (lst.size() > 1)
   334             RawSendNet(QString("PONG%1%2").arg(delimeter).arg(lst[1]));
   334             RawSendNet(QString("PONG%1%2").arg(delimiter).arg(lst[1]));
   335         else
   335         else
   336             RawSendNet(QString("PONG"));
   336             RawSendNet(QString("PONG"));
   337         return;
   337         return;
   338     }
   338     }
   339 
   339 
   871 
   871 
   872 void HWNewNet::onHedgehogsNumChanged(const HWTeam& team)
   872 void HWNewNet::onHedgehogsNumChanged(const HWTeam& team)
   873 {
   873 {
   874     if (isChief)
   874     if (isChief)
   875         RawSendNet(QString("HH_NUM%1%2%1%3")
   875         RawSendNet(QString("HH_NUM%1%2%1%3")
   876                    .arg(delimeter)
   876                    .arg(delimiter)
   877                    .arg(team.name())
   877                    .arg(team.name())
   878                    .arg(team.numHedgehogs()));
   878                    .arg(team.numHedgehogs()));
   879 }
   879 }
   880 
   880 
   881 void HWNewNet::onTeamColorChanged(const HWTeam& team)
   881 void HWNewNet::onTeamColorChanged(const HWTeam& team)
   882 {
   882 {
   883     if (isChief)
   883     if (isChief)
   884         RawSendNet(QString("TEAM_COLOR%1%2%1%3")
   884         RawSendNet(QString("TEAM_COLOR%1%2%1%3")
   885                    .arg(delimeter)
   885                    .arg(delimiter)
   886                    .arg(team.name())
   886                    .arg(team.name())
   887                    .arg(team.color()));
   887                    .arg(team.color()));
   888 }
   888 }
   889 
   889 
   890 void HWNewNet::onParamChanged(const QString & param, const QStringList & value)
   890 void HWNewNet::onParamChanged(const QString & param, const QStringList & value)
   891 {
   891 {
   892     if (isChief)
   892     if (isChief)
   893         RawSendNet(
   893         RawSendNet(
   894             QString("CFG%1%2%1%3")
   894             QString("CFG%1%2%1%3")
   895             .arg(delimeter)
   895             .arg(delimiter)
   896             .arg(param)
   896             .arg(param)
   897             .arg(value.join(QString(delimeter)))
   897             .arg(value.join(QString(delimiter)))
   898         );
   898         );
   899 }
   899 }
   900 
   900 
   901 void HWNewNet::chatLineToNetWithEcho(const QString& str)
   901 void HWNewNet::chatLineToNetWithEcho(const QString& str)
   902 {
   902 {
   909 
   909 
   910 void HWNewNet::chatLineToNet(const QString& str)
   910 void HWNewNet::chatLineToNet(const QString& str)
   911 {
   911 {
   912     if(str != "")
   912     if(str != "")
   913     {
   913     {
   914         RawSendNet(QString("CHAT") + delimeter + str);
   914         RawSendNet(QString("CHAT") + delimiter + str);
   915         QString action = HWProto::chatStringToAction(str);
   915         QString action = HWProto::chatStringToAction(str);
   916         if (action != NULL)
   916         if (action != NULL)
   917             emit(roomChatAction(mynick, action));
   917             emit(roomChatAction(mynick, action));
   918         else
   918         else
   919             emit(roomChatMessage(mynick, str));
   919             emit(roomChatMessage(mynick, str));
   922 
   922 
   923 void HWNewNet::chatLineToLobby(const QString& str)
   923 void HWNewNet::chatLineToLobby(const QString& str)
   924 {
   924 {
   925     if(str != "")
   925     if(str != "")
   926     {
   926     {
   927         RawSendNet(QString("CHAT") + delimeter + str);
   927         RawSendNet(QString("CHAT") + delimiter + str);
   928         QString action = HWProto::chatStringToAction(str);
   928         QString action = HWProto::chatStringToAction(str);
   929         if (action != NULL)
   929         if (action != NULL)
   930             emit(lobbyChatAction(mynick, action));
   930             emit(lobbyChatAction(mynick, action));
   931         else
   931         else
   932             emit(lobbyChatMessage(mynick, str));
   932             emit(lobbyChatMessage(mynick, str));
   933     }
   933     }
   934 }
   934 }
   935 
   935 
   936 void HWNewNet::SendTeamMessage(const QString& str)
   936 void HWNewNet::SendTeamMessage(const QString& str)
   937 {
   937 {
   938     RawSendNet(QString("TEAMCHAT") + delimeter + str);
   938     RawSendNet(QString("TEAMCHAT") + delimiter + str);
   939 }
   939 }
   940 
   940 
   941 void HWNewNet::askRoomsList()
   941 void HWNewNet::askRoomsList()
   942 {
   942 {
   943     if(netClientState != InLobby)
   943     if(netClientState != InLobby)
   976 void HWNewNet::gameFinished(bool correctly)
   976 void HWNewNet::gameFinished(bool correctly)
   977 {
   977 {
   978     if (netClientState == InGame)
   978     if (netClientState == InGame)
   979     {
   979     {
   980         netClientState = InRoom;
   980         netClientState = InRoom;
   981         RawSendNet(QString("ROUNDFINISHED%1%2").arg(delimeter).arg(correctly ? "1" : "0"));
   981         RawSendNet(QString("ROUNDFINISHED%1%2").arg(delimiter).arg(correctly ? "1" : "0"));
   982     }
   982     }
   983 }
   983 }
   984 
   984 
   985 void HWNewNet::banPlayer(const QString & nick)
   985 void HWNewNet::banPlayer(const QString & nick)
   986 {
   986 {
   987     RawSendNet(QString("BAN%1%2").arg(delimeter).arg(nick));
   987     RawSendNet(QString("BAN%1%2").arg(delimiter).arg(nick));
   988 }
   988 }
   989 
   989 
   990 void HWNewNet::banIP(const QString & ip, const QString & reason, int seconds)
   990 void HWNewNet::banIP(const QString & ip, const QString & reason, int seconds)
   991 {
   991 {
   992     RawSendNet(QString("BANIP%1%2%1%3%1%4").arg(delimeter).arg(ip).arg(reason).arg(seconds));
   992     RawSendNet(QString("BANIP%1%2%1%3%1%4").arg(delimiter).arg(ip).arg(reason).arg(seconds));
   993 }
   993 }
   994 
   994 
   995 void HWNewNet::banNick(const QString & nick, const QString & reason, int seconds)
   995 void HWNewNet::banNick(const QString & nick, const QString & reason, int seconds)
   996 {
   996 {
   997     RawSendNet(QString("BANNICK%1%2%1%3%1%4").arg(delimeter).arg(nick).arg(reason).arg(seconds));
   997     RawSendNet(QString("BANNICK%1%2%1%3%1%4").arg(delimiter).arg(nick).arg(reason).arg(seconds));
   998 }
   998 }
   999 
   999 
  1000 void HWNewNet::getBanList()
  1000 void HWNewNet::getBanList()
  1001 {
  1001 {
  1002     RawSendNet(QByteArray("BANLIST"));
  1002     RawSendNet(QByteArray("BANLIST"));
  1003 }
  1003 }
  1004 
  1004 
  1005 void HWNewNet::removeBan(const QString & b)
  1005 void HWNewNet::removeBan(const QString & b)
  1006 {
  1006 {
  1007     RawSendNet(QString("UNBAN%1%2").arg(delimeter).arg(b));
  1007     RawSendNet(QString("UNBAN%1%2").arg(delimiter).arg(b));
  1008 }
  1008 }
  1009 
  1009 
  1010 void HWNewNet::kickPlayer(const QString & nick)
  1010 void HWNewNet::kickPlayer(const QString & nick)
  1011 {
  1011 {
  1012     RawSendNet(QString("KICK%1%2").arg(delimeter).arg(nick));
  1012     RawSendNet(QString("KICK%1%2").arg(delimiter).arg(nick));
  1013 }
  1013 }
  1014 
  1014 
  1015 void HWNewNet::infoPlayer(const QString & nick)
  1015 void HWNewNet::infoPlayer(const QString & nick)
  1016 {
  1016 {
  1017     RawSendNet(QString("INFO%1%2").arg(delimeter).arg(nick));
  1017     RawSendNet(QString("INFO%1%2").arg(delimiter).arg(nick));
  1018 }
  1018 }
  1019 
  1019 
  1020 void HWNewNet::followPlayer(const QString & nick)
  1020 void HWNewNet::followPlayer(const QString & nick)
  1021 {
  1021 {
  1022     if (!isInRoom())
  1022     if (!isInRoom())
  1023     {
  1023     {
  1024         RawSendNet(QString("FOLLOW%1%2").arg(delimeter).arg(nick));
  1024         RawSendNet(QString("FOLLOW%1%2").arg(delimiter).arg(nick));
  1025         isChief = false;
  1025         isChief = false;
  1026     }
  1026     }
  1027 }
  1027 }
  1028 
  1028 
  1029 void HWNewNet::consoleCommand(const QString & cmd)
  1029 void HWNewNet::consoleCommand(const QString & cmd)
  1030 {
  1030 {
  1031     RawSendNet(QString("CMD%1%2").arg(delimeter).arg(cmd));
  1031     RawSendNet(QString("CMD%1%2").arg(delimiter).arg(cmd));
  1032 }
  1032 }
  1033 
  1033 
  1034 bool HWNewNet::allPlayersReady()
  1034 bool HWNewNet::allPlayersReady()
  1035 {
  1035 {
  1036     int ready = 0;
  1036     int ready = 0;
  1045     RawSendNet(QString("START_GAME"));
  1045     RawSendNet(QString("START_GAME"));
  1046 }
  1046 }
  1047 
  1047 
  1048 void HWNewNet::updateRoomName(const QString & name)
  1048 void HWNewNet::updateRoomName(const QString & name)
  1049 {
  1049 {
  1050     RawSendNet(QString("ROOM_NAME%1%2").arg(delimeter).arg(name));
  1050     RawSendNet(QString("ROOM_NAME%1%2").arg(delimiter).arg(name));
  1051 }
  1051 }
  1052 
  1052 
  1053 
  1053 
  1054 void HWNewNet::toggleRestrictJoins()
  1054 void HWNewNet::toggleRestrictJoins()
  1055 {
  1055 {
  1083     return netClientState >= InRoom;
  1083     return netClientState >= InRoom;
  1084 }
  1084 }
  1085 
  1085 
  1086 void HWNewNet::setServerMessageNew(const QString & msg)
  1086 void HWNewNet::setServerMessageNew(const QString & msg)
  1087 {
  1087 {
  1088     RawSendNet(QString("SET_SERVER_VAR%1MOTD_NEW%1%2").arg(delimeter).arg(msg));
  1088     RawSendNet(QString("SET_SERVER_VAR%1MOTD_NEW%1%2").arg(delimiter).arg(msg));
  1089 }
  1089 }
  1090 
  1090 
  1091 void HWNewNet::setServerMessageOld(const QString & msg)
  1091 void HWNewNet::setServerMessageOld(const QString & msg)
  1092 {
  1092 {
  1093     RawSendNet(QString("SET_SERVER_VAR%1MOTD_OLD%1%2").arg(delimeter).arg(msg));
  1093     RawSendNet(QString("SET_SERVER_VAR%1MOTD_OLD%1%2").arg(delimiter).arg(msg));
  1094 }
  1094 }
  1095 
  1095 
  1096 void HWNewNet::setLatestProtocolVar(int proto)
  1096 void HWNewNet::setLatestProtocolVar(int proto)
  1097 {
  1097 {
  1098     RawSendNet(QString("SET_SERVER_VAR%1LATEST_PROTO%1%2").arg(delimeter).arg(proto));
  1098     RawSendNet(QString("SET_SERVER_VAR%1LATEST_PROTO%1%2").arg(delimiter).arg(proto));
  1099 }
  1099 }
  1100 
  1100 
  1101 void HWNewNet::askServerVars()
  1101 void HWNewNet::askServerVars()
  1102 {
  1102 {
  1103     RawSendNet(QString("GET_SERVER_VAR"));
  1103     RawSendNet(QString("GET_SERVER_VAR"));
  1163                 .append(m_passwordHash)
  1163                 .append(m_passwordHash)
  1164                 .append(cProtoVer->toAscii())
  1164                 .append(cProtoVer->toAscii())
  1165                 .append("!hedgewars")
  1165                 .append("!hedgewars")
  1166                 , QCryptographicHash::Sha1).toHex();
  1166                 , QCryptographicHash::Sha1).toHex();
  1167 
  1167 
  1168     RawSendNet(QString("PASSWORD%1%2%1%3").arg(delimeter).arg(hash).arg(m_clientSalt));
  1168     RawSendNet(QString("PASSWORD%1%2%1%3").arg(delimiter).arg(hash).arg(m_clientSalt));
  1169 }
  1169 }