aboutsummaryrefslogtreecommitdiffstats
path: root/lib/gs/contribs/cols/highscore.erl
blob: 9ffbea50a7c07e40efb3d333af0716dd5f259e8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%

%%
-module(highscore).

-export([run/2]).

run(NScore, File) ->
    Scores = read_scores(File),
    case find_pos(NScore, 1, Scores) of
	false ->
	    display(Scores);
	Pos ->
	    NewScores = new_highscore(Scores, Pos, NScore),
	    write_scores(NewScores,File),
	    display(NewScores)
    end.


new_highscore(Scores, Pos, NScore) ->
    Txt = io_lib:format("You entered position ~w", [Pos]),
    W = gs:create(window, gs:start(), [{width, 200},{height, 110},{map,true},
				      {title, "New Highscore!!!"}]),
    gs:create(label, W, [{label, {text, Txt}}, {x, 0}, {y,0}, {align, center},
			 {width, 190},{height, 30}]),
    Entry = gs:create(entry, W, [{x, 0}, {y, 40}, {height, 30}, {width, 200}]),
    Ok = gs:create(button, W, [{label, {text, "Ok"}}, {x, 40}, {y, 75}]),
    receive
	{gs, Ok, click, _,_} -> 
	    T = gs:read(Entry, text),
	    gs:destroy(W),
	    lists:sublist(lists:reverse(
                 lists:keysort(1, [{NScore, T} | Scores])), 1, 10)
    end.



read_scores(File) ->
    case file:read_file(File) of
	{ok, Bin} -> binary_to_term(Bin);
	{error, _Reason} ->
	    mk_empty_high(10)
    end.

mk_empty_high(0) -> [];
mk_empty_high(N) -> [{N,"Erlang"}|mk_empty_high(N-1)].

find_pos(_NScore, _N, []) -> false;
find_pos(NScore, N, [{Score, _Name} | Scores]) when Score > NScore ->
    find_pos(NScore, N+1, Scores);
find_pos(_NScore, N, _) -> N.

write_scores(Scores,File) ->
    file:write_file(File, term_to_binary(Scores)).

display(Scores) ->
    Win = gs:window(gs:start(), [{width, 300},{height, 250},{map,true},
				 {title, "Highscores"}]),
    {W,H} = gs:read(Win,{font_wh,{{screen,12},"aaaaaaa"}}),
    G = gs:grid(Win,[{rows,{1,11}},{columnwidths,[W,4*W]},{hscroll,false},
		     {width, 300},{height, 220},{vscroll,false},
		     {cellheight,H+2},{font,{screen,12}}]),
    insert_scores(G,2,Scores),
    Ok = gs:button(Win, [{label, {text, "OK"}}, {x, 100}, {y, 220}]),
    receive
	{gs, Ok, click, _,_} -> gs:destroy(Win),
				ok
    end.

insert_scores(Grid,_N,[]) ->
    gs:create(gridline,Grid,[{row,1},{font,{screen,bold,12}},
			     {text,{1,"SCORE"}},{text,{2,"NAME"}}]);

insert_scores(Grid,Row,[{Score,Name}|Ss]) ->
    gs:create(gridline,Grid,[{row,Row},{text,{1,io_lib:format("~w",[Score])}},
			    {text,{2,Name}}]),
    insert_scores(Grid,Row+1,Ss).