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

View file

@ -13,7 +13,7 @@ function changeNick(player_id, nick) {
GGS.localStorage.setItem("nick_" + player_id, nick);
if (!old_nick) {
GGS.sendCommandToAll("notice", nick + " joined");
GGS.sendCommandToAll("notice", nick + " joined ä");
} else {
GGS.sendCommandToAll("notice", old_nick + " is now called " + nick);
}

View file

@ -7,7 +7,7 @@ class GGSNetwork
public
attr_accessor :delegate, :player_token
attr_accessor :delegate, :player_token, :table_token
def initialize(delegate, table_token="")
@table_token = table_token
@ -80,7 +80,7 @@ class GGSNetwork
message = ""
message += "Token: #{@player_token}\n" unless @player_token.nil?
message += "#{serverOrGame}-Command: #{command}\n" +
"Content-Length: #{args.length}\n\n"
"Content-Length: #{args.bytesize}\n\n"
message += args if args.length > 0
@ -90,7 +90,6 @@ class GGSNetwork
def parse_hello(message)
@player_token, shall_define, @table_token = message.split(",")
@am_i_host = shall_define == "true"
puts "Table-Token: " + @table_token
end
end

View file

@ -0,0 +1,27 @@
module MyRandom
def random_function
funcs = []
funcs << lambda { ping() }
funcs << lambda { input("/nick " + random_nick) }
20.times { funcs << lambda { input(random_message) } }
funcs[rnd(0,funcs.length)].call
end
def random_message
random_string(rnd(1,30))
end
def random_nick
random_string(rnd(1,6))
end
def random_string(length)
o = [('a'..'z'),('A'..'Z')].map{|i| i.to_a}.flatten;
(0..length).map{ o[rand(o.length)] }.join;
end
def rnd(min, max)
((rand * (max - min)) + min).to_i
end
end