1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-12 02:28:11 +02:00

Correct network data exchange and time parsing

This commit is contained in:
nordsoft 2023-08-26 03:14:01 +04:00
parent 3962b71262
commit fdaf05514a

View File

@ -44,9 +44,7 @@
OptionsTab::OptionsTab() : humanPlayers(0)
{
recActions = 0;
//addCallback("timerFieldChangedBase", <#std::function<void (std::string)> callback#>)
addCallback("setTimerPreset", [&](int index){
if(!variables["timerPresets"].isNull())
{
@ -61,24 +59,43 @@ OptionsTab::OptionsTab() : humanPlayers(0)
});
auto parseTimerString = [](const std::string & str){
std::stringstream sstrm;
int a, b;
sstrm << str;
sstrm >> a;
char c = sstrm.get();
if(c == ':')
auto sc = str.find(":");
if(sc == std::string::npos)
return std::stoi(str);
auto l = str.substr(0, sc);
auto r = str.substr(sc + 1, std::string::npos);
if(r.length() == 3) //symbol added
{
sstrm >> b;
return a * 60 + b;
l.push_back(r.front());
r.erase(r.begin());
}
return -1;
else if(r.length() == 1) //symbol removed
{
r.insert(r.begin(), l.back());
l.pop_back();
}
int sec = std::stoi(r);
if(sec >= 60)
{
if(l.empty()) //9:00 -> 0:09
return sec / 10;
l.push_back(r.front()); //0:090 -> 9:00
r.erase(r.begin());
}
else if(l.empty())
return sec;
return std::stoi(l) * 60 + std::stoi(r);
};
addCallback("parseAndSetTimer_base", [parseTimerString](const std::string & str){
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo = CSH->si->turnTimerInfo;
TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
tinfo.baseTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
@ -87,7 +104,7 @@ OptionsTab::OptionsTab() : humanPlayers(0)
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo = CSH->si->turnTimerInfo;
TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
tinfo.turnTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
@ -96,7 +113,7 @@ OptionsTab::OptionsTab() : humanPlayers(0)
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo = CSH->si->turnTimerInfo;
TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
tinfo.battleTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
@ -105,7 +122,7 @@ OptionsTab::OptionsTab() : humanPlayers(0)
int time = parseTimerString(str) * 1000;
if(time >= 0)
{
TurnTimerInfo tinfo = CSH->si->turnTimerInfo;
TurnTimerInfo tinfo = SEL->getStartInfo()->turnTimerInfo;
tinfo.creatureTimer = time;
CSH->setTurnTimerInfo(tinfo);
}
@ -133,15 +150,24 @@ OptionsTab::OptionsTab() : humanPlayers(0)
if(auto * tObj = reinterpret_cast<const JsonNode *>(item))
{
for(auto wname : (*tObj)["hideWidgets"].Vector())
{
if(auto w = widget<CIntObject>(wname.String()))
{
w->setEnabled(false);
}
}
for(auto wname : (*tObj)["showWidgets"].Vector())
{
if(auto w = widget<CIntObject>(wname.String()))
{
w->setEnabled(true);
}
}
if((*tObj)["default"].isVector())
{
TurnTimerInfo tinfo;
tinfo.baseTimer = (*tObj)["default"].Vector().at(0).Integer() * 1000;
tinfo.turnTimer = (*tObj)["default"].Vector().at(1).Integer() * 1000;
tinfo.battleTimer = (*tObj)["default"].Vector().at(2).Integer() * 1000;
tinfo.creatureTimer = (*tObj)["default"].Vector().at(3).Integer() * 1000;
CSH->setTurnTimerInfo(tinfo);
}
}
redraw();
}
@ -173,15 +199,18 @@ void OptionsTab::recreate()
entries.insert(std::make_pair(pInfo.first, std::make_shared<PlayerOptionsEntry>(pInfo.second, * this)));
}
const auto & turnTimerRemote = SEL->getStartInfo()->turnTimerInfo;
//classic timer
if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
{
if(turnSlider->isActive() && !variables["timerPresets"].isNull())
if(!variables["timerPresets"].isNull() && !turnTimerRemote.battleTimer && !turnTimerRemote.creatureTimer && !turnTimerRemote.baseTimer)
{
for(int idx = 0; idx < variables["timerPresets"].Vector().size(); ++idx)
{
auto & tpreset = variables["timerPresets"].Vector()[idx];
if(tpreset.Vector().at(1).Integer() == SEL->getStartInfo()->turnTimerInfo.turnTimer / 1000)
if(tpreset.Vector().at(1).Integer() == turnTimerRemote.turnTimer / 1000)
{
turnSlider->scrollTo(idx);
if(auto w = widget<CLabel>("labelTurnDurationValue"))
@ -190,6 +219,33 @@ void OptionsTab::recreate()
}
}
}
//chess timer
auto timeToString = [](int time) -> std::string
{
std::stringstream ss;
ss << time / 1000 / 60 << ":" << std::setw(2) << std::setfill('0') << time / 1000 % 60;
return ss.str();
};
if(auto ww = widget<CTextInput>("chessFieldBase"))
ww->setText(timeToString(turnTimerRemote.baseTimer), false);
if(auto ww = widget<CTextInput>("chessFieldTurn"))
ww->setText(timeToString(turnTimerRemote.turnTimer), false);
if(auto ww = widget<CTextInput>("chessFieldBattle"))
ww->setText(timeToString(turnTimerRemote.battleTimer), false);
if(auto ww = widget<CTextInput>("chessFieldCreature"))
ww->setText(timeToString(turnTimerRemote.creatureTimer), false);
if(auto w = widget<ComboBox>("timerModeSwitch"))
{
if(turnTimerRemote.battleTimer || turnTimerRemote.creatureTimer || turnTimerRemote.baseTimer)
{
if(auto turnSlider = widget<CSlider>("sliderTurnDuration"))
if(turnSlider->isActive())
w->setItem(1);
}
}
}
size_t OptionsTab::CPlayerSettingsHelper::getImageIndex(bool big)