wip implement top ten, ish
This commit is contained in:
@@ -1,16 +1,42 @@
|
||||
import { ClientBase } from "pg";
|
||||
import * as sql from "sql-bricks";
|
||||
import * as sql from "sql-bricks-postgres";
|
||||
|
||||
import { _findProfile } from "./_util";
|
||||
import { ExtId } from "../model/base";
|
||||
import { ExtId, RouteNo } from "../model/base";
|
||||
import { Profile } from "../model/profile";
|
||||
import { TimeAttackScore } from "../model/timeAttack";
|
||||
import { TimeAttackRepository } from "../repo";
|
||||
import { TimeAttackRepository, TopTenResult } from "../repo";
|
||||
import { generateId } from "../../db";
|
||||
|
||||
export class SqlTimeAttackRepository implements TimeAttackRepository {
|
||||
constructor(private readonly _conn: ClientBase) {}
|
||||
|
||||
async loadTopTen(routeNo: RouteNo): Promise<TopTenResult[]> {
|
||||
const loadSql = sql
|
||||
.select("p.name", "ta.*")
|
||||
.from("idz.ta_best ta")
|
||||
.join("idz.profile p", { "ta.profile_id": "p.id" })
|
||||
.where("ta.route_no", routeNo)
|
||||
.orderBy(["ta.total_time asc", "ta.timestamp asc"])
|
||||
.limit(10)
|
||||
.toParams();
|
||||
|
||||
const { rows } = await this._conn.query(loadSql);
|
||||
|
||||
return rows.map(row => ({
|
||||
driverName: row.name,
|
||||
ta: {
|
||||
routeNo: row.route_no,
|
||||
timestamp: new Date(row.timestamp),
|
||||
flags: row.flags,
|
||||
totalTime: row.total_time,
|
||||
sectionTimes: row.section_times,
|
||||
grade: row.grade,
|
||||
carSelector: row.car_selector,
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
async loadAll(extId: ExtId<Profile>): Promise<TimeAttackScore[]> {
|
||||
const loadSql = sql
|
||||
.select("ta.*")
|
||||
|
||||
@@ -1,15 +1,60 @@
|
||||
import { LoadTopTenRequest } from "../request/loadTopTen";
|
||||
import { LoadTopTenResponse } from "../response/loadTopTen";
|
||||
import {
|
||||
LoadTopTenResponse,
|
||||
LoadTopTenResponseCourse,
|
||||
LoadTopTenResponseRow,
|
||||
} from "../response/loadTopTen";
|
||||
import { Repositories } from "../repo";
|
||||
|
||||
export function loadTopTen(
|
||||
export async function loadTopTen(
|
||||
w: Repositories,
|
||||
req: LoadTopTenRequest
|
||||
): LoadTopTenResponse {
|
||||
): Promise<LoadTopTenResponse> {
|
||||
const courses = new Array<LoadTopTenResponseCourse>();
|
||||
|
||||
for (const selector of req.selectors) {
|
||||
if (courses.length >= 3) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (selector.field_44 === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { routeNo } = selector;
|
||||
const src = await w.timeAttack().loadTopTen(routeNo);
|
||||
const dest = new Array<LoadTopTenResponseRow>();
|
||||
|
||||
for (const srcItem of src) {
|
||||
dest.push({
|
||||
field_00: 0,
|
||||
field_0E: false,
|
||||
field_0F: false,
|
||||
field_10: 0,
|
||||
driverName: srcItem.driverName,
|
||||
teamName: process.env.TEAM_NAME || "",
|
||||
shopName: process.env.SHOP_NAME || "",
|
||||
field_74: 0,
|
||||
field_78: 0,
|
||||
field_7C: 0,
|
||||
field_7D: 0,
|
||||
ta: srcItem.ta,
|
||||
});
|
||||
}
|
||||
|
||||
courses.push({
|
||||
routeNo,
|
||||
field_02: 0,
|
||||
rows: dest,
|
||||
});
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(courses));
|
||||
|
||||
return {
|
||||
type: "load_top_ten_res",
|
||||
totalSelected: 0,
|
||||
courses: [],
|
||||
courseCount: courses.length,
|
||||
courses: courses,
|
||||
trailers: [],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -65,7 +65,15 @@ export interface ProfileRepository {
|
||||
): Promise<Model.ExtId<Model.Profile>>;
|
||||
}
|
||||
|
||||
// TODO extend and factorize
|
||||
export interface TopTenResult {
|
||||
driverName: string;
|
||||
ta: Model.TimeAttackScore;
|
||||
}
|
||||
|
||||
export interface TimeAttackRepository {
|
||||
loadTopTen(routeNo: Model.RouteNo): Promise<TopTenResult[]>;
|
||||
|
||||
loadAll(
|
||||
profileId: Model.ExtId<Model.Profile>
|
||||
): Promise<Model.TimeAttackScore[]>;
|
||||
|
||||
Reference in New Issue
Block a user