-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
86 lines (69 loc) · 2.23 KB
/
server.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import eventlet
import socketio
import json
#import gameManager
#player1Connected=False
#player2Connected=False
#gameStarted=False
#if player1Connected==True and player2Connected==True and gameStarted==False:
#gameStarted=True
#gameManager.startGame()
DEBUG = True
sio = socketio.Server()
app = socketio.WSGIApp(sio)
roomList = dict()
clients = dict()
@sio.event
def connect(sid, environ):
if DEBUG: print('connect ', sid)
clients[sid] = dict()
@sio.event
def disconnect(sid):
if DEBUG: print('disconnect ', sid)
if "room" in clients[sid]:
sio.emit("sevent_win", room=clients[sid]["room"], skip_sid=sid)
del clients[sid]
@sio.on("char")
def my_char(sid, data):
sio.emit("char", data, room=clients[sid]["room"], skip_sid=sid)
@sio.on("message")
def my_message(sid, data):
try:
sio.emit("message", (sid, data), room=clients[sid]["room"], skip_sid=sid)
if DEBUG: print(sid, "says:", data)
except:
if DEBUG: print("message attempt while not in a room")
@sio.on("joinroom")
def join_room(sid, name):
if DEBUG: print(sid, name)
if name not in roomList:
roomList[name] = 0
if (roomList[name] < 2):
roomList[name] += 1
clients[sid]["room"] = name
sio.enter_room(sid, name)
sio.emit("sevent_roomAccept", room=sid)
if (roomList[name] == 2):
sio.emit("sevent_gameStart", room=name)
cevent_endTurn(sid)
else:
sio.emit("sevent_roomFull", room=sid)
# This is really lazy implementation because we are not worried about cheating right now.
# This implementation also would not work with more than 2 players in a room.
@sio.on("cevent_move")
def cevent_move(sid, nodeNum):
sio.emit("sevent_move", nodeNum, room=clients[sid]["room"], skip_sid=sid)
return
@sio.on("cevent_attack")
def cevent_attack(sid, attackVal):
sio.emit("sevent_attack", attackVal, room=clients[sid]["room"], skip_sid=sid)
return
@sio.on("cevent_defend")
def cevent_defend(sid, damageTaken):
return
@sio.on("cevent_endTurn")
def cevent_endTurn(sid):
sio.emit("sevent_startTurn", room=clients[sid]["room"], skip_sid=sid)
return
if __name__ == '__main__':
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)