Skip to content
Browse files

simple High-Low betting game via Lua & TC

  • Loading branch information...
1 parent 0a871fe commit 675a12f6a128566b885da0d239a033bf211f36e1 @igrigorik committed Jul 7, 2009
Showing with 159 additions and 0 deletions.
  1. +22 −0 README.rdoc
  2. +38 −0 high-low-game/README.rdoc
  3. +89 −0 high-low-game/hlgame.lua
  4. +10 −0 high-low-game/hlgame.rb
View
22 README.rdoc
@@ -0,0 +1,22 @@
+
+
+== Installing Tokyo Cabinet with Lua
+ yum install lua lua-devel
+
+ git clone git://github.com/etrepum/tokyo-cabinet.git
+ git clone git://github.com/etrepum/tokyo-tyrant.git
+
+ (or get latest files from sourceforge)
+ http://sourceforge.net/projects/tokyocabinet/files/
+
+ cd tokyo-cabinet
+ ./configure --enable-lua
+ make && make install
+
+ cd tokyo-tyrant
+ ./configure
+ make && make install
+
+ gem install rufus-tokyo
+
+Source: http://openwferu.rubyforge.org/tokyo.html
View
38 high-low-game/README.rdoc
@@ -0,0 +1,38 @@
+= Echo command
+
+Implements a simple high-low betting game via Lua and TC as a database to persist bet / player information
+
+== Starting server with incr extension
+ > ttserver -ext lhgame.lua test.tch
+
+== Executing from command line
+ [ilya@igrigorik-nix high-low-game]# tcrmgr ext localhost start ilya 5000
+ Welcome, ilya.
+ The current number is 88.
+ Your money is 5000.
+ Round 1 Bet!
+
+ [ilya@igrigorik-nix high-low-game]# tcrmgr ext localhost low ilya 1000
+ The current number is 91.
+ 91:88 (high) ... You lost!
+ Your money is 4000.
+ Round 2 Bet!
+
+ ...
+
+== Executing via Ruby
+
+ > ruby hlgame.rb
+
+ Welcome, ilya.
+ The current number is 5.
+ Your money is 10000.
+ Round 1 Bet!
+ The current number is 42.
+ 42:5 (high) ... You won!
+ Your money is 12000.
+ Round 2 Bet!
+ The current number is 99.
+ 99:42 (high) ... You lost!
+ Your money is 10000.
+ Round 3 Bet!
View
89 high-low-game/hlgame.lua
@@ -0,0 +1,89 @@
+NUMRANGE = 100
+MAXROUND = 5
+math.randomseed(os.time())
+
+function start(key, value)
+ value = tonumber(value)
+ if not value or value <= 0 then
+ return "error: invalid value"
+ end
+ if not _putkeep(key .. ":r", 1) then
+ return "error: already started"
+ end
+ local num = math.random(NUMRANGE)
+ _put(key .. ":m", value)
+ _put(key .. ":n", num)
+ return "Welcome, " .. key .. ".\n" ..
+ "The current number is " .. num .. ".\n" ..
+ "Your money is " .. value .. ".\n" ..
+ "Round 1 Bet!\n"
+end
+
+function high(key, value)
+ return do_bet(key, value, true)
+end
+
+function low(key, value)
+ return do_bet(key, value, false)
+end
+
+function over(key, value)
+ if _vsiz(key .. ":r") < 0 then
+ return "error: not started"
+ end
+ _out(key .. ":r")
+ _out(key .. ":m")
+ _out(key .. ":n")
+ return "Good Bye!"
+end
+
+function do_bet(key, value, ishigh)
+ value = tonumber(value)
+ if not value or value <= 0 then
+ return "error: invalid value"
+ end
+ local round = tonumber(_get(key .. ":r"))
+ if not round then
+ return "error: not started"
+ end
+ local money = tonumber(_get(key .. ":m"))
+ if round > MAXROUND or money < 1 then
+ return "error: already finished"
+ end
+ if value > money then
+ value = money
+ end
+ local num = tonumber(_get(key .. ":n"))
+ local newnum = math.random(NUMRANGE)
+ local cmp = "even"
+ local res = "lost"
+ if newnum > num then
+ cmp = "high"
+ if ishigh then
+ res = "won"
+ end
+ elseif newnum < num then
+ cmp = "low"
+ if not ishigh then
+ res = "won"
+ end
+ end
+ round = round + 1
+ if res == "won" then
+ money = money + value
+ else
+ money = money - value
+ end
+ _put(key .. ":r", round)
+ _put(key .. ":m", money)
+ _put(key .. ":n", newnum)
+ local call = "Round " .. round .. " Bet!\n"
+ if round > MAXROUND or money < 1 then
+ call = "Game Over!\n"
+ end
+ return "The current number is " .. newnum .. ".\n" ..
+ newnum .. ":" .. num .. " (" .. cmp .. ") ... You " .. res .. "!\n" ..
+ "Your money is " .. money .. ".\n" ..
+ call
+end
+
View
10 high-low-game/hlgame.rb
@@ -0,0 +1,10 @@
+require 'rubygems'
+require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo
+
+t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978)
+
+puts t.ext(:start, 'ilya', 10000)
+puts t.ext(:high, 'ilya', 2000)
+puts t.ext(:low, 'ilya', 2000)
+
+t.close

0 comments on commit 675a12f

Please sign in to comment.
Something went wrong with that request. Please try again.