Added default date config in calendar data

This commit is contained in:
Alexis
2024-05-18 18:01:49 +02:00
parent 3de59116aa
commit 73e4b0e3d7
7 changed files with 58 additions and 33 deletions

View File

@@ -10,7 +10,7 @@ import YearLayout from './state/yearly/Layout.vue'
const route = useRoute() const route = useRoute()
const worldId = route.params.id const worldId = route.params.id
const { setMonths, currentConfig, selectedDate, jumpToDate } = useCalendar() const { setMonths, setDefaultDate, currentConfig, selectedDate, jumpToDate } = useCalendar()
const { setEvents } = useCalendarEvents() const { setEvents } = useCalendarEvents()
const { data: calendar, pending, refresh } = await useLazyFetch(`/api/calendars/query?world_id=${worldId}`) const { data: calendar, pending, refresh } = await useLazyFetch(`/api/calendars/query?world_id=${worldId}`)
@@ -21,6 +21,9 @@ if (!calendar.value) {
if (calendar.value?.data?.months) { if (calendar.value?.data?.months) {
setMonths(calendar.value?.data?.months) setMonths(calendar.value?.data?.months)
} }
if (calendar.value?.data?.today) {
setDefaultDate(calendar.value?.data?.today)
}
if (calendar.value?.data?.events) { if (calendar.value?.data?.events) {
setEvents(calendar.value?.data?.events) setEvents(calendar.value?.data?.events)
} }
@@ -31,6 +34,9 @@ watch(pending, (newStatus) => {
if (calendar.value?.data?.months) { if (calendar.value?.data?.months) {
setMonths(calendar.value?.data?.months) setMonths(calendar.value?.data?.months)
} }
if (calendar.value?.data?.today) {
setDefaultDate(calendar.value?.data?.today)
}
if (calendar.value?.data?.events) { if (calendar.value?.data?.events) {
setEvents(calendar.value?.data?.events) setEvents(calendar.value?.data?.events)
} }

View File

@@ -1,12 +1,15 @@
import type { CalendarEvent } from "./CalendarEvent" import type { CalendarEvent } from "./CalendarEvent"
import type { CalendarMonth } from "./CalendarMonth" import type { CalendarMonth } from "./CalendarMonth"
import type { RPGDate } from "./Date"
export interface CalendarConfig { export interface CalendarConfig {
months: CalendarMonth[] months: CalendarMonth[]
daysPerMonth: number daysPerMonth: number
today: RPGDate
} }
export interface Calendar extends CalendarConfig { export interface Calendar extends CalendarConfig {
id: number id: number
name: string
events: CalendarEvent[] events: CalendarEvent[]
} }

View File

@@ -2,11 +2,8 @@ export interface RPGDate {
day: number day: number
month: number month: number
year: number year: number
period?: RPGPeriod
} }
export type RPGPeriod = 'ante' | 'nante'
export type RPGPeriodShort = 'A.R' | 'N.R'
export type RPGDateOrder = 'asc' | 'desc' export type RPGDateOrder = 'asc' | 'desc'
export const monthsPerYear: number = 10 export const monthsPerYear: number = 10
@@ -36,12 +33,6 @@ export function compareDates(a: RPGDate, b: RPGDate, order: RPGDateOrder = 'desc
// Reverses the order if specified // Reverses the order if specified
const orderFactor: number = order === 'desc' ? 1 : -1 const orderFactor: number = order === 'desc' ? 1 : -1
// Compare eras
if ((a.period === 'ante' && b.period === 'nante') || (a.year < 0 && b.year >= 0))
return -1 * orderFactor
if ((a.period === 'nante' && b.period === 'ante') || (a.year >= 0 && b.year < 0))
return 1 * orderFactor
// Compare years // Compare years
if (a.year < b.year) return -1 * orderFactor if (a.year < b.year) return -1 * orderFactor
if (a.year > b.year) return 1 * orderFactor if (a.year > b.year) return 1 * orderFactor

View File

@@ -14,6 +14,8 @@ export default defineEventHandler(async (event) => {
.from('calendars') .from('calendars')
.select(` .select(`
id, id,
name,
today,
months:calendar_months (*), months:calendar_months (*),
events:calendar_events ( events:calendar_events (
id, id,

View File

@@ -25,22 +25,6 @@ export const useCalendar = defineStore('calendar', () => {
* Static calendar config * Static calendar config
* This shouldn't change * This shouldn't change
*/ */
/**
* Month list
*/
const months: Ref<CalendarMonth[]> = ref<CalendarMonth[]>([])
function setMonths(data: CalendarMonth[]) {
months.value = data
}
/**
* Sorted month data
*/
const sortedMonths = computed<CalendarMonth[]>(() => months.value.sort((a, b) => a.position - b.position))
const monthsPerYear = computed(() => months.value.length)
const daysPerYear = computed(() => months.value.reduce((acc, o) => acc + o.days, 0))
const currentConfig: Ref<CalendarCurrentConfig> = ref({ const currentConfig: Ref<CalendarCurrentConfig> = ref({
viewType: 'month' viewType: 'month'
}) })
@@ -51,15 +35,34 @@ export const useCalendar = defineStore('calendar', () => {
'century' 'century'
]) ])
/**
* Month list (queried from API)
*/
const months: Ref<CalendarMonth[]> = ref<CalendarMonth[]>([])
function setMonths(data: CalendarMonth[]) {
months.value = data
}
/**
* Sorted month data using the raw months
*/
const sortedMonths = computed<CalendarMonth[]>(() => months.value.sort((a, b) => a.position - b.position))
const monthsPerYear = computed(() => months.value.length)
const daysPerYear = computed(() => months.value.reduce((acc, o) => acc + o.days, 0))
// Default date settings (current day in the world) // Default date settings (current day in the world)
const defaultDay: number = 23 // The base setting is the first day / month of year 0
const defaultMonth: number = 8 const defaultDay: Ref<number> = ref<number>(1)
const defaultYear: number = 3209 const defaultMonth: Ref<number> = ref<number>(0)
const defaultYear: Ref<number> = ref<number>(0)
// Object representation
const defaultDate: ComputedRef<RPGDate> = computed(() => { const defaultDate: ComputedRef<RPGDate> = computed(() => {
return { return {
day: defaultDay, day: defaultDay.value,
month: defaultMonth, month: defaultMonth.value,
year: defaultYear year: defaultYear.value
} }
}) })
@@ -73,6 +76,24 @@ export const useCalendar = defineStore('calendar', () => {
} }
}) })
/**
* Sets the new defaultDate (aka the "today" value from the calendar)
*
* @param date The new data to set as defaultDate
*/
function setDefaultDate(date: RPGDate) {
defaultDay.value = date.day
defaultMonth.value = date.month
defaultYear.value = date.year
}
// Everytime the defaultDate changes / is set, we should update the params in the URL
watch(defaultDate, () => {
params.day = String(defaultDate.value.day)
params.month = String(defaultDate.value.month)
params.year = String(defaultDate.value.year)
})
const currentDay = computed<number>(() => { const currentDay = computed<number>(() => {
return Number(params.day) return Number(params.day)
}) })
@@ -354,6 +375,7 @@ export const useCalendar = defineStore('calendar', () => {
currentRPGDate, currentRPGDate,
currentMonthData, currentMonthData,
defaultDate, defaultDate,
setDefaultDate,
selectedDate: skipHydrate(selectedDate), selectedDate: skipHydrate(selectedDate),
selectDate, selectDate,
params, params,

View File

@@ -48,6 +48,7 @@ comment on table public.worlds is 'Worlds belonging to a single user ; a game ma
create table public.calendars ( create table public.calendars (
id bigint generated by default as identity primary key, id bigint generated by default as identity primary key,
name text not null, name text not null,
today json not null,
world_id bigint references public.worlds on delete cascade not null world_id bigint references public.worlds on delete cascade not null
); );
comment on table public.calendars is 'Calendar settings and configuration attached to a single world.'; comment on table public.calendars is 'Calendar settings and configuration attached to a single world.';

View File

@@ -39,7 +39,7 @@ insert into public.character_categories (name) values ('commerçant');
insert into public.worlds (name, description, color) values ('Léïm', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis aliquet congue aliquet. Curabitur eu iaculis diam. Nunc iaculis nibh orci, eu semper nunc congue congue. Praesent euismod tortor eget metus tristique lobortis vel in risus. In volutpat ligula orci, id pharetra lectus egestas at.', 'black'); insert into public.worlds (name, description, color) values ('Léïm', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis aliquet congue aliquet. Curabitur eu iaculis diam. Nunc iaculis nibh orci, eu semper nunc congue congue. Praesent euismod tortor eget metus tristique lobortis vel in risus. In volutpat ligula orci, id pharetra lectus egestas at.', 'black');
-- Worlds' calendars -- Worlds' calendars
insert into public.calendars (world_id, name) values (1, 'Calendrier solaire'); insert into public.calendars (world_id, name, today) values (1, 'Calendrier solaire', '{ "day": 3, "month": 4, "year": -1932 }');
-- Calendar's months -- Calendar's months
insert into public.calendar_months (calendar_id, name, days, position) values (1, 'Jalen', 32, 1); insert into public.calendar_months (calendar_id, name, days, position) values (1, 'Jalen', 32, 1);