Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Game API
- By: YoYoYonnY
- First Release: 27/12/2013
- Last Update: 27/12/2013
- ]]--
- --[[ SECTION: Local Variables ]]--
- local w, h = term.getSize()
- local path = "Games"
- local path_games = "Games/Games"
- local path_data = "Games/Data"
- local path_players = "Games/Players"
- local path_saves = "Games/Saves"
- local path_achievements = "Games/Achievements"
- local path_stats = "Games/Stats"
- local path_score = "Games/Score"
- local path_log = "log"
- local debug = false
- --[[ SECTION: Global Variables ]]--
- player = {}
- game = {}
- achievement = {}
- stat = {}
- scoreboard = {}
- score = {}
- --[[ SECTION: Local Functions ]]--
- local function log( s )
- if debug then
- if not fs.exists( path_log ) then
- local f = fs.open( path_log, "w" )
- f.close()
- end
- local f = fs.open( path_log, "a" )
- f.write( tostring( "[" .. textutils.formatTime( os.time(), true ) .. "] " .. s .. "\n" ) )
- f.close()
- end
- end
- local function logstring( s )
- if type( s ) == "table" then
- return textutils.serialize( s )
- elseif type( s ) == "function" then
- return "function() end"
- end
- return tostring( s )
- end
- local function mkdir( path )
- if not fs.exists( path ) then
- log( "mkdir " .. path )
- return fs.makeDir( path )
- end
- end
- local function install()
- fs.delete( path_log )
- log( "Log opened" )
- mkdir( path )
- mkdir( path_games )
- mkdir( path_data )
- mkdir( path_players )
- mkdir( path_saves )
- mkdir( path_achievements )
- mkdir( path_stats )
- mkdir( path_score )
- end
- local function getGameData( game, key )
- log( "gathering data for " .. game .. ": " .. key )
- local path = fs.combine( path_data, game )
- if not fs.isReadOnly( path ) and not fs.isDir( path ) then
- local f = fs.open( path, "r" )
- local s = f.readAll()
- f.close()
- return textutils.unserialize( s )[ key ]
- end
- return false
- end
- local function addGame( path, data )
- local function setGameData( game, key, value )
- local path = fs.combine( path_data, game )
- if not fs.isReadOnly( path ) and not fs.isDir( path ) then
- log( "setting data for " .. game .. ": " .. key .. " = " .. value )
- if fs.exists( path ) then
- local f = fs.open( path, "r" )
- local s = f.readAll()
- local t = textutils.unserialize( s )
- f.close()
- else
- t = {}
- end
- t[ key ] = value
- s = textutils.serialize( t )
- local f = fs.open( path, "w" )
- f.write( s )
- f.close()
- return true
- end
- return false
- end
- local game = data[ "game" ]
- local name = data[ "name" ] or data[ "game" ]
- local path = data[ "path" ] or data[ "game" ]
- local path_game = fs.combine( path_games, game )
- mkdir( path_game )
- setGameData( game, "name", name )
- setGameData( game, "path", path )
- if #data[ "run" ] == 8 then
- if http then
- f = http.get( "http://www.pastebin.com/" .. data[ "run" ] )
- if f then
- run = f.readAll()
- f.close()
- else
- run = data[ "run" ]
- end
- else
- return false
- end
- else
- run = data[ "run" ]
- end
- f = fs.open( fs.combine( path_game, "run" ), "w" )
- f.write( run )
- f.close()
- for k in pairs( data ) do
- if k ~= "game" and k ~= "name" and k ~= "path" and k ~= "run" then
- folder = fs.combine( path_game, string.sub( k, 1, #k - #fs.getName( k ) - 1 ) )
- mkdir( folder )
- f = fs.open( fs.combine( path_game, k ), "w" )
- f.write( data[ k ] )
- f.close()
- end
- end
- return true
- end
- local function valid( s )
- tInvalid = {"\\","/",":",",","*","?","\"","<",">","|","%."}
- for k, v in ipairs( tInvalid ) do
- if string.find( s, v ) then
- log( "invalid string: \"" .. s .. "\" (char " .. v .. " is invalid)" )
- return false
- end
- end
- return true
- end
- local function checkPlayer( username, password )
- local path = fs.combine( path_players, username )
- if fs.exists( path ) and not fs.isDir( path ) then
- local f = fs.open( path, "r" )
- local s = f.readAll()
- local t = textutils.unserialize( s )
- f.close()
- if t[ "username" ] == username and t[ "password" ] == password then
- return true
- end
- end
- return false
- end
- local function playerData( username, key, value )
- local path = fs.combine( path_players, username )
- if fs.exists( path ) and not fs.isReadOnly( path ) then
- local f = fs.open( path, "r" )
- local s = f.readAll()
- local t = textutils.unserialize( s )
- f.close()
- if not key then
- return t
- end
- if value then
- log( "editing " .. logstring( username ) .. "'s data. [\"" .. logstring( key ) .. "\"] = " .. logstring( value ) )
- t[ key ] = value
- f = fs.open( path, "w" )
- s = textutils.serialize( t )
- f.write( s )
- f.close()
- return t
- else
- log( "gathering " .. logstring( username ) .. "'s data. [\"" .. logstring( key ) .. "\"] = " .. logstring( t[ key ] ) )
- return t[ key ]
- end
- end
- return false
- end
- --[[ SECTION: Login System ]]--
- function player.add( username, password, name )
- local path = fs.combine( path_players, username )
- if not fs.exists( path ) and not fs.isDir( path ) and not fs.isReadOnly( path ) and valid( username ) and valid( password ) then
- local f = fs.open( path, "w" )
- local t = {["username"]=username,["password"]=password,["path"]=path,["name"]=name or username,["achievement"]={},["stat"]={},["score"]={}}
- f.write( textutils.serialize( t ) )
- f.close()
- return true
- end
- return false
- end
- function player.remove( username, password )
- local path = fs.combine( path_players, username )
- if fs.exists( path ) and not fs.isReadOnly( path ) and checkPlayer( username, password ) then
- fs.delete( path )
- return true
- end
- return false
- end
- function player.list()
- local users = {}
- if fs.exists( path_players ) then
- local list = fs.list( path )
- for k, v in ipairs( list ) do
- local path = fs.combine( path_players, list[ k ] )
- local f = fs.open( path, "r" )
- local s = f.readAll()
- local t = textutils.unserialize()
- if type( t ) == "table" and t[ "username" ] and t[ "password" ] then
- table.insert( users, t[ "username" ] )
- end
- f.close()
- end
- end
- return users
- end
- function player.current( username )
- local path = fs.combine( path_players, ".current" )
- if not fs.isDir( path ) and not fs.isReadOnly( path ) then
- if not username then
- if fs.exists( path ) then
- local f = fs.open( path, "r" )
- local current = f.readAll()
- f.close()
- return current
- end
- return
- elseif username == "" then
- fs.delete( path )
- else
- local f = fs.open( path, "w" )
- f.write( username )
- f.close()
- end
- return true
- end
- return false
- end
- function player.login( username, password, online )
- if online then
- else
- if checkPlayer( username, password ) then
- player.current( username )
- local t = {}
- t.path = playerData( username, "path" )
- t.name = playerData( username, "name" )
- t.username = playerData( username, "username" )
- t.password = playerData( username, "password" )
- t.online = false
- function t.achievements()
- return playerData( username, "achievement" )
- end
- function t.stats()
- return playerData( username, "stat" )
- end
- function t.score()
- return playerData( username, "score" )
- end
- function t:update( self )
- return self
- end
- function t.logout()
- player.current( "" )
- end
- return t
- end
- return
- end
- end
- --[[ SECTION: Game Engine ]]--
- function game.install( path )
- if fs.exists( path ) and not fs.isDir( path ) then
- log( "installing game from path " .. path )
- local f = fs.open( path, "r" )
- local s = f.readAll()
- local data = textutils.unserialize( s )
- f.close()
- return addGame( path, data )
- end
- return false
- end
- function game.add( game, data )
- local path = fs.combine( path_games, game )
- log( "adding game from game data (" .. game .. ")" )
- return addGame( game, data )
- end
- function game.remove( game )
- local path = fs.combine( path_games, game )
- log( "removing game " .. game )
- fs.delete( game )
- end
- function game.start( game )
- log( "starting game: " .. game )
- local path = fs.combine( path_games, fs.combine( getGameData( game, "path" ), "run" ) )
- local t = {}
- t.game = game
- t.path = path
- function t.save( data, name )
- local path = fs.combine( path_saves, fs.combine( getGameData( t.game, "path" ), name ) )
- if not fs.isReadOnly( path ) and not fs.isDir( path ) then
- local f = fs.open( path, "w" )
- f.write( textutils.serialize( data ) )
- f.close()
- return true
- end
- return false
- end
- function t.load( name )
- local path = fs.combine( path_saves, fs.combine( getGameData( t.game, "path" ), name ) )
- if fs.exists( path ) and not fs.isReadOnly( path ) then
- local f = fs.open( path, "r" )
- local s = f.readAll()
- f.close()
- return textutils.unserialize( s )
- end
- return false
- end
- return t
- end
- function game.run( game )
- log( "running game: " .. game )
- path = fs.combine( path_games, fs.combine( getGameData( game, "path" ), "run" ) )
- if fs.exists( path ) then
- return shell.run( path )
- end
- return false
- end
- --[[ SECTION: Achievements ]]--
- function achievement.add( achievement, desc, secret )
- local path = fs.combine( path_achievements, achievement )
- if not fs.exists( path ) and not fs.isDir( path ) and not fs.isReadOnly( path ) and valid( achievement ) then
- log( "Adding achievement " .. achievement )
- local f = fs.open( path, "w" )
- local t = {["name"]=achievement,["desc"]=desc,["secret"]=secret or false}
- f.write( textutils.serialize( t ) )
- f.close()
- return achievement
- end
- return false
- end
- function achievement.remove( achievement )
- local path = fs.combine( path_achievements, achievement )
- if fs.exists( path ) and not fs.isReadOnly( path ) then
- log( "Removing achievement " .. achievement )
- fs.delete( path )
- return true
- end
- return false
- end
- function achievement.unlock( p, achievement )
- log( "unlocking achievement " .. achievement .. " for player " .. p.name )
- local achievements = playerData( p.username, "achievement" )
- achievements[ achievement ] = true
- playerData( p.username, "achievement", achievements )
- return playerData( p.username )
- end
- function achievement.lock( p, achievement )
- log( "locking achievement " .. achievement .. " for player " .. p.name )
- local achievements = playerData( p.username, "achievement" )
- achievements[ achievement ] = false
- playerData( p.username, "achievement", achievements )
- return playerData( p.username )
- end
- function achievement.list()
- local achievements = {}
- local path = path_achievements
- if fs.exists( path ) then
- log( "listing achievements" )
- local list = fs.list( path )
- for k, v in ipairs( list ) do
- local f = fs.open( fs.combine, "r" )
- local s = f.readAll()
- local t = texutils.unserialize( s )
- f.close()
- table.insert( achievements, t[ "name" ] )
- end
- end
- return achievements
- end
- --[[ SECTION: Stats ]]--
- function stat.add( stat, desc, suffix )
- local path = fs.combine( path_stats, stat )
- if not fs.exists( path ) and not fs.isDir( path ) and not fs.isReadOnly( path ) and valid( stat ) then
- log( "adding stat" .. stat )
- local f = fs.open( path, "w" )
- local t = {["name"]=stat,["desc"]=desc,["suffix"]=suffix}
- f.write( t )
- f.close()
- return stat
- end
- return false
- end
- function stat.remove( stat )
- local path = fs.combine( path_stats, stat )
- if fs.exists( path ) and not fs.isReadOnly( path ) then
- log( "removing stat" .. stat )
- fs.delete( path )
- return true
- end
- return false
- end
- function stat.set( p, stat, value )
- log( "setting stat " .. stat .. " to " .. value .. " for player " .. p.name )
- local stats = playerData( p.username, "stat" )
- stats[ stat ] = value
- return playerData( p.username )
- end
- function stat.list()
- local stats = {}
- local path = path_stats
- if fs.exists( path ) then
- log( "listing stats" )
- local list = fs.list( path )
- for k, v in ipairs( list ) do
- local f = fs.open( fs.combine, "r" )
- local s = f.readAll()
- local t = texutils.unserialize( s )
- f.close()
- table.insert( stats, t[ "name" ] )
- end
- end
- return stats
- end
- --[[ SECTION: Score ]]--
- function scoreboard.add( objective, desc )
- local path = fs.combine( path_score, objective )
- if not fs.exists( path ) and not fs.isDir( path ) and not fs.isReadOnly( path ) and valid( objective ) then
- log( "adding objective " .. objective )
- local f = fs.open( path, "w" )
- local t = {["name"]=objective,["desc"]=desc}
- f.write( textutils.serialize( t ) )
- f.close()
- return objective
- end
- return false
- end
- function scoreboard.remove( objective )
- local path = fs.combine( path_score, objective )
- if fs.exists( path ) and not fs.isReadOnly( path ) then
- log( "removing objective " .. objective )
- fs.delete( path )
- return true
- end
- return false
- end
- function scoreboard.list()
- local scoreboard = {}
- local path = fs.combine( path_score )
- if fs.exists( path ) then
- log( "listing objectives" )
- local list = fs.list( path )
- for k, v in ipairs( list ) do
- local f = fs.open( fs.combine, "r" )
- local s = f.readAll()
- local t = texutils.unserialize( s )
- f.close()
- table.insert( scoreboard, t[ "name" ] )
- end
- end
- return scoreboard
- end
- function score.set( p, objective, value )
- log( "setting score " .. objective .. " to " .. value .. " for player " .. p.name )
- local score = playerData( p.username, "score" )
- score[ objective ] = value
- playerData( p.username, "score", score )
- return playerData( p.username )
- end
- function score.add( p, objective, value )
- log( "adding score " .. objective .. " with " .. value .. " for player " .. p.name )
- local score = playerData( p.username, "score" )
- score[ objective ] = score[ objective ] + value
- playerData( p.username, "score", score )
- return playerData( p.username )
- end
- function score.remove( p, objective, value )
- log( "removing score " .. objective .. " with " .. value .. " for player " .. p.name )
- local score = playerData( p.username, "score" )
- score[ objective ] = score[ objective ] - value
- playerData( p.username, "score", score )
- return playerData( p.username )
- end
- --[[ SECTION: Installation ]]--
- install()
Advertisement
Add Comment
Please, Sign In to add comment