Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(street-fighter): add sfpl db access layer #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions typescript-backend-katas/src/streetFighter/dataAccess/db.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Match } from "../models/models";
import { getMatch, getMatches } from "./db";
import * as path from "path";

describe("getMatch", () => {
it('should parse a string from the CSV to get a match', async () => {
const input: string = "1,chun-li,2,1,sagat";
const match: Match = getMatch(input);

const expected: Match = {
fighter1: "chun-li",
fighter2: "sagat",
winner: "chun-li"
}
expect(match).toEqual(expected);
})
it('should parse a string from the CSV to get another match', async () => {
const input: string = "2,chun-li,1,2,ryu";
const match: Match = getMatch(input);

const expected: Match = {
fighter1: "chun-li",
fighter2: "ryu",
winner: "ryu"
}
expect(match).toEqual(expected);
})
it('should parse a string from the CSV to get a draw', async () => {
const input: string = "2,chun-li,2,2,ryu";
const match: Match = getMatch(input);

const expected: Match = {
fighter1: "chun-li",
fighter2: "ryu",
winner: null
}
expect(match).toEqual(expected);
})
});

describe("getMatches", () => {
it('should parse the CSV and get two Matches', async () => {
const input_path = path.resolve("src/streetFighter/dataAccess/testResources/test.csv");
const matches: Match[] = getMatches(input_path);

const expected_matches: Match[] = [
{
fighter1: "chun-li",
fighter2: "sagat",
winner: "chun-li"
},
{
fighter1: "chun-li",
fighter2: "ryu",
winner: "ryu"
}
]
expect(matches).toEqual(expected_matches);
})
});


31 changes: 31 additions & 0 deletions typescript-backend-katas/src/streetFighter/dataAccess/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Match } from "../models/models";
import * as fs from "fs";


export function getMatch(input: string): Match {
const splitString = input.split(",");

const fighter1 = splitString[1];
const roundsWon1 = Number(splitString[2]);
const roundsWon2 = Number(splitString[3]);
const fighter2 = splitString[4];

let winner = null;
if (roundsWon1 > roundsWon2) {
winner = fighter1;
} else if (roundsWon1 < roundsWon2) {
winner = fighter2;
}

return{
fighter1: fighter1,
fighter2: fighter2,
winner: winner,
}
}

export function getMatches(filePath: string): Match[] {
const allContents = fs.readFileSync(filePath, 'utf-8');
return allContents.split(/\r?\n/).slice(1).filter(line => line).map(getMatch);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
timeslot,home,roundsWon1,roundsWon2,away
1,chun-li,2,1,sagat
1,chun-li,1,2,ryu
5 changes: 5 additions & 0 deletions typescript-backend-katas/src/streetFighter/models/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Match {
fighter1: string;
fighter2: string;
winner: string | null;
}