コード例 #1
0
RideFile *TxtFileReader::openRideFile(QFile &file, QStringList &errors, QList<RideFile*>*) const
{
    QString deviceInfo; // keep a record of all the data in the
                        // sections so we can store it in the device
                        // info metadata field

    QString section = ""; // the text for the section we are currently in
                     // if it is blank we are not in any section, and should
                     // expect to see a section, or 'number of records = ' or
                     // column headings or raw data

    QStringList headings; // the headings array
    int timeIndex = -1;
    int cadIndex = -1;
    int hrIndex = -1;
    int kmIndex = -1;
    int kphIndex = -1;
    int milesIndex = -1;
    int wattsIndex = -1;
    int headwindIndex = -1;

    bool metric = true;   // are the values in metric or imperial?
    QDateTime startTime;  // parsed from filename

    // just to quieten the compiler, since we don't seem to
    // use the metric bool anywhere in the code at present
    if (metric) { }

    // Lets make sure we can open the file
    if (!file.open(QFile::ReadOnly)) {
        errors << ("Could not open ride file: \"" + file.fileName() + "\"");
        return NULL;
    }
    QTextStream in(&file);

    // Now we need to determine if this is a Wattbike export
    // or a Racermate export. We can do this by looking at
    // the first line of the file.
    //
    // For it to be a wattbike, the first line is made up
    // of multiple tokens, separated by a tab, which match
    // the pattern "name [units]"
    bool isWattBike = false;
    QStringList tokens = in.readLine().split(QRegExp("[\t\n\r]"), QString::SkipEmptyParts);

    if (tokens.count() > 1) {
        // ok, so we have a bunch of tokens, thats a good sign this
        // may be a wattbike export, now are all the tokens of the
        // form "name [units]", or just "Index" or "name []"
        isWattBike=true;
        QRegExp wattToken("^[^[]+ \\[[^]]+\\]$");
        foreach(QString token, tokens) {
            if (wattToken.exactMatch(token) != true) {
                QRegExp noUnits("^[^[]+ \\[\\]$");
                if (token != "Index" && !noUnits.exactMatch(token)) {
                    isWattBike = false;
                }
            }
        }
    }
コード例 #2
0
void Calculator::applyAssault(UnitsMap& attacker, PlayerId attackerId, pTerritory defenderTerritory){
    UnitsMap defender;
    PlayerId defenderId = defenderTerritory->ownership();
    int defenderDefBonus = DEFENDER_DEF_BONUS + defenderTerritory->getDefenseBonus();
    int defenderAttBonus = DEFENDER_ATT_BONUS;

    for(int i=0; i<defenderTerritory->units().size(); i++){
        pUnit u = defenderTerritory->units()[i];
        defender.insert(u->type(), u->noUnits());
    }

    int a[2];
    int d[2];
    int an = noUnits(attacker);
    int dn = noUnits(defender);
    int sum;
    int terrId = defenderTerritory->id();

    UnitsMap* m[] = {&attacker, &defender};

    while(true){
        a[0]=a[1]=d[0]=d[1]=0;

        
        a[0] = calculateAttack(*m[0]);
        d[0] = calculateDefence(*m[0]);

        a[1] = int(calculateAttack(*m[1]) * (100 + defenderAttBonus)/100);
        d[1] = int(calculateDefence(*m[1]) * (100 + defenderDefBonus)/100);


        for(int i=0; i<2; i++){
            sum = a[i] + d[(i+1)%2];
            int r = rand(sum);
                FileLogger::write(QString(i==0?"A ":"D ")
                    .append(QString::number(float(a[i])*100/sum))
                    .append("%"));
            if( r <=a[i]){  
                removeUnit(*m[(i+1)%2],rand(noUnits(*m[(i+1)%2])));
                FileLogger::write(QString("Killed. ")
                    .append(QString::number(noUnits(*m[0])))
                    .append("vs")
                    .append(QString::number(noUnits(*m[1]))));
            }
        }

        if(noUnits(attacker) == 0){
            FileLogger::write("defender won");
            for(UnitsMap::iterator i = defender.begin(); i!=defender.end(); ++i){
                raports_.push_back(
                    RaportFactory::getInstance().create(
                    OBJECT, UNIT, terrId, i.key(), i.value()));
                defenderTerritory->setUnit(i.key(), i.value());
            }
            
            raports_.push_back(
                RaportFactory::getInstance().create(
                    BATTLE, TERRITORY_BATTLE_DEFENDED, terrId, dn - noUnits(defender), an));

            
            return;
        }
        if(noUnits(defender) == 0){
            FileLogger::write("attacker won");
            for(UnitsMap::iterator i = defender.begin(); i!=defender.end(); ++i){
                raports_.push_back(
                    RaportFactory::getInstance().create(
                    OBJECT, UNIT, terrId, i.key(), i.value()));
                defenderTerritory->setUnit(i.key(), i.value());
            }
            for(UnitsMap::iterator i = attacker.begin(); i!=attacker.end(); ++i){
                raports_.push_back(
                    RaportFactory::getInstance().create(
                    OBJECT, UNIT, terrId, i.key(), i.value()));
                defenderTerritory->setUnit(i.key(), i.value());
            }
            
            raports_.push_back(
                RaportFactory::getInstance().create(
                BATTLE, TERRITORY_BATTLE_CONQUERED, terrId, an - noUnits(attacker), dn));
            raports_.push_back(
                RaportFactory::getInstance().create(
                OWNERSHIP, terrId, defenderId, attackerId, 0));

            return;
        }

    }
}