Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoassigning Algorithm #25

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions apps/nextjs/src/app/dashboard/Assignments.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { useState } from "react";
import { useEffect, useState } from "react";
import { Metadata } from "next";
import Image from "next/image";
import { Button } from "@/components/ui/button";
Expand All @@ -19,6 +19,7 @@ import {
import { ScrollArea } from "@/components/ui/scroll-area";
import { Separator } from "@/components/ui/separator";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { assignTasks } from "@/lib/utils/autoassign";
import { DndContext, DragOverlay, useDroppable } from "@dnd-kit/core";

import { AssignmentCard, MemberCard } from "./components/cards";
Expand All @@ -33,7 +34,19 @@ import { UserNav } from "./components/user-nav";
const tags = Array.from({ length: 50 }).map(
(_, i, a) => `v1.2.0-beta.${a.length - i}`,
);

function autoAssign(
assignments: string[],
members: { [key: string]: string[] },
setMembers: (members: { [key: string]: string[] }) => void,
setAssignments: (assignments: string[]) => void,
) {
// useEffect(() => {
const autoassign = assignTasks(assignments, Object.keys(members));
console.log(autoassign, typeof autoassign);
setMembers(autoassign);
setAssignments([]);
// }, []);
}
export default function Assignments() {
// XXX: Use real data via tRPC
const [members, setMembers] = useState<{ [key: string]: string[] }>(
Expand Down Expand Up @@ -137,6 +150,13 @@ export default function Assignments() {
</ResizablePanelGroup> */}
</ResizablePanel>
</ResizablePanelGroup>
<Button
onClick={() =>
autoAssign(assignments, members, setMembers, setAssignments)
}
>
Auto Assign
</Button>
</DndContext>
);
}
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ScrollArea } from "@/components/ui/scroll-area";
import { Separator } from "@/components/ui/separator";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";

import Assignments from "./Assignments";
import Assignments, { autoassign } from "./Assignments";
import { CalendarDateRangePicker } from "./components/date-range-picker";
import { MainNav } from "./components/main-nav";
import { Overview } from "./components/overview";
Expand Down
19 changes: 19 additions & 0 deletions apps/nextjs/src/app/matches/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from "react";
import { createTRPCClient } from "@trpc/client";
import { httpBatchLink } from "@trpc/client/links/httpBatchLink";
import { createTRPCReact } from "@trpc/react-query";

import type { AppRouter } from "@acme/api";

const api = createTRPCReact<AppRouter>();

export default async function getTeamEvents(teamKey: string, year: number) {
const teamEvents = await api.tba.teamEvents.useQuery({
teamKey,
year,
});
console.log(teamEvents);
return teamEvents;
}

console.log("HAAAAAAA: ", getTeamEvents("frc254", 2023));
49 changes: 49 additions & 0 deletions apps/nextjs/src/lib/utils/autoassign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
export function assignTasks(times, users) {
// Sort the times for ordered assignment
const sortedTimes = times.sort();

// Initialize assignments, lastAssigned, and taskCount objects
const assignments = {};
const lastAssigned = {};
const taskCount = {};
// biome-ignore lint/complexity/noForEach: <explanation>
users.forEach(user => {
assignments[user] = [];
lastAssigned[user] = null;
taskCount[user] = 0;
});

for (const time of sortedTimes) {
// Find the user with the least tasks and no concurrent task (if possible)
let bestUser = null;
for (const user of users) {
if (!lastAssigned[user] || lastAssigned[user] !== time) {
bestUser = bestUser || user; // Assign the first available user
if (taskCount[user] < taskCount[bestUser]) {
bestUser = user; // Update if a user with fewer tasks is found
}
}
}

// Assign the task to the best user or the first available user
if (bestUser) {
assignments[bestUser].push(time);
lastAssigned[bestUser] = time;
taskCount[bestUser]++;
} else {
console.warn("No user available for time:", time); // Log a warning if no user is available
}
}

return assignments;
}

// // Example usage:
// const times = ['10:00', '10:30', '11:00', '11:30'].reduce((acc, time) => acc.concat(Array(6).fill(time)), []);
// const users = ['Bryan', 'has', 'no', 'girls', 'also', 'the', 'scouting', 'app', 'works','on','his','computer'];

// const assignments = assignTasks(times, users);
// for (const user in assignments) {
// console.log(`${user}: ${assignments[user]}`);
// }

Loading