Class: Dat::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/dat/interface.rb

Constant Summary collapse

RECENT_AMOUNT =
4

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Interface

Returns a new instance of Interface.



14
15
16
17
# File 'lib/dat/interface.rb', line 14

def initialize(logger)
  @games = Games.new(logger)
  @dict_adjustments = File.open(File.expand_path('../../../data/possible-bogus', __FILE__), 'a')
end

Instance Method Details

#define(word) ⇒ Object



111
112
113
# File 'lib/dat/interface.rb', line 111

def define(word)
  @games.dict[word.strip.upcase].definition.capitalize rescue "Not a word."
end

#dict_entry(args) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/dat/interface.rb', line 115

def dict_entry(args)
  result = []
  args.each do |w|
    result << @games.dict[w.upcase].to_dict_entry if @games.dict[w.upcase]
  end
  result.join("\n")
end

#forfeit(gid) ⇒ Object



145
146
147
# File 'lib/dat/interface.rb', line 145

def forfeit(gid)
  @games[gid].forfeit(gid)
end

#helpObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dat/interface.rb', line 19

def help
  <<-END.gsub(/^ {8}/, "")
    dat - #{Dat::VERSION}

    !<command> - send a command to dat bot
    ?<word> - define the word if it exists
    @<word> - play a word

    Commands:
      help - displays this help
      end/forfeit - forfeits the game
      define <word>+ - fully defines words listed
      recent - displays the words recently played
      history - displays the entire game history
      time - time since the last move

      relative/r <word>+ - make words relative
  END
end

#history(gid, timed = false) ⇒ Object



141
142
143
# File 'lib/dat/interface.rb', line 141

def history(gid, timed=false)
  @games[gid].display(:all, timed)
end

#isolate(word) ⇒ Object

TODO concurrent editing of dicitionary is bad



81
82
83
84
85
86
87
# File 'lib/dat/interface.rb', line 81

def isolate(word)
  if @games.dict[word]
    @games.dict[word].isolate!
    @dict_adjustments.puts("!i #{msg}")
    @dict_adjustments.flush
  end
end

#move(from, word) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dat/interface.rb', line 123

def move(from, word)
  @games.check!(from)
  ([] << @games[from].play(from, word.strip.upcase) << @games[from].next_move!).join("\n")
rescue Move => e
  e.message
rescue NoGameError => n
  w = word.strip.upcase
  if @games.dict[w] || w == Dat::Game::START_WORD
    new_game(from, [], :start_word => w)
  else
    n.message
  end
end

#new_game(from, args = [], opt = {}) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dat/interface.rb', line 89

def new_game(from, args=[], opt={})
  opt[:players] = [from]
  args.each do |arg|
    k, v = arg.split(":")
    if k == 'bot'
      case v
      when 'simple' then opt[:players] << SimpleBot.new
      when 'hard' then raise NotImplementedError, "Hard is not implemented yet."
      else raise NotImplementedError, "No other bots are implemented."
      end
    else
      opt[k] = v
    end
  end

  if opt[:players].size < Dat::Game::MIN_PLAYERS
    opt[:players] << SimpleBot.new # Default Bot
  end

  @games.add(from, opt)
end

#recent(gid, timed = false) ⇒ Object



137
138
139
# File 'lib/dat/interface.rb', line 137

def recent(gid, timed=false)
  @games[gid].display(RECENT_AMOUNT, timed)
end

#relatives(args) ⇒ Object

TODO concurrent editing of dicitionary is bad



74
75
76
77
78
# File 'lib/dat/interface.rb', line 74

def relatives(args)
  Word.relatives(args.map { |w| @games.dict[w] }.compact)
  @dict_adjustments.puts("!r #{args}")
  @dict_adjustments.flush
end

#respond(from, message) ⇒ Object



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
# File 'lib/dat/interface.rb', line 39

def respond(from, message)
  return define(@games[from].last) if message.strip == "?!" || message.strip == "!?"
  prefix, msg, *args = message[0], *message[1, message.size].split
  case prefix
  when '!' then
    case msg
    when 'help' then help
    when 'dat', 'new' then new_game(from, args)
    when 'hard' then new_game(from, args, :delete => :false)
    when 'end', 'forfeit' then forfeit(from)
    when 'define', 'd' then dict_entry(args)
    when 'recent' then recent(from)
    when 'trecent', 'tr' then recent(from, true)
    when 'history', 'h' then history(from)
    when 'thistory', 'th' then history(from, true)
    when 'time', 't' then time(from)
    when 'used', 'u' then used(from)
    when 'relative', 'relatives', 'r' then relatives(args)
    when 'isolate', 'i' then isolate(msg)
    else nil
    end
  when '?' then define(msg)
  when '@' then move(from, msg)
  else nil
  end
rescue NoGameError => e
  e.message
rescue
  # TODO debugging code
  puts $!.message
  puts $!.backtrace.join("\n")
  "<<ERROR>> y u break me, daphne? (report this bug)"
end

#time(gid) ⇒ Object



149
150
151
# File 'lib/dat/interface.rb', line 149

def time(gid)
  @games[gid].time
end

#used(gid) ⇒ Object



153
154
155
# File 'lib/dat/interface.rb', line 153

def used(gid)
  @games[gid].used.keys.join(" ")
end