idz: Wire up team create and load

This commit is contained in:
Tau
2019-11-06 17:28:30 -05:00
parent ec214d5beb
commit a3df4df277
21 changed files with 286 additions and 76 deletions
+25 -26
View File
@@ -1,34 +1,33 @@
import { ExtId } from "../model/base";
import { Team } from "../model/team";
import { JoinAutoTeamRequest } from "../request/joinAutoTeam";
import { LoadTeamRequest } from "../request/loadTeam";
import { JoinAutoTeamResponse } from "../response/joinAutoTeam";
import { LoadTeamResponse } from "../response/loadTeam";
import { Repositories } from "../repo";
import { Id } from "../../db";
// Even if a profile does not belong to a team, a team must still be loaded
// (and then ignored by the client).
// Bleh. This factorization is kind of messy.
const dummy: Team = {
extId: 0 as ExtId<Team>,
name: "",
nameBg: 0,
nameFx: 0,
registerTime: new Date(0),
};
export function _team(
export async function _fixupPrevTeam(
w: Repositories,
req: JoinAutoTeamRequest | LoadTeamRequest
): JoinAutoTeamResponse | LoadTeamResponse {
const bits = {
team: dummy,
members: [],
};
prevTeamId: Id<Team> | undefined
): Promise<void> {
if (prevTeamId === undefined) {
return;
}
if (req.type === "join_auto_team_req") {
return { type: "join_auto_team_res", ...bits };
} else {
return { type: "load_team_res", ...bits };
const remaining = await w.teamMembers().loadRoster(prevTeamId);
if (remaining.length === 0) {
// Last member left, GC previous team
await w.teams().delete(prevTeamId);
} else if (remaining.find(member => member.leader) === undefined) {
// Leader left, appoint new leader by seniority
remaining.sort((x, y) => x.joinTime.getTime() - y.joinTime.getTime());
// (need to look up new leader's db id from aime id. ick)
const newLeader = remaining[remaining.length - 1];
const newLeaderId = await w.profile().find(newLeader.profile.aimeId);
await w.teamMembers().makeLeader(prevTeamId, newLeaderId);
}
}
+121
View File
@@ -0,0 +1,121 @@
import { TeamAuto } from "../model/team";
import { Repositories } from "../repo";
import { CreateAutoTeamRequest } from "../request/createAutoTeam";
import { CreateAutoTeamResponse } from "../response/createAutoTeam";
interface AutoTeamTemplate {
prefix: string;
nameBg: number;
}
// Hard-code these for the time being, since AFAIK there are only three.
// These are all references to teams in the Initial D manga/anime.
// If any more auto-teams are added/discovered they *MUST* be added to the
// *END* of this list. Otherwise duplicate auto-teams will be created.
const autoTeams: AutoTeamTemplate[] = [
{
// "Speed Stars"
prefix: "スピードスターズ",
nameBg: 0,
},
{
// "Red Suns"
prefix: "レッドサンズ",
nameBg: 1,
},
{
// "Night Kids" (even though it's written like "Night Keys"...)
prefix: "ナイトキッズ",
nameBg: 2,
},
];
function incrementAuto(prev: TeamAuto): TeamAuto {
if (prev.nameIdx < autoTeams.length - 1) {
return { nameIdx: prev.nameIdx + 1, serialNo: prev.serialNo };
} else {
return { nameIdx: 0, serialNo: prev.serialNo + 1 };
}
}
export async function createAutoTeam(
w: Repositories,
req: CreateAutoTeamRequest
): Promise<CreateAutoTeamResponse> {
const now = new Date();
const { aimeId } = req;
const peek = await w.teamAuto().peek();
let nextAuto: TeamAuto;
//
// Determine if we need to create a new team or not
//
if (peek !== undefined) {
// Look at the highest-numbered auto team. Is it full?
const [lastAuto, lastTeamId] = peek;
const occupancy = await w.teamReservations().occupancyHack(lastTeamId);
console.log(occupancy);
if (occupancy < 6) {
// Team isn't full, so return this one
await w.teamReservations().reserveHack(lastTeamId, aimeId, now);
return {
type: "create_auto_team_res",
team: await w.teams().load(lastTeamId),
members: await w.teamMembers().loadRoster(lastTeamId),
};
}
// Team full, need to create a new one
nextAuto = incrementAuto(lastAuto);
} else {
// No teams exist at all, seed the system with SpeedStars001.
nextAuto = { serialNo: 1, nameIdx: 0 };
}
//
// Build the new team
//
// Make a three-digit serial number using full-width digits
let { serialNo, nameIdx } = nextAuto;
let name = "";
for (let i = 0; i < 3; i++) {
name = String.fromCodePoint(0xff10 + (serialNo % 10)) + name;
serialNo = (serialNo / 10) | 0;
}
// Prepend the name prefix
name = autoTeams[nameIdx].prefix + name;
// Register the new team, make the requestor its leader
const spec = {
name,
nameBg: autoTeams[nameIdx].nameBg,
nameFx: 0,
registerTime: now,
};
const [newTeamId, newTeamExtId] = await w.teams().create(spec);
await w.teamAuto().push(newTeamId, nextAuto);
await w.teamReservations().reserveHack(newTeamId, aimeId, now, "leader");
return {
type: "create_auto_team_res",
team: { ...spec, extId: newTeamExtId },
members: [],
};
}
+2
View File
@@ -47,6 +47,8 @@ export async function createProfile(
await w.unlocks().save(profileId, unlocks);
await w.tickets().save(profileId, {});
await w.teamReservations().commitHack(aimeId);
return {
type: "generic_res",
status: aimeId, // "Generic response" my fucking *ass*
+47 -5
View File
@@ -1,16 +1,58 @@
import { ExtId } from "../model/base";
import { Team } from "../model/team";
import { _fixupPrevTeam } from "./_team";
import { CreateTeamRequest } from "../request/createTeam";
import { CreateTeamResponse } from "../response/createTeam";
import { Repositories } from "../repo";
export function createTeam(
export async function createTeam(
w: Repositories,
req: CreateTeamRequest
): CreateTeamResponse {
): Promise<CreateTeamResponse> {
const profileId = await w.profile().find(req.aimeId);
const prevTeamId = await w.teamMembers().findTeam(profileId);
const now = new Date();
// Create the new team...
const teamSpec = {
name: req.teamName,
nameBg: req.nameBg,
nameFx: 0,
registerTime: now,
};
const [teamId, teamExtId] = await w.teams().create(teamSpec);
await w.teamMembers().join(teamId, profileId, now);
await w.teamMembers().makeLeader(teamId, profileId);
await _fixupPrevTeam(w, prevTeamId);
// Fix up previous team. The previous team's extid is explicitly sent in the
// request, but why rely on it if you don't have to?
if (prevTeamId !== undefined) {
const remaining = await w.teamMembers().loadRoster(prevTeamId);
if (remaining.length === 0) {
// Last member left, GC previous team
await w.teams().delete(prevTeamId);
} else if (remaining.find(member => member.leader) === undefined) {
// Leader left, appoint new leader by seniority
remaining.sort((x, y) => x.joinTime.getTime() - y.joinTime.getTime());
// (need to look up new leader's db id from aime id. ick)
const newLeader = remaining[remaining.length - 1];
const newLeaderId = await w.profile().find(newLeader.profile.aimeId);
await w.teamMembers().makeLeader(prevTeamId, newLeaderId);
}
}
return {
type: "create_team_res",
status: 0,
teamExtId: 3 as ExtId<Team>,
teamExtId,
};
}
+6 -5
View File
@@ -1,5 +1,5 @@
import { _team } from "./_team";
import { checkTeamName } from "./checkTeamName";
import { createAutoTeam } from "./createAutoTeam";
import { createProfile } from "./createProfile";
import { createTeam } from "./createTeam";
import { discoverProfile } from "./discoverProfile";
@@ -15,6 +15,7 @@ import { loadProfile } from "./loadProfile";
import { loadReward as loadRewardTable } from "./loadRewardTable";
import { loadServerList } from "./loadServerList";
import { loadStocker } from "./loadStocker";
import { loadTeam } from "./loadTeam";
import { loadTeamRanking } from "./loadTeamRanking";
import { loadTopTen } from "./loadTopTen";
import { lockGarage } from "./lockGarage";
@@ -48,15 +49,15 @@ export async function dispatch(
case "check_team_name_req":
return checkTeamName(w, req);
case "create_auto_team_req":
return createAutoTeam(w, req);
case "create_profile_req":
return createProfile(w, req);
case "create_team_req":
return createTeam(w, req);
case "join_auto_team_req":
return _team(w, req);
case "load_2on2_req":
return load2on2(w, req);
@@ -97,7 +98,7 @@ export async function dispatch(
return loadStocker(w, req);
case "load_team_req":
return _team(w, req);
return loadTeam(w, req);
case "load_top_ten_req":
return loadTopTen(w, req);
+5 -1
View File
@@ -9,6 +9,8 @@ export async function loadProfile(
const { aimeId } = req;
const profileId = await w.profile().find(aimeId);
const teamId = await w.teamMembers().findTeam(profileId);
const leaderId = teamId && (await w.teamMembers().findLeader(teamId));
// Promise.all would be messy here, who cares anyway this isn't supposed to
// be a high-performance server.
@@ -25,6 +27,7 @@ export async function loadProfile(
const timeAttack = await w.timeAttack().loadAll(profileId);
const unlocks = await w.unlocks().load(profileId);
const tickets = await w.tickets().load(profileId);
const team = teamId && (await w.teams().load(teamId));
return {
type: "load_profile_res",
@@ -36,7 +39,8 @@ export async function loadProfile(
fame: profile.fame,
dpoint: profile.dpoint,
mileage: profile.mileage,
// teamId: TODO
teamId: team && team.extId,
teamLeader: profileId === leaderId,
settings,
chara,
titles,
+37
View File
@@ -0,0 +1,37 @@
import { ExtId } from "../model/base";
import { Team } from "../model/team";
import { LoadTeamRequest } from "../request/loadTeam";
import { LoadTeamResponse } from "../response/loadTeam";
import { Repositories } from "../repo";
// Even if a profile does not belong to a team, a team must still be loaded
// (and then ignored by the client).
const dummyResp: LoadTeamResponse = {
type: "load_team_res",
team: {
extId: 0 as ExtId<Team>,
name: "",
nameBg: 0,
nameFx: 0,
registerTime: new Date(0),
},
members: [],
};
export async function loadTeam(
w: Repositories,
req: LoadTeamRequest
): Promise<LoadTeamResponse> {
if (req.teamExtId === undefined) {
return dummyResp;
}
const teamId = await w.teams().find(req.teamExtId);
return {
type: "load_team_res",
team: await w.teams().load(teamId),
members: await w.teamMembers().loadRoster(teamId),
};
}