-1

Here is the complete code:

#client der mit irc-server kontakt aufnimmt
import time
import socket
from sys import argv

script, mitspieler1, mitspieler2 = argv
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

class funktion:

    def main(self):

            print "Socket(Client)"
            host = "irc.iz-smart.net"
            port = 6667 
            PASS = "7987fshd" 
            NICK = "Testikus" 
            USER = "Testikus localhost irc.iz-smart.net :Testikus" 
            self.login(PASS, NICK, USER, host, port)
            print "Verbindung aufgebaut zu {0}(IP:{1})".format(
            host, socket.gethostbyname(host)
            )
            self.haupt_schleife()

    def haupt_schleife(self):
        while True:
            antwort = sock.recv(4096)
            join = "JOIN #testblablub \r\n"
            print antwort
            if antwort[0:4] == "PING":
                self.pong(antwort, join)
            elif antwort.split()[3] == ":quiz.start":
                sock.sendall("PRIVMSG #testblablub Es spielen mit: "
                            +mitspieler1+" und "+mitspieler2+"\r\n"
                            )
                time.sleep(2)
                self.fragen(antwort)

    def pong(self, antwort, join):
        sock.sendall("PONG " + antwort.split()[1] + "\n")
        time.sleep(3)
        sock.sendall(join)
        sock.sendall("PRIVMSG #testblablub hi \r\n")

    def login(self, PASS, NICK, USER, host, port):
        sock.connect((host, port))
        sock.sendall("PASS "+PASS+"\n")
        sock.sendall("NICK "+NICK+"\n")
        sock.sendall("USER "+USER+"\n")

    def fragen(self, antwort):
            sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \r\n")
            time.sleep(3)
            if antwort.split()[3] == ":blau":
                sock.sendall("PRIVMSG #testblablub RISCHTISCH \r\n")





ausfuehren = funktion()
ausfuehren.main()

(sry some strings are written in german, but I think that's not important)

So my main problem is that I want the function def fragen(self, antwort) to be run in the def haupt_schleife(self) function (or method for python's sake). All of the important stuff is in this def haupt_schleife(self). I want that the rest of the quiz-code to be running in the elif-block of the def haupt_schleife(self), so that it's a complete seperate thing. The stupid thing is, if I type in "quiz.start" the : sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \r\n") is running with no problems, but it doesn't start the if-statement. It just does nothing else in the channel (just prints "Welche Farbe hat der Himmel ? out).

I hope that finaly someone understood this :/ (if you want to run the code you have to type in two names in the command line, because of argv)

JonnyPython
  • 121
  • 1
  • 2
  • 9
  • For future references, output doesn't matter all that much but variable name WOULD be nice if they were written in English. I'm not saying it's a requirement to get help but many people might get picky and just ignore the code because of it so to increase your odds by writing all variables in English :) Jedenfalls, viel Glück :) – Torxed Apr 10 '13 at 20:53

2 Answers2

3

IRC syntax

Sending messages should not be done this way:

PRIVMSG #testblablub Es spielen mit:

It should be:

PRIVMSG #testblablub :Es spielen mit ...

Also, USER should consist of 4 parts:

<username> <hostname> <servername> :<full name/realname>

In irc, it's important to end each string with \r\n
I noticed you sent \n sometimes and that will end badly, always end with \r\n!

Then comes sock.sendall(...), it's supposed to be used on say UDP sockets where you want to broadcast a message on all open sockets, in your case you're sending to and from a server, nothing else.. and it's via TCP so you should be using sock.send(...) (correct me if i'm wrong here)

A side note:
PING, it should only reply with PONG <server-name (usually sent with PING)>\r\n and not be spammed with JOIN every time, JOIN should be sent after MOTD is recieved, but sure it's not a problem really.. it will only annoy the sysadmins. In your case it would probably be PONG irc.iz-smart.net\r\n and nothing else, no JOIN etc.


Now, to the problem

   def haupt_schleife(self):
        while True:
            antwort = sock.recv(4096)
            join = "JOIN #testblablub \r\n"
            print antwort
            if antwort[0:4] == "PING":
                self.pong(antwort, join)
            elif antwort.split()[3] == ":quiz.start":
                sock.sendall("PRIVMSG #testblablub Es spielen mit: "
                            +mitspieler1+" und "+mitspieler2+"\r\n"
                            )
                time.sleep(2)
                self.fragen(antwort)
            elif antwort.split()[3] == ":blau":
                sock.sendall("PRIVMSG #testblablub RISCHTISH!\r\n")

Since self.fragen(...) only get called if the user writes quiz.start, the if check inside def fragen(...) will never be run again.. (because the next time the user write something, it's probably "blau" and that doesn't match ":quiz.start" which is the ONLY way in to the if check inside fragen(...).

    def fragen(self, antwort):
            sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \r\n")
            time.sleep(3)

So we removed the if block and made it an elif in haupt_schleife instead.

This is how the FIRST run/message would get parsed: enter image description here

The next run, the message WOULD be green in fragen but it never reaches there because the only way to get there is via elif antwort.split()[3] == ":quiz.start": :)

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • Hey, thank you very much for the efford and all the details :) That helped me alot. But i think to do a really good quiz, if have to make a lot more complicated stuff, i will see what the future brings. And if i want to implement some other stuff, how can i avoid these if/elif caskades? I hate that.(Just a short answer maybe or so?) Greets – JonnyPython Apr 11 '13 at 18:33
  • There's no "short" version of `if` caskades, when doing a quiz (string comparison) there will be a lot of them sorry to say. One way to do it dynamic is via `self.questions = {'Welche farbe hat der Himmel?' : 'balu', 'Was is mein name?' : 'testikus', 'welcher tag ist es?' : 'freitag'} and so on. When "starting" a game, you could remember it in say `self.game = 'Was is mein name?' ` and for whatever answer the user gives you can do `if antwort == self.questions[self.question]:` and compare it dynamicly, only one if for all questions and answers :) Python dictionaries are awesome, google them :) – Torxed Apr 11 '13 at 18:55
  • Don't forget to accept this as a solution IF (and only if) you think this solved your original problem and feel free to open up more if you need any assistance :) – Torxed Apr 11 '13 at 19:20
  • Thank you :) but maybe i will switch to some other projects(for some time) and see what i can learn there. :) Greets – JonnyPython Apr 12 '13 at 12:46
0

You call the fragen function when this condition is true:

elif antwort.split()[3] == ":quiz.start":

Then, in the fragen function, you enter the if branch when this condition is true:

if antwort.split()[3] == ":blau":

Since the function got called, we know that antwort.split()[3] will be ":quiz.start", hence the if statement will never be executed.

You should recheck the logic behind your application.

Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
  • Hi, thanks for reply :) Is there any way to avoid these if/elif caskades?? – JonnyPython Apr 11 '13 at 18:34
  • Not to be rude but the only way to avoid them is not to write them. – Ionut Hulub Apr 11 '13 at 20:21
  • or create a dictionary with questions matching answeres and keep it to one `if` statement. It's less with more, but it can quickly be complicated to read as a developer if you don't spill it out the way you're suggesting :) But there are other ways around if statements in this case. – Torxed Apr 12 '13 at 12:23
  • 1
    Ok, i will kepp that in mind :) thx for your great help @Torexed ! – JonnyPython Apr 12 '13 at 12:44