source: java-rpg-client/ClientThread.java

Last change on this file was 67f5126, checked in by dportnoy <dmp1488@…>, 17 years ago

[svn r26]

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