Skip to content

Commit

Permalink
Separated upload types. Implemented FGC API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle-flynn committed Aug 14, 2019
1 parent b535d8c commit e814720
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 78 deletions.
3 changes: 2 additions & 1 deletion ems-api/src/controllers/Ranking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ router.get("/calculate/:tournament_level", (req: Request, res: Response, next: N
logger.info(`Re-calculating rankings for ${req.query.type}.`);
const ranker: IMatchRanker = getRankerByType(req.query.type);
const rankJSON: any = ranker.execute(rows).map((rank: IPostableObject) => rank.toJSON());
if (rankJSON.length > 0){
if (rankJSON.length > 0) {
const promises: Array<Promise<any>> = [];
for (const ranking of rankJSON) {
if (typeof ranking.team !== "undefined") delete ranking.team;
promises.push(DatabaseManager.updateWhere("ranking", ranking, "rank_key=\"" + ranking.rank_key + "\""));
}
Promise.all(promises).then((values: any[]) => {
Expand Down
2 changes: 1 addition & 1 deletion ems-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"main": "build/electron.js",
"dependencies": {
"@the-orange-alliance/lib-ems": "^0.5.7",
"@the-orange-alliance/lib-ems": "^0.5.12",
"axios": "^0.19.0",
"dotenv": "^8.0.0",
"local-ipv4-address": "0.0.2",
Expand Down
8 changes: 3 additions & 5 deletions ems-core/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
incrementCompletedStep, setAllianceMembers, setSocketConnected,
} from "./stores/internal/actions";

import {EMSProvider, FGCProvider, TOAProvider, SocketProvider, WebProvider, TOAConfig, MatchConfiguration,
import {EMSProvider, SocketProvider, WebProvider, TOAConfig, MatchConfiguration,
AllianceMember} from "@the-orange-alliance/lib-ems";
import UploadManager from "./managers/UploadManager";

interface IProps {
slaveModeEnabled: boolean,
Expand All @@ -28,12 +29,9 @@ class App extends React.Component<IProps> {

public componentDidMount() {
if (this.props.toaConfig.enabled) {
TOAProvider.initialize(this.props.toaConfig);
UploadManager.initialize(1, this.props.toaConfig);
}

// Debug
FGCProvider.initialize("127.0.0.1", 8088);

this.initializeSocket(this.props.networkHost);
WebProvider.initialize(this.props.networkHost);
if (this.props.slaveModeEnabled) {
Expand Down
1 change: 0 additions & 1 deletion ems-core/src/components/SetupMatchScheduleOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ class SetupMatchScheduleOverview extends React.Component<IProps, IState> {
private publish() {
this.closeConfirmModal();
this.props.onComplete(this.state.postOnline);
console.log(this.state.postOnline);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {EMSProvider, FGCProvider, HttpError, Match, MatchDetails, MatchParticipant, Ranking, Team} from "@the-orange-alliance/lib-ems";
import {
EMSProvider, Event, FGCProvider, HttpError, Match, MatchDetails, MatchParticipant, Ranking, Team
} from "@the-orange-alliance/lib-ems";
import TeamValidator from "../validators/TeamValidator";

class FGCUploadedManager {
private static _instance: FGCUploadedManager;
Expand All @@ -12,6 +15,39 @@ class FGCUploadedManager {

private constructor() {}

public getEvent(eventKey: string): Promise<Event> {
return new Promise<Event>((resolve, reject) => {
FGCProvider.getEvent(eventKey).then((event: Event) => {
if (event && event.eventKey && event.eventKey.length > 0) {
resolve(event);
} else {
reject();
}
}).catch((error: HttpError) => {
reject(error);
});

});
}

public getTeams(eventKey: string): Promise<Team[]> {
return new Promise<Team[]>((resolve, reject) => {
FGCProvider.getTeams(eventKey).then((participants: Team[]) => {
const teams: Team[] = [];
for (const participant of participants) {
const validator: TeamValidator = new TeamValidator(participant);
validator.update(participant);
if (validator.isValid) {
teams.push(participant);
}
}
resolve(teams);
}).catch((error: HttpError) => {
reject(error);
});
});
}

public postEventParticipants(eventKey: string, teams: Team[]): Promise<any> {
return new Promise<any>((resolve, reject) => {
FGCProvider.deleteTeams(eventKey).then(() => {
Expand All @@ -33,7 +69,7 @@ class FGCUploadedManager {
FGCProvider.deleteMatchData(eventKey, matches[0].tournamentLevel).then(() => {
setTimeout(() => {
const fgcMatches: Match[] = matches.map((m: Match) => m);
const fgcDetails: MatchDetails[] = matches.map((m: Match) => m.matchDetails);
const fgcDetails: MatchDetails[] = matches.map((m: Match) => new MatchDetails().fromJSON({match_key: m.matchKey, match_detail_key: m.matchDetailKey}));
const fgcParticipants: MatchParticipant[] = [];
for (const match of matches) {
for (const participant of match.participants) {
Expand Down
44 changes: 42 additions & 2 deletions ems-core/src/managers/TOAUploadManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {EMSProvider, HttpError, Match, MatchDetails, MatchParticipant, Ranking, Team,
import {
EMSProvider, Event, HttpError, Match, MatchDetails, MatchParticipant, Ranking, Team,
TOAEventParticipant, TOAEventParticipantAdapter, TOAMatch, TOAMatchAdapter, TOAMatchDetails, TOAMatchDetailsAdapter,
TOAMatchParticipant, TOAMatchParticipantAdapter, TOAProvider, TOARanking, TOARankingAdapter
TOAMatchParticipant, TOAMatchParticipantAdapter, TOAProvider, TOARanking, TOARankingAdapter, EMSEventAdapter,
TOAEvent, EMSTeamAdapter
} from "@the-orange-alliance/lib-ems";
import TeamValidator from "../validators/TeamValidator";

class TOAUploadManager {
private static _instance: TOAUploadManager;
Expand All @@ -15,6 +18,43 @@ class TOAUploadManager {

private constructor() {}

public getEvent(eventKey: string): Promise<Event> {
return new Promise<Event>((resolve, reject) => {
TOAProvider.getEvent(eventKey).then((event: TOAEvent) => {
if (event && event.eventKey && event.eventKey.length > 0) {
resolve(new EMSEventAdapter(event).get());
} else {
reject();
}
}).catch((error: HttpError) => {
reject(error);
});

});
}

public getTeams(eventKey: string): Promise<Team[]> {
return new Promise<Team[]>((resolve, reject) => {
TOAProvider.getTeams(eventKey).then((toaParticipants: TOAEventParticipant[]) => {
const teams: Team[] = [];
for (const toaParticipant of toaParticipants) {
if (typeof toaParticipant.team !== "undefined") {
const team: Team = new EMSTeamAdapter(toaParticipant.team).get();
team.participantKey = toaParticipant.eventParticipantKey;
const validator: TeamValidator = new TeamValidator(team);
validator.update(team);
if (validator.isValid) {
teams.push(team);
}
}
}
resolve(teams);
}).catch((error: HttpError) => {
reject(error);
});
});
}

public postEventParticipants(eventKey: string, teams: Team[]): Promise<any> {
return new Promise<any>((resolve, reject) => {
TOAProvider.deleteTeams(eventKey).then(() => {
Expand Down
87 changes: 87 additions & 0 deletions ems-core/src/managers/UploadManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import {Event, FGCProvider, Match, Team, TOAConfig, TOAProvider} from "@the-orange-alliance/lib-ems";
import TOAUploadManager from "./TOAUploadManager";
import FGCUploadManager from "./FGCUploadManager";

class UploadManager {
public static TOA: number = 0;
public static FGC: number = 1;

private static _instance: UploadManager;

private _type: number;

public static getInstance(): UploadManager {
if (typeof UploadManager._instance === "undefined") {
UploadManager._instance = new UploadManager();
}
return UploadManager._instance;
}

private constructor() {}

public initialize(type: number, toaConfig: TOAConfig): void {
this._type = type;
if (this._type === UploadManager.TOA) {
TOAProvider.initialize(toaConfig);
}
if (this._type === UploadManager.FGC) {
FGCProvider.initialize("127.0.0.1", 8088); // DEBUG
}
}

public getEvent(eventKey: string): Promise<Event> {
if (this._type === UploadManager.TOA) {
return TOAUploadManager.getEvent(eventKey);
} else if (this._type === UploadManager.FGC) {
return FGCUploadManager.getEvent(eventKey);
} else {
return new Promise<Event>((resolve, reject) => reject());
}
}

public getTeams(eventKey: string): Promise<Team[]> {
if (this._type === UploadManager.TOA) {
return TOAUploadManager.getTeams(eventKey);
} else if (this._type === UploadManager.FGC) {
return FGCUploadManager.getTeams(eventKey);
} else {
return new Promise<Team[]>((resolve, reject) => reject());
}
}

public postEventParticipants(eventKey: string, teams: Team[]): Promise<any> {
if (this._type === UploadManager.TOA) {
return TOAUploadManager.postEventParticipants(eventKey, teams);
} else if (this._type === UploadManager.FGC) {
return FGCUploadManager.postEventParticipants(eventKey, teams);
} else {
return new Promise<any>((resolve, reject) => reject());
}
}

public postMatchSchedule(eventKey: string, matches: Match[]): Promise<any> {
if (this._type === UploadManager.TOA) {
return TOAUploadManager.postMatchSchedule(eventKey, matches);
} else if (this._type === UploadManager.FGC) {
return FGCUploadManager.postMatchSchedule(eventKey, matches);
} else {
return new Promise<any>((resolve, reject) => reject());
}
}

public postMatchResults(eventKey: string, match: Match): Promise<any> {
if (this._type === UploadManager.TOA) {
return TOAUploadManager.postMatchResults(eventKey, match);
} else if (this._type === UploadManager.FGC) {
return FGCUploadManager.postMatchResults(eventKey, match);
} else {
return new Promise<any>((resolve, reject) => reject());
}
}

}

export const TOA = UploadManager.TOA;
export const FGC = UploadManager.FGC;

export default UploadManager.getInstance();
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import TeamValidator from "../../../validators/TeamValidator";
import EventCreationManager from "../../../managers/EventCreationManager";
// import TOAUploadManager from "../../../managers/TOAUploadManager";
import {
EMSTeamAdapter, Event, EventConfiguration, HttpError, Team, TOAEventParticipant, TOAConfig, TOAProvider,
Event, EventConfiguration, HttpError, Team, TOAConfig,
EliminationMatchesFormat
} from "@the-orange-alliance/lib-ems";
import FGCUploadedManager from "../../../managers/FGCUploadedManager";
import UploadManager from "../../../managers/UploadManager";

interface IProps {
onComplete: () => void,
Expand Down Expand Up @@ -61,7 +61,7 @@ class EventParticipantSelection extends React.Component<IProps, IState> {
this.createTestTeam = this.createTestTeam.bind(this);
this.removeTeam = this.removeTeam.bind(this);
this.importTeamsByCSV = this.importTeamsByCSV.bind(this);
this.importByTOA = this.importByTOA.bind(this);
this.importOnline = this.importOnline.bind(this);
this.createTeamList = this.createTeamList.bind(this);
}

Expand Down Expand Up @@ -109,7 +109,7 @@ class EventParticipantSelection extends React.Component<IProps, IState> {
<Button color={getTheme().primary} loading={loadingTeams} disabled={loadingTeams} onClick={this.importTeamsByCSV}>Import By CSV</Button>
{
toaConfig.enabled &&
<Button color={getTheme().primary} loading={loadingTeams} disabled={loadingTeams} onClick={this.importByTOA}>Import From TOA</Button>
<Button color={getTheme().primary} loading={loadingTeams} disabled={loadingTeams} onClick={this.importOnline}>Import From TOA</Button>
}
</div>
<div>
Expand All @@ -130,13 +130,8 @@ class EventParticipantSelection extends React.Component<IProps, IState> {
updatedTeams[i].participantKey = this.props.event.eventKey + "-T" + this.props.teams[i].teamKey;
}
if (this.props.toaConfig.enabled) {
// TOAUploadManager.postEventParticipants(this.props.event.eventKey, updatedTeams).then(() => {
// console.log(`${updatedTeams.length} teams have been posted to TOA`);
// }).catch((error: HttpError) => {
// DialogManager.showErrorBox(error);
// });
FGCUploadedManager.postEventParticipants(this.props.event.eventKey, updatedTeams).then(() => {
console.log(`${updatedTeams.length} teams have been posted to TOA`);
UploadManager.postEventParticipants(this.props.event.eventKey, updatedTeams).then(() => {
console.log(`${updatedTeams.length} teams have been posted online.`);
}).catch((error: HttpError) => {
DialogManager.showErrorBox(error);
});
Expand Down Expand Up @@ -198,33 +193,14 @@ class EventParticipantSelection extends React.Component<IProps, IState> {
});
}

private importByTOA() {
private importOnline() {
this.setState({loadingTeams: true});
this.props.setNavigationDisabled(true);
TOAProvider.getTeams(this.props.event.eventKey).then((toaParticipants: TOAEventParticipant[]) => {
const teams: Team[] = [];
const failedImports: number[] = [];
for (const toaParticipant of toaParticipants) {
if (typeof toaParticipant.team !== "undefined") {
const team: Team = new EMSTeamAdapter(toaParticipant.team).get();
team.participantKey = toaParticipant.eventParticipantKey;
const validator: TeamValidator = new TeamValidator(team);
validator.update(team);
if (validator.isValid) {
teams.push(team);
} else {
failedImports.push(team.teamKey);
}
} else {
failedImports.push(toaParticipant.teamKey);
}
}
UploadManager.getTeams(this.props.event.eventKey).then((teams: Team[]) => {
this.setState({loadingTeams: false});
this.props.setNavigationDisabled(false);
this.props.setTeamList(teams);
setTimeout(() => {
DialogManager.showInfoBox("Team Import Result", "Imported " + teams.length + " of original " + toaParticipants.length + ". The following teams were not imported due to parsing errors: " + failedImports.toString());
}, 250);
DialogManager.showInfoBox("Team Import Result", "Imported " + teams.length + " teams from online.");
}).catch((error: HttpError) => {
this.setState({loadingTeams: false});
this.props.setNavigationDisabled(false);
Expand Down
6 changes: 3 additions & 3 deletions ems-core/src/views/event-manager/views/EventPracticeSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {Dispatch} from "redux";
import {disableNavigation, setPracticeMatches} from "../../../stores/internal/actions";
import EventCreationManager from "../../../managers/EventCreationManager";
import DialogManager from "../../../managers/DialogManager";
import TOAUploadManager from "../../../managers/TOAUploadManager";
import {Event, EventConfiguration, HttpError, Match, Schedule, ScheduleItem, Team, TOAConfig} from "@the-orange-alliance/lib-ems";
import SetupScheduleParticipants from "../../../components/SetupScheduleParticipants";
import UploadManager from "../../../managers/UploadManager";

interface IProps {
onComplete: () => void,
Expand Down Expand Up @@ -95,8 +95,8 @@ class EventPracticeSetup extends React.Component<IProps, IState> {
private onPublishSchedule(postOnline: boolean) {
this.props.setNavigationDisabled(true);
if (postOnline && this.props.toaConfig.enabled) {
TOAUploadManager.postMatchSchedule(this.props.event.eventKey, this.props.practiceMatches).then(() => {
console.log(`${this.props.practiceMatches.length} matches have been posted to TOA.`);
UploadManager.postMatchSchedule(this.props.event.eventKey, this.props.practiceMatches).then(() => {
console.log(`${this.props.practiceMatches.length} matches have been posted online.`);
}).catch((error: HttpError) => {
DialogManager.showErrorBox(error);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {Dispatch} from "redux";
import {disableNavigation, setQualificationMatches} from "../../../stores/internal/actions";
import EventCreationManager from "../../../managers/EventCreationManager";
import DialogManager from "../../../managers/DialogManager";
import TOAUploadManager from "../../../managers/TOAUploadManager";
import {Event, EventConfiguration, HttpError, Match, Schedule, ScheduleItem, Team, TOAConfig} from "@the-orange-alliance/lib-ems";
import SetupScheduleParticipants from "../../../components/SetupScheduleParticipants";
import UploadManager from "../../../managers/UploadManager";

interface IProps {
onComplete: () => void,
Expand Down Expand Up @@ -95,7 +95,7 @@ class EventQualificationSetup extends React.Component<IProps, IState> {
private onPublishSchedule() {
this.props.setNavigationDisabled(true);
if (this.props.toaConfig.enabled) {
TOAUploadManager.postMatchSchedule(this.props.event.eventKey, this.props.qualificationMatches).then(() => {
UploadManager.postMatchSchedule(this.props.event.eventKey, this.props.qualificationMatches).then(() => {
console.log(`${this.props.qualificationMatches.length} matches have been posted to TOA.`);
}).catch((error: HttpError) => {
DialogManager.showErrorBox(error);
Expand Down
Loading

0 comments on commit e814720

Please sign in to comment.