implemented bots

This commit is contained in:
Jeena Paradies 2011-05-05 05:12:55 +02:00
parent d69b12b8d6
commit fcaf8f71bc
4 changed files with 157 additions and 24 deletions

147
games/JS-chat/chat.rb Normal file → Executable file
View file

@ -4,19 +4,34 @@ $: << "."
require 'ggs-network.rb'
require 'ggs-delegate.rb'
require 'my-random.rb'
require 'readline'
class Chat
include GGSDelegate
include MyRandom
def initialize
print "Table token (empty for new): "
table_token = gets.chomp
@@log_file_path = "/tmp/ggs-log.csv"
@@bot_threads = []
def initialize(is_bot=false, table_token="")
@is_bot = is_bot
@log = nil
@ignore = false
stty_save = `stty -g`.chomp
trap('INT') { system('stty', stty_save); exit }
print "Table token (empty for new): " unless @is_bot
table_token = gets.chomp unless @is_bot
@ggs_network = GGSNetwork.new(self, table_token)
@ggs_network.connect("home.jeena.net", 9000)
@ggs_network.connect("ggs.jeena.net", 9000)
end
def ggsNetworkReady(ggs_network, am_i_host)
def ggsNetworkReady(ggs_network, am_i_host)
unless am_i_host
say @ggs_network.table_token
source_code = File.open("chat_server.js", "rb").read
@ggs_network.define(source_code)
else
@ -26,14 +41,29 @@ class Chat
def ggsNetworkDefined(ggs_network, defined)
if defined
print "Your nickname: "
nick = gets.chomp
@ggs_network.sendCommand("/nick", nick)
Thread.new {
@nick = ""
while @nick == ""
print "\rYour nickname: " unless @is_bot
unless @is_bot
@nick = gets.chomp
else
@nick = random_nick
end
end
@ggs_network.sendCommand("/nick", @nick)
t = Thread.new {
loop do
input
unless @is_bot
input
else
sleep(rand 2) # interfall for bot to do something
random_function
end
end
}
@@bot_threads << t if @is_bot
else
source_code = File.open("chat_server.js", "rb").read
@ggs_network.define(source_code)
@ -50,22 +80,60 @@ class Chat
protected
def start_bots(number)
number.times { |n|
say "<starting bot #{n}>"
@@bot_threads << Thread.new {
Chat.new(true, @ggs_network.table_token)
}
}
end
def stop_bots
@@bot_threads.each do |bot|
bot.kill
end
@bot_threads = []
say "<stopped all bots>"
end
def message(message)
puts message
say message
end
def notice(notice)
puts "<#{notice}>"
say "<#{notice}>"
end
def input
message = gets.chomp
if message[0..5] == "/nick "
@ggs_network.sendCommand("/nick", message[6,-1])
def input(message="")
message = Readline.readline('> ', true) unless @is_bot
if message[0,6] == "/nick "
@nick = message[6..-1]
@ggs_network.sendCommand("/nick", @nick)
elsif message == "/ping"
ping()
elsif message[0,6] == "/bots "
number = message[6..-1].to_i
say "<starting #{number} bots>"
start_bots(number)
elsif message == "/bots"
stop_bots
elsif message == "/log"
toggle_log
elsif message == "/help"
help
elsif message == "/ignore"
@ignore = @ignore ? false : true
if @ignore
say "<ignoring on>"
else
say "<ignoring off>"
end
else
@ggs_network.sendCommand("message", message)
@ggs_network.sendCommand("message", message) unless message == ""
end
end
@ -74,12 +142,51 @@ class Chat
@ggs_network.sendCommand("ping", @ggs_network.player_token)
end
def pong(id)
puts "<pong: " + (Time.now - @start_ping).to_s + ">"
def pong(server_log)
time = (Time.now - @start_ping).to_s
say "<pong: #{time} #{server_log}>"
File.open(@@log_file_path, 'a') {|f| f.write(time << ",#{server_log}\n") } unless @log.nil?
end
def say(something)
unless @ignore or @is_bot
puts "\r#{something}"
print "> "
end
end
def toggle_log
if @log.nil?
say "<starting logging>"
@log = Thread.new {
loop {
sleep 1
ping
}
}
else
@log.kill
@log = nil
say "<stopped logging>"
end
end
def help
puts "+-----------------------------------------------+"
puts "| something | normal message |"
puts "| /nick something | changing your nick |"
puts "| /bots n | start n bots |"
puts "| /bots | stop all bots |"
puts "| /log | toggle logging |"
puts "| /ignore | toggle ignoring everyone |"
puts "| /help | show this help |"
puts "+-----------------------------------------------+"
end
end
if __FILE__ == $0
Chat.new
Chat.new
end