Thread Ganz simples User management? (37 answers)
Opened by FlorianL at 2007-07-20 11:06

FlorianL
 2007-07-20 14:29
#78700 #78700
User since
2007-05-18
142 Artikel
BenutzerIn
[default_avatar]
hm ich hab kagge gebaut irgendwo?!

output:
Quote
Hello welcome to SlashEm!

SlashEm is a modification of the great NetHack Game...
More Info: http://nethack.org/v343/Guidebook.html or press '?' ingame...

Login: bla
Password: bla
Use of uninitialized value in exists at slashem.pl line 77, <STDIN> line 2.
irgendwas ganz komisches ist passiert at slashem.pl line 80, <STDIN> line 2.
mindfucked:/home/mindfuck/scripts#


Code (perl): (dl )
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
#!/usr/bin/perl
use warnings;
use strict;
use DBI;

my $gamebin = "/usr/games/slashem";

### DB ###
my $database = "nethack";
my $hostname = "127.0.0.1";
my $dsn      = "DBI:mysql:database=$database;host=$hostname";
my $dbuser   = "nethack";
my $dbpasswd = 'whatApa55';
my $dbh = DBI->connect( $dsn, $dbuser, $dbpasswd, {RaiseError => 1, AutoCommit => 0} ) || die $DBI::errstr;

### SUBS ###
sub countusers {
    my $sth = $dbh->prepare( q{ SELECT username from users });
    my $rc = $sth->execute;
    print "$sth->{COUNT} Users registered\n\n";
    $dbh->disconnect;
}

sub auth {
    my ($login, $password) = shift;
    my $stmt = q~SELECT Password FROM users WHERE Username = ?~;
    my $sth = $dbh->prepare( $stmt ) or die $dbh->errstr;
    $sth->execute( $login ) or die $dbh->errstr();
    my ($password_check) = $sth->fetchrow_array;

    my $return = 0;
    if( $password_check ){
        $return = $password eq $password_check ? 1 : 2;
    }
}

my %map = (
    0 => \&createuser,  # 0 => Userexistiert noch gar nicht
    1 => \&everything_is_fine, # 1 => Login hat geklappt
    2 => \&wrong_login, # 2 => Passwort war falsch eingegeben
);

sub wrong_login {
    print("Seems like you entered a wrong username/pass combo...\n");
    exit 0;
}

sub createuser {
    print("Username not found, do you want to register a new Account? (Y/n) >");
    chomp(my $answer = <STDIN>);
    if ($answer eq 'n') {
        print("Okay cya!\n");
        exit 0;
    } else {
        print("Okay, enter your desired login-name: ");
        chomp(my $login = <STDIN>);
        print("\nWell $login\, please enter a password: ");
        chomp(my $password = <STDIN>);
        print("\n");
        my $stmt = q~INSERT INTO users(username, password) VALUES($login, $password)~;
    }
}

sub everything_is_fine {
    print("Login DONE\n");
}

sub game {
    system("clear");
    print("Hello welcome to SlashEm!\n\nSlashEm is a modification of the great NetHack Game...\nMore Info: http://nethack.org/v343/Guidebook.html or press '?' ingame...\n\n");
    sleep("5");
    print("Login: ");
    chomp(my $login = <STDIN>);
    print("Password: ");
    chomp(my $password = <STDIN>);
    my $retval = auth($login, $password);
    if( exists $map{$retval} ){
        $map{$retval}->();
    } else {
        die "irgendwas ganz komisches ist passiert"
    }
    system("clear");
    print("You enter the World of SlashEm... Currently there are", countusers(), "Users registered\n\n");
    sleep("2");
}


### MAIN ###
main {
    game();
    #system("$gamebin -u $name");
}

View full thread Ganz simples User management?