Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In a previous question, the issue was how to implement a conditional loop (a simple for loop) to affect the onMessage event calling function, which was resolved. However, my current issue is in trying to implement time as the determinant in the conditional loop.

Currently, the code runs but only seems to do the while loop that rechecks the time, ignoring the code before it. I suspect that the issue is in the location that I have placed the time rechecking loop?

Any clarity on this issue would be appreciated!

import ch
import time

class bot(ch.RoomManager):
    timeleft = 0
    starttime = 0

    def onMessage(self, room, user, message):
        print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
        if 100 >= timeleft >= 1:
            print('success')
        else:
            print('fail')

    loop = 1
    while loop == 1 :
        timeleft = starttime - int(time.clock())
        if timeleft <=0:
            timeleft = 200
            starttime = 200

rooms = ["testgroup444"]
username = "user"
password = "name"

bot.easy_start(rooms,username,password)

For clarity on the methods used, the entire ch.py library can be found here: https://pastebin.com/8ukS4VR1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
157 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think you still have an issue with the Python syntax. In Python whitespace is very important as it defines scope. The way your code is written, the while loop is executed at the time the class is defined, before anything is started.

Further, the code gets into an infinite loop, as while loop == 1 will be always True. This is why you see that your code gets stuck. From the discussion in the comments, I imagine you want to write something like:

import ch
import time
import enum

class State(enum.Enum):
    idle = 0
    nominating = 1
    voting = 2
    watching = 3

class bot(ch.RoomManager):

    state = State.idle
    movie_length = 20

    def updateState(self):

        if self.state in [State.idle, State.nominating]:
            self.state = State.voting
            timeout = 10
        elif self.state == voting:
            self.state = State.watching
            timeout = self.movie_length - 15
        else: # if self.state == watching
            self.state = State.nominating
            timeout = 15

        self.setTimeout(timeout*60, bot.updateState, self)

    def onConnect(self, room):
        # Redirect through event loop, not strictly nessecary
        self.setTimeout(0, bot.updateState, self)

    def onMessage(self, room, user, message):

        print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
        print("Current state is {0}".format(self.state))

rooms = ["testgroup444"]
username = "user"
password = "name"

bot.easy_start(rooms,username,password)

Here, the setTimeout method defined in the ch (bot) class is used, to allow messages to be passed at certain times. At every timeout the state is updated. The actual state is then available in all internal methods, e.g. in onMessage and updateState.

As I do not use the chat network or client I cannot guarantee that the solution works, though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...