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

chore: TypeScriptify Games #2966

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 7 additions & 7 deletions src/backend/games/builtin-game-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const gameManager = require("./game-manager");

exports.loadGames = () => {
[
'bid/bid',
'heist/heist',
'slots/slots',
'trivia/trivia'
].forEach(filename => {
const definition = require(`./builtin/${filename}.js`);
'bid',
'heist',
'slots',
'trivia'
].forEach((gameName) => {
const definition = require(`./builtin/${gameName}/${gameName}`).default;
gameManager.registerGame(definition);
});
};
};
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
"use strict";

const util = require("../../../utility");
const twitchChat = require("../../../chat/twitch-chat");
const commandManager = require("../../../chat/commands/command-manager");
const gameManager = require("../../game-manager");
const currencyAccess = require("../../../currency/currency-access").default;
const currencyManager = require("../../../currency/currency-manager");
const moment = require("moment");
const NodeCache = require("node-cache");

let activeBiddingInfo = {
"active": false,
"currentBid": 0,
"topBidder": "",
"topBidderDisplayName": ""
import util from "../../../utility";
import twitchChat from "../../../chat/twitch-chat";
import commandManager from "../../../chat/commands/command-manager";
import gameManager from "../../game-manager";
import currencyAccess from "../../../currency/currency-access";
import currencyManager from "../../../currency/currency-manager";
import { SystemCommand } from "../../../../types/commands";
import { GameSettings } from "../../../../types/game-manager";
import { BidSettings } from "./bid-settings";
import moment from "moment";
import NodeCache from "node-cache";

type GameData = {
active: boolean;
currentBid: number;
topBidder: string;
topBidderDisplayName: string;
};
let bidTimer;

let activeBiddingInfo: GameData = {
active: false,
currentBid: 0,
topBidder: "",
topBidderDisplayName: ""
};
let bidTimer: NodeJS.Timeout|null;
const cooldownCache = new NodeCache({checkperiod: 5});
const BID_COMMAND_ID = "firebot:bid";

function purgeCaches() {
cooldownCache.flushAll();
activeBiddingInfo = {
"active": false,
"currentBid": 0,
"topBidder": ""
active: false,
currentBid: 0,
topBidder: "",
topBidderDisplayName: ""
};
}

async function stopBidding(chatter) {
clearTimeout(bidTimer);
async function stopBidding(chatter: string) {
if (bidTimer) {
clearTimeout(bidTimer);
bidTimer = null;
}
if (activeBiddingInfo.topBidder) {
await twitchChat.sendChatMessage(`${activeBiddingInfo.topBidderDisplayName} has won the bidding with ${activeBiddingInfo.currentBid}!`, null, chatter);
} else {
Expand All @@ -39,7 +51,7 @@ async function stopBidding(chatter) {
purgeCaches();
}

const bidCommand = {
const bidCommand: SystemCommand = {
definition: {
id: BID_COMMAND_ID,
name: "Bid",
Expand Down Expand Up @@ -103,7 +115,7 @@ const bidCommand = {
onTriggerEvent: async (event) => {
const { chatMessage, userCommand } = event;

const bidSettings = gameManager.getGameSettings("firebot-bid");
const bidSettings = gameManager.getGameSettings("firebot-bid") as GameSettings<BidSettings>;
const chatter = bidSettings.settings.chatSettings.chatter;

const currencyId = bidSettings.settings.currencySettings.currencyId;
Expand All @@ -130,9 +142,10 @@ const bidCommand = {
}

activeBiddingInfo = {
"active": true,
"currentBid": bidAmount,
"topBidder": ""
active: true,
currentBid: bidAmount,
topBidder: "",
topBidderDisplayName: ""
};

const raiseMinimum = bidSettings.settings.currencySettings.minIncrement;
Expand Down Expand Up @@ -176,11 +189,9 @@ const bidCommand = {
}

const minBid = bidSettings.settings.currencySettings.minBid;
if (minBid != null && minBid > 0) {
if (bidAmount < minBid) {
await twitchChat.sendChatMessage(`Bid amount must be at least ${minBid} ${currencyName}.`, null, chatter, chatMessage.id);
return;
}
if (minBid != null && minBid > 0 && bidAmount < minBid) {
await twitchChat.sendChatMessage(`Bid amount must be at least ${minBid} ${currencyName}.`, null, chatter, chatMessage.id);
return;
}

const userBalance = await currencyManager.getViewerCurrencyAmount(username, currencyId);
Expand Down Expand Up @@ -228,15 +239,19 @@ function registerBidCommand() {
}

function unregisterBidCommand() {
commandManager.unregisterSystemCommand(BID_COMMAND_ID);
if (commandManager.hasSystemCommand(BID_COMMAND_ID)) {
commandManager.unregisterSystemCommand(BID_COMMAND_ID);
}
}

function setNewHighBidder(username, userDisplayName, amount) {
function setNewHighBidder(username: string, userDisplayName: string, amount: number) {
activeBiddingInfo.currentBid = amount;
activeBiddingInfo.topBidder = username;
activeBiddingInfo.topBidderDisplayName = userDisplayName;
}

exports.purgeCaches = purgeCaches;
exports.registerBidCommand = registerBidCommand;
exports.unregisterBidCommand = unregisterBidCommand;
export default {
purgeCaches,
registerBidCommand,
unregisterBidCommand
};
16 changes: 16 additions & 0 deletions src/backend/games/builtin/bid/bid-settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type BidSettings = {
currencySettings: {
currencyId: string;
minBid?: number;
minIncrement?: number;
};
timeSettings: {
timeLimit?: number;
};
cooldownSettings: {
cooldown?: number;
};
chatSettings: {
chatter: string;
};
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
"use strict";
import { FirebotGame } from "../../../../types/game-manager";
import bidCommand from "./bid-command";
import { BidSettings } from "./bid-settings";

const bidCommand = require("./bid-command");

/**
* @type {import('../../game-manager').FirebotGame}
*/
module.exports = {
const bidGame: FirebotGame<BidSettings> = {
id: "firebot-bid",
name: "Bid",
subtitle: "Put something up for auction",
Expand Down Expand Up @@ -107,4 +104,6 @@ module.exports = {
onSettingsUpdate: () => {
bidCommand.purgeCaches();
}
};
};

export default bidGame;
Loading
Loading