package com square WeakLinkGUI import com square WeakLinkClient import

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.square.WeakLinkGUI;
import com.square.WeakLinkClient.*;
import com.square.WeakLinkServerSide.Question;
import com.square.WeakLinkServerSide.Request;
import com.trolltech.qt.core.QTimer;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QMessageBox;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class ChooseGameTypeMenu extends QMainWindow{
private Ui_ChooseGameTypeForm uiChooseGameTypeForm = new Ui_ChooseGameTypeForm();
private QMessageBox qMessageBox = new QMessageBox(this);
private QMessageBox.StandardButton cancelButton = QMessageBox.StandardButton.Cancel;
private boolean connectionFailed = false;
public volatile Thread myThread;
// public volatile RoundGameStage roundGameStage = null;
// public volatile RateGameStage rateGameStage = null;
public ChooseGameTypeMenu(){
uiChooseGameTypeForm.setupUi(this);
qMessageBox.addButton(cancelButton);
qMessageBox.setWindowTitle("The Weakest Link");
myThread = Thread.currentThread();
uiChooseGameTypeForm.gameWithPeopleButton.clicked.connect(this,"gameWithPeopleButtonClicked()");
uiChooseGameTypeForm.gmaeWithBotsButton.clicked.connect(this, "gameWithBotsButtonClicked()");
}
private void gameWithBotsButtonClicked(){
// RoundGameStage roundGameStage = new RoundGameStage();
//roundGameStage.waitForQuestionState();
// roundGameStage.show();
}
private void showCancelMessage(String msg){
qMessageBox.setText(msg);
qMessageBox.show();
}
// @Override
// public void close() {}
private void changeStateToRate() throws InterruptedException{
Thread.sleep(2000);
}
private void stopConnection(){ // Method called if button cancel would be pushed
if (!connectionFailed)
if (Main.client != null) {
Main.client.stop();
Printer.printLine("Connection stopped by user");
}
Main.client = null;
}
private void gameWithPeopleButtonClicked() {
showCancelMessage("Connecting to server...");
// waiting for 1 sec couse MessageBox haven't time to render
QTimer.singleShot(1000, this, "createClient()");
//qMessageBox.buttonClicked.connect(this, "stopConnection()");
QTimer.singleShot(2000, this, "close()");
}
private void createClient(){
Main.client = new Client("localhost", 888) { // Здесь нужно указать ip сервера и порт
@Override
public void onReceive(Request request) {
RoundGameStage roundGameStage = new RoundGameStage();
RateGameStage rateGameStage = new RateGameStage();
switch (request.cmd.toLowerCase()){
case "yourid":
GameShot.clientID = (Integer) request.data;
break;
case "start":
Printer.printLine("" + request.data);
GameShot.players = (ArrayList<Integer>) request.data;
for (Integer player : GameShot.players)
GameShot.activePlayers.add(player);
Printer.printLine("sdlas");
this.send("ready");
break;
case "round":
roundGameStage.waitForQuestionState();
roundGameStage.show();
this.send("ready");
break;
case "player":
GameShot.currentPlayer_ID = (int) request.data;
roundGameStage.paintCurPlayerLabel();
this.send("ready");
break;
case "bankenable":
roundGameStage.changeBankButton((boolean) request.data);
this.send("ready");
break;
case "question":
GameShot.question = (Question) request.data;
roundGameStage.answerState();
break;
case "check":
roundGameStage.checkAnswerState((int) request.data);
this.send("ready");
break;
case "cost":
GameShot.currentCostID = (int) request.data;
roundGameStage.changeCurrentCost();
this.send("ready");
break;
case "currentbank":
GameShot.bank = (Integer) request.data;
roundGameStage.changeCurrentBank();
this.send("ready");
break;
case "summarybank":
this.send("ready");
// change global bank
break;
case "rate":
try{
changeStateToRate();
roundGameStage.close();
} catch (InterruptedException ie)
{
Printer.printLine(ie.getMessage());
}
rateGameStage.init();
rateGameStage.rateState();
break;
case "drop":
RateInfo rateInfo =(RateInfo) request.data;
GameShot.rates = rateInfo.Rates;
GameShot.lastDropID = rateInfo.Weakest;
rateGameStage.showRateResults();
GameShot.activePlayers.remove(Integer.valueOf(GameShot.lastDropID));
try {
Thread.sleep(2000);
}catch (InterruptedException ie){
Printer.printLine(ie.getMessage());
}
rateGameStage.close();
this.send("ready");
roundGameStage.waitForQuestionState();
break;
default:
Printer.printLine("Unknown cmd");
break;
}
}
@Override
public void onConnect(boolean ok) {
qMessageBox.close();
if (ok){}
//showCancelMessage("Waiting for other players...");
else{
showCancelMessage("Couldn't connect to server.");
connectionFailed = true;
}
}
};
}
}