diff --git a/components/calendar/Calendar.vue b/components/calendar/Calendar.vue index 7bdc17f..3b38126 100644 --- a/components/calendar/Calendar.vue +++ b/components/calendar/Calendar.vue @@ -10,7 +10,7 @@ import YearLayout from './state/yearly/Layout.vue' const route = useRoute() const worldId = route.params.id -const { setMonths, currentConfig, selectedDate, jumpToDate } = useCalendar() +const { setMonths, setDefaultDate, currentConfig, selectedDate, jumpToDate } = useCalendar() const { setEvents } = useCalendarEvents() 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) { setMonths(calendar.value?.data?.months) } + if (calendar.value?.data?.today) { + setDefaultDate(calendar.value?.data?.today) + } if (calendar.value?.data?.events) { setEvents(calendar.value?.data?.events) } @@ -31,6 +34,9 @@ watch(pending, (newStatus) => { if (calendar.value?.data?.months) { setMonths(calendar.value?.data?.months) } + if (calendar.value?.data?.today) { + setDefaultDate(calendar.value?.data?.today) + } if (calendar.value?.data?.events) { setEvents(calendar.value?.data?.events) } diff --git a/models/CalendarConfig.ts b/models/CalendarConfig.ts index 19bd086..3cb7984 100644 --- a/models/CalendarConfig.ts +++ b/models/CalendarConfig.ts @@ -1,12 +1,15 @@ import type { CalendarEvent } from "./CalendarEvent" import type { CalendarMonth } from "./CalendarMonth" +import type { RPGDate } from "./Date" export interface CalendarConfig { months: CalendarMonth[] daysPerMonth: number + today: RPGDate } export interface Calendar extends CalendarConfig { id: number + name: string events: CalendarEvent[] } diff --git a/models/Date.ts b/models/Date.ts index cc87b1a..66dd8ec 100644 --- a/models/Date.ts +++ b/models/Date.ts @@ -2,11 +2,8 @@ export interface RPGDate { day: number month: number year: number - period?: RPGPeriod } -export type RPGPeriod = 'ante' | 'nante' -export type RPGPeriodShort = 'A.R' | 'N.R' export type RPGDateOrder = 'asc' | 'desc' export const monthsPerYear: number = 10 @@ -36,12 +33,6 @@ export function compareDates(a: RPGDate, b: RPGDate, order: RPGDateOrder = 'desc // Reverses the order if specified 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 if (a.year < b.year) return -1 * orderFactor if (a.year > b.year) return 1 * orderFactor diff --git a/server/api/calendars/query.get.ts b/server/api/calendars/query.get.ts index 9d36e57..1979d51 100644 --- a/server/api/calendars/query.get.ts +++ b/server/api/calendars/query.get.ts @@ -14,6 +14,8 @@ export default defineEventHandler(async (event) => { .from('calendars') .select(` id, + name, + today, months:calendar_months (*), events:calendar_events ( id, diff --git a/stores/CalendarStore.ts b/stores/CalendarStore.ts index 46c2698..fe15b45 100644 --- a/stores/CalendarStore.ts +++ b/stores/CalendarStore.ts @@ -25,22 +25,6 @@ export const useCalendar = defineStore('calendar', () => { * Static calendar config * This shouldn't change */ - /** - * Month list - */ - const months: Ref = ref([]) - - function setMonths(data: CalendarMonth[]) { - months.value = data - } - - /** - * Sorted month data - */ - const sortedMonths = computed(() => 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 = ref({ viewType: 'month' }) @@ -51,15 +35,34 @@ export const useCalendar = defineStore('calendar', () => { 'century' ]) + /** + * Month list (queried from API) + */ + const months: Ref = ref([]) + + function setMonths(data: CalendarMonth[]) { + months.value = data + } + + /** + * Sorted month data using the raw months + */ + const sortedMonths = computed(() => 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) - const defaultDay: number = 23 - const defaultMonth: number = 8 - const defaultYear: number = 3209 + // The base setting is the first day / month of year 0 + const defaultDay: Ref = ref(1) + const defaultMonth: Ref = ref(0) + const defaultYear: Ref = ref(0) + + // Object representation const defaultDate: ComputedRef = computed(() => { return { - day: defaultDay, - month: defaultMonth, - year: defaultYear + day: defaultDay.value, + month: defaultMonth.value, + 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(() => { return Number(params.day) }) @@ -354,6 +375,7 @@ export const useCalendar = defineStore('calendar', () => { currentRPGDate, currentMonthData, defaultDate, + setDefaultDate, selectedDate: skipHydrate(selectedDate), selectDate, params, diff --git a/supabase/migrations/202401_init.sql b/supabase/migrations/202401_init.sql index 47492d4..90038e8 100644 --- a/supabase/migrations/202401_init.sql +++ b/supabase/migrations/202401_init.sql @@ -48,6 +48,7 @@ comment on table public.worlds is 'Worlds belonging to a single user ; a game ma create table public.calendars ( id bigint generated by default as identity primary key, name text not null, + today json 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.'; diff --git a/supabase/seed.sql b/supabase/seed.sql index 691f65d..97368cd 100644 --- a/supabase/seed.sql +++ b/supabase/seed.sql @@ -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'); -- 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 insert into public.calendar_months (calendar_id, name, days, position) values (1, 'Jalen', 32, 1);