2017-05-29 82 views
0

我正在尝试用Pyautogui为MMO创建一个叫做“Graal Online Classic”的机器人。它会读取发送消息的玩家的“PM”,然后回复相应的响应。它还会截取玩家名称的截图,并将其转换为带有tesseract的字符串,并将其存储在字典中(还存储特定玩家在机器人的响应层次结构中的位置),并使用它来确定玩家是否有消息他们之前。关键字检测

def player_id(name_coords, option): # Indentifies person who messaged and depending on if this person has messaged before changes response 
    players = {} # Used to store players names and where in the response is 
    keys = players.keys() 

    image = pyautogui.screenshot('name_test.png', region = name_coords) # Names are screenshots 
    name_image = str(pytesseract.image_to_string(Image.open('name_test.png'))) 
    print(name_image) 
    for name_image in keys: 
     print("User is previous user.") 
     if players[name_image] == None: 
      players[name_image] = option 
     else: 
      players[name_image] = players[name_image] + option 
     return players[name_image] 
    else: 
     print("User was not previously found, adding to dictionary.") 
     players[name_image] = None 
     print(players.keys()) 
     print(players) 

问题源于上述功能。当它发现之前有消息的人时,应该去if语句,但无论如何,else语句都会执行。我在播放器中打印值,当播放器第二次发送消息时,它的值相同,但是在键中的name_image:仍然返回false。

如果需要https://pastebin.com/haJRfqJD

这里是3个消息

None 
(454, 222) 
Found a waiting message 
Found an open message 
1003 424 
Player messaged 1 
1 
I hate sand 
User was not previously found, adding to dictionary. 
dict_keys(['I hate sand']) 
{'I hate sand': None} 
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties] 
Replying... 
(454, 222) 
Found a waiting message 
Found an open message 
1003 424 
Player messaged 1 
1 
I hate sand 
User was not previously found, adding to dictionary. 
dict_keys(['I hate sand']) 
{'I hate sand': None} 
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties] 
Replying... 
(454, 222) 
Found a waiting message 
Found an open message 
1003 424 
Player messaged 1 
1 
I hate sand 
User was not previously found, adding to dictionary. 
dict_keys(['I hate sand']) 
{'I hate sand': None} 
The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties] 
Replying... 
+0

如果您正在检查是否存在使用'if not not name_image in players:'的密钥。 – Darkstarone

+0

'name_image'取自'players.keys()'。在什么情况下,它与值None相关联? – khelwood

+0

http://stackoverflow.com/help/someone-answers – e4c5

回答

1

运行程序的日志你并不需要一个for循环这里下面是完整的代码。这是使用字典的重点 - 避免昂贵的O(N)查找。字典查找速度很快。与

name = players.get(name_image) 
if name:  
    # your variable naming is totally unclear. I **think** this is what you want 
    players[name] = option 
    return players[name] 
else: 
    print("User was not previously found, adding to dictionary.") 
    players[name_image] = None 
    print(players.keys()) 
    print(players) 

注意,在你原来的cdoe,要定义name_image如下替换您的for循环:

name_image = str(pytesseract.image_to_string(Image.open('name_test.png'))) 

但该变量得到的比写在for循环。

+0

谢谢你的帮助,但是看起来如果名字:只是不想执行。以下是我发送自己3条同名消息的日志。 CANT FIND DIGIT ANSWER我讨厌沙子 用户以前没有找到,添加到字典。 dict_keys(['我讨厌沙']) {'我讨厌沙':无} 无 我讨厌沙 用户以前找不到或添加到字典中。 dict_keys(''我讨厌沙']) {'我讨厌沙':无}我真的无法发布整个日志由于字符限制,但你可以从上面看到if语句不会执行,即使当该值应该在字典中。 –

+0

我回复得太早,请再次检查。 –

+0

添加日志到主帖子。 –