Skip to content

Instantly share code, notes, and snippets.

View prafulliu's full-sized avatar

Pengfei Liu prafulliu

  • teambition
  • shanghai
View GitHub Profile
JAN = 31
daysFeb = {28, 29}
MAR = 31
APR = 30
MAY = 31
JUN = 30
JUL = 31
AUG = 31
'use strict';
// http://www.infoq.com/cn/presentations/node-js-and-real-time-baas-cloud-development-practice?utm_source=infoq&utm_medium=videos_homepage&utm_campaign=videos_row3
// This is a free list to avoid creating so many same object.
exports.Freelist = function(name, max, constructor) {
this.name = name;
this.constructor = constructor;
this.max = max;
this.list = [];
};
@prafulliu
prafulliu / gist:4411747
Created December 30, 2012 09:14
Get the last element of a table. table.maxn()
GameConfig = {}
GameConfig.CnfCity=
{
[1] = {
name = [[testname]],
value = 1,
list = {
{100, 1, 2000, 100, 0, 3, 1, 0, },
{101, 3, 2001, 101, 4, 3, 1, 1, },
{102, 3, 2002, 100, 0, 3, 1, 0, },
@prafulliu
prafulliu / getTableLength.lua
Created December 30, 2012 09:11
lua get table length
t = {[2] = 2}
local count = 0
for k,v in pairs(t) do
count = count + 1
end
print(count)
@prafulliu
prafulliu / datejudge.lua
Created December 30, 2012 09:09
lua判断两天是否连续
local t1 = os.time{year = 2012, month = 12, day = 19, hour = 0, sec = 0}
local t2 = os.time{year = 2012, month = 12, day = 19, hour = 23, sec = 59}
if (t1 - t2) <= 86400 and
(os.date("%d", t1) ~= os.date("%d",t2)) then
-- 判断是否有新手目标
print("called")
end
@prafulliu
prafulliu / setDefault2.lua
Created December 18, 2012 07:20
setDefault in lua , another version.
local key = {} --unique key
local mt = {__index = function (t) return t[key] end}
function setDefault( t, d )
t[key] = d
setmetatable(t, mt)
end
tab = {x=10, y=20}
@prafulliu
prafulliu / setDefault.lua
Created December 18, 2012 07:19
setDefault in lua.
function setDefault( t, d )
local mt = {__index = function () return d end}
setmetatable(t, mt)
end
tab = {x=10, y=20}
print(tab.x, tab.z)
setDefault(tab, 0)
print(tab.x, tab.z)
@prafulliu
prafulliu / window.lua
Created December 18, 2012 07:18
A window demo in lua. __index
Window = {} -- create a namespace
-- create the prototype with default values
Window.prototype = {x=0, y=0, width=100, height=100}
Window.mt = {} --create a metatable
--declare the constructor function
function Window.new( o )
setmetatable(o, Window.mt)
return o
t = {}
--keep a private access to the original table
local _t = t
--create proxy
t = {}
--create metatable
@prafulliu
prafulliu / trackingTableAccess2.lua
Created December 18, 2012 07:16
tracking table acceses in lua
local index = {}
-- create private index
local mt = {
__index = function ( t, k )
print("*access to element " .. tostring(k))
return t[index][k] --access the original table
end,
__newindex = function ( t, k, v)