-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.rb
105 lines (80 loc) · 2.49 KB
/
player.rb
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
93
94
95
96
97
98
99
100
101
102
103
104
105
class Player # This should be used for players who don't have an assigned role
attr_reader :send_cb, :die_cb, :dead
attr_accessor :name
def initialize(name, cbs = {})
@name = name
@send_cb = (cbs[:send] || proc do |msg| puts msg end)
@die_cb = (cbs[:die] || proc do puts 'You have died' end)
@dead = false
end
def send(msg)
@send_cb.call(msg)
end
def die
@dead = true
@die_cb.call
end
def to_s
@name
end
def lynch_vote(day, who)
day.act([self, :lynch_vote, who])
end
def brief_night(players = [])
end
def brief_day(players = [])
send("It is time for you to be heard. Use the '@Gsay@d' command to speak and the '@Gvote@d' command to vote for who you think should be lynched. You may abstain from voting.")
send("(note: if you don't want to lynch anybody, either don't vote or type '@Gvote@d' by itself)")
end
@color = 'd'
def self.color
@color
end
def self.get_role_by_name(name)
if get_roles.index(name.downcase)
const_get(name.capitalize)
end
end
def self.get_roles
ObjectSpace.each_object(Class).select { |klass| klass < self }.map { |klass| klass.name.downcase }
end
end
module Visiter
def visit(night, who, more = nil)
raise NotImplementedError
end
end
# The two fundamental types of players
class Villager < Player
@color = 'G'
end
class Mafia < Player
include Visiter
@color = 'R'
def kill(night, who, more = nil)
night.act([self, :mafia_kill, who])
end
def mafia_chat(night, message)
night.act([self, :mafia_chat, message])
end
def brief_night(players = [])
send("It is your time to act. You may choose to kill someone by typing '@Gvisit <name>@d'")
send(" @R*@d Who you can kill: @r#{players.select { |pl| !pl.is_a?(Mafia) }.map(&:name).join('@d, @r') }@d")
send(" @R*@d The mafia in this game: @c#{players.select { |pl| pl.is_a? Mafia }.map(&:name).join('@d, @c') }@d")
send(" @R*@d You can chat with the aformentioned with '@Gmchat <message>@d'")
end
alias_method :visit, :kill
end
# Special subcategory of Villager
class Cop < Villager
include Visiter
@color = 'G'
def investigate(night, who, more = nil)
night.act([self, :investigate, who])
end
def brief_night(players = [])
send("It is your time to act. You may choose to kill someone by typing '@Gvisit <name>'@d")
send(" @R*@d Who you can investigate: #{players.map(&:name).join(', ')}")
end
alias_method :visit, :investigate
end