~tyszkiewicz-team/jchess/1.1

« back to all changes in this revision

Viewing changes to src/jchess/server/Console.java

  • Committer: MarioX
  • Date: 2010-05-02 19:30:14 UTC
  • Revision ID: mariiox@gmail.com-20100502193014-jgar9qtr2l7up4wn
* added Console class in server package
  this class is like mini-server, whose can run

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
#    This program is free software: you can redistribute it and/or modify
 
3
#    it under the terms of the GNU General Public License as published by
 
4
#    the Free Software Foundation, either version 3 of the License, or
 
5
#    (at your option) any later version.
 
6
#
 
7
#    This program is distributed in the hope that it will be useful,
 
8
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
#    GNU General Public License for more details.
 
11
#
 
12
#    You should have received a copy of the GNU General Public License
 
13
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 */
 
15
 
 
16
package jchess.server;
 
17
 
 
18
import java.io.IOException;
 
19
import java.util.Map;
 
20
import java.util.logging.Level;
 
21
import java.util.logging.Logger;
 
22
import jchess.MD5;
 
23
 
 
24
/**
 
25
 *
 
26
 * @author MarioX
 
27
 */
 
28
public class Console
 
29
{
 
30
    public static void main(String[] args)
 
31
    {
 
32
        System.out.println("JChess Server Start!");
 
33
  
 
34
        Server server = new Server(); //create server
 
35
        server.isPrintEnable = false;
 
36
 
 
37
        boolean isOK=true;
 
38
        while(isOK)
 
39
        {
 
40
            System.out.println("--------------------");
 
41
            System.out.println("[1] Nowy stół");
 
42
            System.out.println("[2] Lista aktywnych stołów");
 
43
            System.out.println("[3] Włącz/wyłącz komunikaty serwera");
 
44
            System.out.println("[4] Wyłącz serwer");
 
45
            System.out.print("-> ");
 
46
            String str = readString();
 
47
 
 
48
            if(str.equals("1")) //new table
 
49
            {
 
50
                System.out.print("ID gry: ");
 
51
                int gameID = Integer.parseInt(readString());
 
52
 
 
53
                System.out.print("Hasło: ");
 
54
                String pass = MD5.encrypt(readString());
 
55
 
 
56
                String observer;
 
57
                do
 
58
                {
 
59
                    System.out.print("Gra z obserwatorami[t/n]: ");
 
60
                    observer = readString();
 
61
                }while(!observer.equalsIgnoreCase("t") && !observer.equalsIgnoreCase("n"));
 
62
 
 
63
                boolean canObserver = observer.equalsIgnoreCase("t");
 
64
 
 
65
                server.newTable(gameID, pass, canObserver, true); //create new table
 
66
            }
 
67
            else if(str.equals("2")) //list of tables
 
68
            {
 
69
                for (Map.Entry<Integer, Table> entry : server.tables.entrySet())
 
70
                {
 
71
                    Integer id = entry.getKey();
 
72
                    Table table = entry.getValue();
 
73
 
 
74
                    String p1, p2;
 
75
 
 
76
                    if(table.clientPlayer1==null || table.clientPlayer1.nick == null) p1 = "empty";
 
77
                    else p1 = table.clientPlayer1.nick;
 
78
 
 
79
                    if(table.clientPlayer2==null || table.clientPlayer2.nick == null) p2 = "empty";
 
80
                    else p2 = table.clientPlayer2.nick;
 
81
 
 
82
                    System.out.println("\t"+id+": "+p1+" vs "+p2);
 
83
                }
 
84
            }
 
85
            else if(str.equals("3")) //on/off server's communicats
 
86
            {
 
87
                if(server.isPrintEnable == false)
 
88
                {
 
89
                    server.isPrintEnable = true;
 
90
                    System.out.println("Komunikaty serwera zostały włączone");
 
91
                }
 
92
                else
 
93
                {
 
94
                    server.isPrintEnable = false;
 
95
                    System.out.println("Komunikaty serwera zostały wyłączone");
 
96
                }
 
97
            }
 
98
            else if(str.equals("4")) //exit
 
99
            {
 
100
                isOK = false;
 
101
            }
 
102
            else //bad commant
 
103
            {
 
104
                System.out.println("Nierozpoznane polecenie");
 
105
            }
 
106
        }
 
107
        System.exit(0);
 
108
    }
 
109
 
 
110
    public static String readString() //read string from console
 
111
    {
 
112
        int ch;
 
113
        StringBuffer sb = new StringBuffer();
 
114
        try
 
115
        {
 
116
            while ((ch = System.in.read()) != 10)
 
117
            {
 
118
                sb.append((char) ch);
 
119
            }
 
120
        } catch (IOException ex)
 
121
        {
 
122
            Logger.getLogger(Console.class.getName()).log(Level.SEVERE, null, ex);
 
123
        }
 
124
 
 
125
        return sb.toString();
 
126
    }
 
127
}