carps 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. data/COPYING +674 -0
  2. data/GEM_DESCRIPTION +10 -0
  3. data/History.txt +4 -0
  4. data/Manifest.txt +156 -0
  5. data/PostInstall.txt +20 -0
  6. data/README.rdoc +141 -0
  7. data/Rakefile +28 -0
  8. data/bin/carps +123 -0
  9. data/bin/carps_init +29 -0
  10. data/bin/carps_ipc_test +44 -0
  11. data/bin/carps_mod_saver_test +71 -0
  12. data/bin/carps_mod_test +62 -0
  13. data/config/website.yml +2 -0
  14. data/features/character_sheet.feature +40 -0
  15. data/features/crash.feature +15 -0
  16. data/features/crypt.feature +22 -0
  17. data/features/dice.feature +173 -0
  18. data/features/dm.feature +36 -0
  19. data/features/dmll.feature +51 -0
  20. data/features/edit.feature +10 -0
  21. data/features/email.feature +29 -0
  22. data/features/interface.feature +17 -0
  23. data/features/ipc.feature +10 -0
  24. data/features/mailbox.feature +33 -0
  25. data/features/mod.feature +31 -0
  26. data/features/parser.feature +9 -0
  27. data/features/persistent_protocol.feature +14 -0
  28. data/features/player.feature +22 -0
  29. data/features/player_turn.feature +29 -0
  30. data/features/random.feature +15 -0
  31. data/features/rule.feature +19 -0
  32. data/features/safety.feature +13 -0
  33. data/features/sessions.feature +16 -0
  34. data/features/start_dm.feature +18 -0
  35. data/features/start_player.feature +8 -0
  36. data/features/step_definitions/common_steps.rb +170 -0
  37. data/features/steps/character_sheet.rb +89 -0
  38. data/features/steps/crash.rb +27 -0
  39. data/features/steps/crypt.rb +166 -0
  40. data/features/steps/dice.rb +91 -0
  41. data/features/steps/dm.rb +147 -0
  42. data/features/steps/dmll.rb +47 -0
  43. data/features/steps/edit.rb +7 -0
  44. data/features/steps/email.rb +108 -0
  45. data/features/steps/general.rb +5 -0
  46. data/features/steps/interface.rb +64 -0
  47. data/features/steps/ipc.rb +22 -0
  48. data/features/steps/mailbox.rb +126 -0
  49. data/features/steps/mod.rb +25 -0
  50. data/features/steps/parser.rb +23 -0
  51. data/features/steps/persistent_protocol.rb +29 -0
  52. data/features/steps/player.rb +82 -0
  53. data/features/steps/player_turn.rb +56 -0
  54. data/features/steps/random.rb +47 -0
  55. data/features/steps/rule.rb +53 -0
  56. data/features/steps/safety.rb +17 -0
  57. data/features/steps/sessions.rb +37 -0
  58. data/features/steps/start_dm.rb +46 -0
  59. data/features/steps/start_player.rb +65 -0
  60. data/features/steps/timeout.rb +32 -0
  61. data/features/steps/type_verification.rb +36 -0
  62. data/features/steps/wizard.rb +123 -0
  63. data/features/support/common.rb +29 -0
  64. data/features/support/env.rb +14 -0
  65. data/features/support/matchers.rb +11 -0
  66. data/features/timeout.feature +11 -0
  67. data/features/type_verification.feature +48 -0
  68. data/features/wizard.feature +50 -0
  69. data/lib/carps/crypt/accept_handshake.rb +40 -0
  70. data/lib/carps/crypt/default_messages.rb +29 -0
  71. data/lib/carps/crypt/handshake.rb +41 -0
  72. data/lib/carps/crypt/mailbox.rb +265 -0
  73. data/lib/carps/crypt/mailer.rb +220 -0
  74. data/lib/carps/crypt/peer.rb +123 -0
  75. data/lib/carps/crypt/public_key.rb +60 -0
  76. data/lib/carps/crypt.rb +23 -0
  77. data/lib/carps/email/config.rb +122 -0
  78. data/lib/carps/email/imap.rb +156 -0
  79. data/lib/carps/email/smtp.rb +119 -0
  80. data/lib/carps/email/string.rb +30 -0
  81. data/lib/carps/email.rb +21 -0
  82. data/lib/carps/mod/action.rb +36 -0
  83. data/lib/carps/mod/answers.rb +76 -0
  84. data/lib/carps/mod/client_turn.rb +88 -0
  85. data/lib/carps/mod/dice.rb +360 -0
  86. data/lib/carps/mod/dm/interface.rb +160 -0
  87. data/lib/carps/mod/dm/mod.rb +409 -0
  88. data/lib/carps/mod/dm/reporter.rb +112 -0
  89. data/lib/carps/mod/dm/resource.rb +88 -0
  90. data/lib/carps/mod/dm/room.rb +33 -0
  91. data/lib/carps/mod/interface.rb +73 -0
  92. data/lib/carps/mod/launch.rb +108 -0
  93. data/lib/carps/mod/mod.rb +52 -0
  94. data/lib/carps/mod/player/interface.rb +76 -0
  95. data/lib/carps/mod/player/mod.rb +109 -0
  96. data/lib/carps/mod/question.rb +63 -0
  97. data/lib/carps/mod/rule.rb +112 -0
  98. data/lib/carps/mod/sheet/character.rb +82 -0
  99. data/lib/carps/mod/sheet/editor.rb +97 -0
  100. data/lib/carps/mod/sheet/new_sheet.rb +71 -0
  101. data/lib/carps/mod/sheet/schema.rb +84 -0
  102. data/lib/carps/mod/sheet/type.rb +161 -0
  103. data/lib/carps/mod/sheet/verifier.rb +41 -0
  104. data/lib/carps/mod/status_report.rb +51 -0
  105. data/lib/carps/mod.rb +40 -0
  106. data/lib/carps/protocol/keyword.rb +104 -0
  107. data/lib/carps/protocol/message.rb +138 -0
  108. data/lib/carps/protocol.rb +19 -0
  109. data/lib/carps/service/client_parser.rb +34 -0
  110. data/lib/carps/service/dm/config.rb +76 -0
  111. data/lib/carps/service/dm/mailer.rb +70 -0
  112. data/lib/carps/service/dm/new_game.rb +101 -0
  113. data/lib/carps/service/dm/start.rb +47 -0
  114. data/lib/carps/service/game.rb +101 -0
  115. data/lib/carps/service/interface.rb +174 -0
  116. data/lib/carps/service/invite.rb +79 -0
  117. data/lib/carps/service/mod.rb +43 -0
  118. data/lib/carps/service/player/config.rb +78 -0
  119. data/lib/carps/service/player/mailer.rb +49 -0
  120. data/lib/carps/service/player/start.rb +60 -0
  121. data/lib/carps/service/server_parser.rb +33 -0
  122. data/lib/carps/service/session.rb +96 -0
  123. data/lib/carps/service/start/config.rb +71 -0
  124. data/lib/carps/service/start/interface.rb +101 -0
  125. data/lib/carps/service/start/mailer.rb +46 -0
  126. data/lib/carps/service.rb +35 -0
  127. data/lib/carps/test.rb +34 -0
  128. data/lib/carps/ui/colour.rb +28 -0
  129. data/lib/carps/ui/error.rb +39 -0
  130. data/lib/carps/ui/highlight.rb +32 -0
  131. data/lib/carps/ui/question.rb +63 -0
  132. data/lib/carps/ui/warn.rb +37 -0
  133. data/lib/carps/ui.rb +21 -0
  134. data/lib/carps/util/config.rb +162 -0
  135. data/lib/carps/util/editor.rb +147 -0
  136. data/lib/carps/util/error.rb +75 -0
  137. data/lib/carps/util/files.rb +48 -0
  138. data/lib/carps/util/init.rb +93 -0
  139. data/lib/carps/util/process.rb +150 -0
  140. data/lib/carps/util/timeout.rb +31 -0
  141. data/lib/carps/util/windows.rb +29 -0
  142. data/lib/carps/util.rb +24 -0
  143. data/lib/carps/wizard/dm.rb +41 -0
  144. data/lib/carps/wizard/player.rb +40 -0
  145. data/lib/carps/wizard/steps.rb +494 -0
  146. data/lib/carps/wizard/wizard.rb +126 -0
  147. data/lib/carps/wizard.rb +21 -0
  148. data/lib/carps.rb +45 -0
  149. data/permission +16 -0
  150. data/script/console +10 -0
  151. data/script/destroy +14 -0
  152. data/script/generate +14 -0
  153. data/tasks/readme_site.rake +28 -0
  154. data/test/test_carps.rb +11 -0
  155. data/test/test_helper.rb +3 -0
  156. data/website/index.html +271 -0
  157. metadata +304 -0
@@ -0,0 +1,19 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/protocol/keyword"
19
+ require "carps/protocol/message"
@@ -0,0 +1,34 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/service/game"
19
+ require "carps/crypt/default_messages"
20
+
21
+ require "carps/mod/client_turn"
22
+
23
+ module CARPS
24
+
25
+ module Player
26
+
27
+ # Create a parser which parses messages for the client
28
+ def Player::parser
29
+ MessageParser.new default_messages + [Invite, ClientTurn]
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,76 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/service/game"
19
+ require "carps/service/start/config"
20
+
21
+ require "yaml"
22
+
23
+ module CARPS
24
+
25
+ module DM
26
+ # Class to read game configuration files
27
+ class GameConfig < SessionConfig
28
+
29
+ # Create a new GameConfig
30
+ def initialize filename, mod, campaign, about, session, dm
31
+ super session, filename
32
+ @campaign = campaign
33
+ @mod = mod
34
+ @about = about
35
+ @dm = dm
36
+ end
37
+
38
+ # Parse a game config file
39
+ def parse_yaml conf
40
+ super
41
+ @campaign = read_conf conf, "campaign"
42
+ @mod = read_conf conf, "mod"
43
+ @about = read_conf conf, "about"
44
+ @dm = read_conf conf, "dm"
45
+ end
46
+
47
+ # Display information on this configuration
48
+ def display
49
+ puts "Mod: " + @mod
50
+ puts "Campaign: " + @campaign
51
+ puts "Description:"
52
+ puts @about
53
+ puts "DM: " + @dm
54
+ end
55
+
56
+ # Return a GameServer object that can communicate with players
57
+ def spawn
58
+ GameServer.new @mod, @campaign, @about, @session, self, @dm
59
+ end
60
+
61
+ protected
62
+
63
+ # Emit as hash
64
+ def emit
65
+ {"mod" => @mod,
66
+ "campaign" => @campaign,
67
+ "about" => @about,
68
+ "dm" => @dm
69
+ }.merge super
70
+ end
71
+
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/ui"
19
+ require "carps/service"
20
+
21
+ module CARPS
22
+
23
+ module DM
24
+
25
+ # A bridge between the DM mod and CARPS
26
+ class Mailer < ModMailer
27
+
28
+ # Create the mailer from
29
+ #
30
+ # a Mailer,
31
+ #
32
+ # a GameConfig,
33
+ #
34
+ # a session id,
35
+ #
36
+ # the email address of the dm,
37
+ #
38
+ # the name of the mod,
39
+ #
40
+ # and the description of the game
41
+ def initialize mailer, conf, session, dm, mod, desc
42
+ super mailer, conf
43
+ @session = session
44
+ @dm = dm
45
+ @mod = mod
46
+ @about = desc
47
+ end
48
+
49
+ # Invite a new player
50
+ def invite addr
51
+ inv = Invite.new @dm, @mod, @about, @session
52
+ relay addr, inv
53
+ end
54
+
55
+
56
+ # Check for mail of a given type
57
+ def check type
58
+ @mailer.check type
59
+ end
60
+
61
+ # Send mail to the recipient
62
+ def relay to, message
63
+ @mailer.send to, message
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,101 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/service"
19
+
20
+ require "carps/ui"
21
+
22
+ module CARPS
23
+
24
+ module DM
25
+
26
+ # Interface for creating new games
27
+ class NewGameInterface < Interface
28
+
29
+ # Create a new NewGameInterface from a continuation
30
+ def initialize cont, manager, config, mailer
31
+ super()
32
+ @manager = manager
33
+ @game_config = config
34
+ @continuation = cont
35
+ @mailer = mailer
36
+ add_command :go, "Start the game."
37
+ add_command :describe, "Describe the game."
38
+ add_command :name, "Give the game a name.", "NAME"
39
+ add_command :mod, "Choose the mod.\n#{options load_mods.keys}", "MOD"
40
+ add_command :campaign, "Choose the campaign.", "CAMPAIGN"
41
+ end
42
+
43
+ protected
44
+
45
+ # Set the campaign
46
+ def campaign adventure
47
+ @campaign = adventure
48
+ end
49
+
50
+ # Set the mod
51
+ def mod user_mod
52
+ if load_mods.include?(user_mod)
53
+ @mod = user_mod
54
+ else
55
+ UI::put_error "No such mod is installed."
56
+ end
57
+ end
58
+
59
+ # Describe the game
60
+ def describe
61
+ editor = Editor.load
62
+ @description = editor.edit "# Enter description of game."
63
+ end
64
+
65
+ # Give the game a name
66
+ def name nm
67
+ @name = nm
68
+ end
69
+
70
+ # Start the game
71
+ def go
72
+ if @mod and @name and @description and @campaign
73
+ puts "The game details are:"
74
+ puts "Name: #{@name}"
75
+ puts "Mod: #{@mod}"
76
+ puts "Campaign: #{@campaign}"
77
+ puts "Description: #{@description}"
78
+
79
+ happy = UI::confirm "Are these details correct?"
80
+
81
+ if happy
82
+ mods = load_mods
83
+ session_id = @manager.generate @name + @mod + @campaign
84
+ config = @game_config.new @name, @mod, @campaign, @description, session_id, @mailer.address
85
+ config.save
86
+ game = config.spawn
87
+ @continuation.call lambda {
88
+ game.start @mailer
89
+ }
90
+ end
91
+ else
92
+ UI::put_error "You need to set the game options first."
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+ end
@@ -0,0 +1,47 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/service"
19
+
20
+ require "carps/util"
21
+
22
+ require "carps/ui"
23
+
24
+ module CARPS
25
+
26
+ module DM
27
+
28
+ # Interface for the dm to start games
29
+ class StartInterface < StartGameInterface
30
+
31
+ def initialize continuation, mailer, game_config, manager
32
+ super
33
+ add_command :new, "Start a new game."
34
+ end
35
+
36
+ protected
37
+
38
+ # Start new game
39
+ def new
40
+ new_game_interface = NewGameInterface.new @continuation, @manager, @game_config, @mailer
41
+ new_game_interface.run
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,101 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/service"
19
+
20
+ require "carps/protocol"
21
+
22
+ require "carps/ui"
23
+
24
+ require "carps/util"
25
+
26
+ require "drb"
27
+
28
+ module CARPS
29
+
30
+ # Server game
31
+ class GameServer
32
+
33
+ # The first parameter is email account information.
34
+ # The second is the mod.
35
+ # The third is the description.
36
+ # The fourth is the session key
37
+ # The fifth is the configuration file
38
+ # The sixth is the DM's address
39
+ def initialize mod, campaign, desc, session, conf, dm
40
+ @campaign = campaign
41
+ @mod = mod
42
+ @about = desc
43
+ @session = session
44
+ @conf = conf
45
+ @dm = dm
46
+ end
47
+
48
+ # Invite players to this game and begin
49
+ #
50
+ # FIXME: Just use 'play'
51
+ def start mailer
52
+ play mailer
53
+ end
54
+
55
+ # Resume this game
56
+ #
57
+ # FIXME: just use 'play'
58
+ def resume mailer
59
+ play mailer
60
+ end
61
+
62
+ private
63
+
64
+ def play mailer
65
+ mod = load_mods[@mod]
66
+ dm_mailer = DM::Mailer.new mailer, @conf, @session, @dm, @mod, @about
67
+ Process.singleton.launch dm_mailer, mod + " -h \"" + @campaign + "\""
68
+ end
69
+
70
+ end
71
+
72
+ # Client games
73
+ class GameClient
74
+
75
+ # The first parameter is the dungeon master's name
76
+ # The second is the mod.
77
+ # The third is the configuration file
78
+ def initialize dm, mod, conf
79
+ @dm = dm
80
+ @mod = mod
81
+ @conf = conf
82
+ end
83
+
84
+ # Join this game as a client
85
+ #
86
+ # FIXME: just use resume
87
+ def join_game mailer
88
+ resume mailer
89
+ end
90
+
91
+ # Play the game
92
+ def resume mailer
93
+ mod = load_mods[@mod]
94
+ player_mailer = Player::Mailer.new @dm, mailer, @conf
95
+ Process.singleton.launch player_mailer, mod + " -p"
96
+ end
97
+
98
+ end
99
+
100
+
101
+ end
@@ -0,0 +1,174 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/ui/question"
19
+
20
+ require "carps/ui/warn"
21
+
22
+ require "carps/ui/error"
23
+
24
+ module CARPS
25
+
26
+ # A basic user interface
27
+ #
28
+ # Subclass this interface to provide commands
29
+ class Interface
30
+
31
+ def initialize
32
+ @commands = {}
33
+ add_command :help, "Displays this help message."
34
+ end
35
+
36
+ def run
37
+ consistent!
38
+ help
39
+ repl
40
+ end
41
+
42
+ protected
43
+
44
+ # Provide a string of options
45
+ def options *opts
46
+ out = "Options are:\n\t"
47
+ out + opts.join("\n\t\tor\n\t")
48
+ end
49
+
50
+ # Ensure consistency
51
+ def consistent!
52
+ # Check we're working
53
+ @commands.each_key do |cmd|
54
+ unless respond_to?(cmd)
55
+ UI::warn "This menu was intended to provide a '#{cmd}' command!",
56
+ "However, it has been ommitted due to a programmer error."
57
+ @commands.delete cmd
58
+ end
59
+ end
60
+ end
61
+
62
+ # Add a command
63
+ #
64
+ #
65
+ # You must also create a method
66
+ # called name which takes each of args as parameters
67
+ def add_command name, help, *args
68
+ name = name.to_s
69
+ @commands[name] = {"help" => help, "args" => args}
70
+ eval <<-END
71
+ def exec_#{name} args
72
+ check args, #{args.length} do
73
+ #{name} *args
74
+ end
75
+ end
76
+ END
77
+ end
78
+
79
+ # Add a command which receives the text after it, as one, argument: a possibly empty string
80
+ def add_raw_command name, help, *args
81
+ name = name.to_s
82
+ @commands[name] = {"help" => help, "args" => args}
83
+ eval <<-END
84
+ def exec_#{name} args
85
+ #{name} args.join " "
86
+ end
87
+ END
88
+ end
89
+
90
+ # Check the args are of the correct length
91
+ def check args, length
92
+ if args.length == length
93
+ yield *args
94
+ else
95
+ UI::put_error "Expected #{length} parameters."
96
+ end
97
+ end
98
+
99
+ def execute cmd
100
+ if cmd.empty?
101
+ empty_line
102
+ else
103
+ if @commands.member? cmd[0]
104
+ self.send "exec_" + cmd[0], cmd[1..-1]
105
+ else
106
+ UI::put_error "Unknown command: '" + cmd[0] + "'. Try 'help'."
107
+ end
108
+ end
109
+ end
110
+
111
+ def empty_line
112
+ UI::put_error "You must enter a command. Try 'help'."
113
+ end
114
+
115
+ def help
116
+ puts ""
117
+ puts "The available commands, and their formal parameters, are:"
118
+ @commands.each do |cmd, info|
119
+ puts ""
120
+ puts cmd + " " + info["args"].join(" ")
121
+ puts info["help"]
122
+ end
123
+ puts ""
124
+ end
125
+
126
+ # Read eval print loop
127
+ def repl
128
+ loop do
129
+ rep
130
+ end
131
+ end
132
+
133
+ # Read eval print
134
+ def rep
135
+ line = UI::question "Enter command:"
136
+ cmd = line.split /\s+/
137
+ execute cmd
138
+ end
139
+
140
+ end
141
+
142
+ class QuitInterface < Interface
143
+
144
+ def initialize
145
+ super
146
+ add_command :quit, "Quit the program"
147
+ end
148
+
149
+ protected
150
+
151
+ def quit
152
+ @run = false
153
+ end
154
+
155
+ def repl
156
+ @run = true
157
+ while @run
158
+ rep
159
+ end
160
+ end
161
+
162
+ end
163
+
164
+ # Interface which can alter program execution (ie, by terminating)
165
+ module ControlInterface
166
+
167
+ def quit
168
+ puts "Bye!"
169
+ exit
170
+ end
171
+
172
+ end
173
+
174
+ end
@@ -0,0 +1,79 @@
1
+ # Copyright 2010 John Morrice
2
+
3
+ # This file is part of CARPS.
4
+
5
+ # CARPS is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+
10
+ # CARPS is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with CARPS. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require "carps/protocol"
19
+
20
+ require "carps/service"
21
+
22
+ require "carps/ui"
23
+
24
+ module CARPS
25
+
26
+ # An invitation
27
+ class Invite < Message
28
+
29
+ # We are part of the protocol :)
30
+ protoval :master
31
+ protoval :mod
32
+ protoval :about
33
+ protoval :session
34
+
35
+ def initialize dm, mod, about, session
36
+ @dm = dm
37
+ @mod = mod
38
+ @about = about
39
+ @session = session
40
+ end
41
+
42
+ # Parse this from semi-structured text
43
+ def Invite.parse blob
44
+ dm, blob = find K.master, blob
45
+ mod, blob = find K.mod, blob
46
+ about, blob = find K.about, blob
47
+ session, blob = find K.session, blob
48
+ [Invite.new(dm, mod, about, session), blob]
49
+ end
50
+
51
+ # Ask if the player wants to accept this invitation,
52
+ # join the game if they do
53
+ def ask
54
+ puts "You have been invited to a game!"
55
+ if load_mods.member? @mod
56
+ game = Player::GameConfig.new "", @mod, @dm, @about, @session
57
+ game.display
58
+ if UI::confirm("Do you want to join?")
59
+ filename = UI::question "Enter a file name for the game:"
60
+ game.filename = filename
61
+ game.save
62
+ return game
63
+ else
64
+ return nil
65
+ end
66
+ else
67
+ puts "But it's for the mod: " + @mod
68
+ puts "Which you don't have installed."
69
+ return nil
70
+ end
71
+ end
72
+
73
+ def emit
74
+ V.master(@dm) + V.mod(@mod) + V.about(@about) + V.session(@session)
75
+ end
76
+
77
+ end
78
+
79
+ end