Skip to content

Commit

Permalink
api work
Browse files Browse the repository at this point in the history
  • Loading branch information
naueramant committed May 25, 2024
1 parent 6af0708 commit 7a8f6f2
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 85 deletions.
18 changes: 9 additions & 9 deletions src/api/endpoints/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import client from "../client";

export interface ILoginRequest {
export interface LoginRequest {
username: string;
password: string;
}

export interface ILoginResponse {
export interface LoginResponse {
token: string;
id: number;
image: string;
Expand All @@ -14,13 +14,13 @@ export interface ILoginResponse {
export async function login(
username: string,
password: string,
): Promise<ILoginResponse> {
const data: ILoginRequest = {
): Promise<LoginResponse> {
const data: LoginRequest = {
username: username,
password: password,
};

const response = await client.post<ILoginResponse>("/api-token-auth/", data, {
const response = await client.post<LoginResponse>("/api-token-auth/", data, {
headers: {
Authorization: "", // Skip auth interceptor
},
Expand All @@ -29,7 +29,7 @@ export async function login(
return response.data;
}

export interface ICreateUserResponse {
export interface CreateUserResponse {
token: string;
id: number;
image: string;
Expand All @@ -38,13 +38,13 @@ export interface ICreateUserResponse {
export async function createUser(
username: string,
password: string,
): Promise<ICreateUserResponse> {
const data: ILoginRequest = {
): Promise<CreateUserResponse> {
const data: LoginRequest = {
username: username,
password: password,
};

const response = await client.post<ILoginResponse>("/api/users/", data, {
const response = await client.post<LoginResponse>("/api/users/", data, {
headers: {
Authorization: "", // Skip auth interceptor
},
Expand Down
64 changes: 17 additions & 47 deletions src/api/endpoints/game.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,35 @@
import client from "../client";
import { Game } from "../models/game";

export interface IPostStartRequest {
export interface PostStartRequest {
tokens: string[];
official: boolean;
}

export async function postStart(
tokens: string[],
official: boolean,
): Promise<IGameState> {
const data: IPostStartRequest = {
): Promise<Game> {
const data: PostStartRequest = {
tokens: tokens,
official: official,
};

return (await client.post("/api/games/", data)).data;
}

export interface IGameState {
id: number;
start_datetime: string;
end_datetime?: string;
description: string;
official: boolean;
dnf: boolean;
shuffle_indices: number[];
cards: ICard[];
players: IPlayer[];
sips_per_beer: number;
has_ended: boolean;
description_html: string;
location: ILocation;
image?: string;
token: string;
}

export interface IPlayer {
id: number;
username: string;
is_superuser: boolean;
image_url: string;
}

export interface ILocation {
latitude: number;
longitude: number;
accuracy: number;
}

export interface ICard {
value: number;
suit: string;
start_delta_ms?: number;
chug_start_start_delta_ms?: number;
chug_end_start_delta_ms?: number;
}

export async function postUpdate(gameState: IGameState): Promise<void> {
export async function postUpdate(
token: string,
gameState: Game,
): Promise<void> {
return await client.post(
`/api/games/${gameState.id}/update_state/`,
gameState,
{
headers: {
Authorization: `GameToken ${token}`,
},
},
);
}

Expand All @@ -78,7 +48,7 @@ export async function deletePhoto(gameId: number): Promise<void> {
return await client.delete(`/api/games/${gameId}/delete_image/`);
}

export interface IResumableGame {
export interface ResumableGame {
id: number;
start_datetime: string;
players: {
Expand All @@ -90,8 +60,8 @@ export interface IResumableGame {

export async function getResumableGames(
token: string,
): Promise<IResumableGame[]> {
const response = await client.get<IResumableGame[]>("/api/games/resumable/", {
): Promise<ResumableGame[]> {
const response = await client.get<ResumableGame[]>("/api/games/resumable/", {
headers: {
Authorization: `Token ${token}`,
},
Expand All @@ -103,7 +73,7 @@ export async function getResumableGames(
export async function postResumeGame(
token: string,
gameId: number,
): Promise<IGameState> {
): Promise<Game> {
const response = await client.post(
`/api/games/${gameId}/resume/`,
{},
Expand Down
17 changes: 6 additions & 11 deletions src/api/endpoints/stats.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import client from "../client";

export interface IRankedCardResponse {
export interface RankedCardResponse {
user_id: number;
user_username: string;
user_image: string;
ranking_name: string;
ranking_value: string;
}

export async function getRankedCards(): Promise<IRankedCardResponse[]> {
const response =
await client.get<IRankedCardResponse[]>("/api/ranked_cards/");
export async function getRankedCards(): Promise<RankedCardResponse[]> {
const response = await client.get<RankedCardResponse[]>("/api/ranked_cards/");
return response.data;
}

export interface IUserStatsResponse {
export interface UserStatsResponse {
season_number: number;
total_games: number;
total_time_played_seconds: number;
Expand All @@ -29,11 +28,7 @@ export interface IUserStatsResponse {
average_chug_time_seconds: number;
}

export async function getUserStats(
userId: number,
): Promise<IUserStatsResponse> {
const response = await client.get<IUserStatsResponse>(
`/api/stats/${userId}/`,
);
export async function getUserStats(userId: number): Promise<UserStatsResponse> {
const response = await client.get<UserStatsResponse>(`/api/stats/${userId}/`);
return response.data;
}
7 changes: 7 additions & 0 deletions src/api/models/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Card {
value: number;
suit: string;
start_delta_ms?: number;
chug_start_start_delta_ms?: number;
chug_end_start_delta_ms?: number;
}
21 changes: 21 additions & 0 deletions src/api/models/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Card } from "./card";

export interface Game {
start_datetime: string;
official: boolean;
player_names: string[];
cards: Card[];

has_ended: boolean;
description?: string;
dnf: boolean;

dnf_player_ids: number[];

id: number;
player_ids: number[];
token: string;
shuffle_indices: number[];

location?: Location;
}
5 changes: 5 additions & 0 deletions src/api/models/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Location {
latitude: number;
longitude: number;
accuracy: number;
}
6 changes: 6 additions & 0 deletions src/api/models/player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Player {
id: number;
username: string;
is_superuser: boolean;
image_url: string;
}
12 changes: 8 additions & 4 deletions src/models/card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ type CardSuit = (typeof CardSuits)[number];
interface Card {
value: CardValue;
suit: CardSuit;

start_delta_ms?: number;
chug_start_start_delta_ms?: number;
chug_end_start_delta_ms?: number;
}

const getCardASCIISymbol = (card: Card): string => {
Expand Down Expand Up @@ -71,11 +75,11 @@ const getCardImageURI = (card: Card): string => {
};

export {
CardSuits,
CardValues,
getCardASCIISymbol,
getCardSuitColor,
getCardImageURI,
getCardSuitColor,
getCardSuitName,
CardValues,
CardSuits,
};
export type { Card, CardValue, CardSuit };
export type { Card, CardSuit, CardValue };
3 changes: 2 additions & 1 deletion src/models/chug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
interface Chug {
duration: number;
start_start_delta_ms: number;
end_start_delta_ms: number;
}

export type { Chug };
24 changes: 24 additions & 0 deletions src/models/game.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Card } from "./card";
import { Location } from "./location";

interface Game {
start_datetime: string;
official: boolean;
player_names: string[];
cards: Card[];

has_ended: boolean;
description?: string;
dnf: boolean;

dnf_player_ids: number[];

id?: number;
player_ids?: number[];
token?: string;
shuffle_indices?: number[];

location?: Location;
}

export type { Game };
7 changes: 7 additions & 0 deletions src/models/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
interface Location {
latitude: number;
longitude: number;
accuracy: number;
}

export type { Location };
2 changes: 1 addition & 1 deletion src/stores/game.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ const mapToLocal = (state: IGameState): GameState => {
};
};

export { mapToRemote, mapToLocal };
export { mapToLocal, mapToRemote };
Loading

0 comments on commit 7a8f6f2

Please sign in to comment.