Skip to content

Commit

Permalink
Merge pull request #1 from syarhei/scripts/TeamStatisticCreation
Browse files Browse the repository at this point in the history
create script for adding of team statistic
  • Loading branch information
syarhei authored May 5, 2018
2 parents 5974419 + f7f352d commit c1018cc
Show file tree
Hide file tree
Showing 8 changed files with 6,139 additions and 23 deletions.
117 changes: 117 additions & 0 deletions app/scripts/TeamStatisticCreation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import {Team} from "../src/contracts/Team";
import uuid = require("uuid");
import {MatchCategory} from "../src/contracts/MatchCategory";
import container from "../inversify/config";
import {DATABASE_CONTEXT} from "../inversify/identifiers/common";
import {DBContext} from "../src/DB/DBContext";
import {Match} from "../src/contracts/Match";

interface TeamValue {
key: string;
name: string;
code: string;
}

interface Round {
name: string;
matches: MatchValue[]
}

interface MatchValue {
date: string;
team1: TeamValue;
team2: TeamValue;
score1: number;
score2: number;
}

const { clubs }: { name: string, clubs: TeamValue[] } = require("../../statistics/2012-13/en.1.clubs.json");
const BPL: { name: string, rounds: Round[] } = require("../../statistics/2012-13/en.1.json");

let categoryId: string = uuid();
let teams: Team[] = null;
const COUNTRY: string = "England";
const matchValues: MatchValue[] = [];
BPL.rounds.forEach(round => {
matchValues.push(...round.matches);
});

const context = container.get<DBContext>(DATABASE_CONTEXT);

async function createTeams() {
try {
teams = clubs.map(club => {
return {
id: uuid(),
name: club.name,
year: 2018,
country: COUNTRY,
owner: "Siarhei Murkou"
} as Team;
});
await context.TEAM.bulkCreate(teams);
} catch (err) {
console.log(`Error during Team creation`);
throw err;
}
}

async function createMatchCategory() {
try {
const category: MatchCategory = {
id: categoryId,
name: BPL.name,
country: COUNTRY,
isContinental: false,
isFinished: false
};
await context.MATCH_CATEGORY.create(category);
} catch (err) {
console.log(`Error during MatchCategory creation`);
throw err;
}
}

async function createMatches() {
try {
const matches: Match[] = matchValues.map(match => {
return {
id: uuid(),
matchCategoryId: categoryId,
teamHomeId: teams.find(team => team.name === match.team1.name).id,
teamGuestId: teams.find(team => team.name === match.team2.name).id,
date: Date.parse(match.date),
place: COUNTRY,
homeGoals: match.score1,
guestGoals: match.score2
} as Match;
});
await context.MATCH.bulkCreate(matches);
} catch (err) {
console.log(`Error during Match creation`);
throw err;
}
}

export async function createFootballSet(): Promise<{ teams: Team[], categoryId: string }> {
try {
await createTeams();
await createMatchCategory();
await createMatches();
console.log("Football Statistic is uploaded to database");
return { teams, categoryId };
} catch (err) {
console.log(err);
}
}

(async () => {
try {
if (require.main === module) {
await createFootballSet();
process.exit(0);
}
} catch (err) {
process.exit(-1);
}
})();
5 changes: 4 additions & 1 deletion app/src/DB/DBConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export class DBConnection {
dialect: config.DATABASE_DIALECT,
host: config.DATABASE_HOSTNAME,
operatorsAliases: false,
logging: false
logging: false,
define: {
charset: "utf8"
}
});
}
}
7 changes: 0 additions & 7 deletions app/src/contracts/Match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ export interface Match {
coefficientWin1: number;
coefficientDraw: number;
coefficientWin2: number;
// total0?: number;
// total1?: number;
// total2?: number;
// total3?: number;
// total4?: number;
// total5?: number;
// total6?: number;
place: string;
date: number;
result?: RESULTS_ENUM;
Expand Down
2 changes: 1 addition & 1 deletion app/src/models/MatchCategoryModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class MatchCategoryModel {
primaryKey: true
},
"name": {
type: STRING(20),
type: STRING(30),
unique: true,
allowNull: false
},
Expand Down
25 changes: 14 additions & 11 deletions app/src/models/MatchModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {STRING, REAL, INTEGER, Sequelize, default as sequelize} from "sequelize";
import {STRING, REAL, INTEGER, Sequelize, default as sequelize, BIGINT} from "sequelize";
import {injectable, inject} from "inversify";
import {DATABASE_CONNECTION, MATCH_CATEGORY_MODEL, TEAM_MODEL} from "../../inversify/identifiers/common";
import {DBConnection} from "../DB/DBConnection";
Expand Down Expand Up @@ -26,45 +26,48 @@ export class MatchModel {
primaryKey: true
},
"teamHome": {
type: STRING(30),
type: STRING(36),
references: {
model: this.teamModel.model,
key: "id"
}
},
"teamGuest": {
type: STRING(30),
type: STRING(36),
references: {
model: this.teamModel.model,
key: "id"
}
},
"matchCategoryId": {
type: STRING(30),
type: STRING(36),
references: {
model: this.matchCategory.model,
key: "id"
}
},
"coefficientWin1": {
type: REAL,
allowNull: false
type: REAL
},
"coefficientDraw": {
type: REAL,
allowNull: false
type: REAL
},
"coefficientWin2": {
type: REAL,
allowNull: false
type: REAL
},
"place": {
type: STRING(30)
},
"date": {
type: INTEGER,
type: BIGINT,
allowNull: false
},
"homeGoals": {
type: INTEGER
},
"guestGoals": {
type: INTEGER
},
"result": {
type: STRING(2)
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/models/TeamModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export class TeamModel {
primaryKey: true
},
"name": {
type: sequelize.STRING(20),
type: sequelize.STRING(30),
allowNull: false
},
"owner": {
type: sequelize.STRING(20),
type: sequelize.STRING(30),
allowNull: false,
},
"country": {
type: sequelize.STRING(20),
type: sequelize.STRING(30),
allowNull: false,
},
"year": {
Expand Down
105 changes: 105 additions & 0 deletions statistics/2012-13/en.1.clubs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"name": "English Premier League 2012/13",
"clubs": [
{
"key": "chelsea",
"name": "Chelsea",
"code": "CHE"
},
{
"key": "arsenal",
"name": "Arsenal",
"code": "ARS"
},
{
"key": "tottenham",
"name": "Tottenham Hotspur",
"code": "TOT"
},
{
"key": "westham",
"name": "West Ham United",
"code": "WHU"
},
{
"key": "manutd",
"name": "Manchester United",
"code": "MUN"
},
{
"key": "mancity",
"name": "Manchester City",
"code": "MCI"
},
{
"key": "everton",
"name": "Everton",
"code": "EVE"
},
{
"key": "liverpool",
"name": "Liverpool",
"code": "LIV"
},
{
"key": "westbrom",
"name": "West Bromwich Albion",
"code": "WBA"
},
{
"key": "newcastle",
"name": "Newcastle United",
"code": "NEW"
},
{
"key": "stoke",
"name": "Stoke City",
"code": "STK"
},
{
"key": "sunderland",
"name": "Sunderland",
"code": "SUN"
},
{
"key": "astonvilla",
"name": "Aston Villa",
"code": "AVL"
},
{
"key": "southampton",
"name": "Southampton",
"code": "SOU"
},
{
"key": "norwich",
"name": "Norwich",
"code": "NOR"
},
{
"key": "qpr",
"name": "Queens Park Rangers",
"code": "QPR"
},
{
"key": "fulham",
"name": "Fulham",
"code": "FUL"
},
{
"key": "reading",
"name": "Reading",
"code": "RDG"
},
{
"key": "swansea",
"name": "Swansea",
"code": "SWA"
},
{
"key": "wigan",
"name": "Wigan Athletic",
"code": "WIG"
}
]
}
Loading

0 comments on commit c1018cc

Please sign in to comment.