1 | import gamegui.*;
|
---|
2 |
|
---|
3 | public class ClientThread extends Connection {
|
---|
4 | private LostHavenClient main;
|
---|
5 |
|
---|
6 | public ClientThread(String ip, int port, LostHavenClient main) {
|
---|
7 | super(ip, port, "ClientThread");
|
---|
8 |
|
---|
9 | this.main = main;
|
---|
10 | }
|
---|
11 |
|
---|
12 | protected void processMessage(MessageType type, String input) {
|
---|
13 | Player p;
|
---|
14 | int id;
|
---|
15 | String name;
|
---|
16 | Gender gender;
|
---|
17 | boolean online;
|
---|
18 | ProgressBar bar;
|
---|
19 |
|
---|
20 | switch(type) {
|
---|
21 | case Chat:
|
---|
22 | main.mtxChat.append(input);
|
---|
23 | break;
|
---|
24 | case Channel:
|
---|
25 | main.changePending = false;
|
---|
26 | main.player.setChannel(main.mnuChannels.getSelected());
|
---|
27 | main.mtxChat.append("Switched to channel " + main.mnuChannels.getSelected());
|
---|
28 | break;
|
---|
29 | case Login:
|
---|
30 | main.showMessage(input);
|
---|
31 | closeConnection();
|
---|
32 | break;
|
---|
33 | case Create:
|
---|
34 | main.showMessage(input);
|
---|
35 | closeConnection();
|
---|
36 | break;
|
---|
37 | case LoadStart:
|
---|
38 | main.auxState = AuxState.Loading;
|
---|
39 | bar = ((ProgressBar)main.wndProgress.getMember("bar"));
|
---|
40 | bar.setMax(Integer.valueOf(input));
|
---|
41 | bar.setCurrent(1);
|
---|
42 | break;
|
---|
43 | case LoadEnd:
|
---|
44 | bar = ((ProgressBar)main.wndProgress.getMember("bar"));
|
---|
45 | bar.setCurrent(bar.getCurrent()+1);
|
---|
46 | main.gameState = GameState.GameTavern;
|
---|
47 | main.auxState = AuxState.None;
|
---|
48 | main.wndLogin.clear();
|
---|
49 | break;
|
---|
50 | case LoadMap:
|
---|
51 | if(main.map == null) {
|
---|
52 | int x = Integer.valueOf(input.substring(0, input.indexOf("x")));
|
---|
53 | int y = Integer.valueOf(input.substring(input.indexOf("x")+1));
|
---|
54 | main.map = new Map(x, y);
|
---|
55 | }else {
|
---|
56 | int x = Integer.valueOf(input.substring(0, input.indexOf(",")));
|
---|
57 | input = input.substring(input.indexOf(",")+1);
|
---|
58 | int y = Integer.valueOf(input.substring(0, input.indexOf(" ")));
|
---|
59 | input = input.substring(input.indexOf(" ")+1);
|
---|
60 | LandType landType = LandType.valueOf(input.substring(0, input.indexOf(" ")));
|
---|
61 | input = input.substring(input.indexOf(",")+1);
|
---|
62 | StructureType structType = StructureType.valueOf(input.substring(input.indexOf(" ")+1));
|
---|
63 |
|
---|
64 | main.map.setLoc(new Location(main.landMap.get(landType), main.structMap.get(structType)), x, y);
|
---|
65 | }
|
---|
66 | bar = ((ProgressBar)main.wndProgress.getMember("bar"));
|
---|
67 | bar.setCurrent(bar.getCurrent()+1);
|
---|
68 | break;
|
---|
69 | case Registered:
|
---|
70 | id = Integer.parseInt(input.substring(0, input.indexOf(";")));
|
---|
71 | input = input.substring(input.indexOf(";")+1);
|
---|
72 | name = input.substring(0, input.indexOf(";"));
|
---|
73 | input = input.substring(input.indexOf(";")+1);
|
---|
74 | gender = Gender.valueOf(input.substring(0, input.indexOf(";")));
|
---|
75 | input = input.substring(input.indexOf(";")+1);
|
---|
76 | online = input.equals("true");
|
---|
77 |
|
---|
78 | p = new Player(id, name, "", gender);
|
---|
79 |
|
---|
80 | if(!main.registered.containsKey(name)) {
|
---|
81 | main.registered.put(name, p);
|
---|
82 | main.orderedReg.add(p);
|
---|
83 | main.lstRegistered.getList().clear();
|
---|
84 | for(int x=0; x<main.orderedReg.size(); x++)
|
---|
85 | main.lstRegistered.getList().add(main.new Registered((Player)main.orderedReg.toArray()[x]));
|
---|
86 | if(online) {
|
---|
87 | main.orderedOnline.add(p);
|
---|
88 | main.lstOnline.getList().clear();
|
---|
89 | for(int x=0; x<main.orderedOnline.size(); x++)
|
---|
90 | main.lstOnline.getList().add(main.new Online((Player)main.orderedOnline.toArray()[x]));
|
---|
91 | }
|
---|
92 | }
|
---|
93 | if(main.auxState == AuxState.Loading) {
|
---|
94 | bar = ((ProgressBar)main.wndProgress.getMember("bar"));
|
---|
95 | bar.setCurrent(bar.getCurrent()+1);
|
---|
96 | }
|
---|
97 | break;
|
---|
98 | case PlayerJoined:
|
---|
99 | input = input.substring(input.indexOf(" ")+1);
|
---|
100 | name = input.substring(0, input.indexOf(" "));
|
---|
101 |
|
---|
102 | if(!main.orderedOnline.contains(main.registered.get(name))) {
|
---|
103 | main.orderedOnline.add(main.registered.get(name));
|
---|
104 | main.lstOnline.getList().clear();
|
---|
105 | for(int x=0; x<main.orderedOnline.size(); x++)
|
---|
106 | main.lstOnline.getList().add(main.new Online((Player)main.orderedOnline.toArray()[x]));
|
---|
107 | }
|
---|
108 | break;
|
---|
109 | case PlayerLeft:
|
---|
110 | input = input.substring(input.indexOf(" ")+1);
|
---|
111 | name = input.substring(0, input.indexOf(" "));
|
---|
112 |
|
---|
113 | if(main.orderedOnline.contains(main.registered.get(name))) {
|
---|
114 | main.orderedOnline.remove(main.registered.get(name));
|
---|
115 | main.lstOnline.getList().clear();
|
---|
116 | for(int x=0; x<main.orderedOnline.size(); x++)
|
---|
117 | main.lstOnline.getList().add(main.new Online((Player)main.orderedOnline.toArray()[x]));
|
---|
118 | }
|
---|
119 | break;
|
---|
120 | case Movement:
|
---|
121 | String strName = input.substring(0, input.indexOf(" "));
|
---|
122 | input = input.substring(input.indexOf(" ")+1);
|
---|
123 | int y = Integer.valueOf(input.substring(0, input.indexOf(",")));
|
---|
124 | int x = Integer.valueOf(input.substring(input.indexOf(",")+1));
|
---|
125 |
|
---|
126 | main.registered.get(strName).setLoc(new Point(x, y));
|
---|
127 | if(main.player.getName().equals(strName))
|
---|
128 | main.player.setLoc(new Point(x, y));
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | protected void connectionStart() {
|
---|
134 | main.auxState = AuxState.Connecting;
|
---|
135 | }
|
---|
136 |
|
---|
137 | protected void connectionSuccess() {
|
---|
138 | String user, pass;
|
---|
139 | Gender gender;
|
---|
140 | Job job;
|
---|
141 |
|
---|
142 | switch(main.gameState) {
|
---|
143 | case Login:
|
---|
144 | user = ((Textbox)main.wndLogin.getMember("user")).getText();
|
---|
145 | pass = ((Textbox)main.wndLogin.getMember("pass")).getText();
|
---|
146 |
|
---|
147 | sendMessage(MessageType.Login, user + ";" + pass);
|
---|
148 | break;
|
---|
149 | case CreateAccount:
|
---|
150 | user = ((Textbox)main.wndCreateAccount.getMember("user")).getText();
|
---|
151 | pass = ((Textbox)main.wndCreateAccount.getMember("pass")).getText();
|
---|
152 | gender = Gender.valueOf(main.rdgGenderSelection.getButton(main.rdgGenderSelection.getSelected()).getLabel());
|
---|
153 | job = main.player.getJob();
|
---|
154 |
|
---|
155 | sendMessage(MessageType.Create, user + ";" + pass + ";" + gender + ";" + job);
|
---|
156 | break;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | protected void connectionFailure() {
|
---|
161 | main.showMessage("Server not found");
|
---|
162 | }
|
---|
163 |
|
---|
164 | protected void connectionBreak() {
|
---|
165 | main.showMessage("Your connection was interrupted");
|
---|
166 | main.gameState = GameState.Main;
|
---|
167 | }
|
---|
168 |
|
---|
169 | protected void peerDisconnect() {
|
---|
170 | main.showMessage("The server disconnected");
|
---|
171 | main.gameState = GameState.Main;
|
---|
172 | }
|
---|
173 |
|
---|
174 | protected void connectionEnd() {
|
---|
175 | main.registered.clear();
|
---|
176 | main.orderedReg.clear();
|
---|
177 | main.orderedOnline.clear();
|
---|
178 | main.map = null;
|
---|
179 | main.wndTavern.clear();
|
---|
180 | }
|
---|
181 | }
|
---|