Skip to content

Instantly share code, notes, and snippets.

@Pitbi
Created September 17, 2012 19:54
Show Gist options
  • Save Pitbi/3739403 to your computer and use it in GitHub Desktop.
Save Pitbi/3739403 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var qualificationSchema = new mongoose.Schema ({
date : Date,
track : {type: Schema.ObjectId, ref : 'Track'},
mod : {type: Schema.ObjectId, ref : 'Mod'},
entrys : [entrySchema]
});
var Qualification = mongoose.model('Qualification', qualificationSchema);
var entrySchema = new mongoose.Schema ({
driver : {type : Schema.ObjectId, ref : 'Driver'},
car : String,
position : Number,
laps : [lapSchema],
bestlap : Number
});
var lapSchema = new mongoose.Schema ({
number : Number,
s1 : Number,
s2 : Number,
s3 : Number,
s4 : Number,
s5 : Number,
time : Number,
et : Number,
fuel : Number,
});
//sx : s for sector
module.exports = Qualification;
server.get('/parseXML2', function (req, res) {
fs.readFile('./export/2012_09_13_22_14_10-63Q1.xml', function (err, file) {
var xml = libxmljs.parseXml(file);
var dateText = xml.get("/rFactorXML/RaceResults/TimeString").text();
var dateMatches = /^(\d{4})\/(\d{2})\/(\d{2}) (\d\d):(\d\d):(\d\d)/.exec(dateText);
var date = new Date(dateMatches[1], dateMatches[2], dateMatches[3], dateMatches[4], dateMatches[5], dateMatches[6]);
var track = xml.get("/rFactorXML/RaceResults/TrackEvent").text();
var trackLength = xml.get("/rFactorXML/RaceResults/TrackLength").text();
var modName = xml.get("/rFactorXML/RaceResults/Mod").text();
var drivers = xml.find("//Driver");
Track.findOne({name: track}).exec(function (err, existingTrack) {
if(!existingTrack) {
var newTrack = new Track ({name : track, length : trackLength, downloadLink: "#", location: "Need Edit", description: "Need Edit"});
newTrack.save(function (err) {
if (err)
throw err;
});
}
});
Mod.findOne({name: modName}).exec(function (err, mod) {
if (err)
throw err;
function addVehiclesToMod() {
var vehicleNames = [];
mod.vehicles.forEach(function (vehicle) {
vehicleNames.push(vehicle.name);
});
console.log(vehicleNames);
drivers.forEach(function (driver) {
var vehicleName = driver.get("CarType").text();
var vehicleClass = driver.get("CarClass").text();
if (vehicleNames.indexOf(vehicleName) == -1) {
mod.vehicles.push({name: vehicleName, group: vehicleClass});
vehicleNames.push(vehicleName);
}
});
mod.save(function (err) {
if (err)
throw err;
});
}
if (!mod) {
mod = new Mod ({name: modName});
mod.save(function (err) {
if (err)
throw err;
addVehiclesToMod();
});
} else {
addVehiclesToMod();
}
});
if (/Q/.exec(file)) {
Qualification.findOne({date: date}).exec(function (err, existingQualification) {
Mod.findOne({name: modName}).exec(function (err, mod) {
if(!existingQualification) {
Track.findOne({name: track}).exec(function (err, foundTrack) {
var qualification = new Qualification ({date: date, track: foundTrack.id, mod: mod.id});
drivers.forEach(function (driver) {
var driverName = "" + driver.get("Name").text();
var laps = driver.find("Lap");
Driver.findOne({name : driverName}).exec(function (err, driverFound) {
if (driverFound) {
qualification.entrys.push({driver: driverFound.id});
laps.forEach(function (lap) {
lap.attrs().forEach(function (lapAttribute) {
var name = lapAttribute.name();
var value = lapAttribute.value();
qualification.entrys.laps.push({name: value});
});
var lapTime = parseDriveTime(lap.text());
});
qualification.save(function (err) {
if (err)
throw err;
else
res.end("Qualification Added");
});
}
});
});
});
} else {
res.end("Existing Qualification");
}
});
});
} else if (/R/.exec(file)) {
Race.findOne({date: date}).exec(function (err, existingRace) {
if(!existingRace) {
var race = new Race ({date: date, track: track});
drivers.forEach(function (driver) {
var driverName = "" + driver.get("Name").text();
var laps = driver.find("Lap");
Driver.findOne({name : driverName}).exec(function (err, driverFound) {
if (driverFound != null) {
laps.forEach(function (lap) {
var lapTime = parseDriveTime(lap.text());
race.laps.push({driver: driverFound.id, time: lapTime});
});
race.save(function (err) {
if (err)
throw err;
else
res.end("Race Added");
});
}
});
});
} else {
res.end("Existing Race");
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment