-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_human.py
169 lines (160 loc) · 6.44 KB
/
client_human.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
from sys import argv, stdout
from threading import Thread
import GameData
import socket
from constants import *
import os
###
# Basic client file to allow human play
###
if len(argv) < 4:
print("You need the player name to start the game.")
#exit(-1)
playerName = "Test" # For debug
ip = HOST
port = PORT
else:
playerName = argv[3]
ip = argv[1]
port = int(argv[2])
run = True
statuses = ["Lobby", "Game", "GameHint"]
status = statuses[0]
hintState = ("", "")
def manageInput():
global run
global status
while run:
command = input()
# Choose data to send
if command == "exit":
run = False
os._exit(0)
elif command == "ready" and status == statuses[0]:
s.send(GameData.ClientPlayerStartRequest(playerName).serialize())
elif command == "show" and status == statuses[1]:
s.send(GameData.ClientGetGameStateRequest(playerName).serialize())
elif command.split(" ")[0] == "discard" and status == statuses[1]:
try:
cardStr = command.split(" ")
cardOrder = int(cardStr[1])
s.send(GameData.ClientPlayerDiscardCardRequest(playerName, cardOrder).serialize())
except:
print("Maybe you wanted to type 'discard <num>'?")
continue
elif command.split(" ")[0] == "play" and status == statuses[1]:
try:
cardStr = command.split(" ")
cardOrder = int(cardStr[1])
s.send(GameData.ClientPlayerPlayCardRequest(playerName, cardOrder).serialize())
except:
print("Maybe you wanted to type 'play <num>'?")
continue
elif command.split(" ")[0] == "hint" and status == statuses[1]:
try:
destination = command.split(" ")[2]
t = command.split(" ")[1].lower()
if t != "colour" and t != "color" and t != "value":
print("Error: type can be 'color' or 'value'")
continue
value = command.split(" ")[3].lower()
if t == "value":
value = int(value)
if int(value) > 5 or int(value) < 1:
print("Error: card values can range from 1 to 5")
continue
else:
if value not in ["green", "red", "blue", "yellow", "white"]:
print("Error: card color can only be green, red, blue, yellow or white")
continue
s.send(GameData.ClientHintData(playerName, destination, t, value).serialize())
except:
print("Maybe you wanted to type 'hint <type> <destinatary> <value>'?")
continue
elif command == "":
print("[" + playerName + " - " + status + "]: ", end="")
else:
print("Unknown command: " + command)
continue
stdout.flush()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
request = GameData.ClientPlayerAddData(playerName)
s.connect((HOST, PORT))
s.send(request.serialize())
data = s.recv(DATASIZE)
data = GameData.GameData.deserialize(data)
if type(data) is GameData.ServerPlayerConnectionOk:
print("Connection accepted by the server. Welcome " + playerName)
print("[" + playerName + " - " + status + "]: ", end="")
Thread(target=manageInput).start()
while run:
dataOk = False
data = s.recv(DATASIZE)
if not data:
continue
data = GameData.GameData.deserialize(data)
if type(data) is GameData.ServerPlayerStartRequestAccepted:
dataOk = True
print("Ready: " + str(data.acceptedStartRequests) + "/" + str(data.connectedPlayers) + " players")
data = s.recv(DATASIZE)
data = GameData.GameData.deserialize(data)
if type(data) is GameData.ServerStartGameData:
dataOk = True
print("Game start!")
s.send(GameData.ClientPlayerReadyData(playerName).serialize())
status = statuses[1]
if type(data) is GameData.ServerGameStateData:
dataOk = True
print("Current player: " + data.currentPlayer)
print("Player hands: ")
for p in data.players:
print(p.toClientString())
print("Cards in your hand: " + str(data.handSize))
print("Table cards: ")
for pos in data.tableCards:
print(pos + ": [ ")
for c in data.tableCards[pos]:
print(c.toClientString() + " ")
print("]")
print("Discard pile: ")
for c in data.discardPile:
print("\t" + c.toClientString())
print("Note tokens used: " + str(data.usedNoteTokens) + "/8")
print("Storm tokens used: " + str(data.usedStormTokens) + "/3")
if type(data) is GameData.ServerActionInvalid:
dataOk = True
print("Invalid action performed. Reason:")
print(data.message)
if type(data) is GameData.ServerActionValid:
dataOk = True
print("Action valid!")
print("Current player: " + data.player)
if type(data) is GameData.ServerPlayerMoveOk:
dataOk = True
print("Nice move!")
print("Current player: " + data.player)
if type(data) is GameData.ServerPlayerThunderStrike:
dataOk = True
print("OH NO! The Gods are unhappy with you!")
if type(data) is GameData.ServerHintData:
dataOk = True
print("Hint type: " + data.type)
print("Player " + data.destination + " cards with value " + str(data.value) + " are:")
for i in data.positions:
print("\t" + str(i))
if type(data) is GameData.ServerInvalidDataReceived:
dataOk = True
print(data.data)
if type(data) is GameData.ServerGameOver:
dataOk = True
print(data.message)
print(data.score)
print(data.scoreMessage)
stdout.flush()
#run = False
print("Ready for a new game!")
if not dataOk:
print("Unknown or unimplemented data type: " + str(type(data)))
print("[" + playerName + " - " + status + "]: ", end="")
stdout.flush()