added switch for developer mode and fixed crash with removing missiles

This commit is contained in:
Andreas Ortmann 2016-09-30 19:03:22 +02:00
parent b5efac002e
commit 5a4e6aaaed
6 changed files with 65 additions and 21 deletions

View file

@ -6,12 +6,37 @@
#include <iostream>
using namespace std;
#include <sstream>
bool Session::parse(string s)
void trim(std::string &str)
{
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
const std::string ws = " \t\r\n";
size_t endpos = str.find_last_not_of(ws);
if (endpos != std::string::npos) {
str = str.substr(0, endpos+1);
}
// trim leading spaces
size_t startpos = str.find_first_not_of(ws);
if (startpos != std::string::npos) {
str = str.substr(startpos);
}
}
bool Session::parse(std::string s)
{
// strip leading/trailing whitespace/newlines as they are always unintional
trim(s);
std::istringstream iss(s);
std::string token;
iss >> token;
// skip token + next whitespace
std::string rest = s.substr(std::min(s.size(), token.size()+1));
//std::cout<<"[session] token='" << token << "' rest='" << rest << "'" << std::endl;
// TODO:
// from knoc:
@ -23,21 +48,29 @@ bool Session::parse(string s)
m_socket.close();
m_state->quitPlayer(m_pid);
return false;
} else if (s.size()==1&&s[0]=='c') {
//clear
m_state->clear(m_pid);
} else if (s.size()>=3&&s[0]=='n'&&s[1]==' ') {
//set name
m_state->setName(m_pid, s.substr(2));
} else if (s.size()>=3&&s[0]=='v'&&s[1]==' ') {
//set speed
double speed = atof(s.substr(2).c_str());
m_state->setSpeed(m_pid, speed);
} else if (token == "dev") {
m_state->commandForPlayer(m_pid, new game::DeveloperCommand(rest));
} else {
//shoot
double angle = atof(s.c_str());
m_state->commandForPlayer(m_pid, new game::ShootCommand(angle));
}
return true;
}