-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.ts
More file actions
143 lines (125 loc) · 3.48 KB
/
Game.ts
File metadata and controls
143 lines (125 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import axios from 'axios';
import { axiosErrorHandler } from './Error';
import { GameTimer } from './GameTimer';
import { Room } from './Room';
import { User } from './User';
import { Problem } from './Problem';
import { Color } from './Color';
import Language from './Language';
export type Player = {
user: User,
submissions: Submission[],
solved: boolean[],
color: Color,
};
export type Game = {
room: Room,
players: Player[],
gameTimer: GameTimer,
problems: Problem[],
playAgain: boolean,
allSolved: boolean,
gameEnded: boolean,
};
export type StartGameParams = {
initiator: User,
};
export type PlayAgainParams = {
initiator: User,
};
export type EndGameParams = {
initiator: User,
};
export type RunSolutionParams = {
initiator: User,
input: string,
code: string,
problemIndex: number,
language: string,
};
export type SubmitSolutionParams = {
initiator: User,
code: string,
problemIndex: number,
language: string,
};
export type SubmissionResult = {
console: string,
userOutput: string,
error: string,
input: string,
correctOutput: string,
hidden: boolean,
correct: boolean,
};
// Distinguish on frontend between tests and submissions.
export enum SubmissionType {
Test = 'TEST',
Submit = 'SUBMIT',
}
export type Submission = {
code: string,
problemIndex: number,
language: string,
results: SubmissionResult[],
numCorrect: number,
numTestCases: number,
runtime: number,
compilationError: string,
startTime: string,
submissionType: SubmissionType,
};
export type SpectateGame = {
user: User,
problem: Problem,
problemIndex: number,
code: string,
language: string,
codeList?: string[],
languageList?: Language[],
};
const basePath = '/api/v1';
const routes = {
startGame: (roomId: string) => `${basePath}/rooms/${roomId}/start`,
getGame: (roomId: string) => `${basePath}/games/${roomId}`,
runCode: (roomId: string) => `${basePath}/games/${roomId}/run-code`,
submitSolution: (roomId: string) => `${basePath}/games/${roomId}/submission`,
endGame: (roomId: string) => `${basePath}/games/${roomId}/game-over`,
playAgain: (roomId: string) => `${basePath}/games/${roomId}/restart`,
};
export const startGame = (roomId: string, params: StartGameParams):
Promise<Room> => axios.post<Room>(routes.startGame(roomId), params)
.then((res) => res.data)
.catch((err) => {
throw axiosErrorHandler(err);
});
export const getGame = (roomId: string):
Promise<Game> => axios.get<Game>(routes.getGame(roomId))
.then((res) => res.data)
.catch((err) => {
throw axiosErrorHandler(err);
});
export const runSolution = (roomId: string, params: RunSolutionParams):
Promise<Submission> => axios.post<Submission>(routes.runCode(roomId), params)
.then((res) => res.data)
.catch((err) => {
throw axiosErrorHandler(err);
});
export const submitSolution = (roomId: string, params: SubmitSolutionParams):
Promise<Submission> => axios.post<Submission>(routes.submitSolution(roomId), params)
.then((res) => res.data)
.catch((err) => {
throw axiosErrorHandler(err);
});
export const playAgain = (roomId: string, params: PlayAgainParams):
Promise<Room> => axios.post<Room>(routes.playAgain(roomId), params)
.then((res) => res.data)
.catch((err) => {
throw axiosErrorHandler(err);
});
export const manuallyEndGame = (roomId: string, params: EndGameParams):
Promise<Room> => axios.post<Room>(routes.endGame(roomId), params)
.then((res) => res.data)
.catch((err) => {
throw axiosErrorHandler(err);
});