Skip to content

Commit

Permalink
Rewrite leaderboard generation code in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
man90es committed Jan 17, 2024
1 parent 400441a commit 33ff0e2
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 168 deletions.
131 changes: 0 additions & 131 deletions src/core/generateLeaderboardItems.js

This file was deleted.

1 change: 1 addition & 0 deletions src/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as siteName } from "./siteName"
export { default as supportedServers, RegionEnum } from "./supportedServers"
export { PrivacyLevelEnum } from "./privacyLevels"
6 changes: 6 additions & 0 deletions src/data/privacyLevels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum PrivacyLevelEnum {
LEVEL = 0b1,
GUILD = 0b10,
CONTRIB = 0b100,
SPECS = 0b1000,
}
19 changes: 7 additions & 12 deletions src/hooks/API.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { ComputedRef, reactive, readonly } from "vue"
import { RegionEnum } from "@/data"

type Player = {
profileTarget: string
region: RegionEnum
}

type Guild = {
population: number
members: Player[]
}
import type { Guild, Player } from "@/types"

type Err = {
code?: number
message: string
}

type APIResult = {
export type APIResult = {
errors: Err[]
guild: Guild
players: Player[]
Expand Down Expand Up @@ -46,12 +37,14 @@ function parseResponse<T>(response: Response): Promise<T> {
}

// Custom hook to fetch guild and player data
export default function useGuild(params: ComputedRef<{ guildName?: string, region?: RegionEnum, players?: Player[] }>) {
export default function useGuild(params: ComputedRef<{ guildName?: Guild["name"], region?: Guild["region"], players?: Player[] }>) {
const result = reactive<APIResult>({
errors: [],
guild: {
members: [],
name: "",
population: 0,
region: RegionEnum.EU,
},
players: [],
progress: 0
Expand All @@ -63,7 +56,9 @@ export default function useGuild(params: ComputedRef<{ guildName?: string, regio
result.errors = []
result.guild = {
members: [],
name: "",
population: 0,
region: RegionEnum.EU,
}
result.players = []
result.progress = 0
Expand Down
1 change: 0 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default as useIsMobile } from "./useIsMobile"
export { default as useNavigation } from "./useNavigation"
2 changes: 1 addition & 1 deletion src/pages/home/components/SearchCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { ref } from "vue"
import { type RegionEnum, supportedServers } from "@/data"
import { useMainStore } from "@/stores"
import { useNavigation } from "@/hooks"
import { useNavigation } from "../hooks"
const { navigateToCustomLeaderboard, navigateToGuildLeaderboard } = useNavigation()
const store = useMainStore()
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/components/SearchHistoryCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { formatDistanceToNow } from "date-fns"
import { type RegionEnum } from "@/data"
import { useMainStore } from "@/stores"
import { useNavigation } from "@/hooks"
import { useNavigation } from "../hooks"
const { navigateToGuildLeaderboard } = useNavigation()
const store = useMainStore()
Expand Down
1 change: 1 addition & 0 deletions src/pages/home/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useNavigation } from "./useNavigation"
File renamed without changes.
26 changes: 12 additions & 14 deletions src/pages/leaderboard/components/LeaderboardCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@
<LoadingCard v-else id="leaderboard" :progress="data.progress" />
</template>

<script setup>
<script setup lang="ts">
import { computed } from "vue"
import { ContentCard } from "@/components"
import { generateLeaderboardItems } from "../logic"
import { LeaderboardHeaderLine, LeaderboardLine, LoadingCard } from "."
import { PrivacyLevelEnum } from "@/data"
import { routeNameEnum } from "@/router"
import { useDiscipline } from "../hooks"
import { useRoute } from "vue-router"
import generateLeaderboardItems from "@/core/generateLeaderboardItems"
import type { APIResult } from "@/hooks/API"
const route = useRoute()
const props = defineProps({
data: {
type: Object,
required: true,
},
refreshData: {
type: Function,
required: true,
},
})
const props = defineProps<{
data: APIResult
refreshData: () => void
}>()
const discipline = useDiscipline()
const leaderboardItems = computed(() => {
if (props.data.progress < 1) {
Expand All @@ -43,7 +41,7 @@
if (player.guild !== undefined && player.guild.name.toLowerCase() == props.data.guild.name.toLowerCase()) {
// They are probably in the guild: double checked
return true
} else if (player.privacy & 0b10) {
} else if (player.privacy & PrivacyLevelEnum.GUILD) {
// The guild in the profile is set to private: can't double check
return true
} else {
Expand All @@ -52,7 +50,7 @@
}
})
return generateLeaderboardItems(route.params.discipline, players)
return generateLeaderboardItems(discipline.value, players)
})
</script>

Expand Down
12 changes: 4 additions & 8 deletions src/pages/leaderboard/components/LeaderboardHeaderLine.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

<script setup lang="ts">
import { computed } from "vue"
import { useRoute } from "vue-router"
import { useDiscipline } from "../hooks"
const route = useRoute()
const discipline = useDiscipline()
const headers = computed(() => {
const discipline = Array.isArray(route.params.discipline)
? route.params.discipline[0]
: route.params.discipline
const thrirdCol = ["contribution", "combat", "life", "characters", "age"].includes(discipline)
const thrirdCol = ["contribution", "combat", "life", "characters", "age"].includes(discipline.value)
? null
: "Character"
Expand All @@ -24,7 +20,7 @@
life: "Life Fame",
characters: "Characters",
age: "Age",
}[discipline] ?? "Rank"
}[discipline.value] ?? "Rank"
return ["#", "Family Name", thrirdCol, fourthCol]
})
Expand Down
1 change: 1 addition & 0 deletions src/pages/leaderboard/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as useDiscipline } from "./useDiscipline"
12 changes: 12 additions & 0 deletions src/pages/leaderboard/hooks/useDiscipline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { computed, readonly } from "vue"
import { useRoute } from "vue-router"

export default function useDiscipline() {
const route = useRoute()

return readonly(computed(() => {
return Array.isArray(route.params.discipline)
? route.params.discipline[0]
: route.params.discipline
}))
}
Loading

0 comments on commit 33ff0e2

Please sign in to comment.