Skip to content

Commit

Permalink
feat: send message to losers
Browse files Browse the repository at this point in the history
  • Loading branch information
ledouxm committed Feb 1, 2022
1 parent f307ee9 commit 0a3642d
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 7 deletions.
7 changes: 7 additions & 0 deletions electron/LCU/lcu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export const checkFriendList = async () => {
return stats;
};

export const postMessage = (payload: { summonerName: string; message: string }) => {
const url = `/lol-game-client-chat/v1/instant-messages?summonerName=${encodeURI(
payload.summonerName
)}&message=${encodeURI(payload.message)}`;
return connectorStatus.api.post(url);
};

export const getAllApexLeague = async () => {
const tiers: Tier[] = ["MASTER", "GRANDMASTER", "CHALLENGER"];
const payload: Partial<Record<Tier, number>> = {};
Expand Down
2 changes: 2 additions & 0 deletions electron/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const config: { current: Record<string, any> | null } = { current: null }
const initialConfig = {
windowsNotifications: true,
dirname: __dirname,
defaultLossMessage:
"\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}\u{1F602}",
};
const configFilePath = path.join(__dirname, "config.json");
export const loadConfig = async () => {
Expand Down
3 changes: 3 additions & 0 deletions electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
sendFriendList,
sendFriendListWithRankings,
sendFriendRank,
sendInstantMessage,
sendMatches,
sendNbNewNotifications,
sendSelectAllFriends,
Expand Down Expand Up @@ -83,6 +84,8 @@ ipcMain.on("friendList/select", receiveToggleSelectFriends);
ipcMain.on("friendList/select-all", sendSelectAllFriends);
ipcMain.on("friendList/selected", () => sendSelected());
ipcMain.on("friendList/in-game", () => sendToClient("friendList/in-game", inGameFriends.current));
ipcMain.on("friendList/message", sendInstantMessage);

ipcMain.on("notifications/all", sendCursoredNotifications);
ipcMain.on("notifications/nb-new", sendNbNewNotifications);
ipcMain.on("friend/matches", sendMatches);
Expand Down
9 changes: 8 additions & 1 deletion electron/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { config } from "../config";
import { Friend } from "../entities/Friend";
import { getAllApexLeague, getMatchHistoryBySummonerPuuid } from "../LCU/lcu";
import { getAllApexLeague, getMatchHistoryBySummonerPuuid, postMessage } from "../LCU/lcu";
import { sendToClient } from "../utils";
import {
getFriendAndRankingsFromDb,
Expand Down Expand Up @@ -82,3 +83,9 @@ export const sendApex = async () => {
const payload = await getAllApexLeague();
sendToClient("config/apex", payload);
};

export const sendInstantMessage = async (_: any, { summonerName }: { summonerName: string }) => {
if (!config.current) return;
await postMessage({ summonerName, message: config.current.defaultLossMessage });
sendToClient("friendList/message", "ok");
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"lcu-connector": "^2.1.3",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-hook-form": "^7.25.3",
"react-icons": "^4.3.1",
"react-query": "^3.34.8",
"react-router-dom": "6",
Expand Down
29 changes: 24 additions & 5 deletions src/features/Notifications/NotificationItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Box, BoxProps, Flex } from "@chakra-ui/react";
import { ChatIcon } from "@chakra-ui/icons";
import { Box, BoxProps, chakra, Flex, IconButton, Tooltip } from "@chakra-ui/react";
import { useNavigate } from "react-router-dom";
import { FriendDto, NotificationDto } from "../../types";
import { ProfileIcon } from "../DataDragon/Profileicon";
Expand Down Expand Up @@ -39,9 +40,27 @@ export const NotificationItem = ({
)}
{friend?.name || notification.friend.name}
</Flex>
<Box ml="10px" color={isRed ? "red-loss" : "blue-win"}>
{formatNotification(notification)}
</Box>
<Flex ml="10px" alignItems="center">
<chakra.span color={isRed ? "red-loss" : "blue-win"}>
{formatNotificationContent(notification)}
</chakra.span>
{["DEMOTION", "LOSS"].includes(notification.type) && (
<Tooltip label="Send recorded message to this friend">
<IconButton
aria-label="Send message"
icon={<ChatIcon />}
onClick={() =>
window.Main.sendMessage("friendList/message", {
summonerName: notification.friend.name,
})
}
h="auto"
py="6px"
ml="10px"
/>
</Tooltip>
)}
</Flex>
</Flex>
<Box fontSize="small" color="gray.400">
{formatTooltipLabel(notification)}
Expand All @@ -55,7 +74,7 @@ export const NotificationItem = ({
);
};

const formatNotification = (notification: NotificationDto) => {
const formatNotificationContent = (notification: NotificationDto) => {
if (notification.from.slice(0, 4) === "NONE") {
return (
<Flex alignItems="center">
Expand Down
35 changes: 34 additions & 1 deletion src/features/Options/OptionsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { Box, Button, Center, Checkbox, Icon, Input, Spinner, Stack } from "@chakra-ui/react";
import {
Box,
Button,
Center,
CenterProps,
Checkbox,
FormLabel,
Icon,
Input,
Spinner,
Stack,
} from "@chakra-ui/react";
// import { shell } from "electron";
import { useMutation, useQuery } from "react-query";
import { electronRequest } from "../../utils";
import { AiFillGithub, AiFillTwitterCircle } from "react-icons/ai";
import { CopyIcon } from "@chakra-ui/icons";
import { useForm } from "react-hook-form";
export const OptionsPage = () => {
const dlDbMutation = useMutation(() => electronRequest("config/dl-db"));
const openExternalBrowserMutation = useMutation((url: string) =>
Expand Down Expand Up @@ -74,6 +86,7 @@ export const ConfigPanel = () => {

return (
<>
<ConfigForm config={config} mb="50px" />
<Checkbox
isChecked={config.windowsNotifications}
onChange={(e) => editConfigQuery.mutate({ windowsNotifications: e.target.checked })}
Expand All @@ -89,3 +102,23 @@ export const ConfigPanel = () => {
</>
);
};

export const ConfigForm = ({ config, ...props }: { config: Record<string, any> } & CenterProps) => {
const { handleSubmit, register } = useForm({
defaultValues: { defaultLossMessage: config.defaultLossMessage },
});

const onSubmit = (data: any) => console.log(data);

return (
<form onSubmit={handleSubmit(onSubmit)}>
<Center flexDir="column" {...props}>
<FormLabel>Default loss message</FormLabel>
<Input type="text" {...register("defaultLossMessage")} />
<Button mt="10px" type="submit">
Save
</Button>
</Center>
</form>
);
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5082,6 +5082,11 @@ [email protected]:
use-callback-ref "^1.2.1"
use-sidecar "^1.0.1"

react-hook-form@^7.25.3:
version "7.25.3"
resolved "https://registry.yarnpkg.com/react-hook-form/-/react-hook-form-7.25.3.tgz#1475fd52398e905e1f6d88835f96aaa1144635c3"
integrity sha512-jL4SByMaC8U3Vhu9s7CwgJBP4M6I3Kpwxib9LrCwWSRPnXDrNQL4uihSTqLLoDICqSUhwwvian9uVYfv+ITtGg==

react-icons@*, react-icons@^4.3.1:
version "4.3.1"
resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-4.3.1.tgz#2fa92aebbbc71f43d2db2ed1aed07361124e91ca"
Expand Down

1 comment on commit 0a3642d

@Nyuwb
Copy link

@Nyuwb Nyuwb commented on 0a3642d Feb 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahahaha meilleure feature ever !

Please sign in to comment.