TwittMUD/index.js

150 lines
4.5 KiB
JavaScript

/* TwittMUD - Twitter Multi-User Dungeon
````````````````````````````````````````````````````````````````````````````````
This is a weird program that runs a MUD from a Twitter account.
Tweet limit: 123 characters, due to 140 (max) - 17 ('@' + name max + ' ')
Tweet Transaction example:
a: You are in a white room. There is a painting. There are exits: north, east, west, south
b: @TwittMUD examine painting
a: The painting is by Rembrant, it looks expensive.
b: @TwittMUD take painting
a: You take the painting.
b: @TwittMUD south
a: You are on a balcony overlooking the ocean. There is a lighthouse. There are exits: north
--------------------------------------------------------------------------------
How does the game world structure look?
Room-oriented:
start {
things: {
0: 'candle',
1: 'thing'
},
exits: {
'e': 'eastern_plaza'
},
locations: {
'lighthouse': {
distance: 0,
direction: 'w',
location: 'plateu'
}
'beach'
},
users: {
0: '123435388',
1: '123444441'
}
}
*/
var Twit = require('twit');
var fs = require('fs');
/* ================ Core Program Data ================ */
var config = {};
var client = null;
var stream = null;
var vol = {
last_tweet_id: 0
};
/* ================ Main ================ */
var gogogo = function() {
try {
config = JSON.parse(fs.readFileSync("./config.json", "utf8"));
} catch(e) {
console.log(e);
return 1;
}
try {
client = new Twit({
consumer_key: config.consumer_key,
consumer_secret: config.consumer_secret,
access_token: config.access_token,
access_token_secret: config.access_token_secret
});
} catch(e) {
console.log(e);
return 2;
}
stream = client.stream('user', {with: 'user'});
stream.on('follow', handleFollow);
stream.on('unfollow', handleUnfollow);
stream.on('tweet', handleTweet);
stream.on('error', handleError);
};
/* ================ Handler Functions (events, tweets, etc.) ================ */
/* handleTweet
Called on each tweet to this Twitter account. After some checks to ensure the tweet is intended for TwittMUD and is a command, it calls handleCommand, passing the contents of the message, who it was from, the desired lang, and the reply_id if it exists.
*/
var handleTweet = function(tweet) {
var from = tweet.user.id;
var id = tweet.id;
var id_str = tweet.id_str;
var reply_id = tweet.in_reply_to_status_id;
var text = tweet.text;
var lang = tweet.lang;
//vol.last_tweet_id = tweet.id;
// does the response match
var exp = /^@(.*)\ (.*)/
var res = exp.exec(text);
var to = res[1];
var msg = res[2];
// Conditions for handling messages as commands:
// 2. does the user follow us? (is in followers)
// * is the user active? (is in active users list)
// 3. is it a reply?
// *
// 4. Is the message parsable as a command?
// 1. is the message to us? (@TwittMUD)
var user = 'TwittMUD'; // TODO: replace with config.user_name
if (to.toLowerCase().indexOf(user.toLowerCase()) != -1) {
}
console.log(from + '=>' + to + ': ' + msg);
return true;
};
/* handleFollow
This function is called whenever the stream receives a 'follow' user event. This will trigger the activateUser for the given user_id.
*/
var handleFollow = function(event) {
var id = event.source.id;
var name = event.source.name;
var screen_name = event.source.screen_name;
console.log(name+'/'+screen_name+'('+id+') has entered the game!');
};
/* handleUnfollow
*/
var handleUnfollow = function(event) {
console.log(event);
};
/* handleError
*/
var handleError = function(event) {
console.log(err);
};
/* ================ Activity Functions (tweeting, etc.) ================ */
var tweetAt = function(user_id, tweet) {
};
var replyTweet = function(tweet_id, user_id, tweet) {
};
/* ================ User Activate/Deactivate Functions ================ */
/* activateUser
Removes the given user from the "Inactive Players" list and adds them to the "Active Players" list, thereby making them available for gameplay commands.
*/
var activateUser = function(user) {
};
/* deactivateUser
Removes the given user from the "Active Players" list and adds them to the "Inactive Players" list, thereby making them unavailable for gameplay commands. The user may still provide the "deactivate" or "restart" commands.
*/
var deactivateUser = function(user) {
};
/* deleteUser
Removes all user information - this is only ever emitted when the user unfollows @TwittMUD.
*/
var deleteUser = function(user) {
};
gogogo();