3 İşlemeler 15372853bf ... 9f846996fc

Yazar SHA1 Mesaj Tarih
  Bence DOMONKOS 9f846996fc bot.py: Add the simplest bot 1 yıl önce
  Bence DOMONKOS 26be6d178e client.py: implement quit action 1 yıl önce
  Bence DOMONKOS 832a634236 client & game: implement name changing 1 yıl önce
3 değiştirilmiş dosya ile 120 ekleme ve 3 silme
  1. 96 0
      bot.py
  2. 10 0
      client.py
  3. 14 3
      game.py

+ 96 - 0
bot.py

@@ -0,0 +1,96 @@
+import json
+import select
+import socket
+import sys
+import time
+
+HOST = 'localhost'
+PORT = 6546
+
+ssock = None
+
+def listen():
+	global ssock
+	ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+	#ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+	ssock.connect((HOST, PORT))
+	print('connected:', ssock)
+	send_msg(set_name('BOT_select_first'))
+	while True:
+		bmsg = ssock.recv(1024)
+		if len(bmsg) <= 0:
+			print('read 0; hangup')
+			exit(3)
+		msg = str(bmsg, 'utf-8').strip()
+		jmsg = json.loads(msg)
+		msg_type = parse_server_message(jmsg)
+		print('parsed msg_type:', msg_type)
+		if msg_type == 'query':
+			action = get_action(jmsg)
+			print('parsed action:', action)
+			if action == 'give_card':
+				bot_give_card(jmsg)
+			elif action == 'select_row':
+				bot_select_row(jmsg)
+			else:
+				print('unknown message action:', action)
+		else:
+			print('unknown message type:', msg_type)
+
+def bot_give_card(msg):
+	print('about to give card')
+	hand = msg.get('hand')
+	print(hand, '->', hand[0])
+	msg = give_card(hand[0])
+	send_msg(msg)
+	return hand[0]
+
+def bot_select_row(msg):
+	print('about to select row')
+	msg = choose_row(0)
+	send_msg(msg)
+	return 0
+
+def get_action(jmsg):
+	try:
+		action = jmsg.get('action')
+	except:
+		print('unable to get action')
+		exit(8)
+	return action
+
+def parse_server_message(jmsg):
+	try:
+		typ = jmsg.get('type')
+	except:
+		print('unable to get type')
+		exit(8)
+	return typ
+
+def send_msg(msg):
+	jmsg = json.dumps(msg)
+	print('msg to send:', jmsg)
+	ssock.send(bytes(jmsg, 'utf-8'))
+
+def build_action(action, rest = {}):
+	ret = { 'action': action}
+	ret = ret | rest
+	return ret
+
+def choose_row(row_index):
+	return build_action('select_row', { 'i': row_index })
+
+def give_card(card):
+	return build_action('give_card', { 'card': card })
+
+def set_name(name):
+	return build_action('set_name', { 'name': name })
+
+if __name__ == '__main__':
+	try:
+		while True:
+			listen()
+	except KeyboardInterrupt:
+		print('interrupt - exiting')
+		#TODO: close
+		exit(9)

+ 10 - 0
client.py

@@ -119,12 +119,18 @@ def build_action(action, rest = {}):
 def choose_row(row_index):
 	return build_action('select_row', { 'i': row_index })
 
+def set_name(name):
+	return build_action('set_name', { 'name': name })
+
 def start_game():
 	return build_action('start')
 
 def give_card(card):
 	return build_action('give_card', { 'card': card })
 
+def quit_game():
+	return build_action('quit')
+
 def parse(line):
 	print('parse')
 	line = line.strip()
@@ -142,6 +148,10 @@ def parse(line):
 		msg = start_game()
 	elif cmd == 'c':
 		msg = choose_row(int(arg))
+	elif cmd == 'n':
+		msg = set_name(arg)
+	elif cmd == 'q':
+		msg = quit_game()
 	else:
 		print('unknown cmd:', cmd)
 		return

+ 14 - 3
game.py

@@ -14,6 +14,7 @@ class MSG_TYPE(enum.Enum):
 	START = 1
 	QUIT = 2
 	GIVE_CARD = 3
+	CHANGE_NAME = 4
 
 class GAME_STATE(enum.Enum):
 	LOBBY = 0
@@ -46,6 +47,8 @@ class Game:
 			print('action:', action)
 			if action == 'start':
 				return MSG_TYPE.START
+			elif action == 'set_name':
+				return MSG_TYPE.CHANGE_NAME
 			elif action == 'quit':
 				return MSG_TYPE.QUIT
 			return MSG_TYPE.UNKNOWN
@@ -63,19 +66,27 @@ class Game:
 			else:
 				self.game()
 
-	def process_lobby_message(self, msg):
+	def process_lobby_message(self, client, msg):
 		action = self.get_controll_message_action(msg)
 		if action == MSG_TYPE.START:
 			self.game_state = GAME_STATE.PLAY
+		elif action == MSG_TYPE.CHANGE_NAME:
+			self.handle_name_change(client, msg)
 		elif action == MSG_TYPE.QUIT:
 			self.game_state = GAME_STATE.QUIT
 		#elif action == MSG_TYPE.CHAT:
 			#broadcast chat...
 
+	def handle_name_change(self, client, msg):
+		res = json.loads(msg)
+		name = res.get('name')
+		print('renaming', self.players[client].name, 'to', name)
+		self.players[client].name = name
+
 	def lobby(self):
-		for client, msg, op in self.ssock.listen():
+		for client, msg, op in self.ssock.listen(): # TODO: convert to json here
 			if op == server_socket.SOCKET_OP.DATA_IN:
-				self.process_lobby_message(msg)
+				self.process_lobby_message(client, msg)
 			elif op == server_socket.SOCKET_OP.NEW_CONNECTION:
 				self.handle_new_connection(client)
 			elif op == server_socket.SOCKET_OP.HANGUP: