Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

#!/usr/bin/env python3 

 

import linersock 

import multiprocessing, queue 

import logging, logging.handlers 

from .errors import * 

from .theater import * 

 

DEFAULT_HOST = 'localhost' 

DEFAULT_PORT = 53351 

 

class ClientConnectionStage(Stage): 

 

def __init__(self, world, gui_actor, host, port): 

super().__init__() 

self.world = world 

self.gui_actor = gui_actor 

self.host = host 

self.port = port 

self.pipe = None 

self.client = linersock.Client( 

host, port, callback=self.on_connection_established) 

 

def on_update_stage(self, dt): 

self.client.connect() 

try: 

self.gui.on_refresh_gui() 

except AttributeError: 

pass 

 

def on_connection_established(self, pipe): 

self.pipe = pipe 

self.exit_stage() 

 

def on_exit_stage(self): 

game_stage = MultiplayerClientGameStage( 

self.world, self.gui_actor, self.pipe) 

game_stage.successor.successor = PostgameSplashStage() 

self.successor = game_stage 

 

 

class ServerConnectionStage(Stage): 

 

def __init__(self, world, referee, num_clients, ai_actors=None, 

host=DEFAULT_HOST, port=DEFAULT_PORT): 

super().__init__() 

self.world = world 

self.referee = referee 

self.ai_actors = ai_actors or [] 

self.host = host 

self.port = port 

self.pipes = [] 

self.greetings = [] 

self.server = linersock.Server( 

host, port, num_clients, self.on_clients_connected) 

 

def on_enter_stage(self): 

self.server.open() 

 

def on_update_stage(self, dt): 

if not self.server.finished(): 

self.server.accept() 

else: 

self.exit_stage() 

 

def on_clients_connected(self, pipes): 

self.pipes += pipes 

 

def on_exit_stage(self): 

self.successor = MultiplayerServerGameStage( 

self.world, self.referee, self.ai_actors, self.pipes) 

 

 

class PostgameSplashStage(Stage): 

""" 

Until the player closes the window, keep it as it was when the game ended. 

""" 

 

def on_update_stage(self, dt): 

try: 

self.gui.on_refresh_gui() 

except AttributeError: 

self.exit_stage() 

 

 

class ProcessPool: 

""" 

Manage a group of processes running instances of the game loop. 

 

This class wraps around the basic multiprocessing functionality available  

in the python standard library. There are two nice features provided by  

this class. The first is that log messages generated in the subprocesses  

are automatically relayed to the main process, where they are reported  

with the name of the original process included and without any mangling due  

to race conditions. The second is that exceptions, which are usually  

silently ignored in subprocesses, are also relayed to the main process and  

re-raised. 

 

This class is actually not specific to the game engine at all, so in  

principle it could be moved into it's own library. I decided not to do  

that because I can't think of any other scenario where I would want the  

functionality that this class offers, but maybe I'll think of one later. 

""" 

 

def __init__(self, time_limit=None, frame_rate=30): 

self.log_queue = multiprocessing.Queue() 

self.exception_queue = multiprocessing.Queue() 

self.time_limit = time_limit 

self.elapsed_time = 0 

self.frame_rate = frame_rate 

 

def __enter__(self): 

return self 

 

def __exit__(self, *args): 

self._run_supervisor() 

 

 

def start(self, name, worker, *args, **kwargs): 

process = multiprocessing.Process( 

name=name, target=self._run_worker, 

args=(name, worker) + args, kwargs=kwargs, 

) 

process.start() 

 

def _run_worker(self, name, worker, *args, **kwargs): 

 

# Configure all logging message generated by this process to go into a  

# queue that will be read and handled by the supervisor. 

 

handler = logging.handlers.QueueHandler(self.log_queue) 

logging.root.addHandler(handler) 

 

# Catch any exceptions generated by the worker and report them to the  

# supervisor. This is important, because otherwise they would be  

# silently ignored. 

 

try: 

worker(*args, **kwargs) 

except Exception as exception: 

self.exception_queue.put_nowait(exception) 

 

def _run_supervisor(self): 

""" 

Poll the queues that the worker can use to communicate with the  

supervisor, until all the workers are done and all the queues are  

empty. Handle messages as they appear. 

""" 

import time 

 

still_supervising = lambda: ( 

multiprocessing.active_children() 

or not self.log_queue.empty() 

or not self.exception_queue.empty()) 

 

try: 

while still_supervising(): 

# When a log message is received, make a logger with the same  

# name in this process and use it to re-log the message. It  

# will get handled in this process. 

 

try: 

record = self.log_queue.get_nowait() 

logger = logging.getLogger(record.name) 

logger.handle(record) 

except queue.Empty: 

pass 

 

# When an exception is received, immediately re-raise it. 

 

try: 

exception = self.exception_queue.get_nowait() 

except queue.Empty: 

pass 

else: 

raise exception 

 

# Sleep for a little bit, and make sure that the workers haven't  

# outlived their time limit. 

 

time.sleep(1/self.frame_rate) 

self.elapsed_time += 1/self.frame_rate 

 

if self.time_limit and self.elapsed_time > self.time_limit: 

raise RuntimeError("timeout") 

 

# Make sure the workers don't outlive the supervisor, no matter how the  

# polling loop ended (e.g. normal execution or an exception). 

 

finally: 

for process in multiprocessing.active_children(): 

process.terminate() 

 

 

class MultiplayerDebugger: 

""" 

Simultaneously plays any number of different game theaters, executing each  

theater in its own process. This greatly facilitates the debugging and  

testing multiplayer games. 

""" 

 

def __init__(self, world_cls, referee_cls, gui_cls, gui_actor_cls, 

num_guis=2, ai_actor_cls=None, num_ais=0, theater_cls=PygletTheater, 

host=DEFAULT_HOST, port=DEFAULT_PORT, log_format= 

'%(levelname)s: %(processName)s: %(name)s: %(message)s'): 

 

# Members of this class have to be pickle-able, because this object  

# will be pickled and sent to every process that gets started. That's  

# why all the game objects are stored as classes (or factories) rather  

# than instances. Even though some of the game objects can be pickled,  

# none of them are meant to be and avoiding it reduces the risk that  

# things will break for strange reasons. The game objects themselves  

# are instantiated in the worker processes, which is how it would  

# happen if the user just rame multiple instances of the game anyway. 

 

self.theater_cls = theater_cls 

self.world_cls = world_cls 

self.referee_cls = referee_cls 

self.gui_cls = gui_cls 

self.gui_actor_cls = gui_actor_cls 

self.num_guis = num_guis 

self.ai_actor_cls = ai_actor_cls 

self.num_ais = num_ais 

self.host = host 

self.port = port 

self.log_format = log_format 

 

def play(self): 

# Configure the logging system to print to stderr and include the  

# process name in all of its messages. 

 

handler = logging.StreamHandler() 

formatter = logging.Formatter(self.log_format) 

handler.setFormatter(formatter) 

logging.root.addHandler(handler) 

 

# Run the server and the client (each in its own process). 

 

with ProcessPool() as pool: 

pool.start("Server", self.play_server) 

for i in range(self.num_guis): 

pool.start("Client #%d" % i, self.play_client) 

 

def play_server(self): 

# Defer instantiation of all the game objects until we're inside our  

# own process, to avoid having to pickle and unpickle things that  

# should be pickled. 

 

theater = self.theater_cls() 

theater.initial_stage = ServerConnectionStage( 

world=self.world_cls(), 

referee=self.referee_cls(), 

num_clients=self.num_guis, 

ai_actors=[self.ai_actor_cls() for i in range(self.num_ais)], 

host=self.host, 

port=self.port, 

) 

theater.play() 

 

def play_client(self): 

# Defer instantiation of all the game objects until we're inside our  

# own process, to avoid having to pickle and unpickle things that  

# should be pickled. 

 

theater = self.theater_cls() 

theater.gui = self.gui_cls() 

theater.initial_stage = ClientConnectionStage( 

world=self.world_cls(), 

gui_actor=self.gui_actor_cls(), 

host=self.host, 

port=self.port, 

) 

theater.play() 

 

 

 

def main(world_cls, referee_cls, gui_cls, gui_actor_cls, ai_actor_cls, 

theater_cls=PygletTheater, default_host=DEFAULT_HOST, 

default_port=DEFAULT_PORT, argv=None): 

""" 

Run a game being developed with the kxg game engine. 

 

Usage: 

{exe_name} sandbox [<num_ais>] [-v...] 

{exe_name} client [--host HOST] [--port PORT] [-v...] 

{exe_name} server <num_guis> [<num_ais>] [--host HOST] [--port PORT] [-v...]  

{exe_name} debug <num_guis> [<num_ais>] [--host HOST] [--port PORT] [-v...] 

{exe_name} --help 

 

Commands: 

sandbox 

Play a single-player game with the specified number of AIs. None of  

the multiplayer machinery will be used. 

 

client 

Launch a client that will try to connect to a server on the given host  

and port. Once it connects and the game starts, the client will allow  

you to play the game against any other connected clients. 

 

server 

Launch a server that will manage a game between the given number of  

human and AI players. The human players must connect using this  

command's client mode. 

 

debug 

Debug a multiplayer game locally. This command launches a server and  

the given number of clients all in different processes, and configures  

the logging system such that the output from each process can be easily  

distinguished. 

 

Arguments: 

<num_guis> 

The number of human players that will be playing the game. Only needed  

by commands that will launch some sort of multiplayer server. 

 

<num_ais> 

The number of AI players that will be playing the game. Only needed by  

commands that will launch single-player games or multiplayer servers. 

 

Options: 

-x --host HOST [default: {default_host}] 

The address of the machine running the server. Must be accessible from  

the machines running the clients. 

 

-p --port PORT [default: {default_port}] 

The port that the server should listen on. Don't specify a value less  

than 1024 unless the server is running with root permissions. 

 

-v --verbose  

Have the game engine log more information about what it's doing. You  

can specify this option several times to get more and more information. 

 

This command is provided so that you can start writing your game with the least  

possible amount of boilerplate code. However, the clients and servers provided  

by this command are not capable of running a production game. Once you have  

written your game and want to give it a polished set of menus and options,  

you'll have to write new Stage subclasses encapsulating that logic and you'll  

have to call those stages yourself by interacting more directly with the  

Theater class. The online documentation has more information on this process. 

""" 

import sys, os, docopt, nonstdlib 

 

exe_name = os.path.basename(sys.argv[0]) 

usage = main.__doc__.format(**locals()).strip() 

args = docopt.docopt(usage, argv or sys.argv[1:]) 

num_guis = int(args['<num_guis>'] or 1) 

num_ais = int(args['<num_ais>'] or 0) 

host, port = args['--host'], int(args['--port']) 

 

logging.basicConfig( 

format='%(levelname)s: %(name)s: %(message)s', 

level=nonstdlib.verbosity(args['--verbose']), 

) 

 

# Use the given game objects and command line arguments to play a game! 

 

if args['debug']: 

print("""\ 

****************************** KNOWN BUG WARNING ****************************** 

In debug mode, every message produced by the logging system gets printed twice. 

I know vaguely why this is happening, but as of yet I've not been able to fix 

it. In the mean time, don't let this confuse you! 

*******************************************************************************""") 

game = MultiplayerDebugger( 

world_cls, referee_cls, gui_cls, gui_actor_cls, num_guis, 

ai_actor_cls, num_ais, theater_cls, host, port) 

else: 

game = theater_cls() 

ai_actors = [ai_actor_cls() for i in range(num_ais)] 

 

if args['sandbox']: 

game.gui = gui_cls() 

game.initial_stage = UniplayerGameStage( 

world_cls(), referee_cls(), gui_actor_cls(), ai_actors) 

game.initial_stage.successor = PostgameSplashStage() 

 

if args['client']: 

game.gui = gui_cls() 

game.initial_stage = ClientConnectionStage( 

world_cls(), gui_actor_cls(), host, port) 

 

if args['server']: 

game.initial_stage = ServerConnectionStage( 

world_cls(), referee_cls(), num_guis, ai_actors, 

host, port) 

 

game.play()