/lib/gs/contribs/cols/highscore.erl

https://github.com/dustin/otp · Erlang · 95 lines · 61 code · 15 blank · 19 comment · 0 complexity · 4cc1c341be2aaa5bfbd712fe109a5a56 MD5 · raw file

  1. %%
  2. %% %CopyrightBegin%
  3. %%
  4. %% Copyright Ericsson AB 1996-2009. All Rights Reserved.
  5. %%
  6. %% The contents of this file are subject to the Erlang Public License,
  7. %% Version 1.1, (the "License"); you may not use this file except in
  8. %% compliance with the License. You should have received a copy of the
  9. %% Erlang Public License along with this software. If not, it can be
  10. %% retrieved online at http://www.erlang.org/.
  11. %%
  12. %% Software distributed under the License is distributed on an "AS IS"
  13. %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  14. %% the License for the specific language governing rights and limitations
  15. %% under the License.
  16. %%
  17. %% %CopyrightEnd%
  18. %%
  19. %%
  20. -module(highscore).
  21. -export([run/2]).
  22. run(NScore, File) ->
  23. Scores = read_scores(File),
  24. case find_pos(NScore, 1, Scores) of
  25. false ->
  26. display(Scores);
  27. Pos ->
  28. NewScores = new_highscore(Scores, Pos, NScore),
  29. write_scores(NewScores,File),
  30. display(NewScores)
  31. end.
  32. new_highscore(Scores, Pos, NScore) ->
  33. Txt = io_lib:format("You entered position ~w", [Pos]),
  34. W = gs:create(window, gs:start(), [{width, 200},{height, 110},{map,true},
  35. {title, "New Highscore!!!"}]),
  36. gs:create(label, W, [{label, {text, Txt}}, {x, 0}, {y,0}, {align, center},
  37. {width, 190},{height, 30}]),
  38. Entry = gs:create(entry, W, [{x, 0}, {y, 40}, {height, 30}, {width, 200}]),
  39. Ok = gs:create(button, W, [{label, {text, "Ok"}}, {x, 40}, {y, 75}]),
  40. receive
  41. {gs, Ok, click, _,_} ->
  42. T = gs:read(Entry, text),
  43. gs:destroy(W),
  44. lists:sublist(lists:reverse(
  45. lists:keysort(1, [{NScore, T} | Scores])), 1, 10)
  46. end.
  47. read_scores(File) ->
  48. case file:read_file(File) of
  49. {ok, Bin} -> binary_to_term(Bin);
  50. {error, _Reason} ->
  51. mk_empty_high(10)
  52. end.
  53. mk_empty_high(0) -> [];
  54. mk_empty_high(N) -> [{N,"Erlang"}|mk_empty_high(N-1)].
  55. find_pos(_NScore, _N, []) -> false;
  56. find_pos(NScore, N, [{Score, _Name} | Scores]) when Score > NScore ->
  57. find_pos(NScore, N+1, Scores);
  58. find_pos(_NScore, N, _) -> N.
  59. write_scores(Scores,File) ->
  60. file:write_file(File, term_to_binary(Scores)).
  61. display(Scores) ->
  62. Win = gs:window(gs:start(), [{width, 300},{height, 250},{map,true},
  63. {title, "Highscores"}]),
  64. {W,H} = gs:read(Win,{font_wh,{{screen,12},"aaaaaaa"}}),
  65. G = gs:grid(Win,[{rows,{1,11}},{columnwidths,[W,4*W]},{hscroll,false},
  66. {width, 300},{height, 220},{vscroll,false},
  67. {cellheight,H+2},{font,{screen,12}}]),
  68. insert_scores(G,2,Scores),
  69. Ok = gs:button(Win, [{label, {text, "OK"}}, {x, 100}, {y, 220}]),
  70. receive
  71. {gs, Ok, click, _,_} -> gs:destroy(Win),
  72. ok
  73. end.
  74. insert_scores(Grid,_N,[]) ->
  75. gs:create(gridline,Grid,[{row,1},{font,{screen,bold,12}},
  76. {text,{1,"SCORE"}},{text,{2,"NAME"}}]);
  77. insert_scores(Grid,Row,[{Score,Name}|Ss]) ->
  78. gs:create(gridline,Grid,[{row,Row},{text,{1,io_lib:format("~w",[Score])}},
  79. {text,{2,Name}}]),
  80. insert_scores(Grid,Row+1,Ss).