Skip to content

Commit

Permalink
Merge pull request #379 from MethSarcus/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MethSarcus authored Sep 4, 2024
2 parents 41833b1 + bc93dac commit 0200777
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 117 deletions.
58 changes: 27 additions & 31 deletions app/league/[slug]/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,37 @@
'use client'
import {Box, Button, Center, Grid, GridItem, Heading} from '@chakra-ui/react'
import axios from 'axios'
import {enableMapSet} from "immer"
import {enableMapSet} from 'immer'
import Link from 'next/link'
import React, {useContext, useEffect} from 'react'
import useSWR from 'swr'
import {Draft} from '../../../classes/custom/Draft'
import League from '../../../classes/custom/League'
import { DatabasePlayer, PlayerScores, SleeperPlayerDetails } from '../../../classes/custom/Player'
import {DatabasePlayer, PlayerScores, SleeperPlayerDetails} from '../../../classes/custom/Player'
import Footer from '../../../components/Footer'
import Navbar from '../../../components/nav/Navbar'
import {LeagueContext} from '../../../contexts/LeagueContext'
import {PlayerScoresContext} from '../../../contexts/PlayerScoresContext'
import {PlayerDetailsContext} from '../../../contexts/PlayerDetailsContext'
import styles from '../../../styles/Home.module.css'
import { LeagueSettings } from '../../../classes/sleeper/LeagueSettings'
import { SleeperRoster } from '../../../classes/sleeper/SleeperRoster'
import { SleeperUser, UserData } from '../../../classes/sleeper/SleeperUser'
const LeagueLayout = ({
children,
params,
}: {
children: React.ReactNode
params: {slug: string}
}) => {
import {LeagueSettings} from '../../../classes/sleeper/LeagueSettings'
import {SleeperRoster} from '../../../classes/sleeper/SleeperRoster'
import {BlankUserData, SleeperUser, UserData} from '../../../classes/sleeper/SleeperUser'
const LeagueLayout = ({children, params}: {children: React.ReactNode; params: {slug: string}}) => {
enableMapSet()
const [leagueContext, setLeagueContext] = useContext(LeagueContext)
const [playerDetailsContext, setPlayerDetailsContext] = useContext(PlayerDetailsContext)
const [playerScoresContext, setPlayerScoresContext] = useContext(PlayerScoresContext)


const fetcher = (url: string) => axios.get(url).then((res) => res.data)
const disableValidation = {
revalidateIfStale: false,
revalidateOnFocus: false,
revalidateOnReconnect: false
}
revalidateOnReconnect: false,
}

const {data: sleeperLeagueData, error: sleeperLeagueError} = useSWR(
params.slug != undefined
? `https://api.sleeper.app/v1/league/${params.slug}`
: null,
params.slug != undefined ? `https://api.sleeper.app/v1/league/${params.slug}` : null,
fetcher,
disableValidation
)
Expand All @@ -62,9 +53,7 @@ const LeagueLayout = ({
)

const {data: leagueData, error: leagueError} = useSWR(
params.slug != undefined &&
sleeperLeagueError == undefined &&
sleeperLeagueData
params.slug != undefined && sleeperLeagueError == undefined && sleeperLeagueData
? `/api/league/${params.slug}`
: null,
fetcher,
Expand All @@ -83,7 +72,12 @@ const LeagueLayout = ({
let playerDetails = new Map<string, DatabasePlayer>()
leagueData.league.player_details.forEach((player: DatabasePlayer) => {
let settings = leagueData.league.sleeperDetails as LeagueSettings
let playerObj = new PlayerScores(player, settings.scoring_settings, settings.settings.start_week, settings.settings.last_scored_leg)
let playerObj = new PlayerScores(
player,
settings.scoring_settings,
settings.settings.start_week,
settings.settings.last_scored_leg
)
playerDetails.set(player._id, player)
playerScores.set(player._id, playerObj)
})
Expand All @@ -93,10 +87,15 @@ const LeagueLayout = ({
setPlayerDetailsContext(playerDetails)
let users: UserData[] = []
leagueData.league.rosters.forEach((roster: SleeperRoster) => {
users.push(new UserData(leagueData.league.users.find((user: SleeperUser) => {
let user = leagueData.league.users.find((user: SleeperUser) => {
return user.user_id == roster.owner_id
}), roster))
});
})
if (user == undefined) {
users.push(new BlankUserData(leagueData.league.league_id, roster.owner_id, roster))
} else {
users.push(new UserData(user, roster))
}
})
let league = new League(
users,
leagueData.league.matchups,
Expand Down Expand Up @@ -139,8 +138,7 @@ const LeagueLayout = ({
<Link
href={
'https://github.com/MethSarcus/visualeague/issues/new?assignees=MethSarcus&labels=&template=bug_report.md&title='
}
>
}>
<Button mx={5} variant={'outline'} colorScheme={'red'}>
Report bug
</Button>
Expand All @@ -153,8 +151,7 @@ const LeagueLayout = ({
)
}

if (leagueError || tradeError)
return <Heading color={'white'}>Failed to load</Heading>
if (leagueError || tradeError) return <Heading color={'white'}>Failed to load</Heading>
return (
<Grid
bg={'surface.6'}
Expand All @@ -165,8 +162,7 @@ const LeagueLayout = ({
"main main"
"footer footer"`}
color='surface.0'
fontWeight='bold'
>
fontWeight='bold'>
<GridItem area={'header'}>
<Navbar leagueID={params.slug} />
</GridItem>
Expand Down
4 changes: 3 additions & 1 deletion classes/custom/Matchup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ export default class Matchup implements MatchupInterface {
return Math.abs(this.homeTeam.pf - (this.awayTeam?.pf ?? this.homeTeam.pf))
}

public getWinner() {
public getWinner(): MatchupSide | undefined {
let winner
if (!this.isTie) {
if (this.homeTeam.roster_id == this.winnerRosterId) {
winner = this.homeTeam
} else {
winner = this.awayTeam
}
} else {
winner = undefined
}

return winner
Expand Down
213 changes: 139 additions & 74 deletions classes/sleeper/SleeperUser.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,145 @@
import { SleeperRoster } from "./SleeperRoster";
import {SleeperRoster} from './SleeperRoster'

export class UserData {
user_id: string;
settings?: null;
metadata: Metadata;
league_id: string;
is_owner: boolean;
is_bot?: boolean | null;
display_name: string;
avatar: string;
roster: SleeperRoster;
user_id: string
settings?: null
metadata: Metadata
league_id: string
is_owner: boolean
is_bot?: boolean | null
display_name: string
avatar: string
roster: SleeperRoster

constructor(sleeperUser: SleeperUser, roster: SleeperRoster) {
this.user_id = sleeperUser.user_id;
this.settings = sleeperUser.settings;
this.metadata = sleeperUser.metadata;
this.league_id = sleeperUser.league_id;
this.is_owner = sleeperUser.is_owner;
this.display_name = sleeperUser.display_name;
this.avatar = sleeperUser.avatar;
this.roster = roster;
}
constructor(sleeperUser: SleeperUser, roster: SleeperRoster) {
this.user_id = sleeperUser.user_id
this.settings = sleeperUser.settings
this.metadata = sleeperUser.metadata
this.league_id = sleeperUser.league_id
this.is_owner = sleeperUser.is_owner
this.display_name = sleeperUser.display_name
this.avatar = sleeperUser.avatar
this.roster = roster
}
}


export interface SleeperUser {
user_id: string;
settings?: null;
metadata: Metadata;
league_id: string;
is_owner: boolean;
is_bot?: boolean | null;
display_name: string;
avatar: string;
}
export interface Metadata {
user_message_pn?: string | null;
transaction_waiver?: string | null;
transaction_trade?: string | null;
transaction_free_agent?: string | null;
transaction_commissioner?: string | null;
trade_block_pn?: string | null;
team_name_update?: string | null;
player_nickname_update?: string | null;
player_like_pn?: string | null;
mention_pn: string;
mascot_message_emotion_leg_9?: string | null;
mascot_message?: string | null;
mascot_item_type_id_leg_9?: string | null;
mascot_item_type_id_leg_18?: string | null;
mascot_item_type_id_leg_17?: string | null;
mascot_item_type_id_leg_16?: string | null;
mascot_item_type_id_leg_15?: string | null;
mascot_item_type_id_leg_14?: string | null;
mascot_item_type_id_leg_13?: string | null;
mascot_item_type_id_leg_12?: string | null;
mascot_item_type_id_leg_11?: string | null;
mascot_item_type_id_leg_10?: string | null;
join_voice_pn?: string | null;
archived?: string | null;
allow_pn: string;
team_name?: string | null;
mascot_message_leg_3?: string | null;
mascot_message_emotion_leg_3?: string | null;
mascot_item_type_id_leg_8?: string | null;
mascot_item_type_id_leg_7?: string | null;
mascot_item_type_id_leg_6?: string | null;
mascot_item_type_id_leg_5?: string | null;
mascot_item_type_id_leg_4?: string | null;
mascot_item_type_id_leg_3?: string | null;
avatar?: string | null;
allow_sms?: string | null;
mascot_message_emotion_leg_6?: string | null;
mascot_message_emotion_leg_1?: string | null;
mascot_item_type_id_leg_1?: string | null;
mascot_item_type_id_leg_2?: string | null;
show_mascots?: string | null;
}

user_id: string
settings?: null
metadata: Metadata
league_id: string
is_owner: boolean
is_bot?: boolean | null
display_name: string
avatar: string
}

export class BlankUserData implements UserData {
user_id: string
settings?: null = null
metadata: Metadata = new BlankMetadata()
league_id: string
is_owner: boolean = false
is_bot?: boolean | null | undefined
display_name: string = 'Missing Member'
avatar: string = 'https://sleepercdn.com/images/v2/icons/player_default.webp'
roster: SleeperRoster

constructor(rosterId: string, leagueId: string, roster: SleeperRoster) {
this.user_id = rosterId
this.league_id = leagueId
this.roster = roster
}
}
export interface Metadata {
user_message_pn?: string | null
transaction_waiver?: string | null
transaction_trade?: string | null
transaction_free_agent?: string | null
transaction_commissioner?: string | null
trade_block_pn?: string | null
team_name_update?: string | null
player_nickname_update?: string | null
player_like_pn?: string | null
mention_pn: string
mascot_message_emotion_leg_9?: string | null
mascot_message?: string | null
mascot_item_type_id_leg_9?: string | null
mascot_item_type_id_leg_18?: string | null
mascot_item_type_id_leg_17?: string | null
mascot_item_type_id_leg_16?: string | null
mascot_item_type_id_leg_15?: string | null
mascot_item_type_id_leg_14?: string | null
mascot_item_type_id_leg_13?: string | null
mascot_item_type_id_leg_12?: string | null
mascot_item_type_id_leg_11?: string | null
mascot_item_type_id_leg_10?: string | null
join_voice_pn?: string | null
archived?: string | null
allow_pn: string
team_name?: string | null
mascot_message_leg_3?: string | null
mascot_message_emotion_leg_3?: string | null
mascot_item_type_id_leg_8?: string | null
mascot_item_type_id_leg_7?: string | null
mascot_item_type_id_leg_6?: string | null
mascot_item_type_id_leg_5?: string | null
mascot_item_type_id_leg_4?: string | null
mascot_item_type_id_leg_3?: string | null
avatar?: string | null
allow_sms?: string | null
mascot_message_emotion_leg_6?: string | null
mascot_message_emotion_leg_1?: string | null
mascot_item_type_id_leg_1?: string | null
mascot_item_type_id_leg_2?: string | null
show_mascots?: string | null
}

class BlankMetadata implements Metadata {
user_message_pn?: string | null
transaction_waiver?: string | null
transaction_trade?: string | null
transaction_free_agent?: string | null
transaction_commissioner?: string | null
trade_block_pn?: string | null
team_name_update?: string | null
player_nickname_update?: string | null
player_like_pn?: string | null
mention_pn: string
mascot_message_emotion_leg_9?: string | null
mascot_message?: string | null
mascot_item_type_id_leg_9?: string | null
mascot_item_type_id_leg_18?: string | null
mascot_item_type_id_leg_17?: string | null
mascot_item_type_id_leg_16?: string | null
mascot_item_type_id_leg_15?: string | null
mascot_item_type_id_leg_14?: string | null
mascot_item_type_id_leg_13?: string | null
mascot_item_type_id_leg_12?: string | null
mascot_item_type_id_leg_11?: string | null
mascot_item_type_id_leg_10?: string | null
join_voice_pn?: string | null
archived?: string | null
allow_pn: string
team_name?: string | null
mascot_message_leg_3?: string | null
mascot_message_emotion_leg_3?: string | null
mascot_item_type_id_leg_8?: string | null
mascot_item_type_id_leg_7?: string | null
mascot_item_type_id_leg_6?: string | null
mascot_item_type_id_leg_5?: string | null
mascot_item_type_id_leg_4?: string | null
mascot_item_type_id_leg_3?: string | null
avatar?: string | null
allow_sms?: string | null
mascot_message_emotion_leg_6?: string | null
mascot_message_emotion_leg_1?: string | null
mascot_item_type_id_leg_1?: string | null
mascot_item_type_id_leg_2?: string | null
show_mascots?: string | null

constructor() {
this.mention_pn = ''
this.allow_pn = ''
}
}
Loading

0 comments on commit 0200777

Please sign in to comment.