-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation.py
70 lines (54 loc) · 1.62 KB
/
conversation.py
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
import common
from db import db
from db import Game
from message import AppMessageFactory, MessageKind
def find_user_by_name(name):
return next(filter(lambda x: x['name'] == name, db.users), None)
def find_game_by_name(name):
return next(filter(lambda x: x.name == name, db.games), None)
def process_register(socw, msg):
# register user
user_name = msg.params['name']
user = next(filter(lambda x: x['name'] == user_name, db.users), None)
if not user:
user = {}
user['address'] = socw.client_address[0]
user['name'] = user_name
db.users.append(user)
socw.set_user(user)
# send OK
msg = AppMessageFactory.ok()
socw.send_message(msg)
# DEBUG
for user in db.users:
print(user)
def process_create_game(socw, msg):
# create game
name = msg.params['name']
game = find_game_by_name(name)
if not game:
game = Game()
game.name = name
game.players.append(socw.get_user())
db.games.append(game)
# send OK
msg = AppMessageFactory.ok()
socw.send_message(msg)
def process_join_game(socw, msg):
# join game
name = msg.params['name']
game = find_game_by_name(name)
if not game:
msg = AppMessageFactory.ok()
socw.send_message(msg)
return
game.players.append(socw.get_user())
# send OK
msg = AppMessageFactory.start_game(game)
socw.send_message(msg)
def process_get_game_list(socw, msg):
msg = AppMessageFactory.send_game_list(db.games)
socw.send_message(msg)
def process_close(socw, msg):
msg = AppMessageFactory.ok()
socw.send_message(msg)