Skip to content

Commit

Permalink
Platform init (#1043)
Browse files Browse the repository at this point in the history
* step 1a. Add gameId unrequired to schemas. Add gameId to reqUser.

* Step 1b. Add gameId to LevelModel.creates

* Step 1b. Add gameId to ReviewModel.creates

* update some tests

* fix test

* move GameId to constants/

* Step 1b. Add gameId to CollectionModel.creates

* Step 1b. Add gameId to PlayAttemptModel.creates

* Step 1b. Add gameId to StatModel.creates

* Step 1b. Add gameId to RecordModel.creates

* play-attempt upsert gameid

* unpublish.test  promise.all
  • Loading branch information
k2xl authored Nov 13, 2023
1 parent eff9f50 commit 448d6c4
Show file tree
Hide file tree
Showing 49 changed files with 218 additions and 70 deletions.
3 changes: 3 additions & 0 deletions constants/GameId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum GameId {
PATHOLOGY = 'pathology'
}
4 changes: 4 additions & 0 deletions lib/initializeLocalDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PASSWORD_SALTROUNDS } from '@root/models/schemas/userSchema';
import { getNewUserConfig } from '@root/pages/api/user-config';
import bcrypt from 'bcryptjs';
import { Types } from 'mongoose';
import { GameId } from '../constants/GameId';
import Role from '../constants/role';
import TestId from '../constants/testId';
import { generateCollectionSlug, generateLevelSlug } from '../helpers/generateSlug';
Expand Down Expand Up @@ -356,6 +357,7 @@ export async function initLevel(userId: string, name: string, obj: Partial<Level
// based on name length create that many reviews
const lvl = await LevelModel.create({
_id: id,
gameId: GameId.PATHOLOGY,
authorNote: 'test level ' + name + ' author note',
data: '40000\n12000\n05000\n67890\nABCD3',
height: 5,
Expand All @@ -375,6 +377,7 @@ export async function initLevel(userId: string, name: string, obj: Partial<Level
for (let i = 0; i < name.length; i++) {
revs.push({
_id: new Types.ObjectId(),
gameId: GameId.PATHOLOGY,
levelId: id,
score: (3903 * i * i + 33 * i) % 5 + 1,
text: 'Game is OK',
Expand All @@ -393,6 +396,7 @@ export async function initCollection(userId: string, name: string, obj: Partial<
const id = new Types.ObjectId();
const collection = await CollectionModel.create({
_id: id,
gameId: GameId.PATHOLOGY,
authorNote: 'test collection ' + name + ' author note',
name: name,
userId: userId,
Expand Down
30 changes: 30 additions & 0 deletions lib/withAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import jwt, { JwtPayload } from 'jsonwebtoken';
// https://github.com/newrelic/node-newrelic/issues/956#issuecomment-962729137
import type { NextApiRequest, NextApiResponse } from 'next';
import requestIp from 'request-ip';
import { GameId } from '../constants/GameId';
import { parseReq, ReqValidator } from '../helpers/apiWrapper';
import { TimerUtil } from '../helpers/getTs';
import { logger } from '../helpers/logger';
Expand All @@ -11,9 +12,28 @@ import dbConnect from './dbConnect';
import getTokenCookie from './getTokenCookie';
import isLocal from './isLocal';

export enum GameType {
SHORTEST_PATH = 'SHORTEST_PATH',

}
interface Game {
id: GameId;
displayName: string;
type: GameType;
}

export const Games: Record<GameId, Game> = {
[GameId.PATHOLOGY]: {
id: GameId.PATHOLOGY,
displayName: 'Pathology',
type: GameType.SHORTEST_PATH,
}
};

export type NextApiRequestWithAuth = NextApiRequest & {
user: User;
userId: string;
gameId: GameId;
};

export async function getUserFromToken(
Expand Down Expand Up @@ -111,6 +131,16 @@ export default function withAuth(
);

res.setHeader('Set-Cookie', refreshCookie);

const subdomain = req.headers?.referer?.split('://')[1].split('.')[0];

/*if (!subdomain || !Games[subdomain]) {
return res.status(401).json({
error: 'Unauthorized: Game not selected',
});
}*/

req.gameId = Games[subdomain as GameId]?.id || GameId.PATHOLOGY;
req.user = reqUser;
req.userId = reqUser._id.toString();

Expand Down
1 change: 1 addition & 0 deletions models/db/achievement.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import User from './user';
interface Achievement {
_id: Types.ObjectId;
createdAt: Date;
gameId?: string;
type: AchievementType;
updatedAt: Date;
userId: Types.ObjectId & User;
Expand Down
3 changes: 3 additions & 0 deletions models/db/campaign.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Types } from 'mongoose';
import Collection, { EnrichedCollection } from './collection';
import Game from './game';

interface Campaign {
_id: Types.ObjectId;
authorNote?: string;
collections: Types.Array<Types.ObjectId & Collection> | EnrichedCollection[];
collectionsPopulated?: Types.Array<Types.ObjectId & Collection> | EnrichedCollection[]; // virtual
gameId?: string;
name: string;
slug: string;

}

export interface EnrichedCampaign extends Campaign {
Expand Down
1 change: 1 addition & 0 deletions models/db/collection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Collection {
authorNote?: string;
createdAt: Date;
isPrivate?: boolean;
gameId?: string;
levels: Types.Array<Types.ObjectId & Level> | EnrichedLevel[];
levelsPopulated?: Types.Array<Types.ObjectId & Level> | EnrichedLevel[]; // virtual
name: string;
Expand Down
1 change: 1 addition & 0 deletions models/db/emailLog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface EmailLog {
_id: Types.ObjectId;
batchId: Types.ObjectId;
createdAt: Date;
gameId?: string;
error: string,
state: EmailState;
subject: string;
Expand Down
1 change: 1 addition & 0 deletions models/db/keyValue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Schema } from 'mongoose';
interface KeyValue {
key: string;
value: Schema.Types.Mixed;
gameId?: string;
}

export default KeyValue;
1 change: 1 addition & 0 deletions models/db/level.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Level {
calc_reviews_score_laplace: number;
calc_stats_players_beaten: number;
data: string;
gameId?: string;
height: number;
isDeleted: boolean;
isDraft: boolean;
Expand Down
1 change: 1 addition & 0 deletions models/db/multiplayerMatch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface MultiplayerMatch {
createdAt: Date;
createdBy: User;
endTime: Date;
gameId?: string;
gameTable?: {
[key: string]: Level[];
};
Expand Down
1 change: 1 addition & 0 deletions models/db/multiplayerProfile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface MultiplayerProfile {
calcRushRapidCount: number;
calcRushClassicalCount: number;

gameId?: string;
// elo
ratingRushBullet: number;
ratingRushBlitz: number;
Expand Down
1 change: 1 addition & 0 deletions models/db/notification.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Notification {
_id: Types.ObjectId;
createdAt: Date;
message?: string;
gameId?: string;
read: boolean;
// the object that initiates the notification
source: User | Achievement | null;
Expand Down
1 change: 1 addition & 0 deletions models/db/playAttempt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface PlayAttempt {
_id: Types.ObjectId;
attemptContext: AttemptContext;
endTime: number;
gameId?: string;
isDeleted: boolean;
levelId: Types.ObjectId | Level;
startTime: number;
Expand Down
1 change: 1 addition & 0 deletions models/db/record.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import User from './user';
interface Record {
_id: Types.ObjectId;
isDeleted: boolean;
gameId?: string;
levelId: Types.ObjectId & Level;
moves: number;
ts: number;
Expand Down
1 change: 1 addition & 0 deletions models/db/review.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import User from './user';

interface Review {
_id: Types.ObjectId;
gameId?: string;
isDeleted: boolean;
levelId: (Types.ObjectId & Level) | EnrichedLevel;
score: number;
Expand Down
1 change: 1 addition & 0 deletions models/db/stat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Stat {
_id: Types.ObjectId;
attempts: number;
complete: boolean;
gameId?: string;
isDeleted: boolean;
levelId: Types.ObjectId & Level;
moves: number;
Expand Down
1 change: 1 addition & 0 deletions models/db/userConfig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface UserConfig {
_id: Types.ObjectId;
disallowedEmailNotifications: NotificationType[];
disallowedPushNotifications: NotificationType[];
gameId?: string;
emailConfirmationToken: string;
emailConfirmed: boolean;
emailDigest: EmailDigestSettingTypes;
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/achievementSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const AchievementSchema = new mongoose.Schema<Achievement>({
enum: AchievementType,
required: true,
},
gameId: {
type: String,
required: false,
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
Expand Down
5 changes: 5 additions & 0 deletions models/schemas/campaignSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const CampaignSchema = new mongoose.Schema<Campaign>({
type: mongoose.Schema.Types.ObjectId,
ref: 'Collection',
}],
gameId: {
type: String,
required: false,
},
name: {
type: String,
minlength: 1,
Expand All @@ -24,6 +28,7 @@ const CampaignSchema = new mongoose.Schema<Campaign>({
type: String,
required: true,
},

}, {
timestamps: true,
collation: {
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/collectionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const CollectionSchema = new mongoose.Schema<Collection>({
type: Boolean,
default: false,
},
gameId: {
type: String,
required: false,
},
isThemed: {
type: Boolean,
default: false,
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/emailLogSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const EmailLogSchema = new mongoose.Schema<EmailLog>(
type: mongoose.Schema.Types.ObjectId,
required: true,
},
gameId: {
type: String,
required: false,
},
error: {
type: String,
required: false,
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/keyValueSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const KeyValueSchema = new mongoose.Schema<KeyValue>(
minlength: 1,
maxlength: 50,
},
gameId: {
type: String,
required: false,
},
},
{
timestamps: true,
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/levelSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ const LevelSchema = new mongoose.Schema<Level>(
required: false,
default: 0
},
gameId: {
type: String,
required: false,
},
// https://github.com/sspenst/pathology/wiki/Level-data-format
data: {
type: String,
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/multiplayerMatchSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const MultiplayerMatchSchema = new mongoose.Schema<MultiplayerMatch>(
endTime: {
type: Date,
},
gameId: {
type: String,
required: false,
},
levels: [
{
type: mongoose.Schema.Types.ObjectId,
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/multiplayerProfileSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const MultiplayerProfileSchema = new mongoose.Schema<MultiplayerProfile>(
required: true,
default: 0,
},
gameId: {
type: String,
required: false,
},
ratingRushBullet: {
type: Number,
required: true,
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/notificationSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const NotificationSchema = new mongoose.Schema<Notification>({
required: true,
default: false,
},
gameId: {
type: String,
required: false,
},
source: {
type: mongoose.Schema.Types.ObjectId,
refPath: 'sourceModel',
Expand Down
5 changes: 5 additions & 0 deletions models/schemas/playAttemptSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const PlayAttemptSchema = new mongoose.Schema<PlayAttempt>({
type: Number,
required: true,
},
gameId: {
type: String,
required: false,
},

isDeleted: {
type: Boolean,
},
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/recordSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const RecordSchema = new mongoose.Schema<Record>({
isDeleted: {
type: Boolean,
},
gameId: {
type: String,
required: false,
},
levelId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Level',
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/reviewSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const ReviewSchema = new mongoose.Schema<Review>({
isDeleted: {
type: Boolean,
},
gameId: {
type: String,
required: false,
},
levelId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Level',
Expand Down
3 changes: 3 additions & 0 deletions models/schemas/statSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const StatSchema = new mongoose.Schema<Stat>({
type: Boolean,
required: true,
},
gameId: {
type: String
},
isDeleted: {
type: Boolean,
},
Expand Down
4 changes: 4 additions & 0 deletions models/schemas/userConfigSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const UserConfigSchema = new mongoose.Schema<UserConfig>(
required: true,
default: [],
},
gameId: {
type: String,
required: false,
},
emailConfirmationToken: {
type: String,
select: false,
Expand Down
1 change: 1 addition & 0 deletions pages/api/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default withAuth({
collection = (await CollectionModel.create([{
_id: new Types.ObjectId(),
authorNote: authorNote?.trim(),
gameId: req.gameId,
isPrivate: setIsPrivate,
name: trimmedName,
slug: slug,
Expand Down
2 changes: 2 additions & 0 deletions pages/api/level/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GameId } from '@root/constants/GameId';
import mongoose, { Types } from 'mongoose';
import type { NextApiResponse } from 'next';
import { ValidType } from '../../../helpers/apiWrapper';
Expand Down Expand Up @@ -26,6 +27,7 @@ export default withAuth({ POST: {

await LevelModel.create([{
_id: levelId,
gameId: req.gameId,
authorNote: authorNote?.trim(),
data: data,
height: rows.length,
Expand Down
Loading

0 comments on commit 448d6c4

Please sign in to comment.