forked from nickgammon/tinymudserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
executable file
·239 lines (190 loc) · 6.22 KB
/
player.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
tinymudserver - an example MUD server
Author: Nick Gammon
http://www.gammon.com.au/
(C) Copyright Nick Gammon 2004. Permission to copy, use, modify, sell and
distribute this software is granted provided this copyright notice appears
in all copies. This software is provided "as is" without express or implied
warranty, and with no claim as to its suitability for any purpose.
*/
// standard library includes ...
#include <vector>
#include <algorithm>
#include <limits>
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <iterator>
#include <errno.h>
using namespace std;
#include "utils.h"
#include "constants.h"
#include "strings.h"
#include "player.h"
#include "room.h"
#include "globals.h"
/* find a player by name */
tPlayer * FindPlayer (const string & name)
{
tPlayerListIterator i =
find_if (playerlist.begin (), playerlist.end (), findPlayerName (name));
if (i == playerlist.end ())
return NULL;
else
return *i;
} /* end of FindPlayer */
// member function to find another playing, including myself
tPlayer * tPlayer::GetPlayer (istream & args, const string & noNameMessage, const bool & notme)
{
string name;
args >> name;
if (name.empty ())
throw runtime_error (noNameMessage);
tPlayer * p = this;
if (ciStringEqual (name, "me") || ciStringEqual (name, "self"))
p = this;
else
p = FindPlayer (name);
if (p == NULL)
throw runtime_error (MAKE_STRING ("Player " << tocapitals (name) << " is not connected."));
if (notme && p == this)
throw runtime_error ("You cannot do that to yourself.");
return p;
} // end of GetPlayer
void tPlayer::ProcessException ()
{
/* signals can cause exceptions, don't get too excited. :) */
cerr << "Exception on socket " << s << endl;
} /* end of tPlayer::ProcessException */
void tPlayer::Load ()
{
ifstream f ((PLAYER_DIR + playername + PLAYER_EXT).c_str (), ios::in);
if (!f)
throw runtime_error ("That player does not exist, type 'new' to create a new one.");
// read player details
f >> password;
f >> room;
f.ignore (numeric_limits<int>::max(), '\n'); // skip rest of this line
LoadSet (f, flags); // player flags (eg. can_shutdown)
} /* end of tPlayer::Load */
void tPlayer::Save ()
{
ofstream f ((PLAYER_DIR + playername + PLAYER_EXT).c_str (), ios::out);
if (!f)
{
cerr << "Could not write to file for player " << playername << endl;
return;
}
// write player details
f << password << endl;
f << room << endl;
copy (flags.begin (), flags.end (), ostream_iterator<string> (f, " "));
f << endl;
} /* end of tPlayer::Save */
void tPlayer::DoCommand (const string & command)
{
istringstream is (command);
ProcessCommand (this, is);
} /* end of tPlayer::Load */
// is flag set?
bool tPlayer::HaveFlag (const string & name)
{
return flags.find (name) != flags.end ();
} // end of NeedFlag
// flag must be set
void tPlayer::NeedFlag (const string & name)
{
if (!HaveFlag (name))
throw runtime_error ("You are not permitted to do that.");
} // end of NeedFlag
// flag must not be set
void tPlayer::NeedNoFlag (const string & name)
{
if (HaveFlag (name))
throw runtime_error ("You are not permitted to do that.");
} // end of NeedNoFlag
/* Here when there is outstanding data to be read for this player */
void tPlayer::ProcessRead ()
{
if (closing)
return; // once closed, don't handle any pending input
// I make it static to save allocating a buffer each time.
// Hopefully this function won't be called recursively.
static vector<char> buf (1000); // reserve 1000 bytes for reading into
int nRead = read (s, &buf [0], buf.size ());
if (nRead == -1)
{
if (errno != EWOULDBLOCK)
perror ("read from player");
return;
}
if (nRead <= 0)
{
close (s);
cerr << "Connection " << s << " closed" << endl;
s = NO_SOCKET;
DoCommand ("quit"); // tell others the s/he has left
return;
}
inbuf += string (&buf [0], nRead); /* add to input buffer */
/* try to extract lines from the input buffer */
for ( ; ; )
{
string::size_type i = inbuf.find ('\n');
if (i == string::npos)
break; /* no more at present */
string sLine = inbuf.substr (0, i); /* extract first line */
inbuf = inbuf.substr (i + 1, string::npos); /* get rest of string */
ProcessPlayerInput (this, Trim (sLine)); /* now, do something with it */
}
} /* end of tPlayer::ProcessRead */
/* Here when we can send stuff to the player. We are allowing for large
volumes of output that might not be sent all at once, so whatever cannot
go this time gets put into the list of outstanding strings for this player. */
void tPlayer::ProcessWrite ()
{
/* we will loop attempting to write all in buffer, until write blocks */
while (s != NO_SOCKET && !outbuf.empty ())
{
// send a maximum of 512 at a time
int iLength = min<int> (outbuf.size (), 512);
// send to player
int nWrite = write (s, outbuf.c_str (), iLength );
// check for bad write
if (nWrite < 0)
{
if (errno != EWOULDBLOCK )
perror ("send to player"); /* some other error? */
return;
}
// remove what we successfully sent from the buffer
outbuf.erase (0, nWrite);
// if partial write, exit
if (nWrite < iLength)
break;
} /* end of having write loop */
} /* end of tPlayer::ProcessWrite */
// functor for sending messages to all players
struct sendToPlayer
{
const string message;
const tPlayer * except;
const int room;
// ctor
sendToPlayer (const string & m, const tPlayer * e = NULL, const int r = 0)
: message (m), except (e), room (r) {}
// send to this player
void operator() (tPlayer * p)
{
if (p->IsPlaying () && p != except && (room == 0 || p->room == room))
*p << message;
} // end of operator()
}; // end of sendToPlayer
// send message to all connected players
// possibly excepting one (eg. the player who said something)
// possibly only in one room (eg. for saying in a room)
void SendToAll (const string & message, const tPlayer * ExceptThis, const int InRoom)
{
for_each (playerlist.begin (), playerlist.end (),
sendToPlayer (message, ExceptThis, InRoom));
} /* end of SendToAll */