source: java-rpg-server/ServerThread.java

Last change on this file was 52f778b, checked in by dportnoy <dmp1488@…>, 14 years ago

Added necessary text files, better error logging, and allowed the struct file to specify where players spawn upon login.

  • Property mode set to 100644
File size: 7.1 KB
Line 
1import java.net.*;
2import java.util.*;
3
4/*
5 * This thread handles messages from clients.
6 */
7
8public class ServerThread extends Connection {
9 private LostHavenServer main;
10 private Player player;
11 private Client client;
12
13 public ServerThread(Socket socket, LostHavenServer main) {
14 super(socket, "ServerThread");
15 this.main = main;
16 }
17
18 protected void processMessage(MessageType type, String input) {
19 String user, pass;
20 Gender gender;
21 Job job;
22 String message;
23 String receiver = "";
24 String channel = "";
25
26 switch(type) {
27 case Chat:
28 if(input.indexOf("]") != -1 && (input.indexOf(">") == -1 || input.indexOf(">") > input.indexOf("]"))) {
29 channel = input.substring(0, input.indexOf("]"));
30 message = input.substring(input.indexOf("]")+1);
31 }else {
32 receiver = input.substring(0, input.indexOf(">"));
33 message = input.substring(input.indexOf(">")+1);
34 }
35
36 if(receiver.equals("")) {
37 main.updateLog("chatlog.txt", main.dateString() + " [" + channel + "] " + player.getName() + "> " + message);
38
39 if(channel.equals("None")) {
40 sendMessage(MessageType.Chat, player.getName() + "> " + message);
41 }else {
42 Iterator<String> iter = main.online.keySet().iterator();
43 String name;
44
45 while(iter.hasNext())
46 {
47 name = iter.next();
48 if(main.registered.get(name).getChannel().equals(channel)) {
49 main.online.get(name).getOut().println(MessageType.Chat);
50 main.online.get(name).getOut().println(player.getName() + "> " + message);
51 }
52 }
53 }
54 }else {
55 if(!main.registered.containsKey(receiver)) {
56 sendMessage(MessageType.Chat, "Player " + receiver + " does not exist.");
57 }else if(!main.online.containsKey(receiver)) {
58 sendMessage(MessageType.Chat, "Player " + receiver + " is not online.");
59 }else if(receiver.equals(player.getName())) {
60 sendMessage(MessageType.Chat, "Why are you sending a message to yourself?");
61 }else {
62 main.online.get(receiver).getOut().println(MessageType.Chat);
63 main.online.get(receiver).getOut().println("Private message from " + player.getName() + "> " + message);
64 sendMessage(MessageType.Chat, "Private message to " + receiver + "> " + message);
65
66 main.updateLog("chatlog.txt", main.dateString() + " Private message from " + player.getName() + " to " + receiver + "> " + message);
67 }
68 }
69 break;
70 case Channel:
71 player.setChannel(input);
72 sendMessage(MessageType.Channel, "");
73 break;
74 case Login:
75 user = input.substring(0, input.indexOf(";"));
76 input = input.substring(input.indexOf(";")+1);
77 pass = input;
78
79 if(!main.registered.containsKey(user))
80 sendMessage(MessageType.Login, "No such player exists");
81 else if(!main.registered.get(user).getPass().equals(pass))
82 sendMessage(MessageType.Login, "Incorrect password");
83 else if(main.online.containsKey(user))
84 sendMessage(MessageType.Login, "This player is already logged on");
85 else {
86 player = main.registered.get(user);
87 client.setTimeLoggedOn(Calendar.getInstance());
88 main.updateLog("playerlog.txt", "Player " + user + " logged in on " + main.dateString());
89 sendLoadInfo();
90 main.addOnlineList(user, client);
91 main.sendAll(MessageType.PlayerJoined, "Player " + player.getName() + " logged on");
92 player.setLoc(main.map.startingLoc);
93 player.setTarget(new Point(player.getLoc().getX(), player.getLoc().getY()));
94 }
95 break;
96 case Create:
97 user = input.substring(0, input.indexOf(";"));
98 input = input.substring(input.indexOf(";")+1);
99 pass = input.substring(0, input.indexOf(";"));
100 input = input.substring(input.indexOf(";")+1);
101 gender = Gender.valueOf(input.substring(0, input.indexOf(";")));
102 input = input.substring(input.indexOf(";")+1);
103 job = Job.valueOf(input);
104
105 if(main.registered.containsKey(user))
106 sendMessage(MessageType.Create, "Username already exists");
107 else {
108 player = new Player(main.orderedReg.size()+1, user, pass);
109 player.setGender(gender);
110 player.createJob(job);
111 main.addRegList(user, player);
112 main.updateLog("playerlog.txt", "Player " + user + " created on " + main.dateString());
113 sendMessage(MessageType.Create, "Account created successfully");
114 main.sendAll(MessageType.Registered, player.getId() + ";" + player.getName() + ";" + player.getGender()+ ";" + false);
115 }
116 break;
117 case Movement:
118 int y = Integer.valueOf(input.substring(0, input.indexOf(",")));
119 int x = Integer.valueOf(input.substring(input.indexOf(",")+1));
120
121 player.setTarget(new Point(x, y));
122 }
123 }
124
125 private void sendRegisteredPlayers() {
126 String[] reg;
127 Player p;
128 String str;
129
130 reg = (String[])main.orderedReg.toArray(new String[0]);
131 for(int x=0; x<main.orderedReg.size(); x++) {
132 p = main.registered.get(reg[x]);
133
134 str = p.getId() + ";" + p.getName() + ";" + p.getGender() + ";" + main.online.containsKey(p.getName());
135 sendMessage(MessageType.Registered, str);
136 }
137 }
138
139 private void sendLoadInfo() {
140 sendMessage(MessageType.LoadStart, Integer.toString(main.orderedReg.size()+main.map.getLength()*main.map.getHeight()+3));
141 sendRegisteredPlayers();
142 sendMessage(MessageType.LoadMap, main.map.getLength()+"x"+main.map.getHeight());
143
144 for(int x=0; x<main.map.getLength(); x++) {
145 for(int y=0; y<main.map.getHeight(); y++) {
146 sendMessage(MessageType.LoadMap, x+","+y+" "+main.map.getLoc(x, y).getLand().getType().toString()+" "+main.map.getLoc(x, y).getStruct().getType().toString());
147 }
148 }
149
150 sendMessage(MessageType.LoadEnd, "");
151 }
152
153 protected void connectionStart() {
154
155 }
156
157 protected void connectionSuccess() {
158 client = new Client(getOut());
159 }
160
161 protected void connectionFailure() {
162
163 }
164
165 protected void connectionBreak() {
166 if(player != null && main.online.containsKey(player.getName())) {
167 main.removeOnlineList(player.getName());
168 main.updateLog("playerlog.txt", "Player " + player.getName() + " disconnected unexpectedly on " + main.dateString());
169 main.sendAll(MessageType.PlayerLeft, "Player " + player.getName() + " disconnected unexpectedly");
170 }
171 }
172
173 protected void peerDisconnect() {
174 if(player != null && main.online.containsKey(player.getName())) {
175 main.removeOnlineList(player.getName());
176 main.updateLog("playerlog.txt", "Player " + player.getName() + " logged off on " + main.dateString());
177 main.sendAll(MessageType.PlayerLeft, "Player " + player.getName() + " logged off");
178 }
179 }
180
181 protected void connectionEnd() {
182
183 }
184}
Note: See TracBrowser for help on using the repository browser.