Created
September 16, 2012 20:46
-
-
Save jbpros/3734334 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 date = xml.get("/rFactorXML/RaceResults/TimeString").text(); | |
var track = xml.get("/rFactorXML/RaceResults/TrackEvent").text(); | |
var trackLength = xml.get("/rFactorXML/RaceResults/TrackLength").text(); | |
var mod = xml.get("/rFactorXML/RaceResults/Mod").text(); | |
var drivers = xml.find("//Driver"); | |
Track.findOne({name: track}).exec(function (err, existingTrack) { | |
if(!existingTrack) { | |
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: mod}).exec(function (err, mod) { | |
if (err) | |
throw err; | |
function addVehiclesToMod() { | |
var vehicleNames = []; | |
mod.vehicles.forEach(function (vehicle) { | |
vehicleNames.push(vehicle.name); | |
}); | |
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: mod}); | |
mod.save(function (err) { | |
if (err) | |
throw err; | |
addVehiclesToMod(); | |
}); | |
} else { | |
addVehiclesToMod(); | |
} | |
}); | |
if (/Q/.exec(file)) { | |
Qualification.findOne({name: date}).exec(function (err, existingQualification) { | |
if(!existingQualification) { | |
Track.findOne({name: track}).exec(function (err, foundTrack) { | |
qualification = new Qualification ({name: date, track: foundTrack.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 != null) { | |
laps.forEach(function (lap) { | |
var lapTime = parseDriveTime(lap.text()); | |
qualification.laps.push({driver: driverFound.id, time: lapTime}); | |
}); | |
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({name: date}).exec(function (err, existingRace) { | |
if(!existingRace) { | |
race = new Race ({name: 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