-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArena.java
More file actions
163 lines (121 loc) · 3.23 KB
/
Arena.java
File metadata and controls
163 lines (121 loc) · 3.23 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
import java.rmi.*;
import java.rmi.server.*;
import java.util.Date;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
/**
* Remote Class
*/
public class Arena extends UnicastRemoteObject implements ArenaInterface {
private String message;
private static Integer sum;
protected class client_info {
Date start;
Date logout;
int numCalls;
String name ;
int hitpoints;
int posX;
int posY;
};
protected class floor {
int width;
int length;
List obstacles; // rocks? pits? mines?
List bonuses;
}
HashMap<Long, client_info> userMap = new HashMap<Long, client_info> ();
/**
* Construct a remote object
* @param msg the message of the remote object, such as "eat hot lead!".
* @exception RemoteException if the object handle cannot be constructed.
*/
public Arena (String msg) throws RemoteException {
message = msg;
sum=4;
}
/**
* Random long used to identify clients calling this remote
* object's methods. key must be passed to 'secure' methods.
*/
protected long getKey () {
double z=Math.random () * 1000000000;
long zz = Math.round ( z );
return zz;
}
/**
* Return a key that the client needs to pass every time they make a
* call.
*
*/
public long register () throws RemoteException
{
Long key = getKey();
client_info info = new client_info ();
Calendar now = Calendar.getInstance ();
info.start = now.getTime ();
userMap.put(key, info);
tank_lib.log ("Arena : returning key : " + key );
return key;
}
public int count() throws RemoteException {
return userMap.size();
}
protected void update (long key, client_info tainted) {
tank_lib.log ("arena: updating key for : " + tainted.name );
if ( userMap.containsKey ( key) ) {
// should replace record with this key
userMap.put ( key,tainted);
}
else {
tank_lib.log ("huh? code just tried to save a key that doesn't exist");
}
}
public void dump () throws RemoteException {
Iterator it = userMap.keySet().iterator();
while(it.hasNext()) {
Long currL = (Long) it.next();
System.out.print(currL + " " );
System.out.println(userMap.get (currL) );
}
}
protected client_info validate ( Long key ) {
tank_lib.log (" arena : validate : ");
if ( userMap.containsKey ( key) ) {
return ( userMap.get(key) );
}
else {
tank_lib.log (" bad key");
return null;
}
}
public int setName ( long key, String newname ) throws RemoteException {
client_info ci = this.validate ( key );
if ( ci == null ) {
System.out.println (" this bozo tried to pass: " + key);
return -1;
}
System.out.println (" setting name for tank to : " + newname);
ci.name=newname;
update (key, ci );
return 1;
}
public int move ( long key, String cmd ) throws RemoteException {
// first check if long is valid
//
client_info ci = this.validate ( key );
if ( ci == null ) {
System.out.println (" intruder alert");
System.out.println (" this bozo tried to pass: " + cmd);
return -1;
}
else {
System.out.println (" welcome : " + ci.name);
System.out.println (" cmd passed: " + cmd);
}
return 0;
}
}