[go: up one dir, main page]

Menu

[r92]: / main / function.py  Maximize  Restore  History

Download this file

488 lines (407 with data), 13.8 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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# -*- coding: utf8 -*-
# This file is part of CatchX.
#
# CatchX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# CatchX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with CatchX. If not, see <http://www.gnu.org/licenses/>.
import pygtk
import gtk
import os
import xmlrpclib
import time
import thread
import imp
#mapname = 'potsdam'
#map = imp.load_source(mapname,'maps')
from maps import potsdam as map
global started
started = False
global rpoints
rpoints = {}
def create_window(window_name):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_title(window_name)
window.set_icon_from_file("img/logo.png")
window.set_position(gtk.WIN_POS_CENTER)
return window
def blink_window(window):
while(True):
color = gtk.gdk.color_parse('#FF0000')
window.modify_bg(gtk.STATE_NORMAL, color)
time.sleep(0.7)
color = gtk.gdk.color_parse('#FFFFFF')
window.modify_bg(gtk.STATE_NORMAL, color)
time.sleep(0.7)
def init_connection(server):
print("Trying to connect to " + server)
try :
s = xmlrpclib.ServerProxy('http://' + server + ':20211')
ping = s.ping()
except:
print("Error - Couldn't establish a connecting to the server.")
return False
print("Connected to " + server + ':20211')
return s
def get_gamedata(s, password, name, room, window):
room = s.roomreq(name, room, password)
if room == 'running':
print "Game is already running"
thread.start_new_thread(blink_window, (window,))
return "err_running"
if room == 'password':
print "Wrong password or the room doesn't exist"
thread.start_new_thread(blink_window, (window,))
return "err_login"
playerid = room[0]
roomid = room[1]
print("Got assigned playerid #" + str(playerid))
players = s.get_players(playerid)
#print "Players on the server: " + str(players)
return (playerid,players,roomid)
def about_window(self):
text = """Coded by:\n-Lutoma (Lukas Martini)\n-vIiRuS (Philip Thelen)\n\nGrafics by:\n-Pixelmännchen (Robin Eberhard)\n-vIiRuS (Philip Thelen)\n-Waldteufel\n\nSounds and Music by:\n-Waldteufel\n\n\nCatchX is licensed under the GNU\nGeneral Public License. For more\ninformations, read the "COPYING"\nfile you should have in your\nCatchX directory."""
win = create_window("About CatchX")
win.set_border_width(10)
box = gtk.VBox(False, 0)
win.add(box)
image = gtk.Image()
image.set_from_file("img/logo.png")
box.add(image)
label = gtk.Label(text)
box.add(label)
win.show_all()
def load_maingui(cliwindow, players, playerid, version, s): #This is the most complex GUI, the maingui...
#def load_maingui(start):
#cliwindow = start.cliwindow
#players = start.gdata[1]
#playerid = start.gdata[0]
global vpan, startbox, mapbox
def start_game(self): #copied hier for testing prps
global started
if started == True:
chat_update("\n* The game is already started!")
return
started = True
cmd = s.startgame(playerid)
busbutton.set_sensitive(True)
trambutton.set_sensitive(True)
taxibutton.set_sensitive(True)
if cmd[1] == playerid:
shipbutton.set_sensitive(True)
bankrobb.set_sensitive(True)
simpletheftb.set_sensitive(True)
grandtheftb.set_sensitive(True)
burglaryb.set_sensitive(True)
chat_update("* You are the Mister X")
chat_update("* It's your turn")
else:
chat_update("* " + cmd[0] + " is the Mister X")
chat_update("* It's " + cmd[0] + "'s turn")
vpan.remove(startbox)
vpan.add(mapbox)
vpan.show_all()
def destroy_room(self):
return #As this function is not implemented @ the server yet ;)
cmd = s.roomdestroy(pid)
chat_update("\n" + cmd)
def quit_cb(self):
s.playerleave(playerid)
#print "RPoints = " + str(rpoints)
print 'Quitting program'
gtk.main_quit()
cliwindow.maximize() #We need the complete space
size = cliwindow.get_size() #Now get the size of the maximized window
print "WSize = " + str(size)
uppersize = int((size[0] / 95) *100)
print uppersize
print "MapPoints = " + str(map.points)
ui = '''<ui>
<menubar name="MenuBar">
<menu action="File">
<menuitem action="Start"/>
<menuitem action="Destroy"/>
<separator/>
<menuitem action="Quit"/>
</menu>
<menu action="Help">
<menuitem action="About"/>
</menu>
</menubar>
</ui>'''
uimanager = gtk.UIManager()
accelgroup = uimanager.get_accel_group()
cliwindow.add_accel_group(accelgroup)
actiongroup = gtk.ActionGroup('UIManagerExample')
actiongroup.add_actions([('Quit', gtk.STOCK_QUIT, '_Quit', None,
'Quit CatchX', quit_cb),
('Start', gtk.STOCK_APPLY, '_Start Game!', None, 'Start Game!', start_game),
('Destroy', gtk.STOCK_STOP, '_Destroy Room', None, 'Destroy Room', destroy_room),
('File', None, '_File'),
('Help', None, '_Help'),
('About', gtk.STOCK_ABOUT, '_About', None,
'About', about_window)])
actiongroup.get_action('Quit').set_property('short-label', '_Quit')
uimanager.insert_action_group(actiongroup, 0)
uimanager.add_ui_from_string(ui)
menubar = uimanager.get_widget('/MenuBar')
clibox = gtk.VBox(False, 0)
cliwindow.add(clibox)
clibox.pack_start(menubar, False)
mapbox = gtk.HBox(False, 0)
toolbox = gtk.HBox(False, 0)
leftdnbox = gtk.HBox(False, 0)
rightdnbox = gtk.VBox(False, 0)
buttonbox = create_buttonbox()
mapbox.pack_start(buttonbox, False, False, 0)
image = gtk.Image()
image.set_from_file("img/map.png")
pixelsize = image.get_pixbuf() # get the size of the picture to adjust the Layout size
pixelwidth = pixelsize.get_width()
pixelheight = pixelsize.get_height()
global layout, i1
#Layout and Scrollbar settings
layout = gtk.Layout()
vadjust = layout.get_vadjustment()
hadjust = layout.get_hadjustment()
vadjust.step_increment = 10
hadjust.step_increment = 10
layout.set_size(pixelwidth, pixelheight)
vscrollbar = gtk.VScrollbar(vadjust)
hscrollbar = gtk.HScrollbar(hadjust)
vscrollbar.show()
hscrollbar.show()
layout.set_events(gtk.gdk.BUTTON_PRESS_MASK)
layout.connect("button_press_event", button_press)
layout.put(image, 0, 0)
i1 = gtk.Image()
i1.set_from_file("img/figures/green.png")
layout.put(i1, 348, 260)
#packing the layout and the scrollbars in the Table
table = gtk.Table(2, 2, 0)
table.attach(layout, 0, 1, 0, 1, gtk.EXPAND|gtk.FILL, gtk.EXPAND|gtk.FILL, 0, 0)
table.attach(vscrollbar, 1, 2, 0, 1, 0, gtk.EXPAND|gtk.FILL, 0, 0)
table.attach(hscrollbar, 0, 1, 1, 2, gtk.EXPAND|gtk.FILL, 0, 0, 0)
mapbox.pack_start(table)
xbar = gtk.ProgressBar()
xbar.set_orientation(gtk.PROGRESS_BOTTOM_TO_TOP)
xbar.set_fraction(0.35)
mapbox.pack_end(xbar, False, False, 0)
mapbox.pack_end(gtk.VSeparator(), False, False, 3)
chatframe = gtk.Frame()
rightdnbox.add(chatframe)
#label3 = gtk.Label(str)
#label3.set_text("Right Toolbox")
#chatframe.add(label3)
global textbuffer, dtext, textview
textview = gtk.TextView()
textbuffer = textview.get_buffer()
textview.set_editable(False)
dtext = "* Welcome to CatchX v%s" % str(version)
textbuffer.set_text(dtext)
chatframe.add(textview)
downerchat = gtk.HBox(False, 0)
rightdnbox.pack_end(downerchat, False, False, 0)
chatentry = gtk.Entry()
downerchat.pack_start(chatentry, True, True, 0)
def sendmsg(widget):
if chatentry.get_text() <> "":
msg = chatentry.get_text()
chatentry.set_text('')
s.chat(playerid, msg)
sendbutton = gtk.Button("-> Send")
sendbutton.connect("clicked", sendmsg)
sendbutton.set_sensitive(False)
def changebuttonstate(widget):
if chatentry.get_text() <> "":
sendbutton.set_sensitive(True)
else:
sendbutton.set_sensitive(False)
chatentry.connect("changed", changebuttonstate)
chatentry.connect("activate", sendmsg)
downerchat.pack_end(sendbutton, False, False, 0)
#The user-list
global liststore
liststore = gtk.ListStore(str, str, str)
treeview = gtk.TreeView(liststore)
tvcolumn = gtk.TreeViewColumn('Name')
tvcolumn1 = gtk.TreeViewColumn('Money')
tvcolumn2 = gtk.TreeViewColumn('PID')
treeview.append_column(tvcolumn)
treeview.append_column(tvcolumn1)
treeview.append_column(tvcolumn2)
#print players
print players
global iter
iter = ()
for name in players:
iter += (liststore.append([name[0], "--", name[1]]),)
cell = gtk.CellRendererText()
cell1 = gtk.CellRendererText()
cell2 = gtk.CellRendererText()
#tvcolumn.pack_start(cellpb, False)
tvcolumn.pack_start(cell, True)
tvcolumn1.pack_start(cell1, True)
tvcolumn2.pack_start(cell2, True)
tvcolumn.set_attributes(cell, text=0)
tvcolumn1.set_attributes(cell1, text=1)
tvcolumn2.set_attributes(cell2, text=1)
#userlist scrollbar
useradjust = treeview.get_vadjustment()
useradjust.step_increment = 10
userscrollbar = gtk.VScrollbar(useradjust)
treeview.set_search_column(0)
tvcolumn1.set_sort_column_id(0)
leftdnbox.add(treeview)
leftdnbox.pack_end(userscrollbar, False, False, 1)
#the initial start info
startbox = gtk.VBox(False, 0)
image = gtk.Image()
image.set_from_file("img/logo.png")
startbox.add(image)
info = gtk.Label("""To start the game, press File -> Start game!\nBut wait for the other players first, when the game is started, no new players can join the room!""")
startbox.add(info)
vpan = gtk.VPaned()
#vpan.add1(mapbox)
vpan.add1(startbox)
vpan.add2(toolbox)
clibox.pack_start(vpan)
hpan = gtk.HPaned()
hpan.add1(leftdnbox)
hpan.add2(rightdnbox)
toolbox.add(hpan)
vpan.set_position(uppersize)
hpan.set_position(200)
cliwindow.show_all()
def button_press(widget, event):
global rpoints
print "Mapclick = " + str(event.x) + str(event.y)
#rpoints[event.x] = event.y
for xwert, ywert in map.points.items():
if (-10 < (event.x - xwert) < 10) and (-10 < (event.y - ywert) < 10):
layout.move(i1, xwert - 16, ywert - 40)
print ">
return True
return False
def chat_update(text):
global textbuffer, dtext
dtext += "\n" + text
textbuffer.set_text(dtext)
textview.scroll_mark_onscreen(textbuffer.get_insert())
def keepalive(s,pid):
time.sleep(1)
i = 0
while(True): #infinite loop
i += 1
try:
command = s.keepalive(pid)
except:
print "Lost connection to the Server!"
print "Quitting program!"
gtk.main_quit()
if command != "none":
command = command.split("><");
for cmd in command:
cmd = cmd[4:]
cmd = cmd.split("<>");
print cmd
if cmd[0] == 'chatmsg':
chat_update(str(cmd[1]) + ": " + str(cmd[2]))
if cmd[0] == 'playerjoin':
chat_update("* " + cmd[1] + " joined the room")
liststore.append([cmd[1], "--"])
if cmd[0] == 'playerleave':
chat_update("* " + cmd[1] + " has left")
liststore.remove(iter[cmd[1]])
time.sleep(0.20)
#def load_mappoints():
# spots = []
# i = 0
# point = []
# f = file("mapname.mapdata", "r")
# while True:
# line = f.readline()
# if len(line) == 0:
# break
# point.append(line)
# i = i + 1
# for n in range(0, len(point)):
# point[n] = point[n].split(":")
# point[n][1] = (point[n][1].rstrip("\n"))
# print point[n]
# return point
def create_buttonbox():
global busbutton, trambutton, taxibutton, shipbutton, bankrobb, simpletheftb, grandtheftb, burglaryb
buttonbox = gtk.VBox(False, 0)
busbutton = gtk.Button()
trambutton = gtk.Button()
taxibutton = gtk.Button()
shipbutton = gtk.Button()
traveltable = gtk.Table(2, 3, 0)
buttonbox.pack_start(traveltable, False, False, 20)
traveltable.attach(gtk.Label("Travel:"), 0, 2, 0, 1)
traveltable.attach(busbutton, 0, 1, 1, 2)
traveltable.attach(trambutton, 1, 2, 1, 2)
traveltable.attach(taxibutton, 0, 1, 2, 3)
traveltable.attach(shipbutton, 1, 2, 2, 3)
busimage = gtk.Image()
busimage.set_from_file("img/bus.png")
tramimage = gtk.Image()
tramimage.set_from_file("img/tram.png")
taxiimage = gtk.Image()
taxiimage.set_from_file("img/car.png")
shipimage = gtk.Image()
shipimage.set_from_file("img/ship.png")
busbox = gtk.VBox()
trambox = gtk.VBox()
taxibox = gtk.VBox()
shipbox = gtk.VBox()
busbox.add(busimage)
trambox.add(tramimage)
taxibox.add(taxiimage)
shipbox.add(shipimage)
busbox.add(gtk.Label("Bus"))
trambox.add(gtk.Label("Tram"))
taxibox.add(gtk.Label("Taxi"))
shipbox.add(gtk.Label("Ship"))
busbutton.add(busbox)
trambutton.add(trambox)
taxibutton.add(taxibox)
shipbutton.add(shipbox)
busbutton.set_sensitive(False)
trambutton.set_sensitive(False)
taxibutton.set_sensitive(False)
shipbutton.set_sensitive(False)
bankrobb = gtk.Button()
simpletheftb = gtk.Button()
grandtheftb = gtk.Button()
burglaryb = gtk.Button()
bankrobb.set_sensitive(False)
simpletheftb.set_sensitive(False)
grandtheftb.set_sensitive(False)
burglaryb.set_sensitive(False)
buttonbox.pack_start(gtk.HSeparator(), False, False, 0)
bankrobb.add(gtk.Label("Bank\nrobbery"))
simpletheftb.add(gtk.Label("Simple\ntheft"))
grandtheftb.add(gtk.Label("Grand\ntheft"))
burglaryb.add(gtk.Label("Burglary"))
traveltable = gtk.Table(2, 3, 0)
buttonbox.pack_start(traveltable, False, False, 20)
traveltable.attach(gtk.Label("Crimes:"), 0, 2, 0, 1)
traveltable.attach(bankrobb, 0, 1, 1, 2)
traveltable.attach(simpletheftb, 1, 2, 1, 2)
traveltable.attach(grandtheftb, 0, 1, 2, 3)
traveltable.attach(burglaryb, 1, 2, 2, 3)
#buttonbox.pack_start(bankrobb, False, False, 0)
#buttonbox.pack_start(simpletheftb, False, False, 0)
#buttonbox.pack_start(grandtheftb, False, False, 0)
#buttonbox.pack_start(burglaryb, False, False, 0)
return buttonbox