PageRenderTime 34ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/script.js

https://bitbucket.org/miya0001/wp-typing
JavaScript | 132 lines | 112 code | 20 blank | 0 comment | 10 complexity | 711ef4ba3d5a53ee7c7e9ce0dfbe6982 MD5 | raw file
  1. (function($){
  2. var ok_type;
  3. var miss_type;
  4. var word_index;
  5. var text_index;
  6. var sleep; // sec
  7. var start_time;
  8. var end_time;
  9. var timer;
  10. var texts = [];
  11. var label = {
  12. 'function': 'WordPress Function',
  13. 'action': 'Action Hook',
  14. 'filter': 'Filter Hook',
  15. }
  16. var codex = {
  17. 'function': 'http://codex.wordpress.org/Function_Reference/',
  18. 'action': 'http://codex.wordpress.org/Plugin_API/Action_Reference/',
  19. 'filter': 'http://codex.wordpress.org/Plugin_API/Filter_Reference/',
  20. };
  21. display();
  22. $('#myModal').on('hidden', function(){
  23. display();
  24. });
  25. function display(){
  26. ok_type = 0;
  27. miss_type = 0;
  28. word_index = 0;
  29. text_index = 0;
  30. sleep = 3;
  31. $('#word').text('');
  32. $('#cat').text('');
  33. $.getJSON(
  34. '/typing/json/',
  35. function(data){
  36. texts = data;
  37. $('#word').html('<a id="btn-start" href="javascript:void(0);" class="btn btn-success btn-large">Press Space to start</a>');
  38. $(window).unbind('keypress');
  39. $(window).keypress(init);
  40. $('#btn-start').click(init);
  41. }
  42. );
  43. }
  44. function init(e) {
  45. if (e.charCode === 32 || ($(this).attr('id') === 'btn-start')) {
  46. $(window).unbind('keypress', init);
  47. timer = setInterval(wait, 1000);
  48. }
  49. }
  50. function wait(){
  51. if (sleep === 0) {
  52. start_game();
  53. clearInterval(timer);
  54. return;
  55. }
  56. $('#word').html('<span style="color:#ff0000;">'+sleep+'</span>');
  57. sleep = sleep - 1;
  58. }
  59. function start_game(){
  60. set_text();
  61. start_time = new Date();
  62. $(window).keypress(function(e){
  63. var str = String.fromCharCode(e.charCode);
  64. if (str === texts[word_index].word.substr(text_index, 1)) {
  65. ok_type = ok_type + 1;
  66. $('#word-'+text_index).attr('class', 'text-ok');
  67. text_index = text_index + 1;
  68. if (text_index === texts[word_index].word.length) {
  69. word_index = word_index + 1;
  70. text_index = 0;
  71. if (word_index === texts.length) {
  72. end_game();
  73. } else {
  74. set_text();
  75. }
  76. }
  77. } else {
  78. $('#word-'+text_index).css('color', '#ff0000');
  79. $('#box').animate({backgroundColor: '#ffffcc'}, 100, function(){
  80. $(this).animate({backgroundColor: '#f5f5f5'});
  81. });
  82. miss_type = miss_type + 1;
  83. }
  84. });
  85. }
  86. function set_text(){
  87. var str = texts[word_index].word;
  88. $('#word').html('');
  89. $('#cat').text(label[texts[word_index].cat]);
  90. for (var i=0; i<str.length; i++) {
  91. $('#word').append('<span id="word-'+i+'">'+str.substr(i, 1)+'</span>');
  92. }
  93. }
  94. function end_game(){
  95. end_time = new Date();
  96. var time = Math.ceil((end_time - start_time) / 1000);
  97. var wpm = Math.ceil(ok_type / time * 60);
  98. var acc = Math.ceil((1 - (miss_type / (ok_type + miss_type))) * 100);
  99. $('#time').text(time + ' sec');
  100. $('#wpm').text(wpm);
  101. $('#acc').text(acc + ' %');
  102. $('#refs').html('');
  103. for (var i=0; i<texts.length; i++) {
  104. $('#refs').append('<li><a href="'+codex[texts[i].cat]+texts[i].word+'" target="_blank">'+texts[i].word+'</a></li>');
  105. }
  106. var url = 'https://twitter.com/intent/tweet?url=http://firegoby.jp/typing/&text=';
  107. var text = encodeURIComponent("My score is "+wpm+' and Accuracy is '+acc+'% #wptyping');
  108. $('#tweet').attr('href', url+text);
  109. $(window).unbind('keypress');
  110. $('#myModal').modal();
  111. }
  112. })(jQuery);