Skip to content

Instantly share code, notes, and snippets.

Avatar

Pengfei Liu prafulliu

  • teambition
  • shanghai
View GitHub Profile
View date.lua
JAN = 31
daysFeb = {28, 29}
MAR = 31
APR = 30
MAY = 31
JUN = 30
JUL = 31
AUG = 31
View freelist.js
'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 Dec 30, 2012
Get the last element of a table. table.maxn()
View gist:4411747
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, },
View getTableLength.lua
t = {[2] = 2}
local count = 0
for k,v in pairs(t) do
count = count + 1
end
print(count)
@prafulliu
prafulliu / datejudge.lua
Created Dec 30, 2012
lua判断两天是否连续
View datejudge.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 Dec 18, 2012
setDefault in lua , another version.
View setDefault2.lua
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 Dec 18, 2012
setDefault in lua.
View setDefault.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 Dec 18, 2012
A window demo in lua. __index
View window.lua
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
View trackingTableAccess.lua
t = {}
--keep a private access to the original table
local _t = t
--create proxy
t = {}
--create metatable
@prafulliu
prafulliu / trackingTableAccess2.lua
Created Dec 18, 2012
tracking table acceses in lua
View trackingTableAccess2.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)