An HB pencil and carbon paper?
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Hooray!
Wanted!
| [reply] |
Simple, you would extend your code to allow it.
If you would like insightful answers try posting more information, code snippits and a well defined problem. | [reply] |
ok, this is kinda long, and very sloppy, i dug it up from a long time ago, alot of the code is just plain wrong and redundant, but i want the players to be able to communicate with eachother, so here goes:
#!/usr/bin/perl
use strict;
use threads;
use IO::Socket::INET;
$| ++;
my $listener = IO::Socket::INET->new
(
LocalPort => 1337,
Listen => 5,
Reuse => 1
) || die "Cannot create socket\n";
warn "server up and ready for connections...... \n";
our $hold;
our $client;
our $m = 2;
my $client_num = 0;
while (1){
our $client = $listener->accept;
threads->create(\&start_thread, $client, ++ $client_n
+um);
}
sub start_thread{
my @grab;
our $st = 100;
our $hp = 100;
my ($client, $client_num) = @_;
print "thread created for client $client_num\n";
push @grab, $client;
print @grab;
print $client "\t\t\t****** ****** ********\n\r";
print $client "\t\t\t* * * * *\n\r";
print $client "\t\t\t* * * * *\n\r";
print $client "\t\t\t****** * * * *****\n\r";
print $client "\t\t\t* * * * *\n\r";
print $client "\t\t\t* # ****** # ******** #\n\r";
print $client "Welcome to the POG Test server!\n\r";
print $client "you will be know as player $client_num\n\r";
print $client "type : HELP for list of commands\n\r";
&begin;
return;
}
sub begin{
while(our $line = <$client>){
print $line;
if($line =~ /slp/i){&slp;}
if($line =~ /med/i){&med;}
if($line =~ /kik/i){&kik;}
if($line =~ /help/i){&help;}
if($line =~ /wke/i){&wke;}
if($line =~ /hp/i){&hp;}
if($line =~ /pun/i){&pun;}
if($line =~ /st/i){&st;}
}
}
sub hp{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\r";
&begin;
}
print $client "your health is: ";
print $client our $hp;
print $client "\n\r";
}
sub st{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\r";
&begin;
}
print $client "you stamina is: ";
print $client our $st;
print $client "\n\r";
}
sub slp{
if (our $m eq 1){
print $client "you are already sleeping/meditatin
+g.\n\r";
&begin;
}
our $time = time;
our $m = 1;
print $client "you fall into a deep slumber.....\n\r";
&begin;
}
sub med{
if (our $m eq 1){
print $client "you are already sleeping/medita
+ting.\n\r";
&begin;
}
our $time = time;
our $m = 1;
print $client "you sit down cross-legged on the floor and pres
+s your palms together, you slip into a deep stage of meditation\n\r";
&begin;
}
sub wke{
if (our $m eq 2){
print $client "you are already awake\n\r";
&begin;
}
our $time2 = time;
our $check = our $time2 - our $time;
our $hp = $hp + $check;
our $m = 2;
print $client "you wake up from your deep sleep, feeling very
+refreshed!\n\r";
&begin;
}
sub kik{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\
+r";
&begin;
}
print $client "You kick yourself in the shin, and loose 10 HP\
+n\r";
our $hp = $hp - 10;
if(our $hp < 1)
{
print "$client_num has died......\n\r";
print $client "$client_num has died......\n\r";
&start_thread;
}
&begin;
}
sub pun{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\r";
&begin;
}
print $client "You punch yourself in the face, and loo
+se 10 HP\n\r";
our $hp = $hp - 10;
if(our $hp < 1){
print "$client_num has died......\n\r";
print $client "$client_num has died......\n\r";
&start_thread;
}
&begin;
}
sub help{
print $client "***************************************
+*****************************************\n\r";
print $client "MED = Allows you to begin meditating, which re
+gains stamina *\n\r";
print $client "SLP = Allows you to sleep, which regains healt
+h *\n\r";
print $client "WKE = Wakes you up from sleeping or meditation
+ *\n\r";
print $client "KIK = Preforms a kick at the first liv
+ing thing you see *\n\r";
print $client "HELP = brings you back to this menu
+ *\n\r";
print $client "HP = prints your Health
+ *\n\r";
print $client "PUN = preforms a punch at the first living thi
+ng you see *\n\r";
print $client "ST = Prints your stamina
+ *\n\r";
print $client "***********************************************
+*********************************\n\r";
&begin;
}
| [reply] [d/l] |
I liked this question, because it seems like it is a useful feature to have....broadcasting to all threads. I was hoping one of the experts would have handed us an answer, but not yet. :-) So I hacked on it for awhile, and narrowed it down to a problem where a global array is really not global. So the code below is where I left it, I hit a road block. Basically I made a "msg" sub, that when any thread typed "msg somestring" it should be broadcast to the other threads.Well I did get some limited success, where if the message was sent from the last thread created, it would send it to all threads, because the copy of @clients it gets is the only one which contain all the previous
clients. I tried it a second way, getting all threads with threads->list, but was getting an error when I tried to print to them, "Not a glob reference.....". :-(
So what I was doing to test my code was start the main server, then start 3 xterms and telneting in from each of them with "telnet 127.0.01 1337". And if I typed "msg 333333333" from the last created xterm, it would broadcast. But xterms of
an earlier creation would not write to xterms of later creation.
I tried refering to @clients as "main::@clients" but no-good.
#!/usr/bin/perl
use strict;
use threads;
use IO::Socket::INET;
use Data::Dumper;
$| ++;
my $listener = IO::Socket::INET->new
(
LocalPort => 1337,
Listen => 5,
Reuse => 1
) || die "Cannot create socket\n";
warn "server up and ready for connections...... \n";
our $hold;
our $client;
our $m = 2;
my $client_num = 0;
our @clients; #seems global, but isn't
my $broadcastmessage;
while (1){
our $client = $listener->accept;
threads->create(\&start_thread, $client, ++$client_num);
push(@clients,$client);
print Dumper([@clients]),"\n";
}
sub start_thread{
our $st = 100;
our $hp = 100;
my ($client, $client_num) = @_;
push(@clients,$client);
print "thread created for client $client_num\n";
print $client "\t\t\t****** ****** ********\n\r";
print $client "\t\t\t* * * * *\n\r";
print $client "\t\t\t* * * * *\n\r";
print $client "\t\t\t****** * * * *****\n\r";
print $client "\t\t\t* * * * *\n\r";
print $client "\t\t\t* # ****** # ******** #\n\r";
print $client "Welcome to the POG Test server!\n\r";
print $client "you will be know as player $client_num\n\r";
print $client "type : HELP for list of commands\n\r";
&begin;
return;
}
sub begin{
while(our $line = <$client>){
print $line;
if($line =~ /slp/i){&slp;}
if($line =~ /med/i){&med;}
if($line =~ /kik/i){&kik;}
if($line =~ /help/i){&help;}
if($line =~ /wke/i){&wke;}
if($line =~ /hp/i){&hp;}
if($line =~ /pun/i){&pun;}
if($line =~ /st/i){&st;}
if($line =~ /msg/i){&msg($line);}
}
}
sub hp{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\r";
&begin;
}
print $client "your health is: ";
print $client our $hp;
print $client "\n\r";
}
sub st{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\r";
&begin;
}
print $client "you stamina is: ";
print $client our $st;
print $client "\n\r";
}
sub slp{
if (our $m eq 1){
print $client "you are already sleeping/meditating.\n\r";
&begin;
}
our $time = time;
our $m = 1;
print $client "you fall into a deep slumber.....\n\r";
&begin;
}
sub med{
if (our $m eq 1){
print $client "you are already sleeping/meditating.\n\r";
&begin;
}
our $time = time;
our $m = 1;
print $client "you sit down cross-legged on the floor and press your p
+alms together, you s
&begin;
}
sub wke{
if (our $m eq 2){
print $client "you are already awake\n\r";
&begin;
}
our $time2 = time;
our $check = our $time2 - our $time;
our $hp = $hp + $check;
our $m = 2;
print $client "you wake up from your deep sleep, feeling very refreshe
+d!\n\r";
&begin;
}
sub kik{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\r";
&begin;
}
print $client "You kick yourself in the shin, and loose 10 HP\n\r";
our $hp = $hp - 10;
if(our $hp < 1)
{
print "$client_num has died......\n\r";
print $client "$client_num has died......\n\r";
&start_thread;
}
&begin;
}
sub pun{
if (our $m eq 1){
print $client "you are sleeping/meditating.\n\r";
&begin;
}
print $client "You punch yourself in the face, and loose 10 HP
+\n\r";
our $hp = $hp - 10;
if(our $hp < 1){
print "$client_num has died......\n\r";
print $client "$client_num has died......\n\r";
&start_thread;
}
&begin;
}
sub help{
print $client "***********************************************
+********************
print $client "MED = Allows you to begin meditating, which regains st
+amina
print $client "SLP = Allows you to sleep, which regains health
print $client "WKE = Wakes you up from sleeping or meditation
print $client "KIK = Preforms a kick at the first living thin
+g you see
print $client "HELP = brings you back to this menu
print $client "HP = prints your Health
print $client "PUN = preforms a punch at the first living thing you s
+ee
print $client "ST = Prints your stamina
print $client "*******************************************************
+********************
&begin;
}
sub msg{
$broadcastmessage = shift;
my $tid = threads->tid;
print "caller-> $tid\n";
#works but only from last thread created
#can't figure out how to make @clients truly global
print "@clients\n";
foreach my $client (@clients){
print $client "$broadcastmessage\n";
}
#gives all the threads, but gives error
# "Not a glob reference when tryint ot print to it
#my @list = threads->list;
# print "list-> @list\n";
# foreach my $clientz (@list){
# my $tid = $clientz->tid,"\n";
# my $client = threads->object($tid);
# print $client "$broadcastmessage\n";
# }
return;
}
| [reply] [d/l] |