-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.js
137 lines (131 loc) · 2.54 KB
/
simulate.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
const axios = require('axios')
const async = require('async')
const Promise = require('bluebird')
const chalk = require('chalk')
const instance = axios.create({
baseURL: 'http://localhost:3000/'
})
async function main() {
console.log('###### Start Game ######')
try {
const response = await instance.post('/games')
const gameId = response.data.gameId
console.log('==== Deploying ===')
await bulkDeploy(response.data.gameId)
console.log('==== Attacking ===')
await bulkAttack(response.data.gameId)
console.log(`Visit simulation result at http://localhost:3000/grids/${gameId}`)
} catch (error) {
console.error(error)
}
}
main()
function bulkDeploy(gameId, callback) {
const actions = [
{
row: 1,
column: 1,
ship: 'battleship'
},
{
row: 1,
column: 6,
ship: 'cruiser',
isVertical: true
},
{
row: 1,
column: 10,
ship: 'cruiser',
isVertical: true
},
{
row: 5,
column: 1,
ship: 'destroyer',
isVertical: true
},
{
row: 10,
column: 1,
ship: 'destroyer',
},
{
row: 10,
column: 9,
ship: 'destroyer',
},
{
row: 6,
column: 5,
ship: 'submarine',
},
{
row: 6,
column: 7,
ship: 'submarine',
},
{
row: 5,
column: 10,
ship: 'submarine',
},
{
row: 7,
column: 9,
ship: 'submarine',
}
]
return Promise.each(actions, (item) => {
console.log(`deploy: deployed ${item.ship} to row: ${item.row}, column: ${item.column}`)
return instance.post(`/deploy/${gameId}`, item)
})
}
function bulkAttack(gameId) {
const actions = [
[1, 1],
[1, 2],
[1, 3],
[1, 4],
[9, 10],
[1, 6],
[1, 8],
[1, 8],
[2, 6],
[2, 8],
[3, 6],
[1, 10],
[2, 10],
[3, 10],
[5, 1],
[6, 1],
[10, 1],
[10, 2],
[7, 3],
[10, 9],
[10, 10],
[9, 5],
[5, 10],
[6, 5],
[6, 7],
[7, 9]
]
return Promise.each(actions, (item) => {
const row = item[0]
const column = item[1]
console.log(`attack: row: ${row} col: ${column}`)
return instance.post(`/attack/${gameId}`, {
row,
column
}).then((response) => {
const message = response.data.message
if (message === 'Hit') {
console.log(chalk.red(message))
} else if (message === 'Miss') {
console.log(chalk.gray(message))
} else {
console.log(chalk.green(message))
}
})
})
}