Skip to content
This repository has been archived by the owner on Sep 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from Hofled/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Hofled authored Feb 10, 2017
2 parents 577b203 + cb71738 commit 68f337b
Show file tree
Hide file tree
Showing 24 changed files with 461 additions and 234 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ node_modules
main/**/*.js
main/**/*.js.map

!main/management/users/storage/.gitkeep
main/management/users/storage/*

*.log
30 changes: 19 additions & 11 deletions main/bot-settings/bot-settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{
"cooldown": 3000,
"gamble-cooldown": 15000,
"currencies": {
"hofcoins": {
"interval": 60000,
"amount-per-interval": 10,
"channels": [
"hofled"
],
"name": "hofcoins"
}
"cooldown": 1000,
"gamble-cooldown": 1000,
"currencies": {
"hofcoins": {
"interval": 60000,
"amount-per-interval": 1,
"channels": [
"hofled"
],
"name": "hofcoins"
},
"caps": {
"interval": 60000,
"amount-per-interval": 1,
"channels": [
"blasting_cap"
],
"name": "caps"
}
}
}
17 changes: 10 additions & 7 deletions main/bot-settings/settings-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ export class SettingsManager {
readonly settingsFile: string = "main/bot-settings/bot-settings.json";
settingsEmitter: EventEmitter;
private dbManager: DataBaseManager;
private settingsObj: BotSettings;

constructor() {
this.dbManager = new DataBaseManager(this.settingsFile);
this.settingsEmitter = new EventEmitter();
this.settingsObj = this.getBotSettings();
}

getChannelCurrency(channel: string, settings: BotSettings): CurrencySettingsData {
let currencySetting;
for (let currency in settings.currencies) {
if (settings.currencies[currency].channels.indexOf(channel) !== -1) {
return currencySetting = settings.currencies[currency];
getChannelCurrency(channel: string): CurrencySettingsData {
for (let currency in this.settingsObj.currencies) {
if (this.settingsObj.currencies[currency].channels.indexOf(channel) !== -1) {
return this.settingsObj.currencies[currency];
}
}
}
Expand All @@ -30,7 +31,9 @@ export class SettingsManager {
}

notifySettingsChanged() {
this.settingsEmitter.emit('settings-changed', this.dbManager.getEntireDB());
let newSettings = this.dbManager.getEntireDB();
this.settingsObj = newSettings;
this.settingsEmitter.emit('settings-changed', newSettings);
}

/** Updates the cooldown field in the settings
Expand All @@ -43,7 +46,7 @@ export class SettingsManager {
return false;
}

this.dbManager.setValue('cooldown', cooldown * 1000);
this.dbManager.setValue(cooldownType, cooldown * 1000);
this.notifySettingsChanged();
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions main/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export class BotConnection {
let cooldownManager = new CooldownManager(settingsManager, this.tmiOptions.channels);
this.backupManager = new BackupManager();

this.generateChannels(this.client, this.tmiOptions, settingsManager, cooldownManager);

this.welcomeMessage = messageBuilder.formatMessage(this.welcomeMessage, [this.tmiOptions.identity.username]);

this.backupManager.startBackupInterval(1800, "main/management/users/storage", "json");

this.generateChannels(this.client, this.tmiOptions, settingsManager, cooldownManager);

// Starting to listen on messages received in the chats the client is connected to.
this.listenToConnection(this.channels);
}
Expand All @@ -53,12 +53,13 @@ export class BotConnection {

private generateChannels(client: tmi.client, tmiOptions: TmiOptions, settingsManager: SettingsManager, cooldownManager: CooldownManager) {
tmiOptions.channels.forEach(channel => {
this.channels.push(new Channel(channel.substr(1), client, this.tmiOptions, settingsManager, cooldownManager));
this.genChannelDB(channel, tmiOptions);
this.channels.push(new Channel(channel.substr(1), client, this.tmiOptions, settingsManager, cooldownManager));
});
}

private listenToConnection(channels: Channel[]) {

this.client.on("message", function (channel, userstate, message, self) {

let messageData = new MessageData(channel, userstate, message, self);
Expand Down
5 changes: 4 additions & 1 deletion main/definitions/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CasinoManager } from '../management/casino/index';
import { SettingsManager } from '../bot-settings/index';
import { UserManager } from '../management/users/user-manager';
import { CooldownManager } from '../management/cooldown/cooldown-manager';
import { GiveawayManager } from '../functionality/giveaway/index';

/** A logical representation of a single channel connection */
export class Channel {
Expand All @@ -19,12 +20,14 @@ export class Channel {
private userManager: UserManager;

constructor(name: string, client: tmi.client, tmiOptions: TmiOptions, settingsManager: SettingsManager, cooldownManager: CooldownManager) {
this.name = name;
this.messageSender = new MessageSender(client, tmiOptions);
this.messageBuilder = new MessageBuilder();
this.userManager = new UserManager(name);
let permissionValidator = new PermissionValidator(this.userManager);
this.casinoManager = new CasinoManager(this.userManager, settingsManager);
this.commandHandler = new CommandHandler(this.messageSender, this.messageBuilder, settingsManager, this.casinoManager, cooldownManager, permissionValidator, this.userManager);
let giveawayManager = new GiveawayManager(this.userManager, this.casinoManager);
this.commandHandler = new CommandHandler(this.messageSender, this.messageBuilder, settingsManager, this.casinoManager, cooldownManager, permissionValidator, this.userManager, giveawayManager);
}

startCurrencyInterval() {
Expand Down
5 changes: 4 additions & 1 deletion main/definitions/command-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ export class CommandData {
"command-crud": boolean;
/**A flag that indicates if a command is a logging related command*/
log: boolean;
/**A flag that indicates if a command is a giveaway command */
giveaway: boolean;
/**A flag that defines if a command is affected by cooldown*/
cooldown: boolean;

constructor(permission: number = 1, name: string, content?: string, internal: boolean = false, native: boolean = false, botSettings: boolean = false, casino: boolean = false, commandCrud: boolean = false, isLog: boolean = false, cooldown: boolean = true) {
constructor(permission: number = 1, name: string, content?: string, internal: boolean = false, native: boolean = false, botSettings: boolean = false, casino: boolean = false, commandCrud: boolean = false, isLog: boolean = false, cooldown: boolean = true, giveaway: boolean = false) {
this.name = name;
this.permission = permission;
this.content = content;
Expand All @@ -29,6 +31,7 @@ export class CommandData {
this.casino = casino;
this["command-crud"] = commandCrud;
this.log = isLog;
this.giveaway = giveaway;
this.cooldown = cooldown;
}
}
4 changes: 1 addition & 3 deletions main/definitions/currency-data.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export class CurrencyData {
amount: number;
channel: string;

constructor(amount: number, channelName: string) {
constructor(amount: number) {
this.amount = amount;
this.channel = channelName;
}
}
11 changes: 2 additions & 9 deletions main/functionality/db/db-generator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import * as fs from 'fs';
import * as lowdb from 'lowdb';

export class DataBaseGenerator {
private readonly usersStoragePath: string = "main/management/users/storage/users_";

generateUsersDB(channelName: string) {
let options = { flag: 'wx' };

try {
fs.writeFileSync(this.usersStoragePath + channelName + ".json", {}, options);
}
catch (err) {
console.log('error in generating ' + this.usersStoragePath + channelName + ".json");
}
new lowdb(this.usersStoragePath + channelName + ".json").defaults({ users: [] }).value();
}
}
25 changes: 14 additions & 11 deletions main/functionality/db/db-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ export class DataBaseManager {
constructor(dbPath: string, private db = new lowdb(dbPath)) {
}

/** Used to find a specific value within an array entry
/** Used to find a specific value within an entire array by being able to iterate and use a predicate
*/
findValue<T>(arrayPath: string, predicate: any): T {
return this.db.get(arrayPath).find(predicate).value<T>();
}

/** Assings a value to the specified array key using the specified predicate and value
* @param {any} predicate the predicate to use for finding the required entries
* @param {any} predicate the predicate to use for finding the required entries, based on lodash syntax:
* can be an array which sepcifies the location / key of every field
* @param {any} value the key and value to set in the specified entry, can use the format: {objectKey: newValue}
*/
assignValue(arrayPath: string, predicate: any, value: any) {
this.db.get(arrayPath).find(predicate).assign(value);
this.db.get(arrayPath).find(predicate).assign(value).value();
}

/** Removes all objects that satisfy the predicate value from the specified fieldPath,
Expand All @@ -36,20 +37,15 @@ export class DataBaseManager {
}

/** Checks if the specified field exists in the db.
* @param {?string} arrayQueuery search a parent array type entry which contains the fieldPath
* @param {string} fieldPath the field to check using the dot notation such as: key.subkey
*/
checkHas(fieldPath: string, arrayQueuery?: { parentKey: string, predicate: any }): boolean {
if (arrayQueuery) {
return this.db.get(arrayQueuery.parentKey).find(arrayQueuery.predicate).hasIn(fieldPath).value<boolean>();
}

checkHas(fieldPath: string): boolean {
return this.db.hasIn(fieldPath).value<boolean>();
}

/** Returns the entire DB in its current state */
getEntireDB(): any {
this.db.getState();
return this.db.getState();
}

/** Sets a specific value from the db.
Expand All @@ -66,4 +62,11 @@ export class DataBaseManager {
getValue<T>(fieldPath: string): T {
return this.db.get(fieldPath).value<T>();
}

/** Searches the database using the specified array of parameters for usage in the query.
* @param {any} query a paramaters array that describes way to access the value.
*/
querySearch<T>(query: any[]): T {
return this.db.get(query).value<T>();
}
}
54 changes: 54 additions & 0 deletions main/functionality/giveaway/giveaway-executer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { IExecuter } from '../../definitions/interfaces/IExecuter';
import { CooldownManager } from '../../management/cooldown/cooldown-manager';
import { SettingsManager } from '../../bot-settings/index';
import { GiveawayManager } from './index';
import { MessageData, CommandData } from '../../definitions/index';
import { MessageSender } from '../messages/index';

/**
* GiveawayExecuter
*/
export class GiveawayExecuter implements IExecuter {
cooldownManager: CooldownManager;
private giveawayManager: GiveawayManager;
private messageSender: MessageSender;
private settingsManager: SettingsManager;

constructor(giveawayManager: GiveawayManager, messageSender: MessageSender, settingsManager: SettingsManager) {
this.giveawayManager = giveawayManager;
this.messageSender = messageSender;
this.settingsManager = settingsManager;
}

execute(command: CommandData, msgData: MessageData, params?: any[]) {
switch (command.name) {
case "giveaway": {
let msg = this.giveawayManager.startGiveaway(msgData, params);
this.messageSender.sendMessage(msgData.channelName, msg);
break;
}

case "entergiveaway": {
let channelCurrrency = this.settingsManager.getChannelCurrency(msgData.channelName).name;
let msg = this.giveawayManager.registerUser(msgData.userState.username, msgData.channelName, channelCurrrency, params);
if (!(msg == null)) {
this.messageSender.sendMessage(msgData.channelName, msg);
}
break;
}

case "cancelgiveaway": {
let channelCurrrency = this.settingsManager.getChannelCurrency(msgData.channelName).name;
let msg = this.giveawayManager.cancelGiveaway(msgData.channelName, channelCurrrency);
this.messageSender.sendMessage(msgData.channelName, msg);
break;
}

case "endgiveaway": {
let msg = this.giveawayManager.endGiveaway();
this.messageSender.sendMessage(msgData.channelName, msg);
break;
}
}
}
}
Loading

0 comments on commit 68f337b

Please sign in to comment.