Implemented myhighscores API, and used it in pacmine
parent
ebde440751
commit
289cacf17a
|
@ -1,9 +1,75 @@
|
|||
|
||||
-- Store any information we might need for the games that will use highscore
|
||||
-- (icons, description, or whatever)
|
||||
myhighscore.registered_games = {}
|
||||
|
||||
-- This table will contain a table for each registered game which
|
||||
-- will be an array of player scores
|
||||
myhighscore.scores = {}
|
||||
|
||||
-- How many scores to keep saved per game
|
||||
local stored_scores = 50
|
||||
|
||||
-- Name of the folder to save the scores to
|
||||
-- each game highscore list will be saved in a file inside this directory
|
||||
local score_directory = minetest.get_worldpath().."/myhighscores/"
|
||||
|
||||
-- You can register a new arcade game using this function
|
||||
myhighscore.register_game(name, definition)
|
||||
-- The definition will get added to the table of registered games.
|
||||
function myhighscore.register_game(name, definition)
|
||||
definition.description = definition.description or name
|
||||
myhighscore.registered_games[name] = definition
|
||||
myhighscore.load_scores(name)
|
||||
end
|
||||
|
||||
-- Returns true if score from A is smaller than score from B
|
||||
-- Used for sorting the score arra
|
||||
function myhighscore.is_score_higher(scoreA, scoreB)
|
||||
return scoreA.score > scoreB.score
|
||||
end
|
||||
|
||||
-- Saves a given score for the given game. "score" will be a table containing at least:
|
||||
-- player (player name) and score (points)
|
||||
function myhighscore.save_score(name, score)
|
||||
local scores = myhighscore.scores[name]
|
||||
|
||||
-- Check first if the last score is higher
|
||||
if scores[stored_scores] and
|
||||
myhighscore.is_score_higher(scores[stored_scores], score) then
|
||||
return false
|
||||
end
|
||||
|
||||
table.insert(scores, score)
|
||||
-- sort the array
|
||||
table.sort(scores, myhighscore.is_score_higher)
|
||||
-- check position and remove any extra ones
|
||||
local pos = 0
|
||||
for i,sc in pairs(scores) do
|
||||
if sc == score then
|
||||
pos = i
|
||||
elseif i > stored_scores then
|
||||
scores[i] = nil
|
||||
end
|
||||
end
|
||||
-- save it to disk
|
||||
local f, err = io.open(score_directory .. name, "w")
|
||||
f:write(minetest.serialize(scores))
|
||||
f:close()
|
||||
-- return the position we hold on the list
|
||||
return pos
|
||||
end
|
||||
|
||||
|
||||
-- Read scores from disk for the given game, or initialize the scores table if not present
|
||||
function myhighscore.load_scores(name)
|
||||
local f, err = io.open(score_directory .. name, "r")
|
||||
local data = {}
|
||||
if f then
|
||||
data = minetest.deserialize(f:read("*a")) or {}
|
||||
f:close()
|
||||
end
|
||||
myhighscore.scores[name] = data
|
||||
end
|
||||
|
||||
-- Create the scores directory if it doesn't exist!
|
||||
minetest.mkdir(score_directory)
|
||||
|
|
|
@ -13,15 +13,26 @@ local game_name = "the game"
|
|||
local game_player_name = "the player"
|
||||
local game_player_score = "648138"
|
||||
|
||||
local game_form = "size[6,8;]"..
|
||||
"background[0,0;6,8;myhighscore_form_bg.png]"..
|
||||
"label[1,0.5;HIGH SCORES FOR "..game_name.."]"..
|
||||
"label[1,1.5;PLAYER]"..
|
||||
"label[3.5,1.5;SCORE]"..
|
||||
"label[0.5,2;"..game_player_name.."]"..
|
||||
"label[3,2;"..game_player_score.."]"..
|
||||
"button[2,7;1,2;back;Back]"..
|
||||
"button_exit[4,7;1,2;exit;Exit]"
|
||||
local function get_formspec_for_game(name)
|
||||
local def = myhighscore.registered_games[name]
|
||||
local scores = myhighscore.scores[name] or {}
|
||||
-- Obtain a comma separated list of scores to display
|
||||
local scorelist = ""
|
||||
for _,score in pairs(scores) do
|
||||
scorelist = scorelist .. minetest.formspec_escape(score.player) ..
|
||||
"\t\t\t\t " .. score.score ..","
|
||||
end
|
||||
|
||||
return "size[6,8;]"..
|
||||
"background[0,0;6,8;myhighscore_form_bg.png]"..
|
||||
"label[1,0.5;HIGH SCORES FOR "..def.description.."]"..
|
||||
"label[1,1.5;PLAYER]"..
|
||||
"label[3.5,1.5;SCORE]"..
|
||||
"textlist[0.5,2;5,5;;"..scorelist.."]"..
|
||||
"button[2,7;1,2;back;Back]"..
|
||||
"button_exit[4,7;1,2;exit;Exit]"
|
||||
end
|
||||
|
||||
|
||||
minetest.register_node("myhighscore:score_board", {
|
||||
description = "Score Board",
|
||||
|
@ -29,7 +40,7 @@ minetest.register_node("myhighscore:score_board", {
|
|||
"myhighscore_top.png",
|
||||
"myhighscore_back.png",
|
||||
"myhighscore_side.png^[transformFX",
|
||||
"myhighscore_side.png",y
|
||||
"myhighscore_side.png",
|
||||
"myhighscore_back.png",
|
||||
"myhighscore_front.png",
|
||||
},
|
||||
|
@ -56,7 +67,7 @@ minetest.register_node("myhighscore:score_board", {
|
|||
on_receive_fields = function(pos, formname, fields, sender)
|
||||
local meta = minetest.env:get_meta(pos)
|
||||
if fields['game'] then
|
||||
meta:set_string('formspec', game_form)
|
||||
meta:set_string('formspec', get_formspec_for_game("pacmine"))
|
||||
elseif fields["back"] then
|
||||
meta:set_string('formspec', button_form)
|
||||
end
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
myhighscore
|
|
@ -64,6 +64,14 @@ function pacmine.game_end(id)
|
|||
pacmine.remove_hud(player, gamestate.player_name)
|
||||
player:moveto(vector.add(gamestate.pos,{x=0.5,y=0.5,z=-1.5}))
|
||||
end
|
||||
-- Save score
|
||||
local ranking = myhighscore.save_score("pacmine", {
|
||||
player = gamestate.player_name,
|
||||
score = gamestate.score
|
||||
})
|
||||
if ranking then
|
||||
minetest.chat_send_player(gamestate.player_name, "You made it to the highscores! Your Ranking: " .. ranking)
|
||||
end
|
||||
-- Clear the data
|
||||
pacmine.games[id] = nil
|
||||
pacmine.players[id] = nil
|
||||
|
@ -314,3 +322,9 @@ minetest.register_chatcommand("pacmine_exit", {
|
|||
end
|
||||
end
|
||||
})
|
||||
|
||||
-- Register with the myhighscore mod
|
||||
myhighscore.register_game("pacmine", {
|
||||
description = "Pacmine",
|
||||
icon = "pacmine_1.png",
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue