-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyretest.py
124 lines (95 loc) · 2.98 KB
/
pyretest.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
from pyre import Pyre
from pyre import zhelper
from pyre import pyre_peer
import zmq
import json
import uuid
import logging
import sys
import random
def chat_task(ctx,pipe):
print("communication started")
GROUPNAME = "Quizzzzz"
StopCmnd = "$$quit"
OPPONENT = "opponent"
print("name: %s" %NAME)
connected_players = 1
# set up node for the user
n = Pyre(GROUPNAME)
n.set_header('Name', NAME)
# join the groupchat
n.join(GROUPNAME)
#print("UUID %s" %n.uuid())
# start broadcasting signal
n.start()
# set up poller
poller = zmq.Poller()
poller.register(pipe,zmq.POLLIN)
poller.register(n.socket(),zmq.POLLIN)
# looping constantly to recieve messages
while True:
items = dict(poller.poll())
if pipe in items:
message = pipe.recv()
if message.decode('utf-8') == StopCmnd:
break
# uppercase letters for question to whole group
elif message.decode('utf-8').isupper() == True:
print("Question is: %s" %message)
n.shouts(GROUPNAME,"Question to group is: %s" %message.decode('utf-8'))
answer = 0
# lowercase to last person who asked question in the group
elif message.decode('utf-8').islower() == True:
message = NAME + "'s answer is=" + message
n.whisper(PEER,message)
else:
print("please don't mix up or lowercase or use not only numbers")
else:
msgrecv = n.recv()
#print(msgrecv)
msg_type = msgrecv.pop(0)
msg_sender = uuid.UUID(bytes=msgrecv.pop(0))
PEER = msg_sender
msg_name = msgrecv.pop(0)
if msg_type.decode('utf-8') == "ENTER":
headers = json.loads(msgrecv.pop(0).decode('utf-8'))
print("New player discovered in network")
print("New player = %s " %headers.get("Name"))
elif msg_type.decode('utf-8') == "JOIN":
print("New player has joined the group")
print("New player = %s" %headers.get("Name"))
connected_players += 1
print("#players = %s" %connected_players)
elif msg_type.decode('utf-8') == "SHOUT":
print(msgrecv.pop(1))
elif msg_type.decode('utf-8') == "WHISPER":
if msgrecv[0] == "You have to ask next question":
print("You have to ask next question")
n.shout(GROUPNAME, "%s is new quizmaster" %NAME)
answer = 0
else:
print(msgrecv.pop(0))
answer +=1
if answer == connected_players -1: #choosing new quizmaster randomly
players = n.peers()
next_master = players.pop(random.randint(0,connected_players-2))
n.whisper(next_master,"You have to ask next question")
print("Left current game")
n.stop()
if __name__ == '__main__':
#for logging (debugging)
logger = logging.getLogger("pyre")
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
logger.propagate = False
ctx = zmq.Context()
NAME = raw_input("username:")
chat_pipe = zhelper.zthread_fork(ctx, chat_task)
while True:
try:
msg = raw_input()
chat_pipe.send(msg.encode('utf-8'))
except(KeyboardInterrupt, SystemExit):
break
chat_pipe.send(StopCmnd.encode('utf-8'))
print("Leaving current game")