-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStagServer.java
More file actions
197 lines (165 loc) · 6.34 KB
/
StagServer.java
File metadata and controls
197 lines (165 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.io.*;
import java.net.*;
import java.util.*;
class StagServer
{
private ArrayList<Path> Paths = new ArrayList<>();
private ArrayList<Location> Locations;
private ArrayList<Action> actionsList = new ArrayList<>();
private ArrayList<Player> Players = new ArrayList<>();
public static void main(String args[])
{
if(args.length != 2) System.out.println("Usage: java StagServer <entity-file> <action-file>");
else new StagServer(args[0], args[1], 8888);
}
public StagServer(String entityFilename, String actionFilename, int portNumber)
{
try {
ServerSocket ss = new ServerSocket(portNumber);
System.out.println("Server Listening");
DotParser DotParser = new DotParser(entityFilename);
Locations = DotParser.parseEntities(Paths);
for(Path p: Paths) {
for(Location l : Locations) {
if(p.getStart().equals(l.getName())) {
l.setPathList(p);
}
}
}
ActionsParser AParser = new ActionsParser(actionFilename);
AParser.parseActions(actionsList);
while(true) acceptNextConnection(ss);
} catch(IOException ioe) {
System.err.println(ioe);
}
}
private void acceptNextConnection(ServerSocket ss)
{
try {
// Next line will block until a connection is received
Socket socket = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
processNextCommand(in, out);
out.close();
in.close();
socket.close();
} catch(IOException ioe) {
System.err.println(ioe);
}
}
private String parseLine(String s, boolean flag) {
String name;
if(!flag) {
name = s.substring(0, s.indexOf(':'));
} else {
name = s.substring(s.indexOf(':')+2);
}
return name;
}
private Player findPlayer(String name) {
Player player = null;
for(Player p : Players) {
if (p.getName().equals(name) && p.getDescription().equals("Player")) {
player = p;
}
}
return player;
}
private boolean playerAlreadyExists(String name) {
for(Location l : Locations) {
for(Characters c : l.getCharactersHere()) {
if(c.getName().equals(name) && c.getDescription().equals("Player")) {
return true;
}
}
}
return false;
}
private void checkStandardCommands(String line, String name, BufferedWriter out) throws IOException {
Player p = findPlayer(name);
Location loc = p.getLocation();
if(line.contains("inventory") || line.contains("inv")) {
p.getInventory(out);
}
executeGet(p, line, out, loc);
executeDrop(p, line, out);
executeGoTo(p, line, out, loc);
executeLook(line, out, loc);
}
private void executeGet(Player p, String line, BufferedWriter out, Location loc) throws IOException {
if(line.contains("get")) {
Iterator<Artifact> it = loc.getArtifacts().iterator();
while(it.hasNext()) {
Artifact artifact = it.next();
if(line.contains(artifact.getName())) {
p.addToInventory(loc.getArtifact(artifact.getName()), out);
return;
}
}
out.write("There is no artifact with that name at this location.\n");
}
}
private void executeDrop(Player p, String line, BufferedWriter out) throws IOException {
if(line.contains("drop")) {
Iterator<Artifact> it = p.returnInventory().iterator();
while(it.hasNext()) {
Artifact artifact = it.next();
if(line.contains(artifact.getName())) {
p.removeFromInventory(artifact, out);
return;
}
}
out.write("You don't have that item in your inventory.\n");
}
}
private void executeLook(String line, BufferedWriter out, Location loc) throws IOException {
if(line.contains("look") || line.equals("look")) {
loc.lookAround(out);
out.write(loc.getPaths() + "\n");
}
}
//execute our Go To command
private void executeGoTo(Player p, String line, BufferedWriter out, Location loc) throws IOException {
if(line.contains("goto")) {
for(Location location : Locations) {
if(line.contains(location.getName())) {
p.getLocation().getCharactersHere().remove(p);
p.setLocation(location);
location.addPlayer(p);
out.write("You are now in " + location.getName() + "\n");
return;
}
}
out.write("That location doesn't exist.\n");
out.write(loc.getPaths());
}
}
private void processNextCommand(BufferedReader in, BufferedWriter out) throws IOException
{
Player currentPlayer = null;
String line = in.readLine();
String name = parseLine(line, false);
String command = parseLine(line, true);
//Create new player if they're not already in the game, else set current player to our parsed command from client
if(!playerAlreadyExists(name)) {
Player p = new Player(name, "Player");
Locations.get(0).getCharactersHere().add(p);
Players.add(p);
p.setLocation(Locations.get(0));
currentPlayer = p;
} else {
for(Player p : Players) {
if(name.equals(p.getName())) {
currentPlayer = p;
}
}
}
System.out.println(command);
checkStandardCommands(command.toLowerCase(), name, out);
SpecialCommands sc = new SpecialCommands(currentPlayer);
sc.checkSpecialCommands(actionsList, Locations, command, out);
assert currentPlayer != null;
currentPlayer.checkHealth(out, Locations.get(0));
}
}