alaingilbert / Turntable-API

Allows you to create bots for turntable.fm
http://alaingilbert.github.com/Turntable-API/
MIT License
317 stars 97 forks source link

Vote Skipping Code Help #209

Closed ghost closed 11 years ago

ghost commented 11 years ago

I researched javascript over the weekend with Google and i still can't figure out what the something is in this code for Vote Skipping. I put this code into Cloud9 and it says voteCount is a undeclared variable so I need to know how to declare it as a variable as well.

global.voteCount = [];

bot.on('speak', function(data) { 
  var text = data.text.trim();
  // .trim() removes any whitespace around your data.text

  if(text.match(/^\/skip/i)) {
    // ^ means regex must match start of string, not anywhere in the string
    // \/ is the backslash escaped
    // skip is your command
    // i at the end makes it case insensitive
    if(voteCount.indexOf(something) < 0) {
      // if something is not in your array, it will return -1, thus the test for less than 0
      // you need to figure out what "something" should be

      // and here you need to add your user's ID that just typed the command to the array

      if(voteCount.length >= 3) {
        // wreck shop
      }
    } 
  }

});
alaingilbert commented 11 years ago

global.voteCount is not the same as voteCount Change the first line for var voteCount = [];

ghost commented 11 years ago

ok, one problem was solved, now i need to figure out what something is in, if(voteCount.indexOf(something) < 0) {

technobly commented 11 years ago

I'll give you a hint... it starts with:

data.
ghost commented 11 years ago

My Guess: data.room.metadata.userid

technobly commented 11 years ago

Close, Look in the example data for 'speak'

ghost commented 11 years ago

Guess 2: data.room.metadata.name

alaingilbert commented 11 years ago

Do you think before to write your guesses ? I mean, where have you found "data.room.metadata.name" ? What's the process you are doing to come here with this answer ? Explain me the logic behind it... (edit: no hard feelings... just wondering)

ghost commented 11 years ago

I look at the example data for speak, and then I do data.room.metadata with one of the section names in the speak example data.

alaingilbert commented 11 years ago

What is the data your are looking at ? (the url ?) Where did you found it ? have you read the documentation for on('speak'.... data) and clicked on data ?

ghost commented 11 years ago

https://github.com/alaingilbert/Turntable-API/blob/master/turntable_data/speak.js this is the link and I clicked on data and it takes me to this same location.

alaingilbert commented 11 years ago

Good Then you have to learn how to use JSON object. If in your code you write : console.log(data); There is probably no room in there... Which bring me back to: where have you found data.room.metadata.name ? why is there a room and a metadata... where does it come from ? cause I see it nowhere in the link you just sent to me.

ghost commented 11 years ago

I made the data.room.metadata.name part on the top of my head.

alaingilbert commented 11 years ago

That is never good.

ghost commented 11 years ago

yeah :(

technobly commented 11 years ago

I guess to be fair the data examples kind of assume you know how to use Javascript/JSON objects, and there are no explicit examples of how to access the data.

https://github.com/alaingilbert/Turntable-API/blob/master/turntable_data/speak.js 'speak' usage

bot.on('speak', function (data) {
  var command = data.command;
  var userid = data.userid;
  var name = data.name;
  var text = data.text;
  console.log(command + " " + userid + " " + name + " " + text);
  // prints to console: "speak 4dea70c94fe7d0517b1a3519 @richhemsley lol"
});

'speak' data object

{
   "command": "speak",
   "userid": "4dea70c94fe7d0517b1a3519",
   "name": "@richhemsley",
   "text": "lol"
}

and for something with more complex data https://github.com/alaingilbert/Turntable-API/blob/master/turntable_data/newsong.js

{
   "now": 1314316306.64,
   "command": "newsong",
   "room": {
      "name": "Indie Chill/Acoustic",
      "created": 1308717327.01,
      "shortcut": "indie_chillacoustic5",
      "name_lower": "indie chill/acoustic",
      "metadata": {
         "djs": ["4e1ca9eda3f75162f50d5743", "4ddfdaf1e8a6c46220000766", "4dfd8e9b4fe7d0250c041593", "4df032194fe7d063190425ca", "4e273936a3f751246601fa29"],
         "upvotes": 0,
         "privacy": "public",
         "max_djs": 5,
         "downvotes": 0,
         "userid": "4df032194fe7d063190425ca",
         "listeners": 196,
         "djcount": 5,
         "max_size": 200,
         "moderator_id": ["4df032194fe7d063190425ca", "4e0f808fa3f751672105e23f", "4df63cbe4fe7d04a19002051", "4e07fc314fe7d05e14070e43"],
         "current_song": {
            "_id": "4de1a8e8845daf3a4d000092",
            "starttime": 1314316306.63,
            "metadata": {
               "album": "Wincing The Night Away",
               "song": "Red Rabbits",
               "coverart": "http://images.mndigital.com/albums/009/545/409/m.jpeg",
               "artist": "The Shins",
               "length": 271,
               "mnid": "9545411"
            }
         },
         "current_dj": "4ddfdaf1e8a6c46220000766",
         "votelog": []
      },
      "roomid": "4e01710f14169c1c4400241f",
      "description": "If it\'s not chill or acoustic it probably belongs elsewhere. We reserve the right to refuse service to the rude, loud, or unkind. Play nice! Hugs! PIE!\\n"
   },
   "success": true
}

'newsong' usage

bot.on('newsong', function (data) {
  var current_dj = data.room.metadata.current_dj;
  var title = data.room.metadata.current_song.metadata.song;
  console.log(current_dj + " " + title);
  // prints to console: "4ddfdaf1e8a6c46220000766 Red Rabbits"
});

"data" is can be named whatever you want... here we call it "stuff"

bot.on('speak', function (stuff) {
  var command = stuff.command;
  var userid = stuff.userid;
  var name = stuff.name;
  var text = stuff.text;
  console.log(command + " " + userid + " " + name + " " + text);
  // prints to console: "speak 4dea70c94fe7d0517b1a3519 @richhemsley lol"
});
ghost commented 11 years ago

How does this help me figure out what the something is in this code line, if(voteCount.indexOf(something) < 0) {

gizmotronic commented 11 years ago

I recommend getting through your codeacademy.com work, especially JavaScript courses 6 (Data Structures), 7 (Objects I), and 8 (Objects II). None of this will make much sense if you don't understand these concepts.

ghost commented 11 years ago

um, ok, that doesn't help a bit and I need this code finished by tomorrow for my bot.

gizmotronic commented 11 years ago

You do realize that if everyone else writes code for your bot, it won't be yours at all? There really aren't any shortcuts.

technobly commented 11 years ago

look at my last post again please. I've outlined exactly everything you can access in .on('speak')

What does voteCount contain?

How does .indexOf work?

technobly commented 11 years ago

And another thing, we are trying to obviously get you to think for yourself so you will be less reliant upon us as time goes on. If that's not something you are willing to do, we'll then I charge $10 per month to host customized bots. Just pay me and I'll be glad to continue helping you with your bot features. Actually, for all of the previous help we have given you, we could have easily charged a lot more than $10 per month. You should be more grateful, really. This actually is consuming too much of my time for what it's worth. Usually when I teach someone they wanna work and appreciate the knowledge they are gaining.

ghost commented 11 years ago

voteCount contains userids of people who voted .indexOf is a index of people who voted so the bot would know.

technobly commented 11 years ago

First part right

Second part wrong. How does indexOf help you check if a userid is in voteCount?

ghost commented 11 years ago

it gives you how many userids have voted.

technobly commented 11 years ago

Nope, you might wanna go back to the first thread you opened on this and look at the links I gave you.

technobly commented 11 years ago

Please stop guessing... and go figure it out :P

technobly commented 11 years ago

Actually it pretty much says how it works in the comments of the code... lol, at least as much as you need to know about how it works.

ghost commented 11 years ago

so indexOf searches the array for the element and gives back the position

technobly commented 11 years ago

Yes it does, and you know what the elements are in voteCount, so what are you going to use for "something" knowing that you only have 4 choices of data in the on('speak', function(data) { } ); message handler...

ghost commented 11 years ago

the userid

technobly commented 11 years ago

Yes, now show me the code... and you might as well start working on the next part as well. Use this around your code to make it javascript formatted http://i.imgur.com/2LU5UXa.png

ghost commented 11 years ago

var voteCount = [];

bot.on('speak', function(data) { 
  var text = data.text.trim();
  // .trim() removes any whitespace around your data.text

  if(text.match(/^\/skip/i)) {
    // ^ means regex must match start of string, not anywhere in the string
    // \/ is the backslash escaped
    // skip is your command
    // i at the end makes it case insensitive
    if(voteCount.indexOf(data.userid) < 0) {
      // if something is not in your array, it will return -1, thus the test for less than 0
      // you need to figure out what "something" should be

      // and here you need to add your user's ID that just typed the command to the array

      if(voteCount.length >= 3) {
        // wreck shop
      }
    } 
  }

});
technobly commented 11 years ago

That is correct, now add the code to address "// and here you need to add your user's ID that just typed the command to the array"

And "// wreck shop" means remove the DJ from deck, or boot them... or yell at them AND remove them... whatever you want.

After that you might want to refer back to the original thread where I outlined everything you need to do.

ghost commented 11 years ago

This is the updated code:

var voteCount = [];

bot.on('speak', function(data) { 
  var text = data.text.trim();
  // .trim() removes any whitespace around your data.text

  if(text.match(/^\/skip/i)) {
    // ^ means regex must match start of string, not anywhere in the string
    // \/ is the backslash escaped
    // skip is your command
    // i at the end makes it case insensitive
    if(voteCount.indexOf(data.userid) < 0) {
      // if something is not in your array, it will return -1, thus the test for less than 0
      // you need to figure out what "something" should be

      // and here you need to add your user's ID that just typed the command to the array

      if(voteCount.length >= 3) {
        bot.speak('I am sorry but your song had to be skipped!');
        bot.remDj();
      }
    } 
  }

});
technobly commented 11 years ago

add the code to address "// and here you need to add your user's ID that just typed the command to the array"

ghost commented 11 years ago

ok, what is the code supposed to look like, because i am kinda confused on what you just said.

technobly commented 11 years ago

What are you confused about?

Do you see in the code the comments I typed that say "// and here you need to add your user's ID that just typed the command to the array" ?

Well that's where you have to add some code to make this work properly. How do you add an element (the userid) to an array (voteCount)?

After that you have to do at least ONE more thing to make it work properly, which can be found in my original reply on your FIRST thread.

Don't quit now, or I'll stalk you and make you learn javascript wherever you go. >:-)

ghost commented 11 years ago

according to one of the website, you have to use splice(), but i do not know how to make a code to use it correctly.

splice adds and removes elements to a array

technobly commented 11 years ago

There is another method that's easier... keep looking.

ghost commented 11 years ago

I found push() It adds new elements to the end of an array.

technobly commented 11 years ago

Ding... that's it. Now add that code and start thinking about how to reset this process... when would you want to reset things, and how might you do that?

ghost commented 11 years ago

I want to reset the process at the newsong event

technobly commented 11 years ago

Add the push() code... and yes you want to reset things on newsong. What will reset it?

ghost commented 11 years ago

a timer

technobly commented 11 years ago

No, but first add the push() code.

Then we'll come back to this.

ghost commented 11 years ago
var voteCount = [];

bot.on('speak', function(data) { 
  var text = data.text.trim();
  // .trim() removes any whitespace around your data.text

  if(text.match(/^\/skip/i)) {
    // ^ means regex must match start of string, not anywhere in the string
    // \/ is the backslash escaped
    // skip is your command
    // i at the end makes it case insensitive
    if(voteCount.indexOf(data.userid) < 0) {
      // if something is not in your array, it will return -1, thus the test for less than 0
      // you need to figure out what "something" should be

      // and here you need to add your user's ID that just typed the command to the array
     voteCount.push(data.userid);

      if(voteCount.length >= 3) {
        bot.speak('I am sorry but your song had to be skipped!');
        bot.remDj();
      }
    } 
  }

});
technobly commented 11 years ago

Ok that's correct... now by itself that would run and work correctly on at least one song.

To reset it, means you know what trips it...

1) What trips it?

2) How can you reset it so it's as if it starts fresh again? (hint: it's not a timer)

ghost commented 11 years ago
  1. the newsong event
technobly commented 11 years ago

Nope... what causes bot.remDj(); to be called ultimately?

ghost commented 11 years ago

the escort event and the endsong event.