package src;

import java.io.File;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.ini4j.Wini;

public class Tournament {
    String _userName;
    String _game;
    String _fileName;
    String _lang;

    DataBaseWorker _dataBaseWorker;

    public Tournament(String userName, String game, String fileName){
        _userName = userName;
        _game = game;
        _fileName = fileName;

        _lang = DetermineLang(_fileName);
    }

    public static String DetermineLang(String fileName){
        String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();

        if(extension == "cpp")
            return "Cpp";
        if(extension == "cs")
            return "CSharp";
        if(extension == "rb")
            return "Ruby";

        return "unknown";
    }

    /*
    * Возвращать из функции Game() будем целое число
    * 0 - всё ок
    * 1 - ошибка проверки корректности кода
    * --может что-то еще придется добавить
    * */

    public int Game() {
        if(!CheckCode())
            return 1;

        return MakeGame();
    }

    private int MakeGame() {
        InitDataBaseWorker();
        String judge = GlobalData.getInstance().Judge;
        Game game = new Game(_game, _userName, _lang, _dataBaseWorker, judge);

        game.OrganizeGame();

        return 0;
    }

    private void InitDataBaseWorker() {
        String server = GlobalData.getInstance().DBServer;
        String table = GlobalData.getInstance().DBTable;
        String login = GlobalData.getInstance().DBLogin;
        String password = GlobalData.getInstance().DBPassword;

        _dataBaseWorker = new DataBaseWorker(server, table, login, password);
    }

    private boolean CheckCode() {
        CodeMaker codeMaker = new CodeMaker(_fileName, _lang);
        if(!codeMaker.Make())
            return false;

        return true;
    }
}

@@ -7,16 +7,57 @@
 e src;%0A%0A
+import org.apache.commons.io.FileUtils;%0A%0A
 import j
@@ -101,41 +101,8 @@
 on;%0A
-import java.nio.file.CopyOption;%0A
 impo
@@ -129,35 +129,8 @@
 es;%0A
-import java.nio.file.Path;%0A
 impo
@@ -158,32 +158,8 @@
 s;%0A%0A
-import org.ini4j.Wini;%0A%0A
 publ
@@ -232,23 +232,47 @@
 String _
-fileNam
+sourceFile;%0A    String _destFil
 e;%0A    S
@@ -453,23 +453,25 @@
        _
-fileNam
+sourceFil
 e = file
@@ -508,23 +508,25 @@
 neLang(_
-fileNam
+sourceFil
 e);%0A    
@@ -702,18 +702,25 @@
 sion
- == 
+.contains(
 %22cpp%22)
+)
 %0A   
@@ -766,17 +766,24 @@
 sion
- == 
+.contains(
 %22cs%22)
+)
 %0A   
@@ -832,17 +832,24 @@
 sion
- == 
+.contains(
 %22rb%22)
+)
 %0A   
@@ -1098,25 +1098,65 @@
  int Game() 
-%7B
+throws IOException %7B%0A        CopyFiles();
 %0A        if(
@@ -1218,32 +1218,628 @@
 eGame();%0A    %7D%0A%0A
+    private void CopyFiles() throws IOException %7B%0A        _destFile = GlobalData.getInstance().UserPath.replaceAll(%22%25lang%25%22, _lang).%0A                                                      replaceAll(%22%25user%25%22, _userName).%0A                                                      replaceAll(%22%25game%25%22, _game);%0A%0A        FileUtils.cleanDirectory(new File(_destFile));%0A%0A        Files.copy(Paths.get(_sourceFile), Paths.get(_destFile + GlobalData.GetFileName(_sourceFile)));%0A%0A        FileUtils.copyDirectory(new File(GlobalData.getInstance().SDKPath.replace(%22%25lang%25%22, _lang)), new File (_destFile));%0A    %7D%0A%0A
     private int 
@@ -2011,25 +2011,24 @@
 er, judge);%0A
-%0A
         game
@@ -2511,23 +2511,23 @@
 eMaker(_
-fileNam
+destFil
 e, _lang

Change little bugs, Not important changes.