JGSL_grid
JGSL Grid (grid server)
Title |
JGSL Grid (grid server) |
Author(s) |
LiXizhi |
Date |
2008/8/3 |
File |
script/kids/3DMapSystemNetwork/JGSL_grid.lua |
Description
Sample Code
NPL.load("(gl)script/kids/3DMapSystemNetwork/JGSL_grid.lua");
Map3DSystem.JGSL_grid.Restart();
Map3DSystem.JGSL_grid.activate();
Member Functions
GridNode:CanEdit
an array of active grid nodes.
JGSL_grid.nodes = {};
timer ID
JGSL_grid.TimerID = 18;
if true, we will create a new instance if a previous one is full or non existant for a given world and tile position.
JGSL_grid.AutoSpawn = true;
a timer that is enabled when there are active client connected to this server.
should be smaller or equal to Map3DSystem.JGSL.client.NormalUpdateInterval to prevent duplicate group2 info to be sent for the same client.
JGSL_grid.TimerInterval = 3000
if true, the game server is a dedicated server usually without any user interface. Pure server will use a different restart function.
JGSL_grid.IsPureServer = nil;
------------------------------
Grid Node
------------------------------
a grid node is a single piece of JGSL server, it is similar to JGSL_server, but without loading game world specific data
JGSL_grid adds, deletes, maintains many active grid nodes at the same time.
local GridNode = {
-- server session key, it is regenerated each time a grid node is created or reset, or the underlying game world changes.
sk = ParaGlobal.GenerateUniqueID(),
-- grid node id
id = nil,
-- the world path that this grid node is hosting.
worldpath = nil,
-- grid tile pos, it marks the simulation region within the self.worldpath.
-- from (x*size, y*size) to (x*size+size, y*size+size)
x=nil,
y=nil,
-- grid tile size, if nil, it means infinite size. we always return the smallest sized grid server
size=nil,
-- the default role assigned to any incoming users.
UserRole="guest",
-- server version
ServerVersion = 1,
-- required client version
ClientVersion = 1,
-- grid configuration file
configfile = nil,
-- Map3DSystem.JGSL.history: contains all client and server agent creation and environmental action history.
history = nil,
-- clear the history when the creation has reached this number of times. If this is nil, all history are kept.
clear_history_count = 30,
-- max number of creations in a message
max_creations_per_msg = 5,
-- max number of env updates in a message
max_env_per_msg = 5,
-- VIP ATTRIBUTE: max number of connected users allowed in this grid, this is total number of agents and observers.
-- TODO: when there are more than JGSL_grid.max_users on a single server, and when a jid client leaves a such a over crowded grid node and reenters again. the client will receive nothing.
max_users = 500,
-- a timer that is enabled when there are active client connected to this server.
-- should be smaller or equal to Map3DSystem.JGSL.client.NormalUpdateInterval to prevent duplicate group2 info to be sent for the same client.
TimerInterval = 3000,
-- if a client is not responding in 20 seconds, we will make it inactive user and remove from active user list.
ClientTimeOut = 20000,
-- a new character will be located at radius = miniSpawnRadius+math.sqrt(OnlineUserNum+1)*3;
miniSpawnRadius = 5,
-- When the server has broadcasted this number of objects, the server will be automatically restarted; this is usually the setting for testing public server.
RestartOnCreateNum = tonumber(ParaEngine.GetAppCommandLineByParam("RestartOnCreateNum", "0")),
-- we will recover at most maxrecoversize number of intact client agent request at a time.
maxrecoversize = 5,
--
-- TODO: for Andy. For each grid node, we need to load NPC within the region and work with Quest server for the world self.worldpath
-- Multiple grid nodes for the same self.worldpath may need to share the same server DB.
-- For NPC, creatures and dropped items, they should be treated as a special kind of agent in GameObjects table.
-- this part is not implemented yet.
--
GameObjects = {},
-- client agents or observers on server, where agents[jid] = {agent}.
agents = {},
-- increased by one eact time a normal update is replied
timeid = 1,
-- node statistics
statistics = {
-- strange: LXZ 2008.12.25: ParaGlobal.GetDateFormat(nil) returns invalid XML char code. just ignore it for now.
StartTime = nil, -- commonlib.Encoding.DefaultToUtf8(ParaGlobal.GetDateFormat(nil)..ParaGlobal.GetTimeFormat(nil)),
OnlineUserNum = 0,
VisitsSinceStart = 0,
NumObjectCreated = 0,
},
};
JGSL_grid.GridNode = GridNode;
state of the agent
local agentstate = {
agent = 1,
observer = 2,
}
public function
check whether this grid node allow users to edit the world
- param rule : if nil, it means editing right.
syntax
function GridNode:CanEdit()
GridNode:Release
release a grid node.
syntax
function GridNode:Release()
GridNode:log
output log
syntax
function GridNode:log(...)
GridNode:Reset
regenerate session and become a new server. This is usually called by Map3DSystem.JGSL.Reset()
call this to reset the server to inital disconnected state where there is a new session key, no history, no timer, no agents.
syntax
function GridNode:Reset()
GridNode:CheckClose
TODO: close the node, if there are no agents or observers in it and is not active for a good period of time.
syntax
function GridNode:CheckClose()
GridNode:Load
load the grid node from a config structure, but does not reset
- param config : a table of {worldpath, x,y,size, UserRole,}
syntax
function GridNode:Load(config)
parameters
config |
a table of {worldpath, x,y,size, UserRole,} |
GridNode:GetAgent
private functions
it will create the agent structure if it does not exist
syntax
function GridNode:GetAgent(jid)
parameters
GridNode:CreateAgent
create an agent. it will overwrite existing one, if any, with a newly created one.
syntax
function GridNode:CreateAgent(jid)
parameters
GridNode:IsFull
whether the grid node has reached maximum number of connected users allowed.
syntax
function GridNode:IsFull()
GridNode:IsEmpty
whether the grid node is empty.
syntax
function GridNode:IsEmpty()
GridNode:Contains
whether this grid node contains the 3d point x,y,z in worldpath
syntax
function GridNode:Contains(worldpath,x,y,z)
parameters
GridNode:SendToClient
send a message to client
- param jid : the jid of a client. If this is nil. Messages is sent to all active clients.
- param neuronfile : if nil, the DefaultClientFile is used
- return true : if successfully send
syntax
function GridNode:SendToClient(jid, msg, neuronfile)
parameters
jid |
the jid of a client. If this is nil. Messages is sent to all active clients. |
msg |
|
neuronfile |
|
return |
if successfully send |
GridNode:HandleMessage
handle server messages
syntax
function GridNode:HandleMessage(msg)
parameters
GridNode:OnTimer
this function is called periodically.
syntax
function GridNode:OnTimer(curTime)
parameters
JGSL_grid.Reset
Grid Manager related
shut down all server grid node
syntax
function JGSL_grid.Reset()
JGSL_grid.GetGridNode
get a gridnode by id and sessionkey of the grid node
syntax
function JGSL_grid.GetGridNode(id, sk)
parameters
JGSL_grid.GetBestGridNode
get available server grid node for a given world path.
- param worldpath : the world that the grid node is in.
- param x :, y, z: a position that the grid node contains.
- param IsObserver : true if we are just getting for an observer node.
syntax
function JGSL_grid.GetBestGridNode(worldpath, x, y, z, IsObserver)
parameters
worldpath |
the world that the grid node is in. |
x |
|
y |
|
z |
|
IsObserver |
true if we are just getting for an observer node. |
JGSL_grid.CreateGetBestGridNode
Get the best grid node for a given world path. if no suitable node is found, we will spawn a new one.
- return __ : return a grid node structure.
syntax
function JGSL_grid.CreateGetBestGridNode(worldpath, x, y, z, IsObserver)
parameters
worldpath |
|
x |
|
y |
|
z |
|
IsObserver |
|
return |
return a grid node structure. |
JGSL_grid.CreateGridNode
create a new grid node of world path
it will reuse empty grid node, if there is none, it will create a new one at the end.
- return __ : the grid node created or reused are returned.
syntax
function JGSL_grid.CreateGridNode(worldpath)
parameters
worldpath |
|
return |
the grid node created or reused are returned. |
JGSL_grid.Restart
restart the grid server
- param configfile : nil or config file path.
syntax
function JGSL_grid.Restart(configfile)
parameters
configfile |
nil or config file path. |