source: java-rpg-server/LostHavenServer.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: 9.1 KB
Line 
1import java.net.*;
2import java.io.*;
3import java.text.*;
4import java.util.*;
5//import java.util.concurrent.*;
6import java.awt.image.*;
7
8/*
9 * This class initializes the server and then waits for a connection from the portal.
10 */
11
12public class LostHavenServer {
13 public HashMap<String, Player> registered;
14 public HashMap<String, Client> online;
15 public PriorityQueue<String> orderedReg;
16 public PriorityQueue<String> orderedOnline;
17 public HashMap<LandType, Land> landMap;
18 public HashMap<StructureType, Structure> structMap;
19 public Map map;
20
21 public boolean running = false;
22
23 private ServerSocket serverSocket;
24 private ServerSocket portalSocket;
25 public ProcessingThread process;
26
27 public LostHavenServer() {
28 Runtime.getRuntime().addShutdownHook(new ShutdownHook(this));
29 online = new HashMap<String, Client>();
30 registered = new HashMap<String, Player>();
31 orderedReg = new PriorityQueue<String>(11, new PlayerComparator());
32 orderedOnline = new PriorityQueue<String>(11, new PlayerComparator());
33 loadRegistered();
34 loadMap();
35 map = new Map("mapInfo.txt", "structInfo.txt", landMap, structMap);
36 updateLog("serverlog.txt", "Application opened on " + dateString());
37
38 try {
39 portalSocket = new ServerSocket(5829);
40 while(true) {
41 new PortalInterfaceThread(portalSocket.accept(), this).start();
42 }
43 }catch(SocketException se) {
44 se.printStackTrace();
45 }catch(IOException ioe) {
46 ioe.printStackTrace();
47 }
48 }
49
50 public void start() {
51 try {
52 if(!running) {
53 running = true;
54 process = new ProcessingThread(this);
55 process.start();
56 serverSocket = new ServerSocket(5729);
57 new ClientListener(serverSocket, this).start();
58 updateLog("serverlog.txt", "Server started on " + dateString());
59 }
60 }catch (IOException ioe) {
61 ioe.printStackTrace();
62 }
63 }
64
65 public void stop() {
66 try {
67 if(running) {
68 running = false;
69 saveRegistered();
70 serverSocket.close();
71 updateLog("serverlog.txt", "Server shut down on " + dateString());
72 }
73 }catch (IOException ioe) {
74 ioe.printStackTrace();
75 }
76 }
77
78 public void addRegList(String str, Player player) {
79 registered.put(str, player);
80 orderedReg.add(str);
81 }
82
83 public void addOnlineList(String str, Client client) {
84 online.put(str, client);
85 orderedOnline.add(str);
86 }
87
88 public void removeOnlineList(String str) {
89 orderedOnline.remove(str);
90 online.remove(str);
91 }
92
93 public void sendAll(MessageType type, String s)
94 {
95 Iterator<Client> iter = online.values().iterator();
96 Client cur;
97
98 while(iter.hasNext())
99 {
100 cur = iter.next();
101 cur.getOut().println(type);
102 cur.getOut().println(s);
103 }
104 }
105
106 private void loadRegistered() {
107 BufferedReader in = null;
108 Player player;
109 int id;
110 String name, pass;
111
112 registered.clear();
113 online.clear();
114 orderedReg.clear();
115 orderedOnline.clear();
116
117 try {
118 in = new BufferedReader(new FileReader("chars.txt"));
119
120 while(in.ready()) {
121 id = Integer.parseInt(in.readLine());
122 name = in.readLine();
123 pass = in.readLine();
124 player = new Player(id, name, pass);
125
126 player.setGender(Gender.valueOf(in.readLine()));
127 player.setSpeed(Integer.parseInt(in.readLine()));
128 player.setSpeed(6);
129 player.setAttackSpeed(Integer.parseInt(in.readLine()));
130 player.setJob(Job.valueOf(in.readLine()));
131 player.setLevel(Integer.parseInt(in.readLine()));
132 player.setStrength(Integer.parseInt(in.readLine()));
133 player.setDexterity(Integer.parseInt(in.readLine()));
134 player.setConstitution(Integer.parseInt(in.readLine()));
135 player.setCharisma(Integer.parseInt(in.readLine()));
136 player.setWisdom(Integer.parseInt(in.readLine()));
137 player.setIntelligence(Integer.parseInt(in.readLine()));
138 player.setDamage(Integer.parseInt(in.readLine()));
139 player.setExperience(Integer.parseInt(in.readLine()));
140 player.setHitpoints(Integer.parseInt(in.readLine()));
141 player.setMaxHitpoints(Integer.parseInt(in.readLine()));
142 player.setManapoints(Integer.parseInt(in.readLine()));
143 player.setMaxManapoints(Integer.parseInt(in.readLine()));
144 player.setGold(Integer.parseInt(in.readLine()));
145
146 addRegList(name, player);
147 }
148
149 in.close();
150 } catch(IOException ioe) {
151 ioe.printStackTrace();
152 }
153 }
154
155 private void loadMap() {
156 landMap = new HashMap<LandType, Land>();
157 structMap = new HashMap<StructureType, Structure>();
158 BufferedImage nullImg = null;
159
160 landMap.put(LandType.Ocean, new Land(LandType.Ocean, nullImg, false));
161 landMap.put(LandType.Grass, new Land(LandType.Grass, nullImg, true));
162
163 structMap.put(StructureType.None, new Structure(StructureType.None, nullImg, false));
164 structMap.put(StructureType.BlueOrb, new Structure(StructureType.BlueOrb, nullImg, false));
165 structMap.put(StructureType.Cave, new Structure(StructureType.Cave, nullImg, false));
166 structMap.put(StructureType.Gravestone, new Structure(StructureType.Gravestone, nullImg, false));
167 structMap.put(StructureType.GraveyardFence1, new Structure(StructureType.GraveyardFence1, nullImg, false));
168 structMap.put(StructureType.GraveyardFence2, new Structure(StructureType.GraveyardFence2, nullImg, false));
169 structMap.put(StructureType.PicketFence1, new Structure(StructureType.PicketFence1, nullImg, false));
170 structMap.put(StructureType.PicketFence2, new Structure(StructureType.PicketFence2, nullImg, false));
171 structMap.put(StructureType.Hut, new Structure(StructureType.Hut, nullImg, false));
172 structMap.put(StructureType.WitchHut, new Structure(StructureType.WitchHut, nullImg, false));
173 structMap.put(StructureType.Tent, new Structure(StructureType.Tent, nullImg, false));
174 structMap.put(StructureType.LargeTent, new Structure(StructureType.LargeTent, nullImg, false));
175 structMap.put(StructureType.House, new Structure(StructureType.House, nullImg, false));
176 structMap.put(StructureType.Tree, new Structure(StructureType.Tree, nullImg, false));
177 structMap.put(StructureType.BlueOrb, new Structure(StructureType.BlueOrb, nullImg, false));
178 structMap.put(StructureType.RedOrb, new Structure(StructureType.RedOrb, nullImg, false));
179 structMap.put(StructureType.LoginPedestal, new Structure(StructureType.LoginPedestal, nullImg, true));
180 structMap.put(StructureType.RejuvenationPedestal, new Structure(StructureType.RejuvenationPedestal, nullImg, true));
181 structMap.put(StructureType.LifePedestal, new Structure(StructureType.LifePedestal, nullImg, true));
182 structMap.put(StructureType.ManaPedestal, new Structure(StructureType.ManaPedestal, nullImg, true));
183 }
184
185 private void saveRegistered() {
186 PrintWriter out = null;
187 Iterator<String> iter = orderedReg.iterator();
188 Player cur;
189
190 try {
191 out = new PrintWriter(new FileWriter("chars.txt"));
192 } catch(IOException ioe) {
193 ioe.printStackTrace();
194 }
195
196 while(iter.hasNext())
197 {
198 cur = registered.get(iter.next());
199 out.println(cur.getId());
200 out.println(cur.getName());
201 out.println(cur.getPass());
202 out.println(cur.getGender());
203 out.println(cur.getSpeed());
204 out.println(cur.getAttackSpeed());
205 out.println(cur.getJob());
206 out.println(cur.getLevel());
207 out.println(cur.getStrength());
208 out.println(cur.getDexterity());
209 out.println(cur.getConstitution());
210 out.println(cur.getCharisma());
211 out.println(cur.getWisdom());
212 out.println(cur.getIntelligence());
213 out.println(cur.getDamage());
214 out.println(cur.getExperience());
215 out.println(cur.getHitpoints());
216 out.println(cur.getMaxHitpoints());
217 out.println(cur.getManapoints());
218 out.println(cur.getMaxManapoints());
219 out.println(cur.getGold());
220 }
221
222 out.close();
223 }
224
225 public void updateLog(String fileName, String message) {
226 try {
227 System.out.println(message);
228 PrintWriter out = new PrintWriter(new FileWriter(fileName, true));
229 out.println(message);
230 out.close();
231 } catch(IOException ioe) {
232 ioe.printStackTrace();
233 }
234 }
235
236 public String dateString() {
237 return new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new Date());
238 }
239
240 public static void main(String[] args) {
241 try {
242 PrintStream st = new PrintStream(new FileOutputStream("err.txt",true));
243 System.setErr(st);
244 System.setOut(st);
245
246 new LostHavenServer();
247 }catch (Exception e) {
248 e.printStackTrace();
249 }
250 System.exit(0);
251 }
252
253 private class PlayerComparator implements Comparator<String> {
254 public int compare(String str1, String str2) {
255 return registered.get(str1).getId() - registered.get(str2).getId();
256 }
257 }
258
259 private class ShutdownHook extends Thread {
260 LostHavenServer server;
261
262 public ShutdownHook(LostHavenServer server) {
263 this.server = server;
264 }
265
266 public void run() {
267 server.stop();
268 updateLog("serverlog.txt", "Application closed on " + dateString());
269 }
270 }
271}
Note: See TracBrowser for help on using the repository browser.