QTfrontend/game.cpp
changeset 7687 c73fd8cfa7c0
parent 7267 710f3ced8934
parent 7665 e556b44f5bd8
child 7857 2bc61f8841a1
equal deleted inserted replaced
7613:ce6ead3327b2 7687:c73fd8cfa7c0
    19 #include <QString>
    19 #include <QString>
    20 #include <QByteArray>
    20 #include <QByteArray>
    21 #include <QUuid>
    21 #include <QUuid>
    22 #include <QColor>
    22 #include <QColor>
    23 #include <QStringListModel>
    23 #include <QStringListModel>
       
    24 #include <QMessageBox>
    24 #include <QTextStream>
    25 #include <QTextStream>
    25 
    26 
    26 #include "game.h"
    27 #include "game.h"
    27 #include "hwconsts.h"
    28 #include "hwconsts.h"
    28 #include "gameuiconfig.h"
    29 #include "gameuiconfig.h"
    29 #include "gamecfgwidget.h"
    30 #include "gamecfgwidget.h"
    30 #include "teamselect.h"
    31 #include "teamselect.h"
    31 #include "proto.h"
    32 #include "proto.h"
       
    33 #include "campaign.h"
       
    34 
       
    35 #include <QTextStream>
    32 #include "ThemeModel.h"
    36 #include "ThemeModel.h"
    33 
    37 
    34 QString training, campaign; // TODO: Cleaner solution?
    38 QString training, campaign, campaignScript, campaignTeam; // TODO: Cleaner solution?
    35 
    39 
    36 HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, QString ammo, TeamSelWidget* pTeamSelWidget) :
    40 HWGame::HWGame(GameUIConfig * config, GameCFGWidget * gamecfg, QString ammo, TeamSelWidget* pTeamSelWidget) :
    37     TCPBase(true),
    41     TCPBase(true),
    38     ammostr(ammo),
    42     ammostr(ammo),
    39     m_pTeamSelWidget(pTeamSelWidget)
    43     m_pTeamSelWidget(pTeamSelWidget)
    50 
    54 
    51 void HWGame::onClientDisconnect()
    55 void HWGame::onClientDisconnect()
    52 {
    56 {
    53     switch (gameType)
    57     switch (gameType)
    54     {
    58     {
    55         case gtSave:
    59         case gtDemo:
       
    60             // for video recording we need demo anyway 
       
    61             emit HaveRecord(rtNeither, demo);
       
    62             break;
       
    63         case gtNet:
       
    64             emit HaveRecord(rtDemo, demo);
       
    65             break;
       
    66         default:
    56             if (gameState == gsInterrupted || gameState == gsHalted)
    67             if (gameState == gsInterrupted || gameState == gsHalted)
    57                 emit HaveRecord(false, demo);
    68                 emit HaveRecord(rtSave, demo);
    58             else if (gameState == gsFinished)
    69             else if (gameState == gsFinished)
    59                 emit HaveRecord(true, demo);
    70                 emit HaveRecord(rtDemo, demo);
    60             break;
    71             else
    61         case gtDemo:
    72                 emit HaveRecord(rtNeither, demo);
    62             break;
       
    63         case gtNet:
       
    64             emit HaveRecord(true, demo);
       
    65             break;
       
    66         default:
       
    67             if (gameState == gsInterrupted || gameState == gsHalted) emit HaveRecord(false, demo);
       
    68             else if (gameState == gsFinished) emit HaveRecord(true, demo);
       
    69     }
    73     }
    70     SetGameState(gsStopped);
    74     SetGameState(gsStopped);
    71 }
    75 }
    72 
    76 
    73 void HWGame::commonConfig()
    77 void HWGame::commonConfig()
   162 {
   166 {
   163     QByteArray campaigncfg;
   167     QByteArray campaigncfg;
   164     HWProto::addStringToBuffer(campaigncfg, "TL");
   168     HWProto::addStringToBuffer(campaigncfg, "TL");
   165     HWProto::addStringToBuffer(campaigncfg, "eseed " + QUuid::createUuid().toString());
   169     HWProto::addStringToBuffer(campaigncfg, "eseed " + QUuid::createUuid().toString());
   166 
   170 
   167     HWProto::addStringToBuffer(campaigncfg, "escript " + campaign);
   171     HWProto::addStringToBuffer(campaigncfg, "escript " + campaignScript);
   168 
   172 
   169     RawSendIPC(campaigncfg);
   173     RawSendIPC(campaigncfg);
   170 }
   174 }
   171 
   175 
   172 void HWGame::SendNetConfig()
   176 void HWGame::SendNetConfig()
   257         case 'b':
   261         case 'b':
   258         {
   262         {
   259             int size = msg.size();
   263             int size = msg.size();
   260             QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4));
   264             QString msgbody = QString::fromUtf8(msg.mid(2).left(size - 4));
   261             emit SendTeamMessage(msgbody);
   265             emit SendTeamMessage(msgbody);
       
   266             break;
       
   267         }
       
   268         case 'V':
       
   269         {
       
   270             if (msg.at(2) == '?')
       
   271                 sendCampaignVar(msg.right(msg.size() - 3));
       
   272             else if (msg.at(2) == '!')
       
   273                 writeCampaignVar(msg.right(msg.size() - 3));
   262             break;
   274             break;
   263         }
   275         }
   264         default:
   276         default:
   265         {
   277         {
   266             if (gameType == gtNet && !netSuspend)
   278             if (gameType == gtNet && !netSuspend)
   372     demo.clear();
   384     demo.clear();
   373     Start();
   385     Start();
   374     SetGameState(gsStarted);
   386     SetGameState(gsStarted);
   375 }
   387 }
   376 
   388 
   377 void HWGame::StartCampaign(const QString & file)
   389 void HWGame::StartCampaign(const QString & camp, const QString & campScript, const QString & campTeam)
   378 {
   390 {
   379     gameType = gtCampaign;
   391     gameType = gtCampaign;
   380     campaign = "Missions/Campaign/" + file + ".lua";
   392     campaign = camp;
       
   393     campaignScript = "Missions/Campaign/" + camp + "/" + campScript;
       
   394     campaignTeam = campTeam;
   381     demo.clear();
   395     demo.clear();
   382     Start();
   396     Start();
   383     SetGameState(gsStarted);
   397     SetGameState(gsStarted);
   384 }
   398 }
   385 
   399 
   386 void HWGame::SetGameState(GameState state)
   400 void HWGame::SetGameState(GameState state)
   387 {
   401 {
   388     gameState = state;
   402     gameState = state;
   389     emit GameStateChanged(state);
   403     emit GameStateChanged(state);
       
   404     if (gameType == gtCampaign)
       
   405     {
       
   406       emit CampStateChanged(1);
       
   407     }
   390 }
   408 }
   391 
   409 
   392 void HWGame::abort()
   410 void HWGame::abort()
   393 {
   411 {
   394     QByteArray buf;
   412     QByteArray buf;
   395     HWProto::addStringToBuffer(buf, QString("efinish"));
   413     HWProto::addStringToBuffer(buf, QString("efinish"));
   396     RawSendIPC(buf);
   414     RawSendIPC(buf);
   397 }
   415 }
       
   416 
       
   417 void HWGame::sendCampaignVar(const QByteArray &varToSend)
       
   418 {
       
   419     QString varToFind(varToSend);
       
   420     QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + campaignTeam + ".hwt", QSettings::IniFormat, 0);
       
   421     teamfile.setIniCodec("UTF-8");
       
   422     QString varValue = teamfile.value("Campaign " + campaign + "/" + varToFind, "").toString();
       
   423     QByteArray command;
       
   424     HWProto::addStringToBuffer(command, "V." + varValue);
       
   425     RawSendIPC(command);
       
   426 }
       
   427 
       
   428 void HWGame::writeCampaignVar(const QByteArray & varVal)
       
   429 {
       
   430     int i = varVal.indexOf(" ");
       
   431     if(i < 0)
       
   432         return;
       
   433 
       
   434     QString varToWrite = QString::fromUtf8(varVal.left(i));
       
   435     QString varValue = QString::fromUtf8(varVal.mid(i + 1));
       
   436 
       
   437     QSettings teamfile(cfgdir->absolutePath() + "/Teams/" + campaignTeam + ".hwt", QSettings::IniFormat, 0);
       
   438     teamfile.setIniCodec("UTF-8");
       
   439     teamfile.setValue("Campaign " + campaign + "/" + varToWrite, varValue);
       
   440 }
       
   441