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 #12 from Hofled/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Hofled authored Feb 18, 2017
2 parents 68f337b + 2e1be64 commit 59fd495
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 35 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ node_modules
main/**/*.js
main/**/*.js.map

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

*.log
2 changes: 1 addition & 1 deletion main/bot-settings/bot-settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"cooldown": 1000,
"gamble-cooldown": 1000,
"gamble-cooldown": 15000,
"currencies": {
"hofcoins": {
"interval": 60000,
Expand Down
18 changes: 12 additions & 6 deletions main/connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,23 @@ export class BotConnection {
}

connect() {
this.client.connect().
then((data) => {
this.client.connect()
.then((data) => {
this.messageSender.broadcastMessage(this.welcomeMessage);
this.channels.forEach((channel) => channel.startCurrencyInterval());
}).
catch((err) => {

})
.catch((err) => {
console.log(err);
});
}

disconnect() {
this.client.disconnect();
this.client.disconnect()
.then(data => {

})
.catch(err => {
console.log(err);
});
}
}
5 changes: 5 additions & 0 deletions main/functionality/db/db-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export class DataBaseManager {
return this.db.getState();
}

/** Sets the value of the ENTIRE DB - USE WITH CAUTION! */
setEntireDB(value: any) {
this.db.setState(value);
}

/** Sets a specific value from the db.
* @param {string} fieldPath Search parameter specified in the format of a JSON object such as: key.subkey
* @param {any} value value to set the specified field with
Expand Down
2 changes: 1 addition & 1 deletion main/functionality/input/input-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export class InputParser {

/**Removes the command prefix from the input*/
private unwrapCommandPrefix(rawInput: string): string {
return rawInput.slice(1, rawInput.length);
return rawInput.substr(1);
}
}
4 changes: 2 additions & 2 deletions main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { BotConnection } from './connection/index';
import { TmiOptions } from './definitions/connection-options'

// Authentication key required according to the account which you use for the bot
let options = new TmiOptions(true, true, 'Hofcoins', 'oauth:???', ["hofled"]);
let options = new TmiOptions(true, true, 'Hofbot', 'oauth:?????', ["hofled"]);

let botConnection = new BotConnection(options);

botConnection.connect();
botConnection.connect();
23 changes: 23 additions & 0 deletions main/management/casino/casino-executer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,29 @@ export class CasinoExecuter implements IExecuter {
this.cooldownManager.resetLastCommandTime(msgData.messageTime, msgData.channelName);
break;
}
case 'give': {
let currencyType = this.settingsManager.getChannelCurrency(msgData.channelName).name;
let msg;

let userName = params[0];
if (!this.userManager.checkUserExists(userName)) {
msg = "The specified user is not registered in the system.";
this.messageSender.sendMessage(msgData.channelName, msg);
break;
}

let amount = parseInt(params[1]);
if (isNaN(amount) || amount <= 0) {
msg = "The specified amount is invalid.";
this.messageSender.sendMessage(msgData.channelName, msg);
break;
}

this.casinoManager.changeUserCurrency(userName, msgData.channelName, amount, currencyType);
msg = userName + " has been given " + amount + " " + currencyType;
this.messageSender.sendMessage(msgData.channelName, msg);
break;
}
}
}
}
64 changes: 50 additions & 14 deletions main/management/casino/casino-manager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { UserManager } from '../users/user-manager';
import { Observable } from 'rxjs';
import * as moment from 'moment';
import * as _ from 'lodash';

import { UserManager } from '../users/user-manager';
import { SettingsManager } from '../../bot-settings/index';
import { CurrencyData, CurrencySettingsData, GambleData, UserData } from '../../definitions/index';
import { SlotMachineManager } from '../casino/slot-machine/index';
import * as moment from 'moment';

export class CasinoManager {
private userManager: UserManager;
Expand All @@ -20,22 +22,39 @@ export class CasinoManager {
/**Gives every user currently in the chat currency*/
giveAllCurrency(channelName: string, amount: number, currencyType: string, displayMessage: boolean) {
let promise = this.userManager.getCurrentViewers(channelName);
promise.then((viewersObj) => {
for (let viewerType in viewersObj) {
for (let viewer of viewersObj[viewerType]) {
this.changeUserCurrency(viewer, channelName, amount, currencyType);
promise
.then((viewersObj) => {
let usersDB = this.userManager.getAllUsers();
let usersArray = usersDB.users;

for (let viewerType in viewersObj) {
for (let viewer of viewersObj[viewerType]) {

let index = _.findIndex(usersArray, wrapper => wrapper[viewer] != undefined);
index = index == -1 ? usersArray.length : index;
let userData = _.get(usersDB.users, [index, viewer, 'data'], new UserData(viewer));
userData = this.changeUserCurrencyLocal(userData, amount, currencyType);
let userWrap = {};
userWrap[viewer] = { 'data': userData };
_.set(usersArray, [index], userWrap);
}
}
}
})

this.userManager.updateAllUsers(usersArray);
})
.catch(reason => {
console.log("Failed at currency interval:\n");
console.log(reason);
console.log("\n");
});
}

gambleCurrency(channelName: string, userName: string, amount: number, currencyType: string): string {
// Remove the amount the user is gambling and put it
this.changeUserCurrency(userName, channelName, -amount, currencyType);
let gambleResult = this.slotMachineManager.gamble(amount);
this.changeUserCurrency(userName, channelName, gambleResult.gambleOutcome, currencyType);
let gamblerData = this.changeUserCurrency(userName, channelName, gambleResult.gambleOutcome, currencyType);

let gamblerData = this.userManager.getUserData(userName);
gamblerData.gambling["last-gamble-time"] = moment();

this.userManager.updateUserData(userName, gamblerData);
Expand Down Expand Up @@ -63,27 +82,44 @@ export class CasinoManager {
if (!(currencyType in userData.currencies)) {
userData.currencies[currencyType] = new CurrencyData(0);
this.userManager.updateUserData(userName, userData);
return userData.currencies[currencyType].amount;
}

return this.userManager.getUserData(userName).currencies[currencyType].amount;
return userData.currencies[currencyType].amount;
}

/**Gives a user a certain amount of currency (negative amount reduces)*/
changeUserCurrency(userName: string, channelName: string, amount: number, currencyType: string) {
/** Acts the same as changeUserCurrency, but doesnt updates/access the DB */
private changeUserCurrencyLocal(userData: UserData, amount: number, currencyType: string): UserData {
if (!(currencyType in userData.currencies)) {
userData.currencies[currencyType] = new CurrencyData(0);
}

userData.currencies[currencyType].amount += amount;

return userData;
}

/** Gives a user a certain amount of currency (negative amount reduces)
* @returns {UserData} returns the updated UserData object
*/
changeUserCurrency(userName: string, channelName: string, amount: number, currencyType: string): UserData {
let userData = this.userManager.getUserData(userName);
if (!(currencyType in userData.currencies)) {
userData.currencies[currencyType] = new CurrencyData(0);
}
userData.currencies[currencyType].amount += amount;
this.userManager.updateUserData(userName, userData);

return userData;
}

/**Starts the currency interval in the specified channels*/
startCurrencyInterval(channelName: string) {
let channelCurrency = this.settingsManager.getChannelCurrency(channelName);

let id = setInterval(() => {
this.giveAllCurrency(channelName, channelCurrency["amount-per-interval"], channelCurrency.name, false);
this.giveAllCurrency(channelName, channelCurrency["amount-per-interval"], channelCurrency.name,
false);
}, channelCurrency.interval)

this.currencyIntervals.push(id);
Expand Down
22 changes: 18 additions & 4 deletions main/management/commands/storage/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"permission": 5,
"internal": true,
"native": true,
"giveaway": true
"giveaway": true,
"cooldown": false
}
},
{
Expand All @@ -85,7 +86,8 @@
"permission": 1,
"internal": true,
"native": true,
"giveaway": true
"giveaway": true,
"cooldown": false
}
},
{
Expand All @@ -94,7 +96,8 @@
"permission": 5,
"internal": true,
"native": true,
"giveaway": true
"giveaway": true,
"cooldown": false
}
},
{
Expand All @@ -103,7 +106,8 @@
"permission": 5,
"internal": true,
"native": true,
"giveaway": true
"giveaway": true,
"cooldown": false
}
},
{
Expand All @@ -115,6 +119,16 @@
"casino": true,
"cooldown": true
}
},
{
"give": {
"name": "give",
"permission": 5,
"internal": true,
"native": true,
"casino": true,
"cooldown": false
}
}
]
}
2 changes: 2 additions & 0 deletions main/management/users/storage/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
25 changes: 22 additions & 3 deletions main/management/users/user-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import { DataBaseManager } from '../../functionality/db/index';

export class UserManager {
private dbManager: DataBaseManager;
private readonly usersFile: string = 'main/management/users/storage/users_';
private readonly usersKey: string;
private readonly usersFile: string;

constructor(channelName: string) {
this.usersKey = 'users';
this.usersFile = 'main/management/users/storage/users_';
this.dbManager = new DataBaseManager(this.usersFile + channelName + ".json");
}

Expand All @@ -36,12 +39,23 @@ export class UserManager {
http.get('http://tmi.twitch.tv/group/user/' + channel + '/chatters', (res) => {
res.setEncoding('utf8');
let currentUsers = '';

res.on('data', (data) => {
currentUsers += data;
});

res.on('end', () => {
resolve(JSON.parse(currentUsers)['chatters']);
try {
let chatters = JSON.parse(currentUsers)['chatters'];
resolve(chatters);
}
catch (err) {
console.log(err);
}
});

}).on('error', err => {
console.log(err);
});
});
}
Expand All @@ -54,11 +68,16 @@ export class UserManager {
this.dbManager.assignValue('users', searchQuery, { data: userData });
}

/** Returns the entire DB of the users */
getAllUsers(): any {
return this.dbManager.getEntireDB();
}

private checkUserExists(userName: string): boolean {
updateAllUsers(users: any) {
this.dbManager.setValue(this.usersKey, users);
}

checkUserExists(userName: string): boolean {
return this.dbManager.findValue('users', (user) => user[userName] !== undefined) !== undefined;
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Hofbot",
"version": "1.0.0",
"version": "1.2.0",
"description": "A twitch bot for pseudo currency with multi-functions",
"main": "main.js",
"scripts": {
Expand All @@ -19,6 +19,7 @@
"tmi.js": "^1.1.2"
},
"devDependencies": {
"@types/lodash": "^4.14.52",
"@types/node": "^7.0.0",
"ts-node": "^2.0.0",
"typescript": "^2.1.5"
Expand Down

0 comments on commit 59fd495

Please sign in to comment.