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