{"action":"created","comment":{"url":"https://api.github.com/repos/java-course-au/assignments/pulls/comments/44954749","id":44954749,"diff_hunk":"@@ -1,23 +1,139 @@\n package ru.spbau.mit;\n \n-import java.io.BufferedReader;\n-import java.io.FileReader;\n-import java.io.IOException;\n+import java.io.*;\n import java.util.*;\n+import java.util.concurrent.TimeUnit;\n+\n+import static java.lang.Thread.sleep;\n \n \n public class QuizGame implements Game {\n+    private GameServer server;\n+    private int maxLettersToOpen;\n+    private long delayUntilNextLetter;\n+    private String dictionaryFilename;\n+    private Boolean gameContinue = false;\n+    private Boolean wasStarted = false;\n+\n+    private ArrayList<String> questionAndAnswer;\n+    private int currentQuestionNumber = 0;\n+    Thread playThread;\n+\n     public QuizGame(GameServer server) {\n-        throw new UnsupportedOperationException(\"TODO: implement\");\n+        this.server = server;\n+        playThread = new Thread(new PlayGame());\n+    }\n+\n+    public void setDelayUntilNextLetter (Integer delayUntilNextLetter) {\n+        this.delayUntilNextLetter = delayUntilNextLetter;\n+    }\n+\n+    public void setMaxLettersToOpen (Integer maxLettersToOpen) {\n+        this.maxLettersToOpen = maxLettersToOpen;\n+    }\n+\n+    public void setDictionaryFilename (String dictionaryFilename) {\n+        this.dictionaryFilename = dictionaryFilename;\n+    }\n+\n+    private String currentQ, currentA;\n+\n+    private void nextQuestion() throws FileNotFoundException {\n+        if (questionAndAnswer == null) {\n+            questionAndAnswer = new ArrayList<>();\n+            currentQuestionNumber = 0;\n+            Scanner scanner = new Scanner(new File(dictionaryFilename));\n+            while(scanner.hasNext()) {\n+                String s = scanner.nextLine();\n+                questionAndAnswer.add(s);\n+            }\n+        }\n+        if (currentQuestionNumber == questionAndAnswer.size()) {\n+            currentQuestionNumber = 0;\n+        }\n+        currentQ = \"\";\n+        currentA = \"\";\n+        String currentQuestionAndAnswer = questionAndAnswer.get(currentQuestionNumber);\n+        int i = 0;\n+        while (i < currentQuestionAndAnswer.length() && !Objects.equals(currentQuestionAndAnswer.substring(i, i + 1), \";\")) {\n+            ++i;\n+        }\n+        currentQ = currentQuestionAndAnswer.substring(0, i);\n+        currentA = currentQuestionAndAnswer.substring(i + 1);\n+        currentQuestionNumber += 1;\n+    }\n+\n+    class PlayGame implements Runnable {\n+        @Override\n+        public void run() {\n+            while(true) {\n+                if (gameContinue) {\n+                    try {\n+                        nextQuestion();\n+                    } catch (FileNotFoundException e) {\n+                        e.printStackTrace();\n+                    }\n+                    server.broadcast(\"New round started: \" + currentQ + \" (\" + currentA.length() + \" letters)\");\n+                    Boolean newGame = false;\n+                    for (int i = 0; i < maxLettersToOpen; ++i) {\n+                        try {\n+                            TimeUnit.MILLISECONDS.sleep(delayUntilNextLetter);\n+                        } catch (InterruptedException e) {\n+                            newGame = true;\n+                            break;\n+                        }\n+                        if (Thread.interrupted()) {\n+                            newGame = true;\n+                            break;\n+                        } else {\n+                            server.broadcast(\"Current prefix is \" + currentA.substring(0, i + 1));\n+                        }\n+                    }\n+                    if (newGame) {\n+                        continue;\n+                    }\n+                    if (Thread.interrupted()) {\n+                        newGame = true;\n+                        continue;\n+                    } else {\n+                        try {\n+                            TimeUnit.MILLISECONDS.sleep(delayUntilNextLetter);\n+                        } catch (InterruptedException e) {\n+                            Thread.interrupted();\n+                            continue;\n+                        }\n+                    }\n+                    if (!Thread.interrupted()) {\n+                        server.broadcast(\"Nobody guessed, the word was \" + currentA);\n+                    }\n+                }\n+            }\n+        }\n     }\n \n     @Override\n     public void onPlayerConnected(String id) {\n-        throw new UnsupportedOperationException(\"TODO: implement\");\n     }\n \n     @Override\n     public void onPlayerSentMsg(String id, String msg) {\n-        throw new UnsupportedOperationException(\"TODO: implement\");\n+        synchronized (this) {","path":"src/main/java/ru/spbau/mit/QuizGame.java","position":126,"original_position":126,"commit_id":"6c27ee66e4b24d0188b598b797eecd9ed3b37a03","original_commit_id":"6c27ee66e4b24d0188b598b797eecd9ed3b37a03","user":{"login":"dzharkov","id":863207,"avatar_url":"https://avatars.githubusercontent.com/u/863207?v=3","gravatar_id":"","url":"https://api.github.com/users/dzharkov","html_url":"https://github.com/dzharkov","followers_url":"https://api.github.com/users/dzharkov/followers","following_url":"https://api.github.com/users/dzharkov/following{/other_user}","gists_url":"https://api.github.com/users/dzharkov/gists{/gist_id}","starred_url":"https://api.github.com/users/dzharkov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dzharkov/subscriptions","organizations_url":"https://api.github.com/users/dzharkov/orgs","repos_url":"https://api.github.com/users/dzharkov/repos","events_url":"https://api.github.com/users/dzharkov/events{/privacy}","received_events_url":"https://api.github.com/users/dzharkov/received_events","type":"User","site_admin":false},"body":"Очень подозрительно, что в основном потоке отсутствует синхронизация","created_at":"2015-11-16T17:35:56Z","updated_at":"2015-11-16T17:35:56Z","html_url":"https://github.com/java-course-au/assignments/pull/73#discussion_r44954749","pull_request_url":"https://api.github.com/repos/java-course-au/assignments/pulls/73","_links":{"self":{"href":"https://api.github.com/repos/java-course-au/assignments/pulls/comments/44954749"},"html":{"href":"https://github.com/java-course-au/assignments/pull/73#discussion_r44954749"},"pull_request":{"href":"https://api.github.com/repos/java-course-au/assignments/pulls/73"}}},"pull_request":{"url":"https://api.github.com/repos/java-course-au/assignments/pulls/73","id":50746235,"html_url":"https://github.com/java-course-au/assignments/pull/73","diff_url":"https://github.com/java-course-au/assignments/pull/73.diff","patch_url":"https://github.com/java-course-au/assignments/pull/73.patch","issue_url":"https://api.github.com/repos/java-course-au/assignments/issues/73","number":73,"state":"open","locked":false,"title":"ДЗ 03, Черникова Ольга, подгруппа 1","user":{"login":"olga24912","id":10383157,"avatar_url":"https://avatars.githubusercontent.com/u/10383157?v=3","gravatar_id":"","url":"https://api.github.com/users/olga24912","html_url":"https://github.com/olga24912","followers_url":"https://api.github.com/users/olga24912/followers","following_url":"https://api.github.com/users/olga24912/following{/other_user}","gists_url":"https://api.github.com/users/olga24912/gists{/gist_id}","starred_url":"https://api.github.com/users/olga24912/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olga24912/subscriptions","organizations_url":"https://api.github.com/users/olga24912/orgs","repos_url":"https://api.github.com/users/olga24912/repos","events_url":"https://api.github.com/users/olga24912/events{/privacy}","received_events_url":"https://api.github.com/users/olga24912/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-15T13:10:53Z","updated_at":"2015-11-16T17:35:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7bf930d48721df45f0dd08bd6814fd302d5637f8","assignee":{"login":"dzharkov","id":863207,"avatar_url":"https://avatars.githubusercontent.com/u/863207?v=3","gravatar_id":"","url":"https://api.github.com/users/dzharkov","html_url":"https://github.com/dzharkov","followers_url":"https://api.github.com/users/dzharkov/followers","following_url":"https://api.github.com/users/dzharkov/following{/other_user}","gists_url":"https://api.github.com/users/dzharkov/gists{/gist_id}","starred_url":"https://api.github.com/users/dzharkov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dzharkov/subscriptions","organizations_url":"https://api.github.com/users/dzharkov/orgs","repos_url":"https://api.github.com/users/dzharkov/repos","events_url":"https://api.github.com/users/dzharkov/events{/privacy}","received_events_url":"https://api.github.com/users/dzharkov/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/java-course-au/assignments/pulls/73/commits","review_comments_url":"https://api.github.com/repos/java-course-au/assignments/pulls/73/comments","review_comment_url":"https://api.github.com/repos/java-course-au/assignments/pulls/comments{/number}","comments_url":"https://api.github.com/repos/java-course-au/assignments/issues/73/comments","statuses_url":"https://api.github.com/repos/java-course-au/assignments/statuses/6c27ee66e4b24d0188b598b797eecd9ed3b37a03","head":{"label":"olga24912:03-game-server","ref":"03-game-server","sha":"6c27ee66e4b24d0188b598b797eecd9ed3b37a03","user":{"login":"olga24912","id":10383157,"avatar_url":"https://avatars.githubusercontent.com/u/10383157?v=3","gravatar_id":"","url":"https://api.github.com/users/olga24912","html_url":"https://github.com/olga24912","followers_url":"https://api.github.com/users/olga24912/followers","following_url":"https://api.github.com/users/olga24912/following{/other_user}","gists_url":"https://api.github.com/users/olga24912/gists{/gist_id}","starred_url":"https://api.github.com/users/olga24912/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olga24912/subscriptions","organizations_url":"https://api.github.com/users/olga24912/orgs","repos_url":"https://api.github.com/users/olga24912/repos","events_url":"https://api.github.com/users/olga24912/events{/privacy}","received_events_url":"https://api.github.com/users/olga24912/received_events","type":"User","site_admin":false},"repo":{"id":42860939,"name":"assignments","full_name":"olga24912/assignments","owner":{"login":"olga24912","id":10383157,"avatar_url":"https://avatars.githubusercontent.com/u/10383157?v=3","gravatar_id":"","url":"https://api.github.com/users/olga24912","html_url":"https://github.com/olga24912","followers_url":"https://api.github.com/users/olga24912/followers","following_url":"https://api.github.com/users/olga24912/following{/other_user}","gists_url":"https://api.github.com/users/olga24912/gists{/gist_id}","starred_url":"https://api.github.com/users/olga24912/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/olga24912/subscriptions","organizations_url":"https://api.github.com/users/olga24912/orgs","repos_url":"https://api.github.com/users/olga24912/repos","events_url":"https://api.github.com/users/olga24912/events{/privacy}","received_events_url":"https://api.github.com/users/olga24912/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/olga24912/assignments","description":"Домашние задания по Java","fork":true,"url":"https://api.github.com/repos/olga24912/assignments","forks_url":"https://api.github.com/repos/olga24912/assignments/forks","keys_url":"https://api.github.com/repos/olga24912/assignments/keys{/key_id}","collaborators_url":"https://api.github.com/repos/olga24912/assignments/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/olga24912/assignments/teams","hooks_url":"https://api.github.com/repos/olga24912/assignments/hooks","issue_events_url":"https://api.github.com/repos/olga24912/assignments/issues/events{/number}","events_url":"https://api.github.com/repos/olga24912/assignments/events","assignees_url":"https://api.github.com/repos/olga24912/assignments/assignees{/user}","branches_url":"https://api.github.com/repos/olga24912/assignments/branches{/branch}","tags_url":"https://api.github.com/repos/olga24912/assignments/tags","blobs_url":"https://api.github.com/repos/olga24912/assignments/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/olga24912/assignments/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/olga24912/assignments/git/refs{/sha}","trees_url":"https://api.github.com/repos/olga24912/assignments/git/trees{/sha}","statuses_url":"https://api.github.com/repos/olga24912/assignments/statuses/{sha}","languages_url":"https://api.github.com/repos/olga24912/assignments/languages","stargazers_url":"https://api.github.com/repos/olga24912/assignments/stargazers","contributors_url":"https://api.github.com/repos/olga24912/assignments/contributors","subscribers_url":"https://api.github.com/repos/olga24912/assignments/subscribers","subscription_url":"https://api.github.com/repos/olga24912/assignments/subscription","commits_url":"https://api.github.com/repos/olga24912/assignments/commits{/sha}","git_commits_url":"https://api.github.com/repos/olga24912/assignments/git/commits{/sha}","comments_url":"https://api.github.com/repos/olga24912/assignments/comments{/number}","issue_comment_url":"https://api.github.com/repos/olga24912/assignments/issues/comments{/number}","contents_url":"https://api.github.com/repos/olga24912/assignments/contents/{+path}","compare_url":"https://api.github.com/repos/olga24912/assignments/compare/{base}...{head}","merges_url":"https://api.github.com/repos/olga24912/assignments/merges","archive_url":"https://api.github.com/repos/olga24912/assignments/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/olga24912/assignments/downloads","issues_url":"https://api.github.com/repos/olga24912/assignments/issues{/number}","pulls_url":"https://api.github.com/repos/olga24912/assignments/pulls{/number}","milestones_url":"https://api.github.com/repos/olga24912/assignments/milestones{/number}","notifications_url":"https://api.github.com/repos/olga24912/assignments/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/olga24912/assignments/labels{/name}","releases_url":"https://api.github.com/repos/olga24912/assignments/releases{/id}","created_at":"2015-09-21T10:59:33Z","updated_at":"2015-09-15T08:01:56Z","pushed_at":"2015-11-16T13:24:40Z","git_url":"git://github.com/olga24912/assignments.git","ssh_url":"git@github.com:olga24912/assignments.git","clone_url":"https://github.com/olga24912/assignments.git","svn_url":"https://github.com/olga24912/assignments","homepage":"http://mit.spbau.ru/sewiki/index.php/Java_2_2015","size":146,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"java-course-au:03-game-server","ref":"03-game-server","sha":"1a409528799d45902314f8945b255a72b9a7b427","user":{"login":"java-course-au","id":14248092,"avatar_url":"https://avatars.githubusercontent.com/u/14248092?v=3","gravatar_id":"","url":"https://api.github.com/users/java-course-au","html_url":"https://github.com/java-course-au","followers_url":"https://api.github.com/users/java-course-au/followers","following_url":"https://api.github.com/users/java-course-au/following{/other_user}","gists_url":"https://api.github.com/users/java-course-au/gists{/gist_id}","starred_url":"https://api.github.com/users/java-course-au/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/java-course-au/subscriptions","organizations_url":"https://api.github.com/users/java-course-au/orgs","repos_url":"https://api.github.com/users/java-course-au/repos","events_url":"https://api.github.com/users/java-course-au/events{/privacy}","received_events_url":"https://api.github.com/users/java-course-au/received_events","type":"Organization","site_admin":false},"repo":{"id":42502722,"name":"assignments","full_name":"java-course-au/assignments","owner":{"login":"java-course-au","id":14248092,"avatar_url":"https://avatars.githubusercontent.com/u/14248092?v=3","gravatar_id":"","url":"https://api.github.com/users/java-course-au","html_url":"https://github.com/java-course-au","followers_url":"https://api.github.com/users/java-course-au/followers","following_url":"https://api.github.com/users/java-course-au/following{/other_user}","gists_url":"https://api.github.com/users/java-course-au/gists{/gist_id}","starred_url":"https://api.github.com/users/java-course-au/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/java-course-au/subscriptions","organizations_url":"https://api.github.com/users/java-course-au/orgs","repos_url":"https://api.github.com/users/java-course-au/repos","events_url":"https://api.github.com/users/java-course-au/events{/privacy}","received_events_url":"https://api.github.com/users/java-course-au/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/java-course-au/assignments","description":"Домашние задания по Java","fork":false,"url":"https://api.github.com/repos/java-course-au/assignments","forks_url":"https://api.github.com/repos/java-course-au/assignments/forks","keys_url":"https://api.github.com/repos/java-course-au/assignments/keys{/key_id}","collaborators_url":"https://api.github.com/repos/java-course-au/assignments/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/java-course-au/assignments/teams","hooks_url":"https://api.github.com/repos/java-course-au/assignments/hooks","issue_events_url":"https://api.github.com/repos/java-course-au/assignments/issues/events{/number}","events_url":"https://api.github.com/repos/java-course-au/assignments/events","assignees_url":"https://api.github.com/repos/java-course-au/assignments/assignees{/user}","branches_url":"https://api.github.com/repos/java-course-au/assignments/branches{/branch}","tags_url":"https://api.github.com/repos/java-course-au/assignments/tags","blobs_url":"https://api.github.com/repos/java-course-au/assignments/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/java-course-au/assignments/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/java-course-au/assignments/git/refs{/sha}","trees_url":"https://api.github.com/repos/java-course-au/assignments/git/trees{/sha}","statuses_url":"https://api.github.com/repos/java-course-au/assignments/statuses/{sha}","languages_url":"https://api.github.com/repos/java-course-au/assignments/languages","stargazers_url":"https://api.github.com/repos/java-course-au/assignments/stargazers","contributors_url":"https://api.github.com/repos/java-course-au/assignments/contributors","subscribers_url":"https://api.github.com/repos/java-course-au/assignments/subscribers","subscription_url":"https://api.github.com/repos/java-course-au/assignments/subscription","commits_url":"https://api.github.com/repos/java-course-au/assignments/commits{/sha}","git_commits_url":"https://api.github.com/repos/java-course-au/assignments/git/commits{/sha}","comments_url":"https://api.github.com/repos/java-course-au/assignments/comments{/number}","issue_comment_url":"https://api.github.com/repos/java-course-au/assignments/issues/comments{/number}","contents_url":"https://api.github.com/repos/java-course-au/assignments/contents/{+path}","compare_url":"https://api.github.com/repos/java-course-au/assignments/compare/{base}...{head}","merges_url":"https://api.github.com/repos/java-course-au/assignments/merges","archive_url":"https://api.github.com/repos/java-course-au/assignments/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/java-course-au/assignments/downloads","issues_url":"https://api.github.com/repos/java-course-au/assignments/issues{/number}","pulls_url":"https://api.github.com/repos/java-course-au/assignments/pulls{/number}","milestones_url":"https://api.github.com/repos/java-course-au/assignments/milestones{/number}","notifications_url":"https://api.github.com/repos/java-course-au/assignments/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/java-course-au/assignments/labels{/name}","releases_url":"https://api.github.com/repos/java-course-au/assignments/releases{/id}","created_at":"2015-09-15T07:36:54Z","updated_at":"2015-10-04T08:47:09Z","pushed_at":"2015-11-16T17:20:06Z","git_url":"git://github.com/java-course-au/assignments.git","ssh_url":"git@github.com:java-course-au/assignments.git","clone_url":"https://github.com/java-course-au/assignments.git","svn_url":"https://github.com/java-course-au/assignments","homepage":"http://mit.spbau.ru/sewiki/index.php/Java_2_2015","size":1239,"stargazers_count":2,"watchers_count":2,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":26,"mirror_url":null,"open_issues_count":9,"forks":26,"open_issues":9,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/java-course-au/assignments/pulls/73"},"html":{"href":"https://github.com/java-course-au/assignments/pull/73"},"issue":{"href":"https://api.github.com/repos/java-course-au/assignments/issues/73"},"comments":{"href":"https://api.github.com/repos/java-course-au/assignments/issues/73/comments"},"review_comments":{"href":"https://api.github.com/repos/java-course-au/assignments/pulls/73/comments"},"review_comment":{"href":"https://api.github.com/repos/java-course-au/assignments/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/java-course-au/assignments/pulls/73/commits"},"statuses":{"href":"https://api.github.com/repos/java-course-au/assignments/statuses/6c27ee66e4b24d0188b598b797eecd9ed3b37a03"}}}}