Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
anakreon committed Aug 19, 2019
1 parent a6d307b commit 1ab6ac8
Show file tree
Hide file tree
Showing 70 changed files with 389 additions and 474 deletions.
43 changes: 0 additions & 43 deletions grammar.txt

This file was deleted.

20 changes: 10 additions & 10 deletions src/Bulletin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Nation, Document, Vaccine } from './types';
import { allNations, foreignNations } from './constants';

export class Bulletin {
private denied: Set<Nation>;
private deniedNations: Set<Nation>;
private requiredDocumentsByNation: { [nation: string]: Set<Document> };
private requiredDocumentsForWorkers: Set<Document>;
private requiredVaccinationsByNation: { [nation: string]: Set<Vaccine> };
private requiredVaccinationsForWorkers: Set<Vaccine>;
private wantedName: string;

constructor () {
this.denied = new Set<Nation>(allNations);
this.deniedNations = new Set<Nation>(allNations);
this.requiredDocumentsByNation = {};
this.requiredDocumentsForWorkers = new Set<Document>();
this.requiredVaccinationsByNation = {};
Expand All @@ -19,17 +19,18 @@ export class Bulletin {
}

public allow (nation: Nation): void {
if (this.denied.has(nation)) {
this.denied.delete(nation);
if (this.deniedNations.has(nation)) {
this.deniedNations.delete(nation);
}
}
public deny (nation: Nation): void {
this.denied.add(nation);
this.deniedNations.add(nation);
}
public requireDocumentForNation (nation: Nation, document: Document): void {
this.requiredDocumentsByNation[nation] = this.requiredDocumentsByNation[nation] || new Set<Document>();
this.requiredDocumentsByNation[nation].add(document);
}

public noLongerRequireDocumentForNation (nation: Nation, document: Document): void {
this.requiredDocumentsByNation[nation] = this.requiredDocumentsByNation[nation] || new Set<Document>();
this.requiredDocumentsByNation[nation].delete(document);
Expand Down Expand Up @@ -58,7 +59,6 @@ export class Bulletin {
this.requiredVaccinationsByNation[nation].add(vaccine);
}
public noLongerRequireVaccinationForNation (nation: Nation, vaccine: Vaccine): void {
console.log('noLongerRequireVaccinationForNation', nation, vaccine, this.requiredVaccinationsByNation[nation]);
this.requiredVaccinationsByNation[nation] = this.requiredVaccinationsByNation[nation] || new Set<Vaccine>();
this.requiredVaccinationsByNation[nation].delete(vaccine);
}
Expand Down Expand Up @@ -108,19 +108,19 @@ export class Bulletin {
this.wantedName = '';
}

public getDenied (): Set<Nation> {
return this.denied;
public getDeniedNations (): Set<Nation> {
return this.deniedNations;
}
public getRequiredDocumentsByNation (): { [nation: string]: Set<Document> } {
return this.requiredDocumentsByNation;
}
public getrequiredDocumentsForWorkers (): Set<Document> {
public getRequiredDocumentsForWorkers (): Set<Document> {
return this.requiredDocumentsForWorkers;
}
public getRequiredVaccinationsByNation (): { [nation: string]: Set<Vaccine> } {
return this.requiredVaccinationsByNation;
}
public getrequiredVaccinationsForWorkers (): Set<Vaccine> {
public getRequiredVaccinationsForWorkers (): Set<Vaccine> {
return this.requiredVaccinationsForWorkers;
}
public getWantedName (): string {
Expand Down
72 changes: 18 additions & 54 deletions src/Inspector.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { InspectionResult, InputPapers } from './types';
import { Papers } from './Papers';
import { AccessPermitInterpreter } from './interpreters/paper/AccessPermitInterpreter';
import { DiplomaticAuthorizationInterpreter } from './interpreters/paper/DiplomaticAuthorizationInterpreter';
import { GrantOfAsylumInterpreter } from './interpreters/paper/GrantOfAsylumInterpreter';
import { PassportInterpreter } from './interpreters/paper/PassportInterpreter';
import { Bulletin } from './Bulletin';
import { BulletinInterpreter } from './interpreters/bulletin/BulletinInterpreter';
import { RulesetBuilder } from './RulesetBuilder';
import { Ruleset } from './Ruleset';
import { CertificateOfVaccinationInterpreter } from './interpreters/paper/CertificateOfVaccinationInterpreter';
import { IdCardInterpreter } from './interpreters/paper/IdCardInterpreter';
import { WorkPassInterpreter } from './interpreters/paper/WorkPassInterpreter';
import { PapersInterpreter } from './PapersInterpreter';

export class Inspector {
private bulletin: Bulletin;
Expand All @@ -21,73 +15,43 @@ export class Inspector {
}

public receiveBulletin (inputBulletin: string): void {
console.log('inputBulletin', inputBulletin);
this.updateBulletin(inputBulletin);
this.ruleset = this.buildRuleset();
}
private updateBulletin (inputBulletin: string): void {
this.bulletin.clearWanted();
const interpreter = new BulletinInterpreter();
interpreter.interpret(this.bulletin, inputBulletin);
interpreter.interpret(inputBulletin, this.bulletin);
}
private buildRuleset (): Ruleset {
const rulesetBuilder = new RulesetBuilder();
rulesetBuilder.fromBulletin(this.bulletin);
this.ruleset = rulesetBuilder.getRuleset();
console.log('updatedBulletin: ', this.bulletin);
console.log('updatedRuleset: ', this.ruleset);
return rulesetBuilder.getRuleset();
}

public inspect (inputPapers: InputPapers): InspectionResult {
console.log('inputPapers: ', inputPapers);
const papers = this.buildPapers(inputPapers);
console.log('papers', papers);

const detainmentRule = this.ruleset.getDetainmentRule(papers);
if (detainmentRule) {
return 'Detainment: ' + detainmentRule.getErrorMessage();
return 'Detainment: ' + detainmentRule.getMessage();
}

const denialRule = this.ruleset.getDenialRule(papers);
if (denialRule) {
return 'Entry denied: ' + denialRule.getErrorMessage();
return 'Entry denied: ' + denialRule.getMessage();
}

if (papers.getPersonalData().getNation() === 'Arstotzka') {
const personalData = papers.getPersonalData();
if (personalData.isNationalOfArstotzka()) {
return 'Glory to Arstotzka.';
} else {
return 'Cause no trouble.';
}
}

private buildPapers (inputPapers: InputPapers): Papers {
const papers = new Papers();
if (inputPapers.access_permit) {
const interpreter = new AccessPermitInterpreter();
const accessPermit = interpreter.interpret(inputPapers.access_permit);
papers.setAccessPermit(accessPermit);
}
if (inputPapers.diplomatic_authorization) {
const interpreter = new DiplomaticAuthorizationInterpreter();
const diplomaticAuthorization = interpreter.interpret(inputPapers.diplomatic_authorization);
papers.setDiplomaticAuthorization(diplomaticAuthorization);
}
if (inputPapers.grant_of_asylum) {
const interpreter = new GrantOfAsylumInterpreter();
const grantOfAsylum = interpreter.interpret(inputPapers.grant_of_asylum);
papers.setGrantOfAsylum(grantOfAsylum);
}
if (inputPapers.passport) {
const interpreter = new PassportInterpreter();
const passport = interpreter.interpret(inputPapers.passport);
papers.setPassport(passport);
}
if (inputPapers.certificate_of_vaccination) {
const interpreter = new CertificateOfVaccinationInterpreter();
const certificateOfVaccination = interpreter.interpret(inputPapers.certificate_of_vaccination);
papers.setCertificateOfVaccination(certificateOfVaccination);
}
if (inputPapers.ID_card) {
const interpreter = new IdCardInterpreter();
const idCard = interpreter.interpret(inputPapers.ID_card);
papers.setIdCard(idCard);
}
if (inputPapers.work_pass) {
const interpreter = new WorkPassInterpreter();
const workPass = interpreter.interpret(inputPapers.work_pass);
papers.setWorkPass(workPass);
}
return papers;
const interpreter = new PapersInterpreter();
return interpreter.interpret(inputPapers);
}
}
75 changes: 75 additions & 0 deletions src/PapersInterpreter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Papers } from './Papers';
import { AccessPermit } from './papers/AccessPermit';
import { AccessPermitInterpreter } from './interpreters/paper/AccessPermitInterpreter';
import { DiplomaticAuthorizationInterpreter } from './interpreters/paper/DiplomaticAuthorizationInterpreter';
import { DiplomaticAuthorization } from './papers/DiplomaticAuthorization';
import { InputPapers } from './types';
import { GrantOfAsylum } from './papers/GrantOfAsylum';
import { GrantOfAsylumInterpreter } from './interpreters/paper/GrantOfAsylumInterpreter';
import { PassportInterpreter } from './interpreters/paper/PassportInterpreter';
import { Passport } from './papers/Passport';
import { CertificateOfVaccinationInterpreter } from './interpreters/paper/CertificateOfVaccinationInterpreter';
import { CertificateOfVaccination } from './papers/CertificateOfVaccination';
import { IdCardInterpreter } from './interpreters/paper/IdCardInterpreter';
import { IdCard } from './papers/IdCard';
import { WorkPassInterpreter } from './interpreters/paper/WorkPassInterpreter';
import { WorkPass } from './papers/WorkPass';

export class PapersInterpreter {
private inputKeyInterpreterMap = {
access_permit: {
interpreter: new AccessPermitInterpreter(),
addToPapers: (papers: Papers) => (accessPermit: AccessPermit) => {
papers.setAccessPermit(accessPermit);
}
},
diplomatic_authorization: {
interpreter: new DiplomaticAuthorizationInterpreter(),
addToPapers: (papers: Papers) => (diplomaticAuthorization: DiplomaticAuthorization) => {
papers.setDiplomaticAuthorization(diplomaticAuthorization);
}
},
grant_of_asylum: {
interpreter: new GrantOfAsylumInterpreter(),
addToPapers: (papers: Papers) => (grantOfAsylum: GrantOfAsylum) => {
papers.setGrantOfAsylum(grantOfAsylum);
}
},
passport: {
interpreter: new PassportInterpreter(),
addToPapers: (papers: Papers) => (passport: Passport) => {
papers.setPassport(passport);
}
},
certificate_of_vaccination: {
interpreter: new CertificateOfVaccinationInterpreter(),
addToPapers: (papers: Papers) => (certificateOfVaccination: CertificateOfVaccination) => {
papers.setCertificateOfVaccination(certificateOfVaccination);
}
},
ID_card: {
interpreter: new IdCardInterpreter(),
addToPapers: (papers: Papers) => (idCard: IdCard) => {
papers.setIdCard(idCard);
}
},
work_pass: {
interpreter: new WorkPassInterpreter(),
addToPapers: (papers: Papers) => (workPass: WorkPass) => {
papers.setWorkPass(workPass);
}
}
};

public interpret (inputPapers: InputPapers): Papers {
const papers = new Papers();
for (var key in inputPapers) {
if (inputPapers.hasOwnProperty(key)) {
const { interpreter, addToPapers } = this.inputKeyInterpreterMap[key];
const paper = interpreter.interpret(inputPapers[key]);
addToPapers(papers)(paper);
}
}
return papers;
}
}
6 changes: 3 additions & 3 deletions src/Rule.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Papers, Validator } from './types';

export class Rule {
constructor (private validator: Validator, private validationErrorMessage: string) {}
constructor (private validator: Validator, private validationMessage: string) {}

public test (papers: Papers): boolean {
return this.validator.validate(papers);
}

public getErrorMessage (): string {
return this.validationErrorMessage;
public getMessage (): string {
return this.validationMessage;
}
}
Loading

0 comments on commit 1ab6ac8

Please sign in to comment.