Permalink
Browse files

Kata-Server in coffeescript

  • Loading branch information...
1 parent f6b6e7f commit 431abaf1e0ebeaa184301991c923b86051dbea84 @conradthukral committed Sep 29, 2011
Showing with 126 additions and 0 deletions.
  1. +2 −0 kata/.gitignore
  2. +31 −0 kata/lib/index.coffee
  3. +75 −0 kata/lib/trial.coffee
  4. +18 −0 kata/package.json
View
@@ -0,0 +1,2 @@
+node_modules
+
View
@@ -0,0 +1,31 @@
+dgram = require 'dgram'
+trial = require './trial'
+
+runningTrials = {}
+socket = null
+
+createSender = (host, port) ->
+ (message) ->
+ console.log "Sending #{message} to #{host}"
+ buffer = new Buffer(message)
+ socket.send buffer, 0, buffer.length, port, host
+
+findOrCreateTrialFor = (host, port) ->
+ result = runningTrials[host]
+ unless result
+ result = trial.create createSender(host, port)
+ runningTrials[host] = result
+ result
+
+removeTrialFor = (host) ->
+ runningTrials[host] = null
+
+handleMessage = (message, rinfo) =>
+ console.log "Received #{message.toString()} from #{rinfo.address}"
+ currentTrial = findOrCreateTrialFor rinfo.address, rinfo.port
+ currentTrial.handleMessage message.toString()
+ removeTrialFor rinfo.address if currentTrial.isCompleted()
+
+
+socket = dgram.createSocket 'udp4', handleMessage
+socket.bind 9001
View
@@ -0,0 +1,75 @@
+uuid = require 'node-uuid'
+
+generateToken = -> uuid()
+
+randomInt = (max) ->
+ Math.floor(Math.random() * max) + 1
+
+addNumbers = (numbers) ->
+ result = 0
+ result += number for number in numbers
+ result
+
+generateQuestionAndAnswer = (op, numbers, result) ->
+ token = generateToken()
+ question = "#{op}:#{token}:#{numbers.join ':'}"
+ answer = "#{token}:#{result}"
+ [question, answer]
+
+generateAdd = (numbers) ->
+ result = addNumbers numbers
+ generateQuestionAndAnswer 'ADD', numbers, result
+
+generateSubtract = (numbers) ->
+ result = randomInt(100)
+ numbers[0] = addNumbers(numbers[1..]) + result
+ generateQuestionAndAnswer 'SUBTRACT', numbers, result
+
+generateMultiply = (numbers) ->
+ result = 1
+ result *= number for number in numbers
+ generateQuestionAndAnswer 'MULTIPLY', numbers, result
+
+generators = [generateAdd, generateSubtract, generateMultiply]
+
+class Trial
+ constructor: (@sendMessage) ->
+
+ handleMessage: (message) ->
+ if message == 'START'
+ @right = @wrong = 0
+ else
+ if message == @expectedMessage
+ console.log 'CORRECT'
+ @right++
+ else
+ console.log "WRONG. EXPECTED #{@expectedMessage}"
+ @wrong++
+ if @isCompleted()
+ @sendResults()
+ else
+ @sendQuestion()
+
+ sendResults: ->
+ if @wrong == 0
+ @sendMessage 'ALL CORRECT'
+ else
+ @sendMessage "#{@wrong} WRONG, #{@right} CORRECT"
+
+ sendQuestion: ->
+ [question, @expectedMessage] = @generateQuestionAndAnswer()
+ @sendMessage question
+
+ generateQuestionAndAnswer: ->
+ numbersCount = 1 + randomInt(4)
+ numbers = (randomInt(100) for index in [1..numbersCount])
+ opIndex = randomInt generators.length
+ generator = generators[opIndex-1]
+ generator numbers
+
+ isCompleted: ->
+ @right + @wrong == 5
+
+
+exports.create = (sendMessage) -> new Trial(sendMessage)
+
View
@@ -0,0 +1,18 @@
+{
+ "name": "maexchen-kata",
+ "version": "0.1.0",
+ "directories": {
+ "bin": "bin",
+ "lib": "lib"
+ },
+ "main": "lib/index.coffee",
+ "files": ["bin", "lib", "package.json"],
+ "dependencies": {
+ "coffee-script": "1.1.x",
+ "node-uuid": "1.2.x"
+ },
+ "devDependencies": {
+ "jasmine-node": "1.0.8"
+ }
+}
+

0 comments on commit 431abaf

Please sign in to comment.