Skip to content

Commit

Permalink
send user info (closes #18)
Browse files Browse the repository at this point in the history
remove ability to pick announce
  • Loading branch information
Govorunb committed Jun 11, 2024
1 parent beb52c9 commit 2e19ad2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
12 changes: 1 addition & 11 deletions common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ export const enum LiteralTypes {
Vector
}

export const enum AnnounceType {
DefaultAnnounce,
DefaultSilent,
// e.g. "add signal"
AlwaysAnnounce,
// e.g. "say"/"hint" (because the message itself is the announcement)
AlwaysSilent,
}

type EnumTypeName = string;
type ParamType = LiteralTypes | EnumTypeName;

Expand Down Expand Up @@ -63,7 +54,7 @@ export type Redeem = {
title: string;
description: string;
args: Parameter[];
announce?: AnnounceType;
announce?: boolean;
moderated?: boolean;

image: string;
Expand All @@ -86,7 +77,6 @@ export type Cart = {
id: string;
sku: string;
args: { [name: string]: any };
announce: boolean;
};

export type IdentifiableCart = Cart & {
Expand Down
1 change: 1 addition & 0 deletions ebs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "esbuild --bundle --minify --platform=node --sourcemap=inline --outfile=dist/index.js src/index.ts"
},
"dependencies": {
"@twurple/api": "^7.1.0",
"@twurple/ebs-helper": "^7.1.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
Expand Down
13 changes: 7 additions & 6 deletions ebs/src/modules/game/connection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Message, MessageType } from "./messages";
import { ResultMessage, HelloMessage, GameMessage } from "./messages.game";
import { Message, MessageType, TwitchUser } from "./messages";
import { ResultMessage, GameMessage } from "./messages.game";
import * as ServerWS from "ws";
import { v4 as uuid } from "uuid";
import { CommandInvocationSource, RedeemMessage, ServerMessage } from "./messages.server";
import { Redeem } from "common/types";
import { Cart, Redeem } from "common/types";

const VERSION = "0.1.0";

Expand Down Expand Up @@ -124,7 +124,7 @@ export class GameConnection {
timestamp: Date.now()
}
}
public redeem(redeem: Redeem, args: {[name: string]: any}, announce: boolean, transactionId: string) {
public redeem(redeem: Redeem, cart: Cart, user: TwitchUser, transactionId: string) {
if (!transactionId) {
console.error(`Tried to redeem without transaction ID`);
return;
Expand All @@ -136,8 +136,9 @@ export class GameConnection {
source: CommandInvocationSource.Swarm,
command: redeem.id,
title: redeem.title,
announce,
args
announce: redeem.announce,
args: cart.args,
user
} as RedeemMessage;
if (this.outstandingRedeems.has(msg.guid)) {
console.error(`Redeeming ${msg.guid} more than once`);
Expand Down
6 changes: 3 additions & 3 deletions ebs/src/modules/game/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export type Message = {
}

export type TwitchUser = {
/** Channel id */
id: number,
/** Numeric user id */
id: string,
/** Twitch username (login name) */
userName: string,
login: string,
/** User's chosen display name. */
displayName: string
}
40 changes: 28 additions & 12 deletions ebs/src/modules/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { getConfig } from "./config";
import { getPrepurchase, isReceiptUsed, isUserBanned, registerPrepurchase, deletePrepurchase } from "../util/db";
import { logToDiscord } from "../util/logger";
import { connection } from "./game";
import { TwitchUser } from "./game/messages";
import { getHelixUser } from "../util/twitch";

app.post("/public/prepurchase", async (req, res) => {
const cart = req.body as Cart;
Expand Down Expand Up @@ -213,30 +215,32 @@ app.post("/public/transaction", async (req, res) => {
],
}).then();
} else {
const redeemAnnounce = redeem.announce || 0;
const [doAnnounce, canChange] = [!(redeemAnnounce & 1), !(redeemAnnounce & 2)]; // funny
// don't allow to change when you shouldn't
if (cart.announce != doAnnounce && !canChange) {
let userInfo = await getTwitchUser(cart.userId);
if (!userInfo) {
logToDiscord({
transactionToken: transaction.token,
userIdInsecure: req.twitchAuthorization!.user_id!,
important: false,
fields: [
{
header: "Invalid announce",
header: "Could not get Twitch user info",
content: {
config: currentConfig.version,
cart: cart,
transaction: transaction,
announceType: redeemAnnounce,
userPicked: cart.announce,
},
},
],
error: userInfo,
}
}
]
});
cart.announce = doAnnounce!;
// very much not ideal but they've already paid... so...
userInfo = {
id: cart.userId,
login: cart.userId,
displayName: cart.userId,
}
}
connection.redeem(redeem, cart.args, cart.announce, transaction.token);
connection.redeem(redeem, cart, userInfo, transaction.token);
}

res.sendStatus(200);
Expand Down Expand Up @@ -268,3 +272,15 @@ app.post("/public/transaction/cancel", async (req, res) => {
res.sendStatus(404);
}
});

async function getTwitchUser(id: string): Promise<TwitchUser | null> {
const user = await getHelixUser(id);
if (!user) {
return null;
}
return {
id: user.id,
displayName: user.displayName,
login: user.name,
}
}
12 changes: 12 additions & 0 deletions ebs/src/util/twitch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiClient, HelixUser } from "@twurple/api";
import { RefreshingAuthProvider } from "@twurple/auth";

const authProvider = new RefreshingAuthProvider({
clientId: process.env.TWITCH_API_CLIENT_ID!,
clientSecret: process.env.TWITCH_API_CLIENT_SECRET!
});
const api = new ApiClient({authProvider, batchDelay: 100});

export async function getHelixUser(userId: string): Promise<HelixUser | null> {
return api.users.getUserByIdBatched(userId);
}

0 comments on commit 2e19ad2

Please sign in to comment.