579 lines
18 KiB
TypeScript
579 lines
18 KiB
TypeScript
import { Accessor, createSignal } from "solid-js";
|
|
import { localState } from "./state";
|
|
import { UploadFile } from "@solid-primitives/upload";
|
|
|
|
export interface User {
|
|
username: string,
|
|
displayName: string,
|
|
googleId: string,
|
|
}
|
|
|
|
export interface AuthenticationResponse {
|
|
user: User,
|
|
token: string,
|
|
}
|
|
|
|
export interface AuthConfig {
|
|
clientId: string,
|
|
redirect_url: string,
|
|
}
|
|
|
|
export interface Group {
|
|
id: string,
|
|
name: string,
|
|
owner: User,
|
|
members: User[],
|
|
strats: Strat[],
|
|
maps: Map[],
|
|
profiles: Profile[],
|
|
stratTypes: string[],
|
|
}
|
|
|
|
export interface Strat {
|
|
id: string,
|
|
title: string,
|
|
description: string,
|
|
stratType: string,
|
|
attempts: number,
|
|
success: number
|
|
stratStates: StratState[],
|
|
playerTypes: PlayerType[],
|
|
map: Map,
|
|
}
|
|
|
|
export interface Map {
|
|
id: string,
|
|
name: string,
|
|
image: File,
|
|
}
|
|
|
|
export interface StratState {
|
|
id: string,
|
|
description: string,
|
|
image: File,
|
|
lineups: Lineup[],
|
|
}
|
|
|
|
export interface Lineup {
|
|
id: string,
|
|
description: string,
|
|
image: File,
|
|
}
|
|
|
|
export interface PlayerType {
|
|
id: string,
|
|
name: string,
|
|
task: string,
|
|
profiles: Profile[],
|
|
}
|
|
|
|
export interface Profile {
|
|
id: string,
|
|
name: string,
|
|
image: File,
|
|
}
|
|
|
|
export interface GroupRequest {
|
|
name: string,
|
|
}
|
|
|
|
export interface StratRequest {
|
|
title: string,
|
|
description: string,
|
|
stratType: string,
|
|
mapId: string,
|
|
}
|
|
|
|
export interface MapRequest {
|
|
name: string,
|
|
image: string,
|
|
}
|
|
|
|
export interface ProfileRequest {
|
|
name: string,
|
|
image: string,
|
|
}
|
|
|
|
export interface PlayerTypeRequest {
|
|
name: string,
|
|
task: string,
|
|
}
|
|
|
|
export interface StratStateRequest {
|
|
description: string,
|
|
image: string,
|
|
}
|
|
|
|
export interface LineupRequest {
|
|
description: string,
|
|
image: string,
|
|
}
|
|
|
|
export class API {
|
|
public apiURL: string;
|
|
|
|
constructor(apiURL: string) {
|
|
this.apiURL = apiURL;
|
|
}
|
|
|
|
private async checkResponse(response: Response) {
|
|
if (!response.ok) {
|
|
let json = null;
|
|
try {
|
|
json = await response.json();
|
|
} catch (e) { /* ignored */ }
|
|
|
|
throw json?.error || 'Request failed: ' + response.status + ' ' + response.statusText;
|
|
}
|
|
}
|
|
|
|
public ensureLoggedIn(): AuthenticationResponse {
|
|
const info = localState.accountInfo;
|
|
if (info == null) throw 'Not logged in';
|
|
return info;
|
|
}
|
|
|
|
public async authConfig(): Promise<AuthConfig> {
|
|
const response = await fetch(this.apiURL + '/config');
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async login(code: string): Promise<AuthenticationResponse> {
|
|
const response = await fetch(this.apiURL + '/auth/login', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ code })
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async getGroups(): Promise<Group[]> {
|
|
const response = await fetch(this.apiURL + '/group', {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async createGroup(group: GroupRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(group)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeGroup(groupId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async addStratType(groupId: string, stratName: string): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat-type', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(stratName)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeStratType(groupId: string, stratName: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat-type/' + stratName, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updateGroup(id: string, group: GroupRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + id, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(group)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async getGroup(id: string): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + id, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addMember(groupId: string, memberId: string): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/member', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify({ userId: memberId })
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeMember(groupId: string, memberId: string): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/member/' + memberId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addStrat(groupId: string, strat: StratRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(strat)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeStrat(groupId: string, stratId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updateStrat(groupId: string, stratId: string, strat: StratRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(strat)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addMap(groupId: string, map: MapRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/map', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(map)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeMap(groupId: string, mapId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/map/' + mapId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updateMap(groupId: string, mapId: string, map: MapRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/map/' + mapId, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(map)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addProfileToGroup(groupId: string, profile: ProfileRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/profile', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(profile)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeProfileFromGroup(groupId: string, profileId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/profile/' + profileId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updateProfileToGroup(groupId: string, profileId: string, profile: ProfileRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/profile/' + profileId, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(profile)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addPlayerType(groupId: string, stratId: string, playerType: PlayerTypeRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/player-type', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(playerType)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removePlayerType(groupId: string, stratId: string, playerTypeId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/player-type/' + playerTypeId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updatePlayerType(groupId: string, profileId: string, stratId: string, playerTypeId: string, playerType: PlayerTypeRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/player-type/' + playerTypeId, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(playerType)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addProfileToPlayerType(groupId: string, stratId: string, playerTypeId: string, profile: ProfileRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/player-type/' + playerTypeId + '/profile', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(profile)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeProfileToPlayerType(groupId: string, stratId: string, playerTypeId: string, profileId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/player-type/' + playerTypeId + '/profile/' + profileId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updateProfileToPlayerType(groupId: string, profileId: string, stratId: string, playerTypeId: string, profile: ProfileRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/player-type/' + playerTypeId + '/profile/' + profileId, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(profile)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addStratState(groupId: string, stratId: string, stratState: StratStateRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/strat-state', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(stratState)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeStratState(groupId: string, stratId: string, stratStateId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/strat-state/' + stratStateId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updateStratState(groupId: string, profileId: string, stratId: string, stratStateId: string, stratState: StratStateRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/strat-state/' + stratStateId, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(stratState)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async addLineup(groupId: string, stratId: string, stratStateId: string, lineup: LineupRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/strat-state/' + stratStateId + '/lineup', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(lineup)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
|
|
public async removeLineup(groupId: string, stratId: string, stratStateId: string, lineupId: string): Promise<void> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/strat-state/' + stratStateId + '/lineup/' + lineupId, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
}
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
}
|
|
|
|
public async updateLineup(groupId: string, profileId: string, stratId: string, stratStateId: string, lineupId: string, lineup: LineupRequest): Promise<Group> {
|
|
const response = await fetch(this.apiURL + '/group/' + groupId + '/strat/' + stratId + '/strat-state/' + stratStateId + '/lineup/' + lineupId, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ' + this.ensureLoggedIn().token
|
|
},
|
|
body: JSON.stringify(lineup)
|
|
});
|
|
|
|
await this.checkResponse(response);
|
|
|
|
return await response.json();
|
|
}
|
|
}
|
|
|
|
export const [api, setAPI] = createSignal(new API(''));
|
|
export const [authConfig, setAuthConfig] = createSignal(null as AuthConfig | null)
|