-
Notifications
You must be signed in to change notification settings - Fork 5
/
catanCmd.js
executable file
·186 lines (160 loc) · 5.45 KB
/
catanCmd.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env node
'use strict';
var
program = require('commander'),
request = require('request'),
config = require('./config.js'),
resources = require('./lib/resourceService'),
market = require('./lib/marketService'),
houses = require('./lib/houseService');
function showError(message) {
console.log("An Error ocurred:\n\n\t" + message);
process.exit(1);
}
function showOutput(message) {
console.log(JSON.stringify(message, null, 4));
process.exit(0);
}
function showCallback(error, result) {
if (error) {
showError(error);
} else {
showOutput(result);
}
}
program
.version('0.0.1')
.usage('<command> [options]');
program
.command('createDesign [name] [resourceList] [value]')
.description('\n\nUse this command to create new designs, indicating the name, value and a comma-separated\n' +
'list of the resources needed to build it. Repeat the name of the resource when multiple\n' +
' instances of it are to be required\n\n')
.action(function (name, resourceList, value) {
var resources = resourceList.split(',');
houses.createDesign(name, resources, value, showCallback);
});
program
.command('createHouse [login] [designId] [resourceList]')
.description('\n\nThis command buys a new house for the user indicated in the login. The resource used to \n' +
'build the house must exist, and will be deleted as usual once the house is constructed.\n' +
'The resources should be serialized as a comma-separated list of the resource IDs\n\n')
.action(function (login, design, resourceList) {
var resources = resourceList.split(',');
houses.buyHouse(login, design, resources, showCallback);
});
program
.command('createServer [resourceName] [url] [description]')
.description('\n\nThis command register a new Resource Server in the system, to be used in the Resource\n' +
'Server Directory\n\n')
.action(function (resourceName, url, description) {
resources.createServer(resourceName, url, description, showCallback);
});
program
.command('createMerchant [merchantName] [url]')
.description('\n\nThis command register a new Merchant in the system, so they can find themselves to initiate\n' +
' the negotiations\n\n')
.action(function (merchantName, url) {
market.createMerchant(merchantName, url, showCallback);
});
program
.command('listHouses')
.description('\n\nReturn a list of all the created houses\n\n')
.action(function () {
houses.listHouses(showCallback);
});
program
.command('listResources')
.description('\n\nReturn a list of all the available resources\n\n')
.action(function () {
resources.list(showCallback);
});
program
.command('listDesigns')
.description('\n\nReturn a list of all the available designs\n\n')
.action(function () {
houses.listDesigns(showCallback);
});
program
.command('listResourceServers')
.description('\n\nReturn a list of all the available Resource Servers\n\n')
.action(function () {
resources.listServers(showCallback);
});
program
.command('listMerchants')
.description('\n\nReturn the list of available merchants\n\n')
.action(function () {
market.listMerchants(showCallback);
});
program
.command('removeDesign [id]')
.description('\n\nRemove the selected design from the DB\n\n')
.action(function (id) {
houses.removeDesign(id, function (error, removedDesigns) {
if (error) {
showError(error);
} else {
showOutput({
numberOfRemovals: removedDesigns
});
}
});
});
program
.command('removeHouse [id]')
.description('\n\nRemove the selected house from the DB\n\n')
.action(function (id) {
houses.removeHouse(id, function (error, removedHouses) {
if (error) {
showError(error);
} else {
showOutput({
numberOfRemovals: removedHouses
});
}
});
});
program
.command('removeResource [id]')
.description('\n\nRemove the selected resource from the DB\n\n')
.action(function (id) {
resources.remove(id, function (error, removedResources) {
if (error) {
showError(error);
} else {
showOutput({
numberOfRemovals: removedResources
});
}
});
});
program
.command('removeResourceServer [id]')
.description('\n\nRemove the selected Resource Server from the DB\n\n')
.action(function (id) {
resources.removeServer(id, function (error, removedResourceServers) {
if (error) {
showError(error);
} else {
showOutput({
numberOfRemovals: removedResourceServers
});
}
});
});
program
.command('removeMerchant [id]')
.description('\n\nRemove the selected Merchant from the DB\n\n')
.action(function (id) {
market.removeMerchant(id, function (error, removedMerchants) {
if (error) {
showError(error);
} else {
showOutput({
numberOfRemovals: removedMerchants
});
}
});
});
program.parse(process.argv);