Skip to content

Commit

Permalink
Adding more processes and debugging deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
NQNStudios committed Jun 8, 2018
1 parent fbe98c8 commit 2d78069
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Idea.ts
@@ -1,6 +1,17 @@
import { Duration, duration } from "moment";
import { JsonObject, JsonProperty } from "json2typescript";

export interface IdeaJson
{
id: Number;
name: String;
description: String;
tags: { [id: string]: boolean; };
children: Array<IdeaJson>;
_progress: Number;
_duration: Duration;
}

// The basic unit of things the user wants to do. Ideas are all created in
// unstructured form, and structured later by dividing and subdividing them
// into smaller Ideas.
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
@@ -1,6 +1,8 @@
import {Idea} from "./Idea";
import {DaVinciBot, BotStatus, BotProcess} from "./DaVinciBot";
import {AddIdeaProcess} from "./processes/AddIdeaProcess";
import {PlanIdeaProcess} from "./processes/PlanIdeaProcess";
import {WorkOnIdeaProcess} from "./processes/WorkOnIdeaProcess";
import {LoadProcess, SaveProcess, LoadFileProcess, SaveFileProcess} from "./processes/Serialization";
import * as readlineSync from "readline-sync";
import * as os from "os";
Expand Down Expand Up @@ -30,7 +32,7 @@ let bot: DaVinciBot = new DaVinciBot();
bot.startProcess(new LoadFileProcess(bot, rootIdea));
bot.handleInput(path.join(os.homedir(), '.davinci.json'));

bot.startProcess(new AddIdeaProcess(bot, rootIdea));
bot.startProcess(new WorkOnIdeaProcess(bot, rootIdea));

while (bot.status !== BotStatus.Idle) {
switch (bot.status) {
Expand Down
2 changes: 1 addition & 1 deletion src/processes/AddIdeaProcess.ts
Expand Up @@ -26,4 +26,4 @@ export class AddIdeaProcess extends BotProcess
// TODO don't always do this
console.log(this.rootIdea.children);
}
};
}
52 changes: 52 additions & 0 deletions src/processes/PlanIdeaProcess.ts
@@ -0,0 +1,52 @@
import {Idea} from "../Idea";
import {BotProcess, BotStatus} from "../DaVinciBot";
import {AddIdeaProcess} from "./AddIdeaProcess";

// TODO processes should be able to handleInput in a standard form where
// different options are either a number or a case-insensitive string.
// There should also be a standard form for outputting these options as is done
// below

// TODO should process descriptions be markdown-enabled? With options available
// on getOutput() to either ignore or parse formatting into another form the
// client chooses

export class PlanIdeaProcess extends BotProcess
{
start(): void {
this.status = BotStatus.HasOutput;
}

getOutput(): string {
this.status = BotStatus.NeedsInput;
return `Planning for idea ${this.rootIdea.name}:
1. [Break] this idea into smaller ideas.
2. Define a [time] estimate for this idea.
3. [Describe] this idea. (Current description: ${this.rootIdea.description})
4. [quit]
`;

}

handleInput(input:string) {
this.status = BotStatus.HasOutput;
switch (input.toLowerCase()) {
case '1':
case 'break':
this.bot.startProcess(new AddIdeaProcess(this.bot, this.rootIdea));
break;
case '2':
case 'time':
// TODO start a TimeIdeaProcess on rootIdea
break;
case '3':
case 'describe':
// TODO start an add description process on rootIdea
break;
case '4':
case 'quit':
this.status = BotStatus.Idle;
break;
}
}
}
15 changes: 11 additions & 4 deletions src/processes/Serialization.ts
@@ -1,7 +1,7 @@
import * as fs from "fs";
import {JsonConvert} from "json2typescript";

import {Idea} from "../Idea";
import {Idea, IdeaJson} from "../Idea";
import {BotProcess, BotStatus} from "../DaVinciBot";

export class LoadFileProcess extends BotProcess
Expand Down Expand Up @@ -51,8 +51,17 @@ export class LoadProcess extends BotProcess
let countInput = input.substr(0, jsonStart);
let jsonInput = input.substr(jsonStart);

let jsonObject: object = JSON.parse(jsonInput);
let jsonObject: IdeaJson = JSON.parse(jsonInput) as IdeaJson;
let newRootIdea = LoadProcess.converter.deserializeObject(jsonObject, Idea);

// Recursively load all the children as Idea objects
for (let i = 0; i < jsonObject.children.length; ++i) {
let childLoadProcess = new LoadProcess(this.bot, new Idea());
this.bot.startProcess(childLoadProcess);
this.bot.handleInput(countInput + JSON.stringify(jsonObject.children[i]));
newRootIdea.children[i] = childLoadProcess.rootIdea;
}

this.rootIdea.become(newRootIdea);

// Make sure TotalCount is properly set
Expand All @@ -69,8 +78,6 @@ export class LoadProcess extends BotProcess

export class SaveProcess extends BotProcess
{
description(): string { return 'Save ideas to a JSON string.'; }

start(): BotStatus {
return BotStatus.HasOutput;
}
Expand Down
50 changes: 50 additions & 0 deletions src/processes/WorkOnIdeaProcess.ts
@@ -0,0 +1,50 @@
import {Idea} from "../Idea";
import {BotProcess, BotStatus} from "../DaVinciBot";
import {PlanIdeaProcess} from "./PlanIdeaProcess";

export class WorkOnIdeaProcess extends BotProcess
{
start(): void {
this.status = BotStatus.HasOutput;
}

getOutput(): string {
this.status = BotStatus.NeedsInput;
return `Working on idea ${this.rootIdea.name}:
1. Mark this idea [done].
2. Work on this idea's [parts]
3. [Delete] this idea
4. [Plan] this idea.
5. [quit]
`;
}

handleInput(input:string) {
this.status = BotStatus.HasOutput;
switch (input.toLowerCase()) {
case '1':
case 'done':
this.rootIdea.progress = 1;
this.status = BotStatus.Idle;
break;
case '2':
case 'parts':
for (let i = this.rootIdea.children.length - 1; i >= 0; --i) {
this.bot.startProcess(new WorkOnIdeaProcess(this.bot, this.rootIdea.children[i]));
}
break;
case '3':
case 'delete':
// TODO delete this idea from its root
break;
case '4':
case 'plan':
this.bot.startProcess(new PlanIdeaProcess(this.bot, this.rootIdea));
case '5':
case 'quit':
this.status = BotStatus.Idle;
break;
}
}

}
33 changes: 33 additions & 0 deletions test/serialization.js
@@ -0,0 +1,33 @@
'use strict';

var expect = require('chai').expect;
var idea = require('../dist/Idea.js');
var davincibot = require('../dist/DaVinciBot.js');
var addIdeaProcess = require('../dist/processes/AddIdeaProcess.js');
var serialization = require('../dist/processes/Serialization.js');

describe('Serialization test', () => {
var bot = new davincibot.DaVinciBot();
var rootIdea = new idea.Idea();
bot.startProcess(new addIdeaProcess.AddIdeaProcess(bot, rootIdea));
bot.getOutput();
bot.handleInput('test1');
bot.handleInput('test2');
bot.handleInput('test3');
bot.handleInput('quit');


bot.startProcess(new serialization.SaveFileProcess(bot, rootIdea));
bot.handleInput('.serial-test-1.json');

var newRootIdea = new idea.Idea();
bot.startProcess(new serialization.LoadFileProcess(bot, newRootIdea));
bot.handleInput('.serial-test-1.json');

it('Should preserve the Idea names after deserialization', () => {
expect(newRootIdea.children[0].name).to.equal('test1');
expect(newRootIdea.children[1].name).to.equal('test2');
expect(newRootIdea.children[2].name).to.equal('test3');
});

});

0 comments on commit 2d78069

Please sign in to comment.