node-game-server
node-game-server copied to clipboard
A game server that runs in Node.js
node-game-server
A game server that works for at least one specific game, in Node. Provides a web interface, http api, and a game logic runner. Intended initially as a proof-of-concept, may evolve into something that's actually useful!
Requirements
Components
The game server handles logic by providing an interface between storage (redis) and game objects. Game objects can currently either be Maps or Players. All "active" game objects are kept in memory for easy direct access, but are persisted to redis at write time. See the documentation below for api methods available to these objects.
The web server is there to provide both a management interface to the game server and allow for a public api into the game.
Object API
Objects are run sandboxed and do not have direct access to server code or other objects. The world is exposed to them through the following api:
Players
Player objects can define handlers for several events:
-
onTick()
- Called every second since the player was instantiated (or reloaded). -
onCollision(player)
- If the player object collides with another, this handler is called with theplayer
hash of the colliding player.
Player objects can manipulate the world using these functions:
-
apiMove(x, y, z)
- Moves the player to the given position, if the map rules allow for it. -
apiGetPos()
- Returns the player's current position as a hash withx
,y
, andz
keys.
And get information about their world:
-
apiGetPlayersHere(callback)
- Calls the function defined bycallback
with an array of players at the current player's position, excluding the current player.
Additionally, player objects can store/retrieve arbitrary data in a private key/value store:
-
apiStore(key, value)
-value
can be any data type. -
apiGet(key, callback)
- Calls the function defined bycallback
with thevalue
associated withkey
, orundefined
if there is no data forkey
.
There are utility functions:
-
log(msg)
- Echoesmsg
to node's console log. For debugging.
Maps
-
apiGetMapBounds
- Returns the bounds of the current map as a hash withwidth
,height
, anddepth
keys.
TODO
- Authentication for the web server
- Lots more event handlers and api methods!