-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
134 lines (102 loc) · 3.79 KB
/
views.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
from django.http.response import HttpResponse
from django.shortcuts import render,redirect
from django.utils.safestring import mark_safe
from .models import Games
import json,random
import numpy as np
# Create your views here.
def chat_hall(request):
device = request.POST.get("device")
if device:
try:
user_name = request.POST.get("username"+device)
except:
username = None
try:
new_room_name = request.POST.get("new_roomname"+device)
except:
new_room_name = None
room_name = request.POST.get("roomname"+device)
password = request.POST.get("password"+device)
time = request.POST.get("time"+device)
print("AAAAAAAAAAAA",device)
print("AAAAAAAAAAAA",user_name)
print("AAAAAAAAAAAA",room_name)
print("AAAAAAAAAAAA",new_room_name)
# Create a room
if user_name and (room_name or new_room_name):
if room_name:
hall = room_name if Games.objects.filter(hall_name=room_name) else new_room_name
else:
hall = new_room_name
color = random.choice(["w","b"])
print("HALL",hall)
print(Games.objects.filter(hall_name=room_name))
new_board = Games(
hall_name=hall,
player1=user_name,
password=password,
player1_color = color,
time = time,
)
new_board.save()
print(Games.objects.filter(hall_name=hall))
return redirect(f"/chess/{hall}/{user_name}")
return render(request,"chat_hall.html")
return render(request,"chat_hall.html")
def chat_room(request,room_name,user_name):
"""
new_user = UsersConnected(nick = user_name)
new_user.save()
users_connected = UsersConnected.objects.all()
print(users_connected)
"""
# Check if that room exists
exists = Games.objects.filter(hall_name=room_name)
print("BOARDDD", exists)
print(room_name,user_name)
print("EXISTS",exists)
if exists:
if exists[0].player2 == "nan":
if exists[0].player1 != user_name:
print("Jugador 2")
opponent_name = exists[0].player1
color = exists[0].player1_color
color = "w" if color == "b" else "b"
exists.update(player2 = user_name)
print(exists)
else:
# Create a room
print("Jugador 1")
opponent_name = np.nan
color = exists[0].player1_color
#new_board = Games(hall_name=room_name,player1=user_name,password=np.nan,player1_color = color)
#new_board.save()
#Common argues for both players
password = exists[0].password
timer = exists[0].time
board = range(8,0,-1) if color == "b" else range(1,9)
return render(request, "chat_room.html",
{'room_name': room_name,
'user_name' : user_name,
'password' : password,
'opponent_name' : opponent_name,
'color' : color,
'board' : board,
'board_st' : range(1,9),
'time' : timer
})
else:
return render(request,"room_fool.html")
else:
color = random.choice(["w","b"])
new_board = Games(
hall_name=room_name,
player1=user_name,
password="",
player1_color = color,
time = 10,
)
new_board.save()
chat_room(request,room_name,user_name)
return render(request,"404.html")