From 78a1bed1f2b93f127a5f40628203087778ac22f9 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Tue, 22 Sep 2020 12:16:37 +0300 Subject: [PATCH] Generation of textevents for ZWO files --- examples/comments.txt | 35 +++++++++++++++++++++++++++++++++++ package.json | 1 + src/generateZwo.ts | 14 +++++++++++--- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 examples/comments.txt diff --git a/examples/comments.txt b/examples/comments.txt new file mode 100644 index 0000000..ae5194f --- /dev/null +++ b/examples/comments.txt @@ -0,0 +1,35 @@ +Name: Workout with comments + +Warmup: 10:00 25%..75% + # 0:10 Welcome to: #8 + # 6:00 Let's talk about today's workout + # 6:10 We'll be doing 5 blocks of VO2max work + # 6:20 Each block 2 minutes and 30 seconds + # 6:30 30 seconds more than last time :) + # 6:40 Finally we finish off with 10 minutes of threshold work + # 6:50 Easy as that :) + +Interval: 0:30 95% 95rpm + # 0:00 Spin to 95 RPM if you're not already there + +Rest: 0:30 50% 85rpm + # 0:00 Rest a bit + +Interval: 2:30 115% 105rpm + # 0:00 Here we go! + # 1:00 First minute done + # 2:00 Just 30 seconds more + # 2:20 Finish it! + +Rest: 3:00 50% 85rpm + # 0:00 Good! + # 0:10 Rest for 3 minutes + # 2:50 Ready for next round? + +Cooldown: 7:30 90%..25% + # 0:00 We'll now gradually decrease the power + # 0:10 It's all downhill from here :) + # 0:20 until you're cooled down + # 7:10 Thanks for participating + # 7:20 Until next time! + diff --git a/package.json b/package.json index 5eb6f5f..d6675cc 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "test:watch": "jest --watch", "test:1": "ts-node src/index.ts examples/threshold-pushing.txt", "test:2": "ts-node src/index.ts examples/darth-vader.txt", + "test:comments": "ts-node src/index.ts examples/comments.txt", "test:stats:1": "ts-node src/index.ts --stats examples/threshold-pushing.txt", "test:stats:2": "ts-node src/index.ts --stats examples/darth-vader.txt", "format:js": "prettier --write src/" diff --git a/src/generateZwo.ts b/src/generateZwo.ts index 0e1493a..32724f0 100644 --- a/src/generateZwo.ts +++ b/src/generateZwo.ts @@ -1,11 +1,17 @@ import * as xml from "xml"; -import { Interval, Workout } from "./ast"; +import { Interval, Workout, Comment } from "./ast"; // Zwift Workout XML generator +const generateTextEvents = (comments: Comment[]): xml.XmlObject[] => { + return comments.map(({ offset, text }) => ({ + textevent: [{ _attr: { timeoffset: offset, message: text } }], + })); +}; + const generateRangeInterval = ( tagName: "Warmup" | "Cooldown", - { duration, intensity, cadence }: Interval, + { duration, intensity, cadence, comments }: Interval, ): xml.XmlObject => { return { [tagName]: [ @@ -17,11 +23,12 @@ const generateRangeInterval = ( ...(cadence ? { Cadence: cadence } : {}), }, }, + ...generateTextEvents(comments), ], }; }; -const generateSteadyStateInterval = ({ duration, intensity, cadence }: Interval): xml.XmlObject => { +const generateSteadyStateInterval = ({ duration, intensity, cadence, comments }: Interval): xml.XmlObject => { return { SteadyState: [ { @@ -31,6 +38,7 @@ const generateSteadyStateInterval = ({ duration, intensity, cadence }: Interval) ...(cadence ? { Cadence: cadence } : {}), }, }, + ...generateTextEvents(comments), ], }; };