Skip to content

Instantly share code, notes, and snippets.

@zrong
Last active August 29, 2015 14:13
Show Gist options
  • Save zrong/4fe51496102867fc2d3f to your computer and use it in GitHub Desktop.
Save zrong/4fe51496102867fc2d3f to your computer and use it in GitHub Desktop.
Using: host/gift/yourname
#!/usr/bin/lua
-- Using:
-- Deploy this file to openresty
-- and visit host/gift/yourname.
-- E.g http://192.168.18.22/gift/zengrong
-- Will get a gift object for zengrong.
function in_table(t, v)
for key,value in pairs(t) do
if type(value) == 'table' then
if v == value[1] then
return value
end
else
if v == value then
return key
end
end
end
return nil
end
function split(str)
local list = {}
for k in string.gmatch(str, '[^,]+') do
if string.find(k, ':') then
local list = {}
for v in string.gmatch(k, '[^:]+') do
table.insert(list, v)
end
k = list
end
table.insert(list, k)
end
return list
end
function print_table(t)
for key,value in pairs(t) do
if type(value) == 'table' then
ngx.say(key,' ', table.concat(value, ':'))
else
ngx.say(key, ' ', value)
end
end
end
function print_pool(pool)
local num = #pool
if num == 0 then
ngx.say(string.format('The GIFT LOTTERY is over, enjoy it!'))
else
ngx.say(string.format('There are %d stuff(s) in the POOL.', num))
end
end
function init_pool(t, name)
local pool = ngx.shared.lottery:get(name)
if not pool then
pool = {}
for k,v in pairs(t) do
table.insert(pool, v)
end
ngx.shared.lottery:set(name, table.concat(pool, ','))
else
pool = split(pool)
end
return pool
end
function init_names()
local names = ngx.shared.lottery:get('names')
if not names then
names = {
'chentao',
'sunminghong',
'yueli',
'zengrong',
'xukun',
'xiefei',
'zhangming',
'qiaoyan',
'zhulan',
'lizequn',
'wanglong',
'zhangjiawen',
}
ngx.shared.lottery:set('names', table.concat(names, ','))
else
names = split(names)
end
return names
end
function init_done()
local done = ngx.shared.lottery:get('done')
if not done then
done = {}
else
done = split(done)
end
return done
end
math.randomseed(os.time())
local no_restrict_ip = false
local admin_ip = '192.168.18.22'
local uri = ngx.var.uri
local ip = ngx.var.remote_addr
local lottery = ngx.shared.lottery
local names = init_names()
local done = init_done()
local sele = init_pool(names, 'sele')
local pool = init_pool(names, 'pool')
local name = string.sub(uri, 7)
ngx.say('Your name: '..name)
-- For administrator
if ip == admin_ip then
if name == 'allmatching' then
ngx.say('\n----DONE----')
print_table(done)
ngx.say('\n----POOL----')
print_table(pool)
ngx.say('\n----SELE----')
print_table(sele)
ngx.say('\n----NAMES----')
print_table(names)
return
end
if name == 'clearall' then
lottery:set('done', '')
lottery:set('pool', table.concat(names, ','))
lottery:set('sele', table.concat(names, ','))
return
end
end
local is_staff = in_table(names, name)
if not is_staff then
ngx.say('You are NOT our friend! GET OUT!')
return
end
local idx = in_table(sele, name)
if not idx then
local fmt = 'You have already got a object. He/She is -- %s.'
local obj = in_table(done, name)
if not obj then
ngx.say('You are neither in the SELE list nor the DONE list. Please contact administrator.')
return
end
if no_restrict_ip or obj[3] == ip then
ngx.say(string.format(fmt, obj[2]))
else
ngx.say(string.format(fmt, '******'))
end
print_pool(pool)
return
end
--[[
ngx.say(math.random(#names))
ngx.say(math.random(#names))
ngx.say(math.random(#names))
ngx.say(math.random(#names))
]]
while true do
local rnd = math.random(#pool)
obj = pool[rnd]
if #pool == 2 then
local obj2 = nil
rnd2idx = (rnd == 1 and 2) or 1
name2idx = (idx == 1 and 2) or 1
if sele[name2idx] == pool[rnd2idx] then
rnd = rnd2idx
obj = pool[rnd]
end
end
if obj ~= name then
table.remove(sele, idx)
table.remove(pool, rnd)
break
end
end
lottery:set('pool', table.concat(pool, ','))
lottery:set('sele', table.concat(sele, ','))
table.insert(done, {name, obj, ip})
local donestr = {}
for k,v in pairs(done) do
table.insert(donestr, table.concat(v, ':'))
end
lottery:set('done', table.concat(donestr, ','))
ngx.say('Your gift object is '.. obj .. '.')
print_pool(pool)
#!/bin/bash
testonce()
{
curl 192.168.18.22/gift/clearall
for name in chentao sunminghong yueli zengrong xukun xiefei zhangming qiaoyan zhulan lizequn wanglong zhangjiawen; do
curl 192.168.18.22/gift/$name
done
}
testonce
testonce
testonce
testonce
testonce
testonce
testonce
testonce
testonce
testonce
testonce
testonce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment