/* WebStats v1.2 - LFSLapper WWW statistics Copyright (C) 2006 Janez Cufer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include int split(std::string s, char splitchar, std::list& list) { int off = 0; int newoff = 0; int counter = 0; do{ std::string ns; newoff = s.find(splitchar, off); if(newoff != -1) ns = s.substr(off,newoff-off); else ns = s.substr(off); off = newoff+1; list.push_back(ns); counter++; }while(newoff != -1); return counter; } std::string replace(std::string s, std::string sub, std::string rep) { if(sub.size() == 0 || s.size() == 0) return s; int off = 0; do{ off = s.find(sub,off); if(off == -1) return s; s.replace(off,sub.size(),rep); off+=rep.size(); }while(true); } struct Player { std::string nickname; std::string car,time,track; std::string s1,s2,s3; int driftscore; std::string username; std::string date; std::string timeofdate; static int TimeDeserialize(std::string times) { int ticks = 0; int idx1 = times.find('.'); std::string mins = times.substr(0,idx1); int idx2 = times.find('.',idx1+1); std::string secs = times.substr(idx1+1,idx2-idx1); std::string hun = times.substr(idx2+1); return atol(hun.c_str()) + atol(secs.c_str())*100 + atol(mins.c_str())*100*60; } static bool greater(Player p,Player r) { if(TimeDeserialize(p.time) < TimeDeserialize(r.time)) return true; else return false; } static bool greater_driftscore(Player p,Player r) { if(p.driftscore > r.driftscore) return true; else return false; } static void write_driftscore(Player p) { std::cout << ""; std::cout << "" << p.driftscore << ""; std::cout << "" << p.nickname << ""; std::cout << "" << p.car << ""; std::cout << "" << p.track << ""; std::cout << "" << std::endl; } static void write(Player p) { std::cout << ""; std::cout << "" << p.time << ""; std::cout << "" << p.nickname << ""; if(TimeDeserialize(p.s1) != 0) std::cout << "" << p.s1 << ""; else std::cout << ""; if(TimeDeserialize(p.s2) != 0) std::cout << "" << p.s2 << ""; else std::cout << ""; if(TimeDeserialize(p.s3) != 0) std::cout << "" << p.s3 << ""; else std::cout << ""; std::cout << "" << p.car << ""; std::cout << "" << p.track << ""; std::cout << "" << std::endl; } }; typedef std::map tPlayers; typedef std::map tCars; typedef std::map tTracks; std::string NickNameToWWW(std::string s) { std::string out = ""; bool IsNextSpecial = false; for(int i=0;i"; break; case '1'://red out+=""; break; case '2'://green out+=""; break; case '3'://yellow out+=""; break; case '4'://blue out+=""; break; case '5'://violet out+=""; break; case '6'://cyan out+=""; break; case '7'://white out+=""; break; case '8'://default out+=""; break; //skip language designations case 'L':case 'C':case 'E':case 'J':case 'T':case 'G':case 'B': break; default: out+=s[i]; break; } IsNextSpecial = false; } } else { if(s[i] == '^') IsNextSpecial = true; else out+=s[i]; } } return out + ""; } int ReadPBFile(std::string filename,tTracks& tracks) { std::ifstream f(filename.c_str()/*,std::ios_base::in,(int)std::ios_base::_Openprot*/); if(f.fail()) return 0; char line[128]; f.getline(line,128); if(strcmp(line,"USERNAME")!=0) return 0; while(!f.eof()) { Player p; f.getline(line,128); if(f.eof()) break; p.username = line; f.getline(line,128);p.nickname = NickNameToWWW(line); f.getline(line,128);p.date = line; f.getline(line,128);p.timeofdate = line; f.getline(line,128);p.car = line; f.getline(line,128);p.time = line; f.getline(line,128);p.track = line; f.getline(line,128);p.s1 = line; f.getline(line,128);p.s2 = line; f.getline(line,128);p.s3 = line; tCars* carMap = &tracks[p.track]; tPlayers* playerMap = &(*carMap)[p.car]; (*playerMap)[p.nickname] = p; (*carMap)[p.car] = *playerMap; tracks[p.track] = *carMap; } f.close(); return 1; } int ReadDriftPBFile(std::string filename,tTracks& tracks) { std::ifstream f(filename.c_str()/*,std::ios_base::in,(int)std::ios_base::_Openprot*/); if(f.fail()) return 0; char line[128]; f.getline(line,128); if(strcmp(line,"USERNAME")!=0) return 0; while(!f.eof()) { Player p; f.getline(line,128); if(f.eof()) break; p.username = line; f.getline(line,128);p.nickname = NickNameToWWW(line); f.getline(line,128);p.date = line; f.getline(line,128);p.timeofdate = line; f.getline(line,128);p.car = line; f.getline(line,128);p.driftscore = atoi(line); f.getline(line,128);p.track = line; tCars* carMap = &tracks[p.track]; tPlayers* playerMap = &(*carMap)[p.car]; (*playerMap)[p.nickname] = p; (*carMap)[p.car] = *playerMap; tracks[p.track] = *carMap; } f.close(); return 1; } int display_pbs(std::string filename, std::string carfilter, std::string trackfilter, int from, int to) { tTracks tracks; if(!ReadPBFile(filename,tracks)) { std::cout << "

Could not open file " << filename << "

\n"; return 0; } std::cout << "\n"; // write header of table std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; //std::cout << "\n"; //std::cout << "\n"; std::cout << " \n"; for(tTracks::iterator iTrack = tracks.begin(); iTrack != tracks.end(); iTrack++) { //std::cout << iTrack->first << std::endl; if(iTrack->first != trackfilter) continue; for(tCars::iterator iCar = iTrack->second.begin(); iCar != iTrack->second.end(); iCar++) { //std::cout << iCar->first << std::endl; if(iCar->first != carfilter) continue; std::list players; for(tPlayers::iterator iPlayer = iCar->second.begin(); iPlayer != iCar->second.end(); iPlayer++) { // std::cout << "" << std::endl; players.push_back(iPlayer->second); } players.sort(Player::greater); //std::for_each(players.begin(),players.end(),Player::write); int n = 1; for(std::list::iterator iP = players.begin(); iP != players.end(); n++,iP++) { if(n>=from && n<=to) { std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; if(Player::TimeDeserialize(iP->s1) != 0) std::cout << " \n"; else std::cout << " \n"; if(Player::TimeDeserialize(iP->s2) != 0) std::cout << " \n"; else std::cout << " "; if(Player::TimeDeserialize(iP->s3) != 0) std::cout << " \n"; else std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; } } } } std::cout<<"
PositionLap timeNickname1st split2nd split3rd splitDateCarTrack
" << iPlayer->second.nickname << " - " << iPlayer->second.time << "
" << n << "" << iP->time << "" << iP->nickname << "" << iP->s1 << "" << iP->s2 << "" << iP->s3 << "" << iP->date << " " << iP->timeofdate << "
\n"; return 1; } int display_driftpbs(std::string filename, std::string carfilter, std::string trackfilter, int from, int to) { tTracks tracks; if(!ReadDriftPBFile(filename,tracks)) { std::cout << "

Could not open file " << filename << "

\n"; return 0; } std::cout << "\n"; // write header of table std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; //std::cout << "\n"; //std::cout << "\n"; std::cout << " \n"; for(tTracks::iterator iTrack = tracks.begin(); iTrack != tracks.end(); iTrack++) { if(iTrack->first != trackfilter) continue; for(tCars::iterator iCar = iTrack->second.begin(); iCar != iTrack->second.end(); iCar++) { if(iCar->first != carfilter) continue; std::list players; for(tPlayers::iterator iPlayer = iCar->second.begin(); iPlayer != iCar->second.end(); iPlayer++) { // std::cout << "" << std::endl; players.push_back(iPlayer->second); } players.sort(Player::greater_driftscore); //std::for_each(players.begin(),players.end(),Player::write_driftscore); int n = 1; for(std::list::iterator iP = players.begin(); iP != players.end(); n++,iP++) { if(n>=from && n<=to) { std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; std::cout << " \n"; } } } } std::cout<<"
PositionScoreNicknameDateCarTrack
" << iPlayer->second.nickname << " - " << iPlayer->second.time << "
" << n << "" << iP->driftscore << "" << iP->nickname << "" << iP->date << " " << iP->timeofdate << "
\n"; return 1; } void FromQueryStringToMap(std::string q,std::map& m) { q = replace(q,"+"," "); std::list l; split(q,'&',l); for(std::list::iterator i = l.begin();i!=l.end();i++) { std::list pair; int explen2 = split(*i,'=',pair); if(explen2 == 2) { std::string key = *pair.begin(); std::string val = *(++pair.begin()); m[key] = val; } } } int main() { int n; std::cout << "Content-type: text/html\n"; char* env = getenv("QUERY_STRING"); if(!env)env = ""; std::map Environment; FromQueryStringToMap(std::string(env),Environment); std::string Car = Environment["car"]; std::string Track = Environment["track"]; std::string Table = Environment["table"]; int from = atoi(Environment["from"].c_str()); int to = atoi(Environment["to"].c_str()); std::string pbfilename = Environment["pbfile"].c_str(); std::string driftfilename = Environment["driftfile"].c_str(); // default options, if not found in environment if(pbfilename == "") pbfilename = "PB.txt"; if(driftfilename == "") driftfilename = "DriftPB.txt"; if(Car == "") Car = "XR GT TURBO"; if(Track == "") Track = "BL1R"; if(Table == "") Table = "hotlapping"; if(from == 0) from = 1; if(to == 0) to = 15; std::cout << "\n"; std::cout << "\n"; std::cout << "\n"; std::cout << "\n"; std::cout << "LFSLapper WebStats\n"; std::cout << "\n"; std::cout << "\n"; std::cout << "\n"; std::cout << "\n"; std::cout << "
\n"; /* if(env) std::cout << "

" << env << "

\n"; //testing environment string */ if(Table == "hotlapping") { //std::string pbfilename = "PB.txt"; display_pbs(pbfilename, Car, Track, from, to); } else if(Table == "drifting") { //std::string driftfilename = "DriftPB.txt"; display_driftpbs(driftfilename, Car, Track, from, to); } //std::cout << "

\"Valid

"; std::cout << "· LFSLapper output information is powered by WebStats v1.2 ! Script developer: Janez Cufer\n"; std::cout << "\n"; std::cout << "\n"; std::cout << ""; std::cin >> n; return 0; }