Debian Codegraph @

tests/inputs/messages.rb

raw
1 #!/usr/local/bin/ruby
2 # messages.rb - this is a test for the Ruby SLOC counter.
3 # You should get 110 SLOC for this file.
4
5 # Guru module: private messages among players
6 # Copyright (C) 2001, 2002 Josef Spillner, [email protected]
7 # This is used as a test case in SLOCCount, a toolsuite that counts
8 # source lines of code (SLOC).
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #
24 # To contact David A. Wheeler, see his website at:
25 # http://www.dwheeler.com.
26 #
27 #
28
29 # Commands:
30 # guru do i have any messages
31 # guru tell grubby nice to meet myself :)
32 # guru alert grubby
33
34 databasedir = ENV['HOME'] + "/.ggz/grubby"
35
36 ####################################################################################
37
38 class GuruMessages
39 def initialize
40 @msg = Array.new
41 @alerts = Array.new
42 end
43 def add(fromplayer, player, message)
44 @entry = Array.new
45 newmessage = (fromplayer + " said: " + message.join(" ")).split(" ")
46 @entry << player << newmessage
47 @msg.push(@entry)
48 print "OK, I make sure he gets the message."
49 $stdout.flush
50 sleep 1
51 end
52 def tell(player)
53 len = @msg.length
54 a = 0
55 for i in 0..len
56 unless @msg[len-i] == nil
57 print @msg[len-i][1][0..@msg[len-i][1].length - 1].join(" ") + "\n" if player == @msg[len-i][0]
58 if player == @msg[len-i][0]
59 @msg.delete_at(len-i)
60 a = 1
61 end
62 end
63 end
64 if a == 0
65 print "Sorry " + player + ", I guess you're not important enough to get any messages."
66 end
67 $stdout.flush
68 sleep 1
69 end
70 def alert(fromplayer, player)
71 @entry = Array.new << fromplayer << player
72 @alerts.push(@entry)
73 print "OK, I alert " + player + " when I see him."
74 $stdout.flush
75 sleep 1
76 end
77 def trigger(player)
78 len = @alerts.length
79 a = 0
80 for i in 0..len
81 unless @alerts[len-i] == nil
82 if player == @alerts[len-i][0]
83 print player + ": ALERT from " + @alerts[len-i][1] + "\n"
84 @alerts.delete_at(len-i)
85 a = 1
86 end
87 end
88 end
89 if a == 1
90 $stdout.flush
91 sleep 1
92 return 1
93 end
94 return 0
95 end
96 end
97
98 input = $stdin.gets.chomp.split(/\ /)
99
100 mode = 0
101 if (input[1] == "do") && (input[2] == "i") && (input[3] == "have") &&
102 (input[4] == "any") && (input[5] == "messages")
103 mode = 1
104 player = ARGV[0]
105 end
106 if (input[1] == "tell")
107 mode = 2
108 fromplayer = ARGV[0]
109 player = input[2]
110 message = input[3..input.length]
111 end
112 if(input[1] == "alert")
113 mode = 3
114 fromplayer = ARGV[0]
115 player = input[2]
116 end
117
118 m = nil
119 begin
120 File.open(databasedir + "/messages") do |f|
121 m = Marshal.load(f)
122 end
123 rescue
124 m = GuruMessages.new
125 end
126
127 if mode == 0
128 ret = m.trigger ARGV[0]
129 if ret == 0
130 exit
131 end
132 end
133 if mode == 1
134 if player != nil
135 m.tell player
136 else
137 print "If you mind telling me who you are?"
138 $stdout.flush
139 sleep 1
140 end
141 end
142 if mode == 2
143 m.add fromplayer, player, message
144 end
145 if mode == 3
146 m.alert fromplayer, player
147 end
148
149 File.open(databasedir + "/messages", "w+") do |f|
150 Marshal.dump(m, f)
151 end
152