aboutsummaryrefslogtreecommitdiffstats
path: root/autoload/space_vlaze/leaderboard.vim
diff options
context:
space:
mode:
authorTeddy Wing2015-10-08 02:44:51 -0400
committerTeddy Wing2015-10-08 02:44:51 -0400
commit07c59fb8017ee35e71bf809178cbc0bdbc20cc15 (patch)
treed756006f6676e4561728a34b2efbc66470ca1ae1 /autoload/space_vlaze/leaderboard.vim
parent00571ace4745be3f4083867622dd7c7f42a7b103 (diff)
downloadvim-space-vlaze-07c59fb8017ee35e71bf809178cbc0bdbc20cc15.tar.bz2
Save high scores
When you get a game over, save your high score to a file so that we can display it on the leaderboard. The saved scores get sorted from highest to lowest. If we don't have your name yet (saved to a separate file), we prompt you for it. This means that you can only have one player (name) per installation. Not the best scenario because maybe you have multiple people playing on your computer, but I made a judgement call to do it this way so we don't have to keep prompting the player for their name every time they get a Game Over. That could get annoying fast.
Diffstat (limited to 'autoload/space_vlaze/leaderboard.vim')
-rw-r--r--autoload/space_vlaze/leaderboard.vim43
1 files changed, 43 insertions, 0 deletions
diff --git a/autoload/space_vlaze/leaderboard.vim b/autoload/space_vlaze/leaderboard.vim
new file mode 100644
index 0000000..5ad25dd
--- /dev/null
+++ b/autoload/space_vlaze/leaderboard.vim
@@ -0,0 +1,43 @@
+if !exists('s:LEADERBOARD_PATH')
+ let s:LEADERBOARD_PATH = resolve(expand('<sfile>:p:h')) . '/../../leaderboard.tsv'
+endif
+
+
+if !exists('s:PLAYER_NAME_PATH')
+ let s:PLAYER_NAME_PATH = resolve(expand('<sfile>:p:h')) . '/../../player_name.txt'
+endif
+
+
+function! space_vlaze#leaderboard#MaybePromptForName()
+ if !filereadable(s:PLAYER_NAME_PATH)
+ let name = space_vlaze#menus#Prompt('Enter your name: ')
+
+ call writefile([name], s:PLAYER_NAME_PATH)
+ endif
+endfunction
+
+
+function! space_vlaze#leaderboard#SaveHighScore()
+ call space_vlaze#leaderboard#MaybePromptForName()
+
+ if filereadable(s:LEADERBOARD_PATH)
+ let scores = readfile(s:LEADERBOARD_PATH)
+ let scores = add(scores, space_vlaze#score#Score())
+ let scores = sort(scores, 'space_vlaze#leaderboard#Sort')
+ call writefile(scores, s:LEADERBOARD_PATH)
+ else
+ call writefile([space_vlaze#score#Score()], s:LEADERBOARD_PATH)
+ endif
+endfunction
+
+
+" Sorting function that orders numbers from highest to lowest.
+" For use with the |sort()| Vim function
+function! space_vlaze#leaderboard#Sort(item1, item2)
+ return a:item1 ==# a:item2 ? 0 :
+ \ a:item1 ># a:item2 ? -1 : 1
+endfunction
+
+
+function! space_vlaze#leaderboard#RenderLeaderboard()
+endfunction