Interface | Params |
---|---|
_check_all_switch | [] |
cleartbl | [
"type"
] |
deltbl | [
"type",
"player"
] |
getTableObjects | [
"table_name",
"account"
] |
init | [] |
login | [
"ref"
] |
ontransfer | [
"from",
"amount",
"memo",
"symbol"
] |
setswitch | [
"bSwitch"
] |
settimeswitch | [
"nTimeSwitch"
] |
Method | Caller | Call time | Transaction |
---|---|---|---|
deltbl |
2021-03-07 19:02:00
| ||
getTableObjects |
2021-03-07 19:01:58
| ||
login |
2021-03-07 19:01:56
| ||
deltbl |
2021-03-07 16:41:32
| ||
getTableObjects |
2021-03-07 16:41:30
| ||
login |
2021-03-07 16:41:28
| ||
deltbl |
2021-03-07 15:56:48
| ||
getTableObjects |
2021-03-07 15:56:46
| ||
ontransfer |
2021-03-07 15:56:44
| ||
deltbl |
2021-03-07 15:53:44
| ||
getTableObjects |
2021-03-07 15:53:42
| ||
ontransfer |
2021-03-07 15:53:40
| ||
deltbl |
2021-03-07 15:48:16
| ||
getTableObjects |
2021-03-07 15:48:14
| ||
login |
2021-03-07 15:48:12
| ||
deltbl |
2021-03-07 15:25:08
| ||
getTableObjects |
2021-03-07 15:25:06
| ||
login |
2021-03-07 15:25:04
| ||
deltbl |
2021-03-07 15:02:12
| ||
getTableObjects |
2021-03-07 15:02:10
|
local TEAM = "ckttm"
local TAG_SWITCH_KEY = "switchkey"
local PROFIT_SWITCH_KEY = "profitkey"
local TAG_TIME_SWITCH_KEY = "timekey"
local TAG_MIN_DEPOSIT_KEY = "mindeposit"
local TAG_MAX_DEPOSIT_KEY = "maxdeposit"
local ITEM_TYPE = 1
local HERO_TYPE = 2
local TRX_TYPE_DELEGATE = 1
local TRX_TYPE_BUY = 2
local TRX_TYPE_USER_UPGRADE = 3
local TRX_TYPE_STAKE =4
local TRX_TYPE_SYSTEM_BUY = 5
local DELEGATE_TYPE_HERO = 2
local COCOS_ACCURACY = 100000
local TOKEN_UNIT = 10000
local _MIN_DEPOSIT = 1
local _MAX_DEPOSIT = COCOS_ACCURACY * 99
local IN_TOKEN_SYMBOL = 'COCOS'
local STAKE_TOKEN_SYMBOL = 'LWT'
local COOL_TIME_INTERVAL = 60
local REBACK_TIME_INTERVAL = 600
local MAX_REC_NUM = 30
local function split(str)
local fields = {}
for w in str:gmatch("([^-]+)") do
fields[#fields + 1] = w
end
return fields
end
local function split2(str)
local fields = {}
for w in str:gmatch("([^ ]+)") do
table.insert(fields, w)
end
return fields
end
local function keepTableSpace(T)
local idx = -1
local count = 0
local tAvai = {}
local cooldown = chainhelper:time() - COOL_TIME_INTERVAL
for k, v in pairs(T) do
count = count + 1
if v.created_at <= cooldown then
table.insert(tAvai, k)
end
end
if count >= MAX_REC_NUM then
if #tAvai > 0 then
for i = 1, #tAvai do
T[tAvai[i]] = nil
end
return true
else
return false
end
else
return true
end
end
local function set_global(key, value)
public_data._global[key] = value
end
local function get_global(key)
local value = public_data._global[key]
if value == nil then
return 0
else
return value
end
end
local function set_global_wrapper(key, value)
read_list = {public_data={_global=true}}
chainhelper:read_chain()
public_data._global[key] = value
write_list = {public_data={_global=true}}
chainhelper:write_chain()
end
local function next_id(tag)
local id = get_global(tag) + 1
set_global(tag, id)
return id
end
function init()
assert(chainhelper:is_owner(),'no auth')
read_list = {public_data={_global=true,_users=true,_item_buys=true,_hero_buys=true,_hero_deles=true,_user_upgrade=true,_system_buy=true,_logins=true,_stakes=true}}
chainhelper:read_chain()
public_data._global = {}
public_data._users = {}
public_data._item_buys = {}
public_data._hero_buys = {}
public_data._hero_deles = {}
public_data._user_upgrade = {}
public_data._system_buy = {}
public_data._logins = {}
public_data._stakes={}
public_data._global[TAG_MIN_DEPOSIT_KEY] = _MIN_DEPOSIT
public_data._global[TAG_MAX_DEPOSIT_KEY] = _MAX_DEPOSIT
write_list = {public_data={_global=true,_users=true,_item_buys=true,_hero_buys=true,_hero_deles=true,_user_upgrade=true,_system_buy=true,_logins=true,_stakes=true}}
chainhelper:write_chain()
end
function login(ref)
local account = contract_base_info.caller
read_list = {public_data={_logins=true,_global = true}}
chainhelper:read_chain()
_check_all_switch()
local login_info = public_data._logins[account]
if login_info == nil then
assert(keepTableSpace(public_data._logins) == true, "login table space overflow")
login_info = {}
login_info.player = account
login_info.created_at = chainhelper:time()
public_data._logins[account] = login_info
write_list = {public_data={_logins=true}}
chainhelper:write_chain()
end
chainhelper:log('#login#'..account)
end
local ACTION_USER=1
local ACTION_LOGIN=2
local ACTION_HERO_STAKE=3
local ACTION_PERMISSION=4
local ACTION_CHARGE=5
local ACTION_ITEM_BUY=6
local ACTION_HERO_BUY=7
local ACTION_TOKEN_STAKE=8
function deltbl(type, player)
assert(chainhelper:is_owner(),'no auth')
if type == ACTION_USER then
read_list = {public_data={_users=true}}
chainhelper:read_chain()
public_data._users[player] = nil
write_list = {public_data={_users=true}}
chainhelper:write_chain()
elseif type == ACTION_LOGIN then
read_list = {public_data={_logins=true}}
chainhelper:read_chain()
public_data._logins[player] = nil;
write_list = {public_data={_logins=true}}
chainhelper:write_chain()
elseif type == ACTION_ITEM_BUY then
read_list = {public_data={_item_buys=true}}
chainhelper:read_chain()
public_data._item_buys[player] = nil;
write_list = {public_data={_item_buys=true}}
chainhelper:write_chain()
elseif type == ACTION_HERO_BUY then
read_list = {public_data={_hero_buys=true}}
chainhelper:read_chain()
public_data._hero_buys[player] = nil;
write_list = {public_data={_hero_buys=true}}
chainhelper:write_chain()
elseif type == ACTION_HERO_STAKE then
read_list = {public_data={_hero_deles=true}}
chainhelper:read_chain()
public_data._hero_deles[player] = nil;
write_list = {public_data={_hero_deles=true}}
chainhelper:write_chain()
elseif type == ACTION_PERMISSION then
read_list = {public_data={_user_upgrade=true}}
chainhelper:read_chain()
public_data._user_upgrade[player] = nil;
write_list = {public_data={_user_upgrade=true}}
chainhelper:write_chain()
elseif type == ACTION_CHARGE then
read_list = {public_data={_system_buy=true}}
chainhelper:read_chain()
public_data._system_buy[player] = nil;
write_list = {public_data={_system_buy=true}}
chainhelper:write_chain()
elseif type == ACTION_TOKEN_STAKE then
read_list = {public_data={_stakes=true}}
chainhelper:read_chain()
public_data._stakes[player] = nil;
write_list = {public_data={_stakes=true}}
chainhelper:write_chain()
end
end
function cleartbl(type)
assert(chainhelper:is_owner(),'no auth')
if type == ACTION_USER then
read_list = {public_data={_users=true}}
chainhelper:read_chain()
public_data._users = {}
write_list = {public_data={_users=true}}
chainhelper:write_chain()
elseif type == ACTION_LOGIN then
read_list = {public_data={_logins=true}}
chainhelper:read_chain()
public_data._logins={}
write_list = {public_data={_logins=true}}
chainhelper:write_chain()
elseif type == ACTION_ITEM_BUY then
read_list = {public_data={_item_buys=true}}
chainhelper:read_chain()
public_data._item_buys = {}
write_list = {public_data={_item_buys=true}}
chainhelper:write_chain()
elseif type == ACTION_HERO_BUY then
read_list = {public_data={_hero_buys=true}}
chainhelper:read_chain()
public_data._hero_buys = {}
write_list = {public_data={_hero_buys=true}}
chainhelper:write_chain()
elseif type == ACTION_HERO_STAKE then
read_list = {public_data={_hero_deles=true}}
chainhelper:read_chain()
public_data._hero_deles = {}
write_list = {public_data={_hero_deles=true}}
chainhelper:write_chain()
elseif type == ACTION_PERMISSION then
read_list = {public_data={_user_upgrade=true}}
chainhelper:read_chain()
public_data._user_upgrade = {}
write_list = {public_data={_user_upgrade=true}}
chainhelper:write_chain()
elseif type == ACTION_CHARGE then
read_list = {public_data={_system_buy=true}}
chainhelper:read_chain()
public_data._system_buy = {}
write_list = {public_data={_system_buy=true}}
chainhelper:write_chain()
elseif type == ACTION_TOKEN_STAKE then
read_list = {public_data={_stakes=true}}
chainhelper:read_chain()
public_data._stakes = {}
write_list = {public_data={_stakes=true}}
chainhelper:write_chain()
else
assert(false, "type was not supported. type=>" .. tostring(type))
end
end
-- 购买
local function buy(buyer, item_type, sell_id, seller, item_guid, price, count, qty)
assert(count >0 , "invalid count!")
local now_time = chainhelper:time()
if item_type == ITEM_TYPE then
assert(math.floor(price * count) == math.floor(qty/10), "invalid amount!")
read_list = {public_data={_item_buys=true,_global=true}}
chainhelper:read_chain()
_check_all_switch()
local u = public_data._item_buys[buyer];
if u ~= nil then
--保护时间内可以追单,超时将自动退单
assert(chainhelper:time()-u.created_at>REBACK_TIME_INTERVAL,"#error_001#invalid item exchange request!")
public_data._item_buys[buyer] = nil;
--回滚订单
chainhelper:transfer_from_owner(buyer, u.amount*10, IN_TOKEN_SYMBOL, true)
else
assert(keepTableSpace(public_data._item_buys) == true)
end
u = {}
u.id = sell_id
u.buyer = buyer
u.sell_id = sell_id
u.seller = seller
u.item_guid = item_guid
u.price = string.format("%.4f", price / TOKEN_UNIT) .. " COCOS"
u.count = count
u.amount = math.floor(qty / 10)
--u.static_id = static_id
u.created_at = now_time
public_data._item_buys[buyer] = u
write_list = {public_data={_item_buys=true,_global=true}}
chainhelper:write_chain()
elseif item_type == HERO_TYPE then
assert(math.floor(price * count) == math.floor(qty/10), "invalid amount!")
read_list = {public_data={_hero_buys=true,_global=true}}
chainhelper:read_chain()
_check_all_switch()
local u = public_data._hero_buys[buyer]
if u ~= nil then
--保护时间内可以追单,超时将自动退单
assert(chainhelper:time()-u.created_at>REBACK_TIME_INTERVAL,"#error_002#invalid hero exchange request!")
public_data._hero_buys[buyer] = nil;
--回滚订单
chainhelper:transfer_from_owner(buyer, u.amount*10, IN_TOKEN_SYMBOL, true)
else
assert(keepTableSpace(public_data._hero_buys) == true)
end
u = {}
u.id = sell_id
u.buyer = buyer
u.sell_id = sell_id
u.seller = seller
u.item_guid = item_guid
u.price = string.format("%.4f", price / TOKEN_UNIT) .. " COCOS"
u.count = count
u.amount = math.floor(qty / 10)
--u.static_id = static_id
u.created_at = now_time
public_data._hero_buys[buyer] = u
write_list = {public_data={_hero_buys=true,_global=true}}
chainhelper:write_chain()
else
assert(false, "buy invalid item_type")
end
end
-- 用户权限升级
local function user_upgrade(player, qty)
read_list = {public_data={_users=true,_user_upgrade=true,_global=true}}
chainhelper:read_chain()
_check_all_switch()
assert(qty == COCOS_ACCURACY * 3000, "##erroCode##20021");
if public_data._user_upgrade[player] ~= nil then
assert( false , "#error_003#invalid update request!");
else
assert(keepTableSpace(public_data._user_upgrade) == true, "user_upgrade table space overflow")
end
local u = {}
u.player = player
u.created_at = chainhelper:time()
public_data._user_upgrade[player] = u
write_list = {public_data={_users=true,_user_upgrade=true}}
chainhelper:write_chain()
end
-- 激活英雄
local function hero_delegate(from, item_id, amount)
read_list = {public_data={_hero_deles=true,_global=true}}
chainhelper:read_chain()
_check_all_switch()
local u = public_data._hero_deles[from];
if u ~= nil then
--保护时间内可以追单,超时将自动退单
assert(chainhelper:time()-u.created_at>REBACK_TIME_INTERVAL,"#error_004#invalid delegate request!")
public_data._hero_deles[from] = nil;
--回滚订单
chainhelper:transfer_from_owner(from, u.stake_amt*10, IN_TOKEN_SYMBOL, true)
else
assert(keepTableSpace(public_data._hero_deles) == true, "hero dele table space overflow")
end
u = {}
u.item_id = item_id
u.player = from
u.stake_amt = math.floor(amount / 10)
u.created_at = chainhelper:time()
public_data._hero_deles[from] = u
write_list = {public_data={_hero_deles=true}}
chainhelper:write_chain()
end
-- 购买道具
local function system_buy(player, mystery_shop_id, qty)
read_list = {public_data={_users=true,_system_buy=true,_global=true}}
chainhelper:read_chain()
_check_all_switch()
local u = public_data._system_buy[player]
if u ~= nil then
--保护时间内可以追单,超时将自动退单
assert(chainhelper:time()-u.created_at>REBACK_TIME_INTERVAL,"#error_005#invalid charge request!")
public_data._system_buy[player] = nil;
--回滚订单
chainhelper:transfer_from_owner(player, u.amt*10, IN_TOKEN_SYMBOL, true)
else
assert(keepTableSpace(public_data._system_buy) == true, "system_buy table space overflow")
end
u = {}
u.player = player
u.mystery_shop_id = mystery_shop_id
u.amt = tostring(math.floor(qty / 10));
u.created_at = chainhelper:time()
public_data._system_buy[player] = u
write_list = {public_data={_system_buy=true}}
chainhelper:write_chain()
end
-- 购买道具
local function stake(player, exid, qty)
read_list = {public_data={_users=true,_stakes=true,_global=true}}
chainhelper:read_chain()
_check_all_switch()
local u = public_data._stakes[player]
if u ~= nil then
--保护时间内可以追单,超时将自动退单
assert(chainhelper:time()-u.created_at>REBACK_TIME_INTERVAL,"#error_006#invalid stake request!")
public_data._stakes[player] = nil;
--回滚订单,4位精度,不*10了
chainhelper:transfer_from_owner(player, u.amt, STAKE_TOKEN_SYMBOL, true)
else
assert(keepTableSpace(public_data._stakes) == true, "stake table space overflow")
end
u = {}
u.player = player
u.id = exid
-- u.skuid = skuid
u.amt = tostring(math.floor(qty / 10));
u.created_at = chainhelper:time()
public_data._stakes[player] = u
write_list = {public_data={_stakes=true}}
chainhelper:write_chain()
end
function setswitch(bSwitch)
assert(chainhelper:is_owner(),'no auth')
local nSwitch = 0
if bSwitch == true then
nSwitch = 1
end
set_global_wrapper(TAG_SWITCH_KEY, nSwitch)
end
function settimeswitch(nTimeSwitch)
assert(chainhelper:is_owner(),'no auth')
set_global_wrapper(TAG_TIME_SWITCH_KEY, chainhelper:time() + nTimeSwitch)
end
function _check_all_switch()
local nSwitch = public_data._global[TAG_SWITCH_KEY];
assert(nSwitch == 1, "switch was not open");
local nSwitchTime = public_data._global[TAG_TIME_SWITCH_KEY];
assert(chainhelper:time() > nSwitchTime, "timeswitch was not open");
end
function ontransfer(from,amount,memo,symbol)
assert(amount >0 , "invalid amount!")
assert(from==contract_base_info.caller,"invalid from account!")
local tMemo = split(memo)
assert(#tMemo >= 1, "transfer memo=> transaction type | transaction content")
local trxType = tonumber(tMemo[1])
if trxType == TRX_TYPE_DELEGATE then
assert(symbol ==IN_TOKEN_SYMBOL , "invalid token symbol!")
assert(#tMemo == 3, "transfer memo=> transaction type | delegateType | item_id")
local delegateType = tonumber(tMemo[2])
local item_id = tMemo[3] --数字太大,还是存字符吧 否则 bcx采用科学计数法取整就乱了 tonumber(
assert(delegateType == DELEGATE_TYPE_HERO, "invalid delegateType")
hero_delegate(from,item_id,amount)
chainhelper:transfer_from_caller(contract_base_info.owner, amount, IN_TOKEN_SYMBOL, true)
elseif trxType == TRX_TYPE_BUY then
assert(symbol ==IN_TOKEN_SYMBOL , "invalid token symbol!")
local item_type = tonumber(tMemo[2])
local sell_id = tMemo[3]
local seller = tMemo[4]
local item_guid = tMemo[5]
local price = tonumber(tMemo[6])
local count = tonumber(tMemo[7])
--local static_id = tonumber(tMemo[8])
buy(from, item_type, sell_id, seller, item_guid, price, count,amount)
chainhelper:transfer_from_caller(contract_base_info.owner, amount, IN_TOKEN_SYMBOL, true)
elseif trxType == TRX_TYPE_USER_UPGRADE then
assert(symbol ==IN_TOKEN_SYMBOL , "invalid token symbol!")
user_upgrade(from, amount)
chainhelper:transfer_from_caller(contract_base_info.owner, amount, IN_TOKEN_SYMBOL, true)
elseif trxType == TRX_TYPE_SYSTEM_BUY then
assert(symbol ==IN_TOKEN_SYMBOL , "invalid token symbol!")
local mystery_shop_id = tonumber(tMemo[2])
system_buy(from, mystery_shop_id, amount)
chainhelper:transfer_from_caller(contract_base_info.owner, amount, IN_TOKEN_SYMBOL, true)
elseif trxType == TRX_TYPE_STAKE then
assert(symbol ==STAKE_TOKEN_SYMBOL , "invalid token symbol!")
--4-4000000-
local exid = tonumber(tMemo[2])
stake(from, exid, amount)
--4位精度,必须/10
chainhelper:transfer_from_caller(contract_base_info.owner, amount/10, STAKE_TOKEN_SYMBOL, true)
else
assert(false, "trxType was not supported")
end
end
function getTableObjects(table_name, account)
assert(chainhelper:is_owner(),'no auth')
if table_name == "_users" then
read_list = {public_data={_users = true}}
elseif table_name == "_item_buys" then
read_list = {public_data={_item_buys = true}}
elseif table_name == "_hero_buys" then
read_list = {public_data={_hero_buys = true}}
elseif table_name == "_hero_deles" then
read_list = {public_data={_hero_deles = true}}
elseif table_name == "userupgrade" then
table_name = "_user_upgrade"
read_list = {public_data={_user_upgrade = true}}
elseif table_name == "_system_buy" then
read_list = {public_data={_system_buy = true}}
elseif table_name == "_stakes" then
read_list = {public_data={_stakes = true}}
elseif table_name == "login" then
table_name = "_logins"
read_list = {public_data={_logins = true}}
elseif table_name == "_global" then
read_list = {public_data={_global = true}}
end
chainhelper:read_chain()
local item = {}
if table_name =='_global' then
item = public_data[table_name]
else
if public_data[table_name] ~= nil then
local table_item = public_data[table_name]
-- if table_name == "_item_buys" or table_name == "_hero_buys" then
-- table.insert(item, table_item[account]);
-- else
item = table_item[account]
-- end
end
end
local output = cjson.encode(item)
chainhelper:log('#getTableObjects#'..output)
end