Created
May 20, 2010 04:06
-
-
Save dmerrick/407180 to your computer and use it in GitHub Desktop.
A cheap SCVNGR clone to try out node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // config variables | |
| var port = 8090, | |
| host = "localhost"; | |
| // define the clues and answers | |
| var clues = ['What is the most beautiful programming language?', | |
| 'That\'s right! What is the greatest city in the USA?', | |
| 'Perfect! Who makes the best online scavenger hunts?'], | |
| answers = ['Ruby', | |
| 'Boston', | |
| 'SCVNGR'], | |
| wrong = 'Sorry, try again!', | |
| finished = 'Congratulations! You win!'; | |
| // current clue | |
| var question; | |
| // required libraries | |
| var net = require('net'), | |
| sys = require('sys'); | |
| // create the server | |
| var server = net.createServer(function (socket) { | |
| socket.setEncoding("utf8"); | |
| socket.addListener("connect", function () { | |
| question = 0; | |
| sys.puts(new Date().toGMTString() + ": New user has connected!"); | |
| // send welcome message to the user | |
| socket.write("\r\n"); | |
| socket.write("Welcome to DNMRRCK.\r\n"); | |
| socket.write("The online, telnet-based scavenger hunt!\r\n"); | |
| socket.write("\r\n"); | |
| socket.write(clues[question] + "\r\n"); | |
| socket.write("> "); | |
| }); | |
| socket.addListener("data", function (data) { | |
| var response = data.substring(0, data.length - 2).toLowerCase(); | |
| if (response == answers[question].toLowerCase()) { | |
| question++; | |
| if (question < answers.length) { | |
| socket.write(clues[question]); | |
| } else { // we're out of questions | |
| socket.write(finished + "\r\n\r\n"); | |
| sys.puts(new Date().toGMTString() + ": A user has finished the game!"); | |
| socket.end(); | |
| return; | |
| } | |
| } else { // the user got it wrong | |
| socket.write(wrong); | |
| } | |
| socket.write("\r\n"); | |
| socket.write("> "); | |
| }); | |
| socket.addListener("end", function () { | |
| sys.puts(new Date().toGMTString() + ": A user disconnected."); | |
| socket.end(); | |
| }); | |
| }); | |
| // let's start this thing! | |
| server.listen(port, host); | |
| sys.puts('Server running on ' + host + " at port " + port + "."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment