-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumers.py
154 lines (140 loc) · 5.11 KB
/
consumers.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
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
# chat/consumers.py
# python file that handles the consumer side of the websockets
# relays updated game data to client
from cgitb import text
import json
from tokenize import Name
from channels.generic.websocket import AsyncWebsocketConsumer
from django.contrib.auth.models import User
from myapp import models
from chat import views
from channels.db import database_sync_to_async
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope['url_route']['kwargs']['room_name']
self.room_group_name = 'chat_%s' % self.room_name
# Join room group
await self.channel_layer.group_add(
self.room_group_name,
self.channel_name
)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(
self.room_group_name,
self.channel_name
)
# Receive message from WebSocket
async def receive(self, text_data):
text_data_json = json.loads(text_data)
if 'chatMessage' in text_data_json:
message = text_data_json['chatMessage']
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'chat_message',
'username': self.scope['user'].username,
'message': message
}
)
elif 'systemMessage' in text_data_json:
message = text_data_json['systemMessage']
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'system_message',
'username': self.scope['user'].username,
'message': message
}
)
elif 'shipArray' in text_data_json:
shiparray = text_data_json['shipArray']
readycount = text_data_json['readyCount']
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'ship_array',
'array': shiparray,
'readyCount': readycount,
'user': self.scope['user'].username
}
)
elif 'shotX' in text_data_json:
shotX = text_data_json['shotX']
shotY = text_data_json['shotY']
readycount = text_data_json['readyCount']
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'shot_coord',
'shotX': shotX,
'shotY': shotY,
'readyCount': readycount,
'user': self.scope['user'].username
}
)
elif 'winMessage' in text_data_json:
message = text_data_json['winMessage']
await self.channel_layer.group_send(
self.room_group_name,
{
'type': 'win_message',
'username': self.scope['user'].username,
'message': message
}
)
# Receive message from room group
async def chat_message(self, event):
message = event['username'] + ": " + event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message
}))
async def system_message(self, event):
message = event['username'] + event['message']
# Send message to WebSocket
await self.send(text_data=json.dumps({
'message': message
}))
@database_sync_to_async
def win_message(self, event):
message = event['username'] + event['message']
targetPlayer = User.objects.get(username=self.scope['user'].username)
targetWinner = User.objects.get(username=event['username'])
player = models.ProfileModel.objects.get(Name=targetPlayer)
winner = models.ProfileModel.objects.get(Name=targetWinner)
#if targetWinner == targetPlayer:
winner.Wins += 1
winner.save()
player.GamesPlayed += 1
player.save()
#Send message to WebSocket
self.send(text_data=json.dumps({
'message': message,
}))
async def ship_array(self, event):
array = event['array']
readyCount = event['readyCount']
user = event['user']
await self.send(text_data=json.dumps({
'array': array,
'readyCount': readyCount,
'user': user
}))
async def shot_coord(self, event):
shotX = event['shotX']
shotY = event['shotY']
readyCount = event['readyCount']
attacker = event['user']
shootingSelf = False
if attacker == self.scope['user'].username:
shootingSelf = True
await self.send(text_data=json.dumps({
'shotX': shotX,
'shotY': shotY,
'readyCount': readyCount,
'user': attacker,
'shootingSelf': shootingSelf
}))