Compare commits
431 commits
Author | SHA1 | Date | |
---|---|---|---|
f29b189a42 | |||
|
fe8029a4b3 | ||
35b25dde98 | |||
804f82dd20 | |||
4e4af1a1be | |||
b114b68ff7 | |||
1200b0a2a2 | |||
6a44633d40 | |||
636f0ed773 | |||
018c122861 | |||
e66b899e2a | |||
70f041ae5d | |||
d7bda3f607 | |||
94767f06d3 | |||
500e0f9557 | |||
4f2a7edeb1 | |||
cf1dff2814 | |||
71a0ef9920 | |||
9b03ffb7f1 | |||
4821a90143 | |||
ac3844fe7a | |||
70d92c2d5e | |||
bb50fae00b | |||
a321e5d72f | |||
49ffa18f23 | |||
e542999b91 | |||
74e1cd4564 | |||
c0d4a809c9 | |||
1514603517 | |||
9dcbbee1a2 | |||
bcf71f0bf9 | |||
cec61a9db7 | |||
47e2e13c49 | |||
6891ec8439 | |||
113090fbcb | |||
a235f6a5f5 | |||
cd97220a11 | |||
f9b3ce3edb | |||
abf67f7f37 | |||
5d4fd801c1 | |||
fa05e7f253 | |||
1f5aa765f4 | |||
7be0f83c5e | |||
a0e04b7e3a | |||
a93ce97b0b | |||
57621d169a | |||
fdfcc88540 | |||
6f2d67bd7c | |||
e840b3e47b | |||
1e4d642038 | |||
5b2ea533f4 | |||
edd3e5a6cb | |||
b0ff3eb6ea | |||
be36f0631b | |||
c321f88d96 | |||
7cccb4394f | |||
524487ce14 | |||
2f1ccdfdb9 | |||
ce6e10afbb | |||
2c1bb80779 | |||
a5ac809387 | |||
41cd93e986 | |||
8cc3b9155e | |||
8a8a27ab47 | |||
3bfc91b8f6 | |||
8279a08a37 | |||
46d904acc6 | |||
be74183878 | |||
13e4d6a5e6 | |||
f7192b3997 | |||
4cbf4a9ab5 | |||
2bc2477128 | |||
d49d034f0e | |||
6adc652cea | |||
1e18610c5c | |||
5d80cb2596 | |||
c89c5f6e74 | |||
c94ac68f44 | |||
075a502103 | |||
9e42764398 | |||
447dbe3582 | |||
6b5d5abea1 | |||
8667f55143 | |||
f78efd1223 | |||
6f30b665c7 | |||
6c75f07aff | |||
89123394cd | |||
77212c7258 | |||
bffad84181 | |||
04d7a5a4ca | |||
211771121f | |||
d3c480cb04 | |||
1281713211 | |||
9130cf2517 | |||
c48274eb23 | |||
ddbbdce1f8 | |||
faa1271c5a | |||
37d828c457 | |||
959cbfa15a | |||
c637ed37b9 | |||
87a9abcd12 | |||
1eba9a8652 | |||
4f1a9c2c2b | |||
713699687f | |||
fd0992353d | |||
e27398dbc0 | |||
4b7510ffa1 | |||
57f668ae54 | |||
c13151b60f | |||
db8ed0302d | |||
cd6a275bb9 | |||
4e0e55af88 | |||
3796ee09a3 | |||
0a8b8d5979 | |||
e9188dbbf6 | |||
da91d31649 | |||
4a89cb9d6e | |||
7bdfa44fb6 | |||
305f64ec38 | |||
24d4fe9273 | |||
7e41ac0028 | |||
f665436007 | |||
acdd019093 | |||
53d6ad08d9 | |||
a0b811a659 | |||
5814ac95b8 | |||
c2e3c9f090 | |||
0e731df1a3 | |||
105d69bd7c | |||
79598c3373 | |||
9593602dc5 | |||
327c246c8e | |||
346bc940e2 | |||
427db95b5b | |||
a0946f9e58 | |||
5c4d7469bb | |||
3bad9f0122 | |||
3f2ae97048 | |||
737534f739 | |||
46f81abbd7 | |||
9ccb7ac019 | |||
f0ab2abf6e | |||
f40f7123d4 | |||
5b1fe61097 | |||
97d3be000b | |||
065f79cbb3 | |||
b534ac0516 | |||
b451ea519d | |||
dc2800fa54 | |||
c1267829ac | |||
d6349d081e | |||
9a7c48ae78 | |||
a0be8a463c | |||
655e00b41f | |||
fb2ffc5252 | |||
5ac44d85cb | |||
3665a62c6e | |||
6c1ee311cd | |||
fe6dae4c52 | |||
0be6aa7c12 | |||
96a137ad2f | |||
7d780d05bd | |||
59a1bdc14b | |||
bffe539d77 | |||
b032fdbfe2 | |||
23764a3fc3 | |||
091469dd91 | |||
db19e4a657 | |||
e21023ec25 | |||
d4d82c38e0 | |||
2671398593 | |||
30ca488890 | |||
b748846c55 | |||
55c85d2a22 | |||
b2f169d99f | |||
8866f28bf5 | |||
0dc3cab9c0 | |||
84be8bff05 | |||
9682cfa7ea | |||
4b715bdff3 | |||
7b4541716a | |||
ed9ecef80a | |||
10959ef726 | |||
2ff4bcded7 | |||
0386617c51 | |||
089f5e5337 | |||
3e1e3a5456 | |||
d9e97ea468 | |||
47df02ec7e | |||
92222127a6 | |||
df30a0d689 | |||
5980de5ba7 | |||
254957cde5 | |||
3ba7652c74 | |||
1412579c6c | |||
526e9c69eb | |||
20385c5ae7 | |||
5928dfdb20 | |||
c9af3772da | |||
3faf709f10 | |||
3493d51d36 | |||
b6d47833f6 | |||
ad13ed8ee2 | |||
5b4b9c30eb | |||
a1b79a8df5 | |||
c66e61921f | |||
fc4fa3decf | |||
75910efbe5 | |||
945b3299ac | |||
ed6de9eac4 | |||
718252731b | |||
2fbe2dc1a8 | |||
408fffb0c6 | |||
62741caf5e | |||
7563896a59 | |||
771c95da14 | |||
2c6aba6897 | |||
9eb2e08667 | |||
35d7400a83 | |||
a112c3ed47 | |||
233a6b3281 | |||
ea4b7de4ac | |||
1554b7c21b | |||
31c0753341 | |||
28639b4cac | |||
894359142b | |||
f8e594cb39 | |||
98eea89484 | |||
31308db6b5 | |||
87af905bc8 | |||
7272749614 | |||
e875f82b13 | |||
15c337c067 | |||
8b83672dcc | |||
dbe944bb4e | |||
143fba505e | |||
b066277ddf | |||
2eaed1447d | |||
e6ff453ff1 | |||
9cd5324465 | |||
1a1ef9a376 | |||
c4b0762ebe | |||
062f9cfec8 | |||
74dcae6542 | |||
e6bfc7a8fc | |||
821c83549b | |||
c0726a2345 | |||
13482c1893 | |||
b5707ab71c | |||
204c786103 | |||
abcebcebb6 | |||
984ff56664 | |||
c07a9b2659 | |||
77bde53a52 | |||
|
1f2669b643 | ||
e3169cdbdd | |||
e09cea328b | |||
b9198c546c | |||
0d3aac49b1 | |||
06cf44f129 | |||
e5ee0eaece | |||
9bb91920c9 | |||
b116cdf291 | |||
2556c0d049 | |||
6ba6584ae1 | |||
5b5f80be7d | |||
5bbb2ff37f | |||
693b8a54f1 | |||
7957454ec1 | |||
12dc9e478d | |||
adf3def488 | |||
1ed1c60de0 | |||
f9c30f541c | |||
4a343f36a8 | |||
b2f6c1a0b1 | |||
e6a25a5310 | |||
e1bff92a56 | |||
d08a15e201 | |||
71c2c09270 | |||
f0d40cc46c | |||
6c09ee3826 | |||
d624a3ca21 | |||
2f6465987d | |||
383e834cef | |||
00bdad6b40 | |||
2846ed796e | |||
8b1eca0aee | |||
db3c2c2c2e | |||
a84709564c | |||
7ee340e976 | |||
8019ea4e25 | |||
15b8f0a427 | |||
93b99b7989 | |||
f0f5a6b2e0 | |||
5c12fabb63 | |||
ccd9e13c51 | |||
8aeb23e40b | |||
58f9d8baf2 | |||
5f732bd6c5 | |||
98349b8935 | |||
996970fd81 | |||
073f8846aa | |||
ddc2631bb0 | |||
4232c547ce | |||
ef718199f0 | |||
c268555e2e | |||
19b3685e8c | |||
dd76e61e44 | |||
9fb0a79480 | |||
cc29f13ce5 | |||
d4b686f510 | |||
edce245c6f | |||
|
06e5c0bf2f | ||
|
3f1b8a4c2a | ||
|
b33e366500 | ||
bea08d54f1 | |||
187a0a6165 | |||
ba52458fd6 | |||
42ee69b1a2 | |||
389fb7e9db | |||
a4a04a15fb | |||
fcbc29c80b | |||
dc9eb8cf2e | |||
914eef05c0 | |||
3f879f9501 | |||
98249948a1 | |||
7c830f58be | |||
9d91fb836f | |||
55cd4fe4a5 | |||
84163ce9e1 | |||
856771dac7 | |||
ccb34d4452 | |||
51030ac162 | |||
8c6230ca20 | |||
7ababb79f6 | |||
b815284199 | |||
50c3795578 | |||
04a1b8ea1e | |||
c1d7952034 | |||
86a1200113 | |||
6cfc9444f3 | |||
663cd77c94 | |||
47921e6c41 | |||
b121b4463e | |||
30f8d666c3 | |||
6342d9dc4d | |||
faaa556ad0 | |||
8cd7b06c0c | |||
de3c2b9134 | |||
790e1464e5 | |||
bf88f9984b | |||
1a6bd8fed9 | |||
2f5863b17a | |||
1cafd4f2ce | |||
223c50228e | |||
537d309235 | |||
7d1a2d7efd | |||
14424c16ca | |||
99da6c5d67 | |||
8f25f55658 | |||
7749854eb7 | |||
b3b369f608 | |||
6fb7ee598d | |||
62d6213c1a | |||
14c4e61660 | |||
2ee68e74ac | |||
217e579cec | |||
6a868d3e41 | |||
a784b06c2a | |||
339163e06d | |||
4558e8a9b0 | |||
51fe918090 | |||
aae668fb57 | |||
520f78b5c3 | |||
ae2cb6598a | |||
de94b39a50 | |||
066df58705 | |||
43588c0e4b | |||
fba75960f8 | |||
b0e076ac1a | |||
4e6bf385ad | |||
175c66cddf | |||
35806576f9 | |||
65818ed0e1 | |||
de0ccbaa6e | |||
4d00af248f | |||
e82fb3dbfe | |||
43648926ca | |||
fc54775df9 | |||
3781a4d10d | |||
cb725c96d2 | |||
289bab2a7a | |||
82e6c4e33f | |||
aa4df38ead | |||
ceca4eb7fa | |||
8586bdabd7 | |||
141827fa57 | |||
77be28f913 | |||
ca17d89f6e | |||
1a3ae0ca3e | |||
a2375bbd9a | |||
55cfa37ac5 | |||
3274bc93b9 | |||
0fbc973899 | |||
0e9082aab7 | |||
5c549ad8c3 | |||
91947f614c | |||
b344c4549c | |||
2b87c2b4c0 | |||
50cc8b5dbf | |||
dc957f7fc1 | |||
78dc6f3d4c | |||
b9dd1f60bc | |||
e804cdf12b | |||
4645ef81e5 | |||
5e1f9b0d31 | |||
db38ad6091 | |||
6ac5a559b5 | |||
ea67fe1cbe | |||
b9df3833ed | |||
c30339143b | |||
3a36f8f84f | |||
ca00a64112 | |||
1a82c3a6bd | |||
c6fbf1341a | |||
573bfbf9c7 | |||
0a0a32cf58 | |||
e3ce87e964 | |||
97d3583384 | |||
1d2087e2b9 | |||
bc366583a4 |
62 changed files with 376870 additions and 1887 deletions
40
.github/workflows/docker-image.yml
vendored
40
.github/workflows/docker-image.yml
vendored
|
@ -1,40 +0,0 @@
|
||||||
name: Build and Test
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
pull_request:
|
|
||||||
branches: [ master ]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
|
|
||||||
build:
|
|
||||||
name: Build and Test
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Set up Go 1.x
|
|
||||||
uses: actions/setup-go@v2
|
|
||||||
with:
|
|
||||||
go-version: ^1.13
|
|
||||||
id: go
|
|
||||||
|
|
||||||
- name: Check out repo
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Get go dependencies
|
|
||||||
run: |
|
|
||||||
go get -v -t -d ./...
|
|
||||||
if [ -f Gopkg.toml ]; then
|
|
||||||
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
|
|
||||||
dep ensure
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Build and Test
|
|
||||||
run: ./script/test.sh
|
|
||||||
|
|
||||||
- name: Upload test coverage result
|
|
||||||
uses: actions/upload-artifact@v1
|
|
||||||
with:
|
|
||||||
name: Coverage
|
|
||||||
path: /tmp/coverage.html
|
|
25
.github/workflows/docker.yml
vendored
Normal file
25
.github/workflows/docker.yml
vendored
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
name: Docker
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Docker Login
|
||||||
|
uses: azure/docker-login@v1
|
||||||
|
with:
|
||||||
|
login-server: docker.pkg.github.com
|
||||||
|
username: $GITHUB_ACTOR
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build the Docker image
|
||||||
|
run: |
|
||||||
|
VERSION=$(git describe --always --long --dirty --tags)
|
||||||
|
docker build . --tag docker.pkg.github.com/mdiluz/rove/rove:$VERSION --tag docker.pkg.github.com/mdiluz/rove/rove:latest
|
||||||
|
docker push docker.pkg.github.com/mdiluz/rove/rove:$VERSION
|
||||||
|
docker push docker.pkg.github.com/mdiluz/rove/rove:latest
|
44
.github/workflows/tests.yml
vendored
Normal file
44
.github/workflows/tests.yml
vendored
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
name: Tests
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
|
||||||
|
build:
|
||||||
|
name: Build and Test
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Run golangci-lint
|
||||||
|
uses: actions-contrib/golangci-lint@v1
|
||||||
|
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: ^1.13
|
||||||
|
id: go
|
||||||
|
|
||||||
|
- name: Check generated files
|
||||||
|
run: |
|
||||||
|
PROTOC_ZIP=protoc-3.6.1-linux-x86_64.zip
|
||||||
|
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.6.1/$PROTOC_ZIP
|
||||||
|
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
|
||||||
|
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
|
||||||
|
sudo chmod -R o+r /usr/local/include/google/
|
||||||
|
rm -f $PROTOC_ZIP
|
||||||
|
make gen
|
||||||
|
git update-index --refresh || (git diff; exit 1)
|
||||||
|
|
||||||
|
- name: Build and Test
|
||||||
|
run: make test
|
||||||
|
|
||||||
|
- name: Upload test coverage result
|
||||||
|
uses: actions/upload-artifact@v1
|
||||||
|
with:
|
||||||
|
name: Coverage
|
||||||
|
path: /tmp/coverage.html
|
18
.vscode/launch.json
vendored
18
.vscode/launch.json
vendored
|
@ -1,18 +0,0 @@
|
||||||
{
|
|
||||||
// Use IntelliSense to learn about possible attributes.
|
|
||||||
// Hover to view descriptions of existing attributes.
|
|
||||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
|
||||||
"configurations": [
|
|
||||||
{
|
|
||||||
"name": "Launch Rove Server",
|
|
||||||
"type": "go",
|
|
||||||
"request": "launch",
|
|
||||||
"mode": "auto",
|
|
||||||
"program": "${workspaceFolder}/cmd/rove-server/main.go",
|
|
||||||
"cwd": "${workspaceFolder}",
|
|
||||||
"env": {},
|
|
||||||
"args": [],
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -5,6 +5,8 @@ WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
|
# Build the executables
|
||||||
|
RUN go build -o rove -ldflags="-X 'github.com/mdiluz/rove/pkg/version.Version=$(git describe --always --long --dirty --tags)'" cmd/rove/main.go
|
||||||
RUN go build -o rove-server -ldflags="-X 'github.com/mdiluz/rove/pkg/version.Version=$(git describe --always --long --dirty --tags)'" cmd/rove-server/main.go
|
RUN go build -o rove-server -ldflags="-X 'github.com/mdiluz/rove/pkg/version.Version=$(git describe --always --long --dirty --tags)'" cmd/rove-server/main.go
|
||||||
|
|
||||||
CMD [ "./rove-server" ]
|
CMD [ "./rove-server" ]
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 Marc Di Luzio
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
28
Makefile
Normal file
28
Makefile
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
VERSION := $(shell git describe --always --long --dirty --tags)
|
||||||
|
|
||||||
|
build:
|
||||||
|
@echo Running no-output build
|
||||||
|
go mod download
|
||||||
|
go build -ldflags="-X 'github.com/mdiluz/rove/cmd/version.Version=${VERSION}'" ./...
|
||||||
|
|
||||||
|
install:
|
||||||
|
@echo Installing to GOPATH
|
||||||
|
go mod download
|
||||||
|
go install -ldflags="-X 'github.com/mdiluz/rove/pkg/version.Version=${VERSION}'" ./...
|
||||||
|
|
||||||
|
gen:
|
||||||
|
@echo Installing go dependencies
|
||||||
|
go install github.com/golang/protobuf/protoc-gen-go
|
||||||
|
go mod download
|
||||||
|
@echo Generating rove server gRPC
|
||||||
|
protoc --proto_path proto --go_out=plugins=grpc,paths=source_relative:proto/ proto/roveapi/roveapi.proto
|
||||||
|
|
||||||
|
test:
|
||||||
|
@echo Run unit and integration tests
|
||||||
|
docker-compose -f docker-compose-test.yml up --build --exit-code-from=rove-tests --abort-on-container-exit rove-tests
|
||||||
|
docker-compose -f docker-compose-test.yml down
|
||||||
|
go tool cover -html=/tmp/coverage-data/c.out -o /tmp/coverage.html
|
||||||
|
|
||||||
|
@echo Done, coverage data can be found in /tmp/coverage.html
|
||||||
|
|
||||||
|
.PHONY: build install test gen
|
|
@ -1,11 +1,8 @@
|
||||||
Rove
|
Rove
|
||||||
====
|
====
|
||||||
|
|
||||||
Rove is an asynchronous nomadic game about exploring a planet as part of a loose community.
|

|
||||||
|
|
||||||
This repository is a growing document as well as the source code repository for the game server.
|
Rove is an asynchronous nomadic game about exploring as part of a loose community.
|
||||||
|
|
||||||
Build Status
|
|
||||||
------------
|
|
||||||

|
|
||||||
|
|
||||||
|
This repository contains the source code for the `rove-server` deployment and the `rove` command line client. See [mdiluz.github.io/rove](https://mdiluz.github.io/rove/) for game details, and [roveapi.proto](proto/roveapi/roveapi.proto) for the current server-client API.
|
||||||
|
|
169
cmd/rove-server/internal/routes.go
Normal file
169
cmd/rove-server/internal/routes.go
Normal file
|
@ -0,0 +1,169 @@
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/version"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ServerStatus returns the status of the current server to a gRPC request
|
||||||
|
func (s *Server) ServerStatus(context.Context, *roveapi.ServerStatusRequest) (*roveapi.ServerStatusResponse, error) {
|
||||||
|
response := &roveapi.ServerStatusResponse{
|
||||||
|
Ready: true,
|
||||||
|
Version: version.Version,
|
||||||
|
TickRate: int32(s.minutesPerTick),
|
||||||
|
CurrentTick: int32(s.world.CurrentTicks),
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's a schedule, respond with it
|
||||||
|
if len(s.schedule.Entries()) > 0 {
|
||||||
|
response.NextTick = s.schedule.Entries()[0].Next.Format("15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register registers a new account for a gRPC request
|
||||||
|
func (s *Server) Register(ctx context.Context, req *roveapi.RegisterRequest) (*roveapi.RegisterResponse, error) {
|
||||||
|
log.Printf("Handling register request: %s\n", req.Name)
|
||||||
|
|
||||||
|
if len(req.Name) == 0 {
|
||||||
|
return nil, fmt.Errorf("empty account name")
|
||||||
|
}
|
||||||
|
|
||||||
|
if acc, err := s.world.Accountant.RegisterAccount(req.Name); err != nil {
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
} else if _, err := s.SpawnRoverForAccount(req.Name); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to spawn rover for account: %s", err)
|
||||||
|
|
||||||
|
} else if err := s.SaveWorld(); err != nil {
|
||||||
|
return nil, fmt.Errorf("internal server error when saving world: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return &roveapi.RegisterResponse{
|
||||||
|
Account: &roveapi.Account{
|
||||||
|
Name: acc.Name,
|
||||||
|
Secret: acc.Data["secret"],
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status returns rover information for a gRPC request
|
||||||
|
func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (response *roveapi.StatusResponse, err error) {
|
||||||
|
log.Printf("Handling status request: %s\n", req.Account.Name)
|
||||||
|
|
||||||
|
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
} else if !valid {
|
||||||
|
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
|
||||||
|
|
||||||
|
} else if resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover"); err != nil {
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
} else if rover, err := s.world.GetRover(resp); err != nil {
|
||||||
|
return nil, fmt.Errorf("error getting rover: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
var inv []byte
|
||||||
|
for _, i := range rover.Inventory {
|
||||||
|
inv = append(inv, byte(i.Type))
|
||||||
|
}
|
||||||
|
|
||||||
|
queued := s.world.RoverCommands(resp)
|
||||||
|
var logs []*roveapi.Log
|
||||||
|
for _, log := range rover.Logs {
|
||||||
|
logs = append(logs, &roveapi.Log{
|
||||||
|
Text: log.Text,
|
||||||
|
Time: fmt.Sprintf("%d", log.Time.Unix()), // proto uses strings under the hood for 64bit ints anyway
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
response = &roveapi.StatusResponse{
|
||||||
|
Readings: &roveapi.RoverReadings{
|
||||||
|
Position: &roveapi.Vector{
|
||||||
|
X: int32(rover.Pos.X),
|
||||||
|
Y: int32(rover.Pos.Y),
|
||||||
|
},
|
||||||
|
Logs: logs,
|
||||||
|
Wind: s.world.Wind,
|
||||||
|
},
|
||||||
|
Spec: &roveapi.RoverSpecifications{
|
||||||
|
Name: rover.Name,
|
||||||
|
Range: int32(rover.Range),
|
||||||
|
Capacity: int32(rover.Capacity),
|
||||||
|
MaximumIntegrity: int32(rover.MaximumIntegrity),
|
||||||
|
MaximumCharge: int32(rover.MaximumCharge),
|
||||||
|
},
|
||||||
|
Status: &roveapi.RoverStatus{
|
||||||
|
Bearing: rover.Bearing,
|
||||||
|
Inventory: inv,
|
||||||
|
Integrity: int32(rover.Integrity),
|
||||||
|
Charge: int32(rover.Charge),
|
||||||
|
QueuedCommands: queued,
|
||||||
|
SailPosition: rover.SailPosition,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Radar returns the radar information for a rover
|
||||||
|
func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi.RadarResponse, error) {
|
||||||
|
log.Printf("Handling radar request: %s\n", req.Account.Name)
|
||||||
|
|
||||||
|
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
} else if !valid {
|
||||||
|
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
response := &roveapi.RadarResponse{}
|
||||||
|
|
||||||
|
resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
} else if rover, err := s.world.GetRover(resp); err != nil {
|
||||||
|
return nil, fmt.Errorf("error getting rover attributes: %s", err)
|
||||||
|
|
||||||
|
} else if radar, objs, err := s.world.RadarFromRover(resp); err != nil {
|
||||||
|
return nil, fmt.Errorf("error getting radar from rover: %s", err)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
response.Objects = objs
|
||||||
|
response.Tiles = radar
|
||||||
|
response.Range = int32(rover.Range)
|
||||||
|
}
|
||||||
|
|
||||||
|
return response, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command issues commands to the world based on a gRPC request
|
||||||
|
func (s *Server) Command(ctx context.Context, req *roveapi.CommandRequest) (*roveapi.CommandResponse, error) {
|
||||||
|
log.Printf("Handling command request: %s and %+v\n", req.Account.Name, req.Commands)
|
||||||
|
|
||||||
|
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
|
||||||
|
return nil, err
|
||||||
|
|
||||||
|
} else if !valid {
|
||||||
|
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.world.Enqueue(resp, req.Commands...); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &roveapi.CommandResponse{}, nil
|
||||||
|
}
|
234
cmd/rove-server/internal/server.go
Normal file
234
cmd/rove-server/internal/server.go
Normal file
|
@ -0,0 +1,234 @@
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/persistence"
|
||||||
|
"github.com/mdiluz/rove/pkg/rove"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
"github.com/robfig/cron"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
|
"google.golang.org/grpc/reflection"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cert = os.Getenv("CERT_NAME")
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PersistentData will allow the server to load and save it's state
|
||||||
|
PersistentData = iota
|
||||||
|
|
||||||
|
// EphemeralData will let the server neither load or save out any of it's data
|
||||||
|
EphemeralData
|
||||||
|
)
|
||||||
|
|
||||||
|
// Server contains the relevant data to run a game server
|
||||||
|
type Server struct {
|
||||||
|
|
||||||
|
// Internal state
|
||||||
|
world *rove.World
|
||||||
|
|
||||||
|
// gRPC server
|
||||||
|
netListener net.Listener
|
||||||
|
grpcServ *grpc.Server
|
||||||
|
|
||||||
|
// Config settings
|
||||||
|
address string
|
||||||
|
persistence int
|
||||||
|
minutesPerTick int
|
||||||
|
|
||||||
|
// sync point for sub-threads
|
||||||
|
sync sync.WaitGroup
|
||||||
|
|
||||||
|
// cron schedule for world ticks
|
||||||
|
schedule *cron.Cron
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServerOption defines a server creation option
|
||||||
|
type ServerOption func(s *Server)
|
||||||
|
|
||||||
|
// OptionAddress sets the server address for hosting
|
||||||
|
func OptionAddress(address string) ServerOption {
|
||||||
|
return func(s *Server) {
|
||||||
|
s.address = address
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OptionPersistentData sets the server data to be persistent
|
||||||
|
func OptionPersistentData() ServerOption {
|
||||||
|
return func(s *Server) {
|
||||||
|
s.persistence = PersistentData
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OptionTick defines the number of minutes per tick
|
||||||
|
// 0 means no automatic server tick
|
||||||
|
func OptionTick(minutes int) ServerOption {
|
||||||
|
return func(s *Server) {
|
||||||
|
s.minutesPerTick = minutes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewServer sets up a new server
|
||||||
|
func NewServer(opts ...ServerOption) *Server {
|
||||||
|
|
||||||
|
// Set up the default server
|
||||||
|
s := &Server{
|
||||||
|
address: "",
|
||||||
|
persistence: EphemeralData,
|
||||||
|
schedule: cron.New(),
|
||||||
|
world: rove.NewWorld(32),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply all options
|
||||||
|
for _, o := range opts {
|
||||||
|
o(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialise sets up internal state ready to serve
|
||||||
|
func (s *Server) Initialise(fillWorld bool) (err error) {
|
||||||
|
|
||||||
|
// Add to our sync
|
||||||
|
s.sync.Add(1)
|
||||||
|
|
||||||
|
// Load the world file
|
||||||
|
if err := s.LoadWorld(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up the RPC server and register
|
||||||
|
s.netListener, err = net.Listen("tcp", s.address)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to listen: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load TLS
|
||||||
|
var opts []grpc.ServerOption
|
||||||
|
if len(os.Getenv("NO_TLS")) == 0 {
|
||||||
|
pem := path.Join("/etc/letsencrypt/live/", cert, "fullchain.pem")
|
||||||
|
key := path.Join("/etc/letsencrypt/live/", cert, "privkey.pem")
|
||||||
|
creds, err := credentials.NewServerTLSFromFile(pem, key)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to setup TLS: %v", err)
|
||||||
|
}
|
||||||
|
opts = append(opts, grpc.Creds(creds))
|
||||||
|
}
|
||||||
|
|
||||||
|
s.grpcServ = grpc.NewServer(opts...)
|
||||||
|
roveapi.RegisterRoveServer(s.grpcServ, s)
|
||||||
|
reflection.Register(s.grpcServ)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Addr will return the server address set after the listen
|
||||||
|
func (s *Server) Addr() string {
|
||||||
|
return s.address
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes the server
|
||||||
|
func (s *Server) Run() {
|
||||||
|
defer s.sync.Done()
|
||||||
|
|
||||||
|
// Set up the schedule if requested
|
||||||
|
if s.minutesPerTick != 0 {
|
||||||
|
if err := s.schedule.AddFunc(fmt.Sprintf("0 */%d * * *", s.minutesPerTick), func() {
|
||||||
|
// Ensure we don't quit during this function
|
||||||
|
s.sync.Add(1)
|
||||||
|
defer s.sync.Done()
|
||||||
|
|
||||||
|
log.Println("Executing server tick")
|
||||||
|
|
||||||
|
// Tick the world
|
||||||
|
s.world.Tick()
|
||||||
|
|
||||||
|
// Save out the new world state
|
||||||
|
if err := s.SaveWorld(); err != nil {
|
||||||
|
log.Fatalf("Failed to save the world: %s", err)
|
||||||
|
}
|
||||||
|
}); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
s.schedule.Start()
|
||||||
|
log.Printf("First server tick scheduled for %s\n", s.schedule.Entries()[0].Next.Format("15:04:05"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve the RPC server
|
||||||
|
log.Printf("Serving gRPC on %s\n", s.address)
|
||||||
|
if err := s.grpcServ.Serve(s.netListener); err != nil && err != grpc.ErrServerStopped {
|
||||||
|
log.Fatalf("failed to serve gRPC: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stop will stop the current server
|
||||||
|
func (s *Server) Stop() error {
|
||||||
|
// Stop the cron
|
||||||
|
s.schedule.Stop()
|
||||||
|
|
||||||
|
// Stop the gRPC
|
||||||
|
s.grpcServ.Stop()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close waits until the server is finished and closes up shop
|
||||||
|
func (s *Server) Close() error {
|
||||||
|
// Wait until the world has shut down
|
||||||
|
s.sync.Wait()
|
||||||
|
|
||||||
|
// Save and return
|
||||||
|
return s.SaveWorld()
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopAndClose waits until the server is finished and closes up shop
|
||||||
|
func (s *Server) StopAndClose() error {
|
||||||
|
// Stop the server
|
||||||
|
if err := s.Stop(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close and return
|
||||||
|
return s.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveWorld will save out the world file
|
||||||
|
func (s *Server) SaveWorld() error {
|
||||||
|
if s.persistence == PersistentData {
|
||||||
|
s.world.RLock()
|
||||||
|
defer s.world.RUnlock()
|
||||||
|
if err := persistence.SaveAll("world", s.world); err != nil {
|
||||||
|
return fmt.Errorf("failed to save out persistent data: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadWorld will load all persistent data
|
||||||
|
func (s *Server) LoadWorld() error {
|
||||||
|
if s.persistence == PersistentData {
|
||||||
|
s.world.Lock()
|
||||||
|
defer s.world.Unlock()
|
||||||
|
if err := persistence.LoadAll("world", &s.world); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SpawnRoverForAccount spawns the rover rover for an account
|
||||||
|
func (s *Server) SpawnRoverForAccount(account string) (string, error) {
|
||||||
|
inst, err := s.world.SpawnRover(account)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return inst, nil
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -11,12 +12,12 @@ func TestNewServer(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewServer_OptionPort(t *testing.T) {
|
func TestNewServer_OptionAddress(t *testing.T) {
|
||||||
server := NewServer(OptionPort(1234))
|
server := NewServer(OptionAddress(":1234"))
|
||||||
if server == nil {
|
if server == nil {
|
||||||
t.Error("Failed to create server")
|
t.Error("Failed to create server")
|
||||||
} else if server.port != 1234 {
|
} else if server.address != ":1234" {
|
||||||
t.Error("Failed to set server port")
|
t.Error("Failed to set server address")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,29 +31,33 @@ func TestNewServer_OptionPersistentData(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_Run(t *testing.T) {
|
func TestServer_Run(t *testing.T) {
|
||||||
|
os.Setenv("NO_TLS", "1")
|
||||||
server := NewServer()
|
server := NewServer()
|
||||||
if server == nil {
|
if server == nil {
|
||||||
t.Error("Failed to create server")
|
t.Error("Failed to create server")
|
||||||
} else if err := server.Initialise(); err != nil {
|
} else if err := server.Initialise(true); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go server.Run()
|
go server.Run()
|
||||||
|
|
||||||
if err := server.Close(); err != nil {
|
if err := server.StopAndClose(); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_RunPersistentData(t *testing.T) {
|
func TestServer_RunPersistentData(t *testing.T) {
|
||||||
|
os.Setenv("NO_TLS", "1")
|
||||||
server := NewServer(OptionPersistentData())
|
server := NewServer(OptionPersistentData())
|
||||||
if server == nil {
|
if server == nil {
|
||||||
t.Error("Failed to create server")
|
t.Error("Failed to create server")
|
||||||
} else if err := server.Initialise(); err != nil {
|
} else if err := server.Initialise(true); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
go server.Run()
|
go server.Run()
|
||||||
|
|
||||||
if err := server.Close(); err != nil {
|
if err := server.StopAndClose(); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,40 +3,79 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strconv"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/cmd/rove-server/internal"
|
||||||
"github.com/mdiluz/rove/pkg/persistence"
|
"github.com/mdiluz/rove/pkg/persistence"
|
||||||
"github.com/mdiluz/rove/pkg/server"
|
|
||||||
"github.com/mdiluz/rove/pkg/version"
|
"github.com/mdiluz/rove/pkg/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ver = flag.Bool("version", false, "Display version number")
|
var ver = flag.Bool("version", false, "Display version number")
|
||||||
var port = flag.Int("port", 8080, "The port to host on")
|
|
||||||
var data = flag.String("data", os.TempDir(), "Directory to store persistant data")
|
|
||||||
|
|
||||||
func main() {
|
// Path for persistent storage
|
||||||
|
var data = os.Getenv("DATA_PATH")
|
||||||
|
|
||||||
|
// The tick rate of the server in seconds
|
||||||
|
var tick = os.Getenv("TICK_RATE")
|
||||||
|
|
||||||
|
// InnerMain is our main function so tests can run it
|
||||||
|
func InnerMain() {
|
||||||
|
// Ensure we've seeded rand
|
||||||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Print the version if requested
|
// Print the version if requested
|
||||||
if *ver {
|
if *ver {
|
||||||
fmt.Println(version.Version)
|
log.Println(version.Version)
|
||||||
os.Exit(0)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Initialising version %s...\n", version.Version)
|
// Address to host the server on
|
||||||
|
var iport int
|
||||||
|
var port = os.Getenv("PORT")
|
||||||
|
if len(port) == 0 {
|
||||||
|
iport = 9090
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
iport, err = strconv.Atoi(port)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("$PORT not valid int")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("Initialising version %s...\n", version.Version)
|
||||||
|
|
||||||
// Set the persistence path
|
// Set the persistence path
|
||||||
persistence.SetPath(*data)
|
if len(data) == 0 {
|
||||||
|
log.Fatal("DATA_PATH not set")
|
||||||
|
} else if err := persistence.SetPath(data); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the tick rate
|
||||||
|
tickRate := 1
|
||||||
|
if len(tick) > 0 {
|
||||||
|
var err error
|
||||||
|
tickRate, err = strconv.Atoi(tick)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("TICK_RATE not set to valid int: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Create the server data
|
// Create the server data
|
||||||
s := server.NewServer(
|
s := internal.NewServer(
|
||||||
server.OptionPort(*port),
|
internal.OptionAddress(fmt.Sprintf(":%d", iport)),
|
||||||
server.OptionPersistentData())
|
internal.OptionPersistentData(),
|
||||||
|
internal.OptionTick(tickRate))
|
||||||
|
|
||||||
// Initialise the server
|
// Initialise the server
|
||||||
if err := s.Initialise(); err != nil {
|
if err := s.Initialise(true); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,15 +84,12 @@ func main() {
|
||||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||||
go func() {
|
go func() {
|
||||||
<-c
|
<-c
|
||||||
fmt.Println("SIGTERM recieved, exiting...")
|
log.Println("Quit requested, exiting...")
|
||||||
if err := s.Close(); err != nil {
|
if err := s.Stop(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fmt.Println("Running...")
|
|
||||||
|
|
||||||
// Run the server
|
// Run the server
|
||||||
s.Run()
|
s.Run()
|
||||||
|
|
||||||
|
@ -62,3 +98,7 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
InnerMain()
|
||||||
|
}
|
||||||
|
|
14
cmd/rove-server/main_test.go
Normal file
14
cmd/rove-server/main_test.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_InnerMain_Version(t *testing.T) {
|
||||||
|
assert.NoError(t, flag.Set("version", "1"))
|
||||||
|
InnerMain()
|
||||||
|
assert.NoError(t, flag.Set("version", "0"))
|
||||||
|
}
|
70
cmd/rove/internal/glyph.go
Normal file
70
cmd/rove/internal/glyph.go
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Glyph represents the text representation of something in the game
|
||||||
|
type Glyph byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
// GlyphGroundRock is solid rock ground
|
||||||
|
GlyphGroundRock = Glyph('-')
|
||||||
|
|
||||||
|
// GlyphGroundGravel is loose rocks
|
||||||
|
GlyphGroundGravel = Glyph(':')
|
||||||
|
|
||||||
|
// GlyphGroundSand is sand
|
||||||
|
GlyphGroundSand = Glyph('~')
|
||||||
|
|
||||||
|
// GlyphRoverLive represents a live rover
|
||||||
|
GlyphRoverLive = Glyph('R')
|
||||||
|
|
||||||
|
// GlyphRoverDormant represents a dormant rover
|
||||||
|
GlyphRoverDormant = Glyph('r')
|
||||||
|
|
||||||
|
// GlyphRoverParts represents spare rover parts
|
||||||
|
GlyphRoverParts = Glyph('*')
|
||||||
|
|
||||||
|
// GlyphRockSmall is a small stashable rock
|
||||||
|
GlyphRockSmall = Glyph('o')
|
||||||
|
|
||||||
|
// GlyphRockLarge is a large blocking rock
|
||||||
|
GlyphRockLarge = Glyph('O')
|
||||||
|
)
|
||||||
|
|
||||||
|
// TileGlyph returns the glyph for this tile type
|
||||||
|
func TileGlyph(t roveapi.Tile) Glyph {
|
||||||
|
switch t {
|
||||||
|
case roveapi.Tile_Rock:
|
||||||
|
return GlyphGroundRock
|
||||||
|
case roveapi.Tile_Gravel:
|
||||||
|
return GlyphGroundGravel
|
||||||
|
case roveapi.Tile_Sand:
|
||||||
|
return GlyphGroundSand
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatalf("Unknown tile type: %c", t)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// ObjectGlyph returns the glyph for this object type
|
||||||
|
func ObjectGlyph(o roveapi.Object) Glyph {
|
||||||
|
switch o {
|
||||||
|
case roveapi.Object_RoverLive:
|
||||||
|
return GlyphRoverLive
|
||||||
|
case roveapi.Object_RockSmall:
|
||||||
|
return GlyphRockSmall
|
||||||
|
case roveapi.Object_RoverDormant:
|
||||||
|
return GlyphRoverDormant
|
||||||
|
case roveapi.Object_RockLarge:
|
||||||
|
return GlyphRockLarge
|
||||||
|
case roveapi.Object_RoverParts:
|
||||||
|
return GlyphRoverParts
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Fatalf("Unknown object type: %c", o)
|
||||||
|
return 0
|
||||||
|
}
|
461
cmd/rove/main.go
461
cmd/rove/main.go
|
@ -1,206 +1,423 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/mdiluz/rove/pkg/rove"
|
"github.com/mdiluz/rove/cmd/rove/internal"
|
||||||
|
"github.com/mdiluz/rove/pkg/version"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
)
|
)
|
||||||
|
|
||||||
var USAGE = ""
|
var home = os.Getenv("HOME")
|
||||||
|
var defaultDataPath = path.Join(home, ".local/share/")
|
||||||
|
|
||||||
// Command usage
|
// Command usage
|
||||||
func Usage() {
|
func printUsage() {
|
||||||
fmt.Printf("Usage: %s [OPTIONS]... COMMAND\n", os.Args[0])
|
fmt.Fprintln(os.Stderr, "Usage: rove ARG [OPT...]")
|
||||||
fmt.Println("\nCommands:")
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
fmt.Println("\tstatus \tprints the server status")
|
fmt.Fprintln(os.Stderr, "Arguments:")
|
||||||
fmt.Println("\tregister\tregisters an account and stores it (use with -name)")
|
fmt.Fprintln(os.Stderr, "\tversion outputs version")
|
||||||
fmt.Println("\tspawn \tspawns a rover for the current account")
|
fmt.Fprintln(os.Stderr, "\thelp outputs this usage text")
|
||||||
fmt.Println("\tcommand \tissues commands to the rover")
|
fmt.Fprintln(os.Stderr, "\tconfig [HOST] outputs the local config, optionally sets host")
|
||||||
fmt.Println("\tradar \tgathers radar data for the current rover")
|
fmt.Fprintln(os.Stderr, "\tserver-status prints the server status")
|
||||||
fmt.Println("\trover \tgets data for current rover")
|
fmt.Fprintln(os.Stderr, "\tregister NAME registers an account and spawns a rover")
|
||||||
fmt.Println("\nOptions:")
|
fmt.Fprintln(os.Stderr, "\tradar prints radar data in ASCII form")
|
||||||
flag.PrintDefaults()
|
fmt.Fprintln(os.Stderr, "\tstatus gets rover status")
|
||||||
|
fmt.Fprintln(os.Stderr, "\tcommand CMD [VAL...] [REPEAT] sets the command queue, accepts multiple in sequence")
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
fmt.Fprintln(os.Stderr, "Rover commands:")
|
||||||
|
fmt.Fprintln(os.Stderr, "\ttoggle toggles the current sail mode")
|
||||||
|
fmt.Fprintln(os.Stderr, "\tstash stores the object at the rover location in the inventory")
|
||||||
|
fmt.Fprintln(os.Stderr, "\trepair repairs the rover using inventory item")
|
||||||
|
fmt.Fprintln(os.Stderr, "\tbroadcast MSG broadcast a simple ASCII triplet to nearby rovers")
|
||||||
|
fmt.Fprintln(os.Stderr, "\tsalvage salvages a dormant rover for parts")
|
||||||
|
fmt.Fprintln(os.Stderr, "\ttransfer transfer's control into a dormant rover")
|
||||||
|
fmt.Fprintln(os.Stderr, "\tupgrade SPEC spends rover parts to upgrade one rover spec (capacity, range, integrity, charge")
|
||||||
|
fmt.Fprintln(os.Stderr, "\twait waits before performing the next command")
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
fmt.Fprintln(os.Stderr, "Environment")
|
||||||
|
fmt.Fprintln(os.Stderr, "\tROVE_USER_DATA path to user data, defaults to "+defaultDataPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
var home = os.Getenv("HOME")
|
const gRPCport = 9090
|
||||||
var filepath = path.Join(home, ".local/share/rove.json")
|
|
||||||
|
|
||||||
var host = flag.String("host", "", "path to game host server")
|
// Account stores data for an account
|
||||||
var data = flag.String("data", filepath, "data file for storage")
|
type Account struct {
|
||||||
|
Name string
|
||||||
|
Secret string
|
||||||
|
}
|
||||||
|
|
||||||
// Config is used to store internal data
|
// Config is used to store internal data
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string `json:"host,omitempty"`
|
Host string
|
||||||
Accounts map[string]string `json:"accounts,omitempty"`
|
Account Account
|
||||||
}
|
}
|
||||||
|
|
||||||
var name = flag.String("name", "", "used with status command for the account name")
|
// ConfigPath returns the configuration path
|
||||||
|
func ConfigPath() string {
|
||||||
|
// Allow overriding the data path
|
||||||
|
var datapath = defaultDataPath
|
||||||
|
var override = os.Getenv("ROVE_USER_DATA")
|
||||||
|
if len(override) > 0 {
|
||||||
|
datapath = override
|
||||||
|
}
|
||||||
|
datapath = path.Join(datapath, "roveapi.json")
|
||||||
|
|
||||||
// verifyId will verify an account ID
|
return datapath
|
||||||
func verifyId(id string) error {
|
}
|
||||||
if len(id) == 0 {
|
|
||||||
|
// LoadConfig loads the config from a chosen path
|
||||||
|
func LoadConfig() (config Config, err error) {
|
||||||
|
|
||||||
|
datapath := ConfigPath()
|
||||||
|
|
||||||
|
// Create the path if needed
|
||||||
|
path := filepath.Dir(datapath)
|
||||||
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
|
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
||||||
|
return Config{}, fmt.Errorf("Failed to create data path %s: %s", path, err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Read the file
|
||||||
|
_, err = os.Stat(datapath)
|
||||||
|
if !os.IsNotExist(err) {
|
||||||
|
if b, err := ioutil.ReadFile(datapath); err != nil {
|
||||||
|
return Config{}, fmt.Errorf("failed to read file %s error: %s", datapath, err)
|
||||||
|
|
||||||
|
} else if len(b) == 0 {
|
||||||
|
return Config{}, fmt.Errorf("file %s was empty, assumin fresh data", datapath)
|
||||||
|
|
||||||
|
} else if err := json.Unmarshal(b, &config); err != nil {
|
||||||
|
return Config{}, fmt.Errorf("failed to unmarshal file %s error: %s", datapath, err)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// SaveConfig saves the config out
|
||||||
|
func SaveConfig(config Config) error {
|
||||||
|
// Save out the persistent file
|
||||||
|
datapath := ConfigPath()
|
||||||
|
if b, err := json.MarshalIndent(config, "", "\t"); err != nil {
|
||||||
|
return fmt.Errorf("failed to marshal data error: %s", err)
|
||||||
|
} else if err := ioutil.WriteFile(datapath, b, os.ModePerm); err != nil {
|
||||||
|
return fmt.Errorf("failed to save file %s error: %s", datapath, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkAccount will verify an account ID
|
||||||
|
func checkAccount(a Account) error {
|
||||||
|
if len(a.Name) == 0 {
|
||||||
return fmt.Errorf("no account ID set, must register first")
|
return fmt.Errorf("no account ID set, must register first")
|
||||||
|
} else if len(a.Secret) == 0 {
|
||||||
|
return fmt.Errorf("empty account secret, must register first")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BearingFromString converts a string to a bearing
|
||||||
|
func BearingFromString(s string) roveapi.Bearing {
|
||||||
|
switch s {
|
||||||
|
case "N":
|
||||||
|
return roveapi.Bearing_North
|
||||||
|
case "NE":
|
||||||
|
return roveapi.Bearing_NorthEast
|
||||||
|
case "E":
|
||||||
|
return roveapi.Bearing_East
|
||||||
|
case "SE":
|
||||||
|
return roveapi.Bearing_SouthEast
|
||||||
|
case "S":
|
||||||
|
return roveapi.Bearing_South
|
||||||
|
case "SW":
|
||||||
|
return roveapi.Bearing_SouthWest
|
||||||
|
case "W":
|
||||||
|
return roveapi.Bearing_West
|
||||||
|
case "NW":
|
||||||
|
return roveapi.Bearing_NorthWest
|
||||||
|
}
|
||||||
|
return roveapi.Bearing_BearingUnknown
|
||||||
|
}
|
||||||
|
|
||||||
// InnerMain wraps the main function so we can test it
|
// InnerMain wraps the main function so we can test it
|
||||||
func InnerMain(command string) error {
|
func InnerMain(command string, args ...string) error {
|
||||||
|
|
||||||
|
// Early simple bails
|
||||||
|
switch command {
|
||||||
|
case "help":
|
||||||
|
printUsage()
|
||||||
|
return nil
|
||||||
|
case "version":
|
||||||
|
fmt.Println(version.Version)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Load in the persistent file
|
// Load in the persistent file
|
||||||
var config = Config{
|
config, err := LoadConfig()
|
||||||
Accounts: make(map[string]string),
|
if err != nil {
|
||||||
}
|
return err
|
||||||
_, err := os.Stat(*data)
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
if b, err := ioutil.ReadFile(*data); err != nil {
|
|
||||||
return fmt.Errorf("failed to read file %s error: %s", *data, err)
|
|
||||||
|
|
||||||
} else if len(b) == 0 {
|
|
||||||
return fmt.Errorf("file %s was empty, assumin fresh data", *data)
|
|
||||||
|
|
||||||
} else if err := json.Unmarshal(b, &config); err != nil {
|
|
||||||
return fmt.Errorf("failed to unmarshal file %s error: %s", *data, err)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there's a host set on the command line, override the one in the config
|
// Run config command before server needed
|
||||||
if len(*host) != 0 {
|
if command == "config" {
|
||||||
config.Host = *host
|
if len(args) > 0 {
|
||||||
|
config.Host = args[0]
|
||||||
|
}
|
||||||
|
fmt.Printf("host: %s\taccount: %s\n", config.Host, config.Account)
|
||||||
|
return SaveConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there's still no host, bail
|
// If there's still no host, bail
|
||||||
if len(config.Host) == 0 {
|
if len(config.Host) == 0 {
|
||||||
return fmt.Errorf("no host set, please set one with -host")
|
return fmt.Errorf("no host set in %s, set one with '%s config {HOST}'", ConfigPath(), os.Args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
var opts []grpc.DialOption
|
||||||
|
if len(os.Getenv("NO_TLS")) == 0 {
|
||||||
|
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
|
||||||
|
} else {
|
||||||
|
opts = append(opts, grpc.WithInsecure())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the server
|
// Set up the server
|
||||||
var server = rove.Server(config.Host)
|
clientConn, err := grpc.Dial(fmt.Sprintf("%s:%d", config.Host, gRPCport), opts...)
|
||||||
|
if err != nil {
|
||||||
// Grab the account
|
return err
|
||||||
var account = config.Accounts[config.Host]
|
}
|
||||||
|
var client = roveapi.NewRoveClient(clientConn)
|
||||||
// Print the config info
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
fmt.Printf("host: %s\taccount: %s\n", config.Host, account)
|
defer cancel()
|
||||||
|
|
||||||
// Handle all the commands
|
// Handle all the commands
|
||||||
switch command {
|
switch command {
|
||||||
case "status":
|
case "server-status":
|
||||||
if response, err := server.Status(); err != nil {
|
response, err := client.ServerStatus(ctx, &roveapi.ServerStatusRequest{})
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
return err
|
return err
|
||||||
|
|
||||||
} else {
|
default:
|
||||||
fmt.Printf("Ready: %t\n", response.Ready)
|
fmt.Printf("Ready: %t\n", response.Ready)
|
||||||
fmt.Printf("Version: %s\n", response.Version)
|
fmt.Printf("Version: %s\n", response.Version)
|
||||||
|
fmt.Printf("Tick Rate: %d\n", response.TickRate)
|
||||||
|
fmt.Printf("Current Tick: %d\n", response.CurrentTick)
|
||||||
|
fmt.Printf("Next Tick: %s\n", response.NextTick)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "register":
|
case "register":
|
||||||
d := rove.RegisterData{
|
if len(args) == 0 || len(args[0]) == 0 {
|
||||||
Name: *name,
|
return fmt.Errorf("must pass name to 'register'")
|
||||||
}
|
}
|
||||||
if response, err := server.Register(d); err != nil {
|
|
||||||
|
resp, err := client.Register(ctx, &roveapi.RegisterRequest{
|
||||||
|
Name: args[0],
|
||||||
|
})
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
return err
|
return err
|
||||||
|
|
||||||
} else if !response.Success {
|
default:
|
||||||
return fmt.Errorf("Server returned failure: %s", response.Error)
|
fmt.Printf("Registered account with id: %s\n", resp.Account.Name)
|
||||||
|
config.Account.Name = resp.Account.Name
|
||||||
} else {
|
config.Account.Secret = resp.Account.Secret
|
||||||
fmt.Printf("Registered account with id: %s\n", response.Id)
|
|
||||||
config.Accounts[config.Host] = response.Id
|
|
||||||
}
|
|
||||||
case "spawn":
|
|
||||||
d := rove.SpawnData{}
|
|
||||||
if err := verifyId(account); err != nil {
|
|
||||||
return err
|
|
||||||
} else if response, err := server.Spawn(account, d); err != nil {
|
|
||||||
return err
|
|
||||||
|
|
||||||
} else if !response.Success {
|
|
||||||
return fmt.Errorf("Server returned failure: %s", response.Error)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Spawned at position %+v\n", response.Position)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "command":
|
case "command":
|
||||||
// TODO: Actually assemble requested commands
|
if err := checkAccount(config.Account); err != nil {
|
||||||
d := rove.CommandData{}
|
|
||||||
|
|
||||||
if err := verifyId(account); err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else if response, err := server.Command(account, d); err != nil {
|
} else if len(args) == 0 {
|
||||||
|
return fmt.Errorf("must pass commands to 'commands'")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate through each command
|
||||||
|
var commands []*roveapi.Command
|
||||||
|
for i := 0; i < len(args); i++ {
|
||||||
|
|
||||||
|
var cmd *roveapi.Command
|
||||||
|
switch args[i] {
|
||||||
|
case "turn":
|
||||||
|
i++
|
||||||
|
if len(args) == i {
|
||||||
|
return fmt.Errorf("turn command must be passed a compass bearing")
|
||||||
|
}
|
||||||
|
b := BearingFromString(args[i])
|
||||||
|
if b == roveapi.Bearing_BearingUnknown {
|
||||||
|
return fmt.Errorf("turn command must be given a valid bearing %s", args[i])
|
||||||
|
}
|
||||||
|
cmd = &roveapi.Command{
|
||||||
|
Command: roveapi.CommandType_turn,
|
||||||
|
Bearing: b,
|
||||||
|
}
|
||||||
|
case "broadcast":
|
||||||
|
i++
|
||||||
|
if len(args) == i {
|
||||||
|
return fmt.Errorf("broadcast command must be passed an ASCII triplet")
|
||||||
|
} else if len(args[i]) > 3 {
|
||||||
|
return fmt.Errorf("broadcast command must be given ASCII triplet of 3 or less: %s", args[i])
|
||||||
|
}
|
||||||
|
cmd = &roveapi.Command{
|
||||||
|
Command: roveapi.CommandType_broadcast,
|
||||||
|
Data: []byte(args[i]),
|
||||||
|
}
|
||||||
|
case "upgrade":
|
||||||
|
i++
|
||||||
|
if len(args) == i {
|
||||||
|
return fmt.Errorf("upgrade command must be passed a spec to upgrade")
|
||||||
|
}
|
||||||
|
var u roveapi.RoverUpgrade
|
||||||
|
switch args[i] {
|
||||||
|
case "capacity":
|
||||||
|
u = roveapi.RoverUpgrade_Capacity
|
||||||
|
case "range":
|
||||||
|
u = roveapi.RoverUpgrade_Range
|
||||||
|
case "integrity":
|
||||||
|
u = roveapi.RoverUpgrade_MaximumIntegrity
|
||||||
|
case "charge":
|
||||||
|
u = roveapi.RoverUpgrade_MaximumCharge
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("upgrade command must be passed a known upgrade spec")
|
||||||
|
}
|
||||||
|
cmd = &roveapi.Command{
|
||||||
|
Command: roveapi.CommandType_upgrade,
|
||||||
|
Upgrade: u,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
// By default just use the command literally
|
||||||
|
cmd = &roveapi.Command{
|
||||||
|
Command: roveapi.CommandType(roveapi.CommandType_value[args[i]]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try and convert the next command to a number
|
||||||
|
number := 0
|
||||||
|
if len(args) > i+1 {
|
||||||
|
num, err := strconv.Atoi(args[i+1])
|
||||||
|
if err == nil {
|
||||||
|
number = num
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd.Repeat = int32(number)
|
||||||
|
|
||||||
|
commands = append(commands, cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := client.Command(ctx, &roveapi.CommandRequest{
|
||||||
|
Account: &roveapi.Account{
|
||||||
|
Name: config.Account.Name,
|
||||||
|
Secret: config.Account.Secret,
|
||||||
|
},
|
||||||
|
Commands: commands,
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
return err
|
return err
|
||||||
|
|
||||||
} else if !response.Success {
|
default:
|
||||||
return fmt.Errorf("Server returned failure: %s", response.Error)
|
fmt.Printf("Request succeeded\n")
|
||||||
|
|
||||||
} else {
|
|
||||||
// TODO: Pretify the response
|
|
||||||
fmt.Printf("%+v\n", response)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "radar":
|
case "radar":
|
||||||
if err := verifyId(account); err != nil {
|
if err := checkAccount(config.Account); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if response, err := server.Radar(account); err != nil {
|
|
||||||
return err
|
|
||||||
|
|
||||||
} else if !response.Success {
|
|
||||||
return fmt.Errorf("Server returned failure: %s", response.Error)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
fmt.Printf("nearby rovers: %+v\n", response.Rovers)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "rover":
|
response, err := client.Radar(ctx, &roveapi.RadarRequest{
|
||||||
if err := verifyId(account); err != nil {
|
Account: &roveapi.Account{
|
||||||
return err
|
Name: config.Account.Name,
|
||||||
} else if response, err := server.Rover(account); err != nil {
|
Secret: config.Account.Secret,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
return err
|
return err
|
||||||
|
|
||||||
} else if !response.Success {
|
default:
|
||||||
return fmt.Errorf("Server returned failure: %s", response.Error)
|
|
||||||
|
|
||||||
|
// Print out the radar
|
||||||
|
num := int(math.Sqrt(float64(len(response.Tiles))))
|
||||||
|
for j := num - 1; j >= 0; j-- {
|
||||||
|
for i := 0; i < num; i++ {
|
||||||
|
t := response.Tiles[i+num*j]
|
||||||
|
o := response.Objects[i+num*j]
|
||||||
|
if o != roveapi.Object_ObjectUnknown {
|
||||||
|
fmt.Printf("%c", internal.ObjectGlyph(o))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("position: %v\n", response.Position)
|
fmt.Printf("%c", internal.TileGlyph(t))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
fmt.Print("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case "status":
|
||||||
|
if err := checkAccount(config.Account); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
response, err := client.Status(ctx, &roveapi.StatusRequest{
|
||||||
|
Account: &roveapi.Account{
|
||||||
|
Name: config.Account.Name,
|
||||||
|
Secret: config.Account.Secret,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
|
||||||
|
default:
|
||||||
|
fmt.Printf("rover info: %+v\n", response)
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unknown command: %s", command)
|
// Print the usage
|
||||||
|
fmt.Fprintf(os.Stderr, "Error: unknown command %s\n", command)
|
||||||
|
printUsage()
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save out the persistent file
|
return SaveConfig(config)
|
||||||
if b, err := json.MarshalIndent(config, "", "\t"); err != nil {
|
|
||||||
return fmt.Errorf("failed to marshal data error: %s", err)
|
|
||||||
} else {
|
|
||||||
if err := ioutil.WriteFile(*data, b, os.ModePerm); err != nil {
|
|
||||||
return fmt.Errorf("failed to save file %s error: %s", *data, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple main
|
// Simple main
|
||||||
func main() {
|
func main() {
|
||||||
flag.Usage = Usage
|
// Bail without any args
|
||||||
flag.Parse()
|
if len(os.Args) == 1 {
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
// Verify we have a single command line arg
|
fmt.Fprintln(os.Stderr, "m mm mmm m m mmm")
|
||||||
args := flag.Args()
|
fmt.Fprintln(os.Stderr, "#\" \" #\" \"# \"m m\" #\" #")
|
||||||
if len(args) != 1 {
|
fmt.Fprintln(os.Stderr, "# # # #m# #\"\"\"\"")
|
||||||
Usage()
|
fmt.Fprintln(os.Stderr, "# \"#m#\" # \"#mm\"")
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
fmt.Fprintln(os.Stderr, "Rove is an asychronous nomadic game about exploring a planet as part of a loose community.")
|
||||||
|
fmt.Fprintln(os.Stderr, "Visit https://mdiluz.github.io/rove/ for more information.")
|
||||||
|
fmt.Fprintf(os.Stderr, "\n")
|
||||||
|
printUsage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the inner main
|
// Run the inner main
|
||||||
if err := InnerMain(args[0]); err != nil {
|
if err := InnerMain(os.Args[1], os.Args[2:]...); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,66 +1,67 @@
|
||||||
|
// +build integration
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/mdiluz/rove/pkg/server"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
s := server.NewServer(server.OptionPort(8080))
|
|
||||||
s.Initialise()
|
|
||||||
go s.Run()
|
|
||||||
|
|
||||||
code := m.Run()
|
|
||||||
|
|
||||||
s.Close()
|
|
||||||
|
|
||||||
os.Exit(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_InnerMain(t *testing.T) {
|
func Test_InnerMain(t *testing.T) {
|
||||||
// Set up the flags to act locally and use a temporary file
|
os.Setenv("NO_TLS", "1")
|
||||||
flag.Set("data", path.Join(os.TempDir(), uuid.New().String()))
|
|
||||||
|
|
||||||
// First attempt should error
|
// Use temporary local user data
|
||||||
assert.Error(t, InnerMain("status"))
|
tmp, err := ioutil.TempDir(os.TempDir(), "rove-")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
os.Setenv("ROVE_USER_DATA", tmp)
|
||||||
|
|
||||||
// Now set the host
|
// Used for configuring this test
|
||||||
flag.Set("host", "localhost:8080")
|
var address = os.Getenv("ROVE_GRPC")
|
||||||
|
if len(address) == 0 {
|
||||||
|
log.Fatal("Must set $ROVE_GRPC")
|
||||||
|
}
|
||||||
|
|
||||||
// No error now as we have a host
|
// First attempt should error without a host
|
||||||
assert.NoError(t, InnerMain("status"))
|
assert.Error(t, InnerMain("server-status"))
|
||||||
|
|
||||||
|
// Set the host in the config
|
||||||
|
assert.NoError(t, InnerMain("config", address))
|
||||||
|
assert.NoError(t, InnerMain("server-status"))
|
||||||
|
|
||||||
// Register should fail without a name
|
// Register should fail without a name
|
||||||
assert.Error(t, InnerMain("register"))
|
assert.Error(t, InnerMain("register"))
|
||||||
|
|
||||||
// These methods should fail without an account
|
// These methods should fail without an account
|
||||||
assert.Error(t, InnerMain("spawn"))
|
|
||||||
assert.Error(t, InnerMain("command"))
|
|
||||||
assert.Error(t, InnerMain("radar"))
|
assert.Error(t, InnerMain("radar"))
|
||||||
assert.Error(t, InnerMain("rover"))
|
assert.Error(t, InnerMain("status"))
|
||||||
|
|
||||||
// Now set the name
|
// Now set the name
|
||||||
flag.Set("name", uuid.New().String())
|
assert.NoError(t, InnerMain("register", uuid.New().String()))
|
||||||
|
|
||||||
// Perform the register
|
|
||||||
assert.NoError(t, InnerMain("register"))
|
|
||||||
|
|
||||||
// We've not spawned a rover yet so these should fail
|
|
||||||
assert.Error(t, InnerMain("command")) // Currently not erroring, needs investigation
|
|
||||||
assert.Error(t, InnerMain("radar"))
|
|
||||||
assert.Error(t, InnerMain("rover"))
|
|
||||||
|
|
||||||
// Spawn a rover
|
|
||||||
assert.NoError(t, InnerMain("spawn"))
|
|
||||||
|
|
||||||
// These should now work
|
// These should now work
|
||||||
assert.NoError(t, InnerMain("command"))
|
|
||||||
assert.NoError(t, InnerMain("radar"))
|
assert.NoError(t, InnerMain("radar"))
|
||||||
assert.NoError(t, InnerMain("rover"))
|
assert.NoError(t, InnerMain("status"))
|
||||||
|
|
||||||
|
// Commands should fail with no commands
|
||||||
|
assert.Error(t, InnerMain("command"))
|
||||||
|
|
||||||
|
// Give it commands
|
||||||
|
assert.NoError(t, InnerMain("command", "toggle"))
|
||||||
|
assert.NoError(t, InnerMain("command", "stash"))
|
||||||
|
assert.NoError(t, InnerMain("command", "repair"))
|
||||||
|
assert.NoError(t, InnerMain("command", "upgrade", "capacity"))
|
||||||
|
assert.NoError(t, InnerMain("command", "broadcast", "abc"))
|
||||||
|
assert.NoError(t, InnerMain("command", "wait", "10"))
|
||||||
|
assert.NoError(t, InnerMain("command", "wait", "1", "turn", "NW", "toggle", "broadcast", "zyx"))
|
||||||
|
|
||||||
|
// Give it malformed commands
|
||||||
|
assert.Error(t, InnerMain("command", "unknown"))
|
||||||
|
assert.Error(t, InnerMain("command", "broadcast"))
|
||||||
|
assert.Error(t, InnerMain("command", "upgrade"))
|
||||||
|
assert.Error(t, InnerMain("command", "1"))
|
||||||
}
|
}
|
||||||
|
|
3
data/icon.svg
Normal file
3
data/icon.svg
Normal file
File diff suppressed because one or more lines are too long
After Width: | Height: | Size: 5.7 KiB |
36
data/io.github.mdiluz.Rove.json
Normal file
36
data/io.github.mdiluz.Rove.json
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
{
|
||||||
|
"app-id": "io.github.mdiluz.Rove",
|
||||||
|
"runtime": "org.freedesktop.Platform",
|
||||||
|
"runtime-version": "19.08",
|
||||||
|
"sdk": "org.freedesktop.Sdk",
|
||||||
|
"sdk-extensions" : [
|
||||||
|
"org.freedesktop.Sdk.Extension.golang"
|
||||||
|
],
|
||||||
|
"finish-args" : [
|
||||||
|
"--share=network"
|
||||||
|
],
|
||||||
|
"command": "rove",
|
||||||
|
"modules": [
|
||||||
|
{
|
||||||
|
"name": "rove",
|
||||||
|
"buildsystem": "simple",
|
||||||
|
"build-options": {
|
||||||
|
"env": {
|
||||||
|
"GOBIN": "/app/bin/"
|
||||||
|
},
|
||||||
|
"build-args": [
|
||||||
|
"--share=network"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"build-commands" : [
|
||||||
|
". /usr/lib/sdk/golang/enable.sh; make install"
|
||||||
|
],
|
||||||
|
"sources": [
|
||||||
|
{
|
||||||
|
"type": "dir",
|
||||||
|
"path": ".."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
370104
data/words_alpha.txt
Normal file
370104
data/words_alpha.txt
Normal file
File diff suppressed because it is too large
Load diff
32
docker-compose-test.yml
Normal file
32
docker-compose-test.yml
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
rove-test-server:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
image: rove:latest
|
||||||
|
ports:
|
||||||
|
- "9090:9090"
|
||||||
|
environment:
|
||||||
|
- PORT=9090
|
||||||
|
- DATA_PATH=/tmp/
|
||||||
|
- WORDS_FILE=data/words_alpha.txt
|
||||||
|
- TICK_RATE=10
|
||||||
|
- NO_TLS=1
|
||||||
|
command: [ "./rove-server"]
|
||||||
|
|
||||||
|
rove-tests:
|
||||||
|
depends_on: [ rove-test-server ]
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
image: rove:latest
|
||||||
|
environment:
|
||||||
|
- ROVE_GRPC=rove-test-server
|
||||||
|
command: [ "./script/wait-for-it.sh", "rove-test-server:9090", "--", "go", "test", "-v", "./...", "--tags=integration", "-cover", "-coverprofile=/mnt/coverage-data/c.out", "-count", "1" ]
|
||||||
|
volumes:
|
||||||
|
- /tmp/coverage-data:/mnt/coverage-data:rw
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,10 +7,19 @@ services:
|
||||||
rove-server:
|
rove-server:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: cmd/rove-server/Dockerfile
|
dockerfile: Dockerfile
|
||||||
image: rove-server:latest
|
image: rove:latest
|
||||||
ports:
|
ports:
|
||||||
- "80:80"
|
- "9090:9090"
|
||||||
command: ./rove-server --port 80 --data=/mnt/rove-server
|
environment:
|
||||||
|
- PORT=9090
|
||||||
|
- DATA_PATH=/mnt/rove-server
|
||||||
|
- WORDS_FILE=data/words_alpha.txt
|
||||||
|
- TICK_RATE=3
|
||||||
|
- CERT_NAME=${CERT_NAME}
|
||||||
volumes:
|
volumes:
|
||||||
- persistent-data:/mnt/rove-server:rw
|
- persistent-data:/mnt/rove-server:rw
|
||||||
|
- /etc/letsencrypt/:/etc/letsencrypt/
|
||||||
|
command: [ "./rove-server"]
|
||||||
|
|
||||||
|
|
||||||
|
|
61
docs/README.md
Normal file
61
docs/README.md
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
Rove
|
||||||
|
=====
|
||||||
|
|
||||||
|
Rove is an asynchronous nomadic game about exploring a planet as part of a loose community.
|
||||||
|
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
## Core gameplay
|
||||||
|
|
||||||
|
Remotely explore the surface of a planet with an upgradable and customisable rover. Send commands to be executed asynchronously, view the rover's radar, and communicate and coordinate with other nearby rovers.
|
||||||
|
|
||||||
|
### Key Components
|
||||||
|
|
||||||
|
* Navigate an expansive world
|
||||||
|
* Collect resources to repair and upgrade
|
||||||
|
* Keep the rover batteries charged as you explore
|
||||||
|
* Help other players on their journey
|
||||||
|
* Explore north to discover more
|
||||||
|
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
## Installing
|
||||||
|
|
||||||
|
On Ubuntu:
|
||||||
|
```
|
||||||
|
$ snap install rove
|
||||||
|
```
|
||||||
|
|
||||||
|
Elsewhere (with [go](https://golang.org/doc/install) installed)
|
||||||
|
```
|
||||||
|
go get github.com/mdiluz/rove
|
||||||
|
cd $GOPATH/src/github.com/mdiluz/rove/
|
||||||
|
make install
|
||||||
|
```
|
||||||
|
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
### Implementation Details
|
||||||
|
|
||||||
|
`rove-server` hosts the game world and a gRPC server to allow users to interact from any client.
|
||||||
|
|
||||||
|
`rove` is a basic example command-line client that allows for simple play, to explore it's usage, see the output of `rove help`
|
||||||
|
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
### "Find the fun" issues to solve
|
||||||
|
|
||||||
|
* What kinds of progression/upgrades exist?
|
||||||
|
* How does the game encourage cooperation?
|
||||||
|
* How would the gameplay prevent griefing?
|
||||||
|
* What drives the exploration?
|
||||||
|
|
||||||
|
-------------------------------------------
|
||||||
|
|
||||||
|
### Key ideas left to integrate
|
||||||
|
|
||||||
|
* Feeling “known for” something - the person who did X thing. Limit number of X things that can be done, possibly over time.
|
||||||
|
* A significant aspect of failure - failing must be a focus of the game. Winning the game might actually be failing in some specific way.
|
||||||
|
* A clear and well defined investment vs. payoff curve.
|
||||||
|
* Not an infinite game, let the game have a point where you’re done and can move on.
|
||||||
|
|
1
docs/_config.yml
Normal file
1
docs/_config.yml
Normal file
|
@ -0,0 +1 @@
|
||||||
|
theme: jekyll-theme-merlot
|
|
@ -1,86 +0,0 @@
|
||||||
Rove
|
|
||||||
=====
|
|
||||||
|
|
||||||
An asynchronous nomadic game about exploring a planet as part of a loose
|
|
||||||
community.
|
|
||||||
|
|
||||||
-------------------------------------------
|
|
||||||
|
|
||||||
## The Basics
|
|
||||||
|
|
||||||
### Core
|
|
||||||
|
|
||||||
Control a rover on the surface of the planet using a remote control interface.
|
|
||||||
|
|
||||||
Commands are sent and happen asynchronously, and the rover feeds back information about position and surroundings, as well as photos.
|
|
||||||
|
|
||||||
### Goal
|
|
||||||
|
|
||||||
To reach the pole.
|
|
||||||
|
|
||||||
### General
|
|
||||||
|
|
||||||
Movement is slow and sometimes dangerous. Hazards damage the rover.
|
|
||||||
|
|
||||||
Resources can be collected to fix and upgrade the rover.
|
|
||||||
|
|
||||||
Rovers recharge power during the day.
|
|
||||||
|
|
||||||
Enough collected resources allow you to create and fire a new rover a significant distance in any direction, losing control of the current one and leaving it dormant.
|
|
||||||
|
|
||||||
Finding a dormant rover gives you a choice - scrap it to gain minor resources, or fire it a distance just like a new rover, taking control of it.
|
|
||||||
|
|
||||||
“Dying” triggers a self destruct and fires a new basic rover in a random direction towards the equator
|
|
||||||
|
|
||||||
## Multiplayer
|
|
||||||
|
|
||||||
The planet itself and things that happen on it are persistent. Players can view each other, and use very rudimentary signals.
|
|
||||||
|
|
||||||
Dormant rovers store full history of travel, owners, and keep damage, improvements and resources.
|
|
||||||
|
|
||||||
Players have no other forms of direct communication.
|
|
||||||
|
|
||||||
Players can view progress of all rovers attached to their name.
|
|
||||||
|
|
||||||
Limit too many players in one location with a simple interference mechanic - only a certain density can exist at once to operate properly, additional players can’t move within range.
|
|
||||||
|
|
||||||
-------------------------------------------
|
|
||||||
|
|
||||||
### Implementation
|
|
||||||
|
|
||||||
Two functional parts
|
|
||||||
|
|
||||||
A server that receives the commands, sends out data, and handles interactions between players.
|
|
||||||
|
|
||||||
An app, or apps, that interface with the server to let you control and view rover information
|
|
||||||
|
|
||||||
-------------------------------------------
|
|
||||||
|
|
||||||
### To Solve
|
|
||||||
|
|
||||||
#### What kinds of progression/upgrades exist?
|
|
||||||
Needs a very simple set of rover internals defined, each of which can be upgraded.
|
|
||||||
|
|
||||||
#### How does the game encourage lateral movement?
|
|
||||||
Could simply be the terrain is constructed in very lateral ways, blocking progress frequently
|
|
||||||
|
|
||||||
#### How does the game encourage cooperation?
|
|
||||||
How exactly would a time delay mechanic enhance the experience?
|
|
||||||
Currently it’s just to make the multiplayer easier to use, and to make interactions a little more complicated. The game could limit the number of bytes (commands) you can send over time.
|
|
||||||
|
|
||||||
#### How would the gameplay prevent griefing?
|
|
||||||
|
|
||||||
-------------------------------------------
|
|
||||||
|
|
||||||
### Key ideas left to integrate
|
|
||||||
|
|
||||||
Feeling “known for” something - the person who did X thing. Limit number of X things that can be done, possibly over time.
|
|
||||||
|
|
||||||
Build up a certain level of knowledge and ownership of a place, but then destroy it or give it up. Or build up a character and then leave it behind.
|
|
||||||
|
|
||||||
A significant aspect of failure - failing must be a focus of the game. Winning the game might actually be failing in some specific way.
|
|
||||||
|
|
||||||
A clear and well defined investment vs. payoff curve.
|
|
||||||
|
|
||||||
Not an infinite game, let the game have a point where you’re done and can move on.
|
|
||||||
|
|
30
docs/poc.md
30
docs/poc.md
|
@ -1,30 +0,0 @@
|
||||||
Proof Of Concept
|
|
||||||
================
|
|
||||||
|
|
||||||
For a proof of concept we'll implement a very small subset of the final features
|
|
||||||
|
|
||||||
### Core Features
|
|
||||||
|
|
||||||
* Create an account (done)
|
|
||||||
* Control an rover in a 2D map (done)
|
|
||||||
* Query for what the rover can see (done)
|
|
||||||
* Persistent accounts and world (done)
|
|
||||||
* Multiple users (done)
|
|
||||||
* Populate map with locations/objects
|
|
||||||
* Commands happen in "real time"
|
|
||||||
|
|
||||||
### Stretch goals
|
|
||||||
|
|
||||||
* Rover inventory
|
|
||||||
* Rover internals
|
|
||||||
* Basic time mechanics
|
|
||||||
* Basic survival mechanics
|
|
||||||
|
|
||||||
### Key missing features
|
|
||||||
|
|
||||||
* No token security (very hackable)
|
|
||||||
* No time dilation effects
|
|
||||||
* No global coordinates
|
|
||||||
* No render of rover camera view
|
|
||||||
* No rover replacement mechanic
|
|
||||||
* No advanced app/interface
|
|
14
go.mod
14
go.mod
|
@ -3,7 +3,19 @@ module github.com/mdiluz/rove
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/golang/protobuf v1.4.2
|
||||||
github.com/google/uuid v1.1.1
|
github.com/google/uuid v1.1.1
|
||||||
github.com/gorilla/mux v1.7.4
|
github.com/grpc-ecosystem/grpc-gateway v1.14.6
|
||||||
|
github.com/kr/pretty v0.1.0 // indirect
|
||||||
|
github.com/ojrac/opensimplex-go v1.0.1
|
||||||
|
github.com/robfig/cron v1.2.0
|
||||||
github.com/stretchr/testify v1.6.0
|
github.com/stretchr/testify v1.6.0
|
||||||
|
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
|
||||||
|
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
|
||||||
|
golang.org/x/text v0.3.3 // indirect
|
||||||
|
google.golang.org/grpc v1.30.0
|
||||||
|
google.golang.org/protobuf v1.25.0
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.3.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
133
go.sum
133
go.sum
|
@ -1,14 +1,143 @@
|
||||||
|
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
|
cloud.google.com/go v0.34.0 h1:eOI3/cP2VTU6uZLDYAoic+eyzzB9YyGmJ7eIjl8rOPg=
|
||||||
|
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
|
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||||
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
|
||||||
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
|
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||||
|
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f h1:WBZRG4aNOuI15bLRrCgN8fCq8E5Xuty6jGbmSNEvSsU=
|
||||||
|
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.4 h1:rEvIZUSZ3fx39WIi3JkQqQBitGwpELBIYWeBVh6wn+E=
|
||||||
|
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||||
|
github.com/envoyproxy/protoc-gen-validate v0.1.0 h1:EQciDnbrYxy13PgWoY8AqoxGiPrpgBZ1R8UNe3ddc+A=
|
||||||
|
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||||
|
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
|
||||||
|
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||||
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
|
||||||
|
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||||
|
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||||
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
|
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||||
|
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||||
|
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
|
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
||||||
|
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
|
||||||
|
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||||
|
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
|
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
|
||||||
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
|
||||||
|
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
|
github.com/grpc-ecosystem/grpc-gateway v1.14.6 h1:8ERzHx8aj1Sc47mu9n/AksaKCSWrMchFtkdrS4BIj5o=
|
||||||
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4GkDEdKCRJduHpTxT3/jcw=
|
||||||
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/ojrac/opensimplex-go v1.0.1 h1:XslvpLP6XqQSATUtsOnGBYtFPw7FQ6h6y0ihjVeOLHo=
|
||||||
|
github.com/ojrac/opensimplex-go v1.0.1/go.mod h1:MoSgj04tZpH8U0RefZabnHV2AbLgv/2mo3hLJtWqSEs=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
|
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
|
||||||
|
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
|
||||||
|
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
|
github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho=
|
||||||
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||||
|
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
|
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||||
|
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||||
|
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20191002035440-2ec189313ef0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 h1:pNX+40auqi2JqRfOP1akLGtYcn15TUbkhwuCO3foqqM=
|
||||||
|
golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
|
||||||
|
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
|
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 h1:OjiUf46hAmXblsZdnoSXsEUSKU8r1UEzcL5RVZ4gO9Y=
|
||||||
|
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||||
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A=
|
||||||
|
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
|
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||||
|
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE=
|
||||||
|
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||||
|
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 h1:fiNLklpBwWK1mth30Hlwk+fcdBmIALlgF5iy77O37Ig=
|
||||||
|
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||||
|
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY=
|
||||||
|
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
||||||
|
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||||
|
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||||
|
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||||
|
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
||||||
|
google.golang.org/grpc v1.29.1 h1:EC2SB8S04d2r73uptxphDSUG+kTKVgjRPF+N3xpxRB4=
|
||||||
|
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
|
||||||
|
google.golang.org/grpc v1.30.0 h1:M5a8xTlYTxwMn5ZFkwhRabsygDY5G8TYLyQDBxJNAxE=
|
||||||
|
google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||||
|
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||||
|
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||||
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
|
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
|
||||||
|
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
|
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
||||||
|
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
||||||
|
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=
|
||||||
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
|
|
@ -1,85 +1,28 @@
|
||||||
package accounts
|
package accounts
|
||||||
|
|
||||||
import (
|
// Accountant decribes something that stores accounts and account values
|
||||||
"fmt"
|
type Accountant interface {
|
||||||
|
// RegisterAccount will register a new account and return it's info
|
||||||
|
RegisterAccount(name string) (acc Account, err error)
|
||||||
|
|
||||||
"github.com/google/uuid"
|
// AssignData stores a custom account key value pair
|
||||||
)
|
AssignData(account string, key string, value string) error
|
||||||
|
|
||||||
const kAccountsFileName = "rove-accounts.json"
|
// GetValue returns custom account data for a specific key
|
||||||
|
GetValue(account string, key string) (string, error)
|
||||||
|
|
||||||
|
// VerifySecret will verify whether the account secret matches with the
|
||||||
|
VerifySecret(account string, secret string) (bool, error)
|
||||||
|
|
||||||
|
// GetSecret gets the secret associated with an account
|
||||||
|
GetSecret(account string) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
// Account represents a registered user
|
// Account represents a registered user
|
||||||
type Account struct {
|
type Account struct {
|
||||||
// Name simply describes the account and must be unique
|
// Name simply describes the account and must be unique
|
||||||
Name string `json:"name"`
|
Name string
|
||||||
|
|
||||||
// Id represents a unique ID per account and is set one registered
|
// Data represents internal account data
|
||||||
Id uuid.UUID `json:"id"`
|
Data map[string]string
|
||||||
|
|
||||||
// Rover represents the rover that this account owns
|
|
||||||
Rover uuid.UUID `json:"rover"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Represents the accountant data to store
|
|
||||||
type accountantData struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
// Accountant manages a set of accounts
|
|
||||||
type Accountant struct {
|
|
||||||
Accounts map[uuid.UUID]Account `json:"accounts"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAccountant creates a new accountant
|
|
||||||
func NewAccountant() *Accountant {
|
|
||||||
return &Accountant{
|
|
||||||
Accounts: make(map[uuid.UUID]Account),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterAccount adds an account to the set of internal accounts
|
|
||||||
func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
|
|
||||||
|
|
||||||
// Set the account ID to a new UUID
|
|
||||||
acc.Id = uuid.New()
|
|
||||||
acc.Name = name
|
|
||||||
|
|
||||||
// Verify this acount isn't already registered
|
|
||||||
for _, a := range a.Accounts {
|
|
||||||
if a.Name == acc.Name {
|
|
||||||
return Account{}, fmt.Errorf("Account name already registered")
|
|
||||||
} else if a.Id == acc.Id {
|
|
||||||
return Account{}, fmt.Errorf("Account ID already registered")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simply add the account to the map
|
|
||||||
a.Accounts[acc.Id] = acc
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// AssignRover assigns rover ownership of an rover to an account
|
|
||||||
func (a *Accountant) AssignRover(account uuid.UUID, rover uuid.UUID) error {
|
|
||||||
|
|
||||||
// Find the account matching the ID
|
|
||||||
if this, ok := a.Accounts[account]; ok {
|
|
||||||
this.Rover = rover
|
|
||||||
a.Accounts[account] = this
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("no account found for id: %s", account)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRover gets the rover rover for the account
|
|
||||||
func (a *Accountant) GetRover(account uuid.UUID) (uuid.UUID, error) {
|
|
||||||
// Find the account matching the ID
|
|
||||||
if this, ok := a.Accounts[account]; !ok {
|
|
||||||
return uuid.UUID{}, fmt.Errorf("no account found for id: %s", account)
|
|
||||||
} else if this.Rover == uuid.Nil {
|
|
||||||
return uuid.UUID{}, fmt.Errorf("no rover spawned for account %s", account)
|
|
||||||
} else {
|
|
||||||
return this.Rover, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
func TestNewAccountant(t *testing.T) {
|
func TestNewAccountant(t *testing.T) {
|
||||||
// Very basic verify here for now
|
// Very basic verify here for now
|
||||||
accountant := NewAccountant()
|
accountant := NewSimpleAccountant()
|
||||||
if accountant == nil {
|
if accountant == nil {
|
||||||
t.Error("Failed to create accountant")
|
t.Error("Failed to create accountant")
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ func TestNewAccountant(t *testing.T) {
|
||||||
|
|
||||||
func TestAccountant_RegisterAccount(t *testing.T) {
|
func TestAccountant_RegisterAccount(t *testing.T) {
|
||||||
|
|
||||||
accountant := NewAccountant()
|
accountant := NewSimpleAccountant()
|
||||||
|
|
||||||
// Start by making two accounts
|
// Start by making two accounts
|
||||||
|
|
||||||
|
@ -36,11 +36,6 @@ func TestAccountant_RegisterAccount(t *testing.T) {
|
||||||
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
|
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify our accounts have differing IDs
|
|
||||||
if acca.Id == accb.Id {
|
|
||||||
t.Error("Duplicate account IDs fo separate accounts")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify another request gets rejected
|
// Verify another request gets rejected
|
||||||
_, err = accountant.RegisterAccount(namea)
|
_, err = accountant.RegisterAccount(namea)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -48,11 +43,8 @@ func TestAccountant_RegisterAccount(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAccountant_AssignGetRover(t *testing.T) {
|
func TestAccountant_AssignGetData(t *testing.T) {
|
||||||
accountant := NewAccountant()
|
accountant := NewSimpleAccountant()
|
||||||
if len(accountant.Accounts) != 0 {
|
|
||||||
t.Error("New accountant created with non-zero account number")
|
|
||||||
}
|
|
||||||
|
|
||||||
name := uuid.New().String()
|
name := uuid.New().String()
|
||||||
a, err := accountant.RegisterAccount(name)
|
a, err := accountant.RegisterAccount(name)
|
||||||
|
@ -60,16 +52,12 @@ func TestAccountant_AssignGetRover(t *testing.T) {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
inst := uuid.New()
|
err = accountant.AssignData(a.Name, "key", "value")
|
||||||
|
|
||||||
err = accountant.AssignRover(a.Id, inst)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Failed to set rover for created account")
|
t.Error("Failed to set data for created account")
|
||||||
} else if accountant.Accounts[a.Id].Rover != inst {
|
} else if id, err := accountant.GetValue(a.Name, "key"); err != nil {
|
||||||
t.Error("Rover for assigned account is incorrect")
|
t.Error("Failed to get data for account")
|
||||||
} else if id, err := accountant.GetRover(a.Id); err != nil {
|
} else if id != "value" {
|
||||||
t.Error("Failed to get rover for account")
|
t.Error("Fetched data is incorrect for account")
|
||||||
} else if id != inst {
|
|
||||||
t.Error("Fetched rover is incorrect for account")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
90
pkg/accounts/simpleAccountant.go
Normal file
90
pkg/accounts/simpleAccountant.go
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
package accounts
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SimpleAccountant manages a set of accounts
|
||||||
|
type SimpleAccountant struct {
|
||||||
|
Accounts map[string]Account
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSimpleAccountant creates a new accountant
|
||||||
|
func NewSimpleAccountant() Accountant {
|
||||||
|
return &SimpleAccountant{
|
||||||
|
Accounts: make(map[string]Account),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterAccount adds an account to the set of internal accounts
|
||||||
|
func (a *SimpleAccountant) RegisterAccount(name string) (acc Account, err error) {
|
||||||
|
|
||||||
|
// Set up the account info
|
||||||
|
acc.Name = name
|
||||||
|
acc.Data = make(map[string]string)
|
||||||
|
|
||||||
|
// Verify this acount isn't already registered
|
||||||
|
for _, a := range a.Accounts {
|
||||||
|
if a.Name == acc.Name {
|
||||||
|
return Account{}, fmt.Errorf("account name already registered: %s", a.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the creation time
|
||||||
|
acc.Data["created"] = time.Now().String()
|
||||||
|
|
||||||
|
// Create a secret
|
||||||
|
acc.Data["secret"] = uuid.New().String()
|
||||||
|
|
||||||
|
// Simply add the account to the map
|
||||||
|
a.Accounts[acc.Name] = acc
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifySecret verifies if an account secret is correct
|
||||||
|
func (a *SimpleAccountant) VerifySecret(account string, secret string) (bool, error) {
|
||||||
|
// Find the account matching the ID
|
||||||
|
if this, ok := a.Accounts[account]; ok {
|
||||||
|
return this.Data["secret"] == secret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, fmt.Errorf("no account found for id: %s", account)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSecret gets the internal secret
|
||||||
|
func (a *SimpleAccountant) GetSecret(account string) (string, error) {
|
||||||
|
// Find the account matching the ID
|
||||||
|
if this, ok := a.Accounts[account]; ok {
|
||||||
|
return this.Data["secret"], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("no account found for id: %s", account)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssignData assigns data to an account
|
||||||
|
func (a *SimpleAccountant) AssignData(account string, key string, value string) error {
|
||||||
|
|
||||||
|
// Find the account matching the ID
|
||||||
|
if this, ok := a.Accounts[account]; ok {
|
||||||
|
this.Data[key] = value
|
||||||
|
a.Accounts[account] = this
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("no account found for id: %s", account)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetValue gets the rover rover for the account
|
||||||
|
func (a *SimpleAccountant) GetValue(account string, key string) (string, error) {
|
||||||
|
// Find the account matching the ID
|
||||||
|
this, ok := a.Accounts[account]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("no account found for id: %s", account)
|
||||||
|
}
|
||||||
|
return this.Data[key], nil
|
||||||
|
}
|
|
@ -1,14 +0,0 @@
|
||||||
package game
|
|
||||||
|
|
||||||
import "github.com/google/uuid"
|
|
||||||
|
|
||||||
// A command is simply a function that acts on the a given rover in the world
|
|
||||||
type Command func() error
|
|
||||||
|
|
||||||
// CommandMove will move the rover in question
|
|
||||||
func (w *World) CommandMove(id uuid.UUID, bearing Direction, duration int) Command {
|
|
||||||
return func() error {
|
|
||||||
_, err := w.MoveRover(id, bearing, duration)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,33 +0,0 @@
|
||||||
package game
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestCommand_Move(t *testing.T) {
|
|
||||||
world := NewWorld()
|
|
||||||
a := world.SpawnRover()
|
|
||||||
pos := Vector{
|
|
||||||
X: 1.0,
|
|
||||||
Y: 2.0,
|
|
||||||
}
|
|
||||||
|
|
||||||
attribs, err := world.RoverAttributes(a)
|
|
||||||
assert.NoError(t, err, "Failed to get rover attribs")
|
|
||||||
|
|
||||||
err = world.WarpRover(a, pos)
|
|
||||||
assert.NoError(t, err, "Failed to set position for rover")
|
|
||||||
|
|
||||||
bearing := North
|
|
||||||
duration := 1
|
|
||||||
// Try the move command
|
|
||||||
moveCommand := world.CommandMove(a, bearing, duration)
|
|
||||||
assert.NoError(t, world.Execute(moveCommand), "Failed to execute move command")
|
|
||||||
|
|
||||||
newpos, err := world.RoverPosition(a)
|
|
||||||
assert.NoError(t, err, "Failed to set position for rover")
|
|
||||||
pos.Add(Vector{0.0, duration * attribs.Speed}) // We should have moved duration*speed north
|
|
||||||
assert.Equal(t, pos, newpos, "Failed to correctly set position for rover")
|
|
||||||
}
|
|
114
pkg/game/geom.go
114
pkg/game/geom.go
|
@ -1,114 +0,0 @@
|
||||||
package game
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"math"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Vector desribes a 3D vector
|
|
||||||
type Vector struct {
|
|
||||||
X int `json:"x"`
|
|
||||||
Y int `json:"y"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add adds one vector to another
|
|
||||||
func (v *Vector) Add(v2 Vector) {
|
|
||||||
v.X += v2.X
|
|
||||||
v.Y += v2.Y
|
|
||||||
}
|
|
||||||
|
|
||||||
// Added calculates a new vector
|
|
||||||
func (v Vector) Added(v2 Vector) Vector {
|
|
||||||
v.Add(v2)
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
|
|
||||||
// Negated returns a negated vector
|
|
||||||
func (v Vector) Negated() Vector {
|
|
||||||
return Vector{-v.X, -v.Y}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Length returns the length of the vector
|
|
||||||
func (v Vector) Length() float64 {
|
|
||||||
return math.Sqrt(float64(v.X*v.X + v.Y*v.Y))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Distance returns the distance between two vectors
|
|
||||||
func (v Vector) Distance(v2 Vector) float64 {
|
|
||||||
// Negate the two vectors and calciate the length
|
|
||||||
return v.Added(v2.Negated()).Length()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Multiplied returns the vector multiplied by an int
|
|
||||||
func (v Vector) Multiplied(val int) Vector {
|
|
||||||
return Vector{v.X * val, v.Y * val}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Direction describes a compass direction
|
|
||||||
type Direction int
|
|
||||||
|
|
||||||
const (
|
|
||||||
North Direction = iota
|
|
||||||
NorthEast
|
|
||||||
East
|
|
||||||
SouthEast
|
|
||||||
South
|
|
||||||
SouthWest
|
|
||||||
West
|
|
||||||
NorthWest
|
|
||||||
)
|
|
||||||
|
|
||||||
// DirectionString simply describes the strings associated with a direction
|
|
||||||
type DirectionString struct {
|
|
||||||
Long string
|
|
||||||
Short string
|
|
||||||
}
|
|
||||||
|
|
||||||
// DirectionStrings is the set of strings for each direction
|
|
||||||
var DirectionStrings = []DirectionString{
|
|
||||||
{"North", "N"},
|
|
||||||
{"NorthEast", "NE"},
|
|
||||||
{"East", "E"},
|
|
||||||
{"SouthEast", "SE"},
|
|
||||||
{"South", "S"},
|
|
||||||
{"SouthWest", "SW"},
|
|
||||||
{"West", "W"},
|
|
||||||
{"NorthWest", "NW"},
|
|
||||||
}
|
|
||||||
|
|
||||||
// String converts a Direction to a String
|
|
||||||
func (d Direction) String() string {
|
|
||||||
return DirectionStrings[d].Long
|
|
||||||
}
|
|
||||||
|
|
||||||
// ShortString converts a Direction to a short string version
|
|
||||||
func (d Direction) ShortString() string {
|
|
||||||
return DirectionStrings[d].Short
|
|
||||||
}
|
|
||||||
|
|
||||||
// DirectionFromString gets the Direction from a string
|
|
||||||
func DirectionFromString(s string) (Direction, error) {
|
|
||||||
for i, d := range DirectionStrings {
|
|
||||||
if strings.ToLower(d.Long) == strings.ToLower(s) || strings.ToLower(d.Short) == strings.ToLower(s) {
|
|
||||||
return Direction(i), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1, fmt.Errorf("Unknown direction: %s", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
var DirectionVectors = []Vector{
|
|
||||||
{0, 1}, // N
|
|
||||||
{1, 1}, // NE
|
|
||||||
{1, 0}, // E
|
|
||||||
{1, -1}, // SE
|
|
||||||
{0, -1}, // S
|
|
||||||
{-1, 1}, // SW
|
|
||||||
{-1, 0}, // W
|
|
||||||
{-1, 1}, // NW
|
|
||||||
}
|
|
||||||
|
|
||||||
// Vector converts a Direction to a Vector
|
|
||||||
func (d Direction) Vector() Vector {
|
|
||||||
return DirectionVectors[d]
|
|
||||||
}
|
|
|
@ -1,156 +0,0 @@
|
||||||
package game
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
// World describes a self contained universe and everything in it
|
|
||||||
type World struct {
|
|
||||||
// Rovers is a id->data map of all the rovers in the game
|
|
||||||
Rovers map[uuid.UUID]Rover `json:"rovers"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// RoverAttributes contains attributes of a rover
|
|
||||||
type RoverAttributes struct {
|
|
||||||
// Speed represents the Speed that the rover will move per second
|
|
||||||
Speed int `json:"speed"`
|
|
||||||
|
|
||||||
// Range represents the distance the unit's radar can see
|
|
||||||
Range int `json:"range"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rover describes a single rover in the world
|
|
||||||
type Rover struct {
|
|
||||||
// Id is a unique ID for this rover
|
|
||||||
Id uuid.UUID `json:"id"`
|
|
||||||
|
|
||||||
// Pos represents where this rover is in the world
|
|
||||||
Pos Vector `json:"pos"`
|
|
||||||
|
|
||||||
// Attributes represents the physical attributes of the rover
|
|
||||||
Attributes RoverAttributes `json:"attributes"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewWorld creates a new world object
|
|
||||||
func NewWorld() *World {
|
|
||||||
return &World{
|
|
||||||
Rovers: make(map[uuid.UUID]Rover),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SpawnRover adds an rover to the game
|
|
||||||
func (w *World) SpawnRover() uuid.UUID {
|
|
||||||
// Initialise the rover
|
|
||||||
rover := Rover{
|
|
||||||
Id: uuid.New(),
|
|
||||||
|
|
||||||
// TODO: Set this somehow
|
|
||||||
Pos: Vector{},
|
|
||||||
|
|
||||||
// TODO: Stop these being random numbers
|
|
||||||
Attributes: RoverAttributes{
|
|
||||||
Speed: 1.0,
|
|
||||||
Range: 20.0,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Append the rover to the list
|
|
||||||
w.Rovers[rover.Id] = rover
|
|
||||||
|
|
||||||
return rover.Id
|
|
||||||
}
|
|
||||||
|
|
||||||
// Removes an rover from the game
|
|
||||||
func (w *World) DestroyRover(id uuid.UUID) error {
|
|
||||||
if _, ok := w.Rovers[id]; ok {
|
|
||||||
delete(w.Rovers, id)
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RoverAttributes returns the attributes of a requested rover
|
|
||||||
func (w World) RoverAttributes(id uuid.UUID) (RoverAttributes, error) {
|
|
||||||
if i, ok := w.Rovers[id]; ok {
|
|
||||||
return i.Attributes, nil
|
|
||||||
} else {
|
|
||||||
return RoverAttributes{}, fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RoverPosition returns the position of a given rover
|
|
||||||
func (w World) RoverPosition(id uuid.UUID) (Vector, error) {
|
|
||||||
if i, ok := w.Rovers[id]; ok {
|
|
||||||
return i.Pos, nil
|
|
||||||
} else {
|
|
||||||
return Vector{}, fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// WarpRover sets an rovers position
|
|
||||||
func (w *World) WarpRover(id uuid.UUID, pos Vector) error {
|
|
||||||
if i, ok := w.Rovers[id]; ok {
|
|
||||||
i.Pos = pos
|
|
||||||
w.Rovers[id] = i
|
|
||||||
return nil
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPosition sets an rovers position
|
|
||||||
func (w *World) MoveRover(id uuid.UUID, bearing Direction, duration int) (Vector, error) {
|
|
||||||
if i, ok := w.Rovers[id]; ok {
|
|
||||||
// Calculate the distance
|
|
||||||
distance := i.Attributes.Speed * duration
|
|
||||||
|
|
||||||
// Calculate the full movement based on the bearing
|
|
||||||
move := bearing.Vector().Multiplied(distance)
|
|
||||||
|
|
||||||
// Increment the position by the movement
|
|
||||||
i.Pos.Add(move)
|
|
||||||
|
|
||||||
// Set the rover values to the new ones
|
|
||||||
w.Rovers[id] = i
|
|
||||||
return i.Pos, nil
|
|
||||||
} else {
|
|
||||||
return Vector{}, fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RadarDescription describes what a rover can see
|
|
||||||
type RadarDescription struct {
|
|
||||||
// Rovers is the set of rovers that this radar can see
|
|
||||||
Rovers []Vector `json:"rovers"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// RadarFromRover can be used to query what a rover can currently see
|
|
||||||
func (w World) RadarFromRover(id uuid.UUID) (RadarDescription, error) {
|
|
||||||
if r1, ok := w.Rovers[id]; ok {
|
|
||||||
var desc RadarDescription
|
|
||||||
|
|
||||||
// Gather nearby rovers within the range
|
|
||||||
for _, r2 := range w.Rovers {
|
|
||||||
if r1.Id != r2.Id && r1.Pos.Distance(r2.Pos) < float64(r1.Attributes.Range) {
|
|
||||||
desc.Rovers = append(desc.Rovers, r2.Pos)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return desc, nil
|
|
||||||
} else {
|
|
||||||
return RadarDescription{}, fmt.Errorf("no rover matching id")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute will run the commands given
|
|
||||||
func (w *World) Execute(commands ...Command) error {
|
|
||||||
for _, c := range commands {
|
|
||||||
if err := c(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,102 +0,0 @@
|
||||||
package game
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNewWorld(t *testing.T) {
|
|
||||||
// Very basic for now, nothing to verify
|
|
||||||
world := NewWorld()
|
|
||||||
if world == nil {
|
|
||||||
t.Error("Failed to create world")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWorld_CreateRover(t *testing.T) {
|
|
||||||
world := NewWorld()
|
|
||||||
a := world.SpawnRover()
|
|
||||||
b := world.SpawnRover()
|
|
||||||
|
|
||||||
// Basic duplicate check
|
|
||||||
if a == b {
|
|
||||||
t.Errorf("Created identical rovers")
|
|
||||||
} else if len(world.Rovers) != 2 {
|
|
||||||
t.Errorf("Incorrect number of rovers created")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWorld_RoverAttributes(t *testing.T) {
|
|
||||||
world := NewWorld()
|
|
||||||
a := world.SpawnRover()
|
|
||||||
|
|
||||||
attribs, err := world.RoverAttributes(a)
|
|
||||||
assert.NoError(t, err, "Failed to get rover attribs")
|
|
||||||
assert.NotZero(t, attribs.Range, "Rover should not be spawned blind")
|
|
||||||
assert.NotZero(t, attribs.Speed, "Rover should not be spawned unable to move")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWorld_DestroyRover(t *testing.T) {
|
|
||||||
world := NewWorld()
|
|
||||||
a := world.SpawnRover()
|
|
||||||
b := world.SpawnRover()
|
|
||||||
|
|
||||||
err := world.DestroyRover(a)
|
|
||||||
assert.NoError(t, err, "Error returned from rover destroy")
|
|
||||||
|
|
||||||
// Basic duplicate check
|
|
||||||
if len(world.Rovers) != 1 {
|
|
||||||
t.Error("Too many rovers left in world")
|
|
||||||
} else if _, ok := world.Rovers[b]; !ok {
|
|
||||||
t.Error("Remaining rover is incorrect")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWorld_GetSetMovePosition(t *testing.T) {
|
|
||||||
world := NewWorld()
|
|
||||||
a := world.SpawnRover()
|
|
||||||
attribs, err := world.RoverAttributes(a)
|
|
||||||
assert.NoError(t, err, "Failed to get rover attribs")
|
|
||||||
|
|
||||||
pos := Vector{
|
|
||||||
X: 1.0,
|
|
||||||
Y: 2.0,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = world.WarpRover(a, pos)
|
|
||||||
assert.NoError(t, err, "Failed to set position for rover")
|
|
||||||
|
|
||||||
newpos, err := world.RoverPosition(a)
|
|
||||||
assert.NoError(t, err, "Failed to set position for rover")
|
|
||||||
assert.Equal(t, pos, newpos, "Failed to correctly set position for rover")
|
|
||||||
|
|
||||||
bearing := North
|
|
||||||
duration := 1
|
|
||||||
newpos, err = world.MoveRover(a, bearing, duration)
|
|
||||||
assert.NoError(t, err, "Failed to set position for rover")
|
|
||||||
pos.Add(Vector{0, attribs.Speed * duration}) // We should have move one unit of the speed north
|
|
||||||
assert.Equal(t, pos, newpos, "Failed to correctly move position for rover")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWorld_RadarFromRover(t *testing.T) {
|
|
||||||
world := NewWorld()
|
|
||||||
a := world.SpawnRover()
|
|
||||||
b := world.SpawnRover()
|
|
||||||
c := world.SpawnRover()
|
|
||||||
|
|
||||||
// Get a's attributes
|
|
||||||
attrib, err := world.RoverAttributes(a)
|
|
||||||
assert.NoError(t, err, "Failed to get rover attribs")
|
|
||||||
|
|
||||||
// Warp the rovers so a can see b but not c
|
|
||||||
assert.NoError(t, world.WarpRover(a, Vector{0, 0}), "Failed to warp rover")
|
|
||||||
assert.NoError(t, world.WarpRover(b, Vector{attrib.Range - 1, 0}), "Failed to warp rover")
|
|
||||||
assert.NoError(t, world.WarpRover(c, Vector{attrib.Range + 1, 0}), "Failed to warp rover")
|
|
||||||
|
|
||||||
radar, err := world.RadarFromRover(a)
|
|
||||||
assert.NoError(t, err, "Failed to get radar from rover")
|
|
||||||
assert.Equal(t, 1, len(radar.Rovers), "Radar returned wrong number of rovers")
|
|
||||||
assert.Equal(t, Vector{attrib.Range - 1, 0}, radar.Rovers[0], "Rover on radar in wrong position")
|
|
||||||
|
|
||||||
}
|
|
57
pkg/maths/maths.go
Normal file
57
pkg/maths/maths.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package maths
|
||||||
|
|
||||||
|
// Abs gets the absolute value of an int
|
||||||
|
func Abs(x int) int {
|
||||||
|
if x < 0 {
|
||||||
|
return -x
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pmod is a mositive modulo
|
||||||
|
// golang's % is a "remainder" function si misbehaves for negative modulus inputs
|
||||||
|
func Pmod(x, d int) int {
|
||||||
|
if x == 0 || d == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
x = x % d
|
||||||
|
if x >= 0 {
|
||||||
|
return x
|
||||||
|
} else if d < 0 {
|
||||||
|
return x - d
|
||||||
|
} else {
|
||||||
|
return x + d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Max returns the highest int
|
||||||
|
func Max(x, y int) int {
|
||||||
|
if x < y {
|
||||||
|
return y
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min returns the lowest int
|
||||||
|
func Min(x, y int) int {
|
||||||
|
if x > y {
|
||||||
|
return y
|
||||||
|
}
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundUp rounds a value up to the nearest multiple
|
||||||
|
func RoundUp(toRound int, multiple int) int {
|
||||||
|
remainder := Pmod(toRound, multiple)
|
||||||
|
if remainder == 0 {
|
||||||
|
return toRound
|
||||||
|
}
|
||||||
|
|
||||||
|
return (multiple - remainder) + toRound
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundDown rounds a value down to the nearest multiple
|
||||||
|
func RoundDown(toRound int, multiple int) int {
|
||||||
|
remainder := Pmod(toRound, multiple)
|
||||||
|
return toRound - remainder
|
||||||
|
}
|
47
pkg/maths/maths_test.go
Normal file
47
pkg/maths/maths_test.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
package maths
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAbs(t *testing.T) {
|
||||||
|
assert.Equal(t, 0, Abs(0))
|
||||||
|
assert.Equal(t, 1, Abs(1))
|
||||||
|
assert.Equal(t, 1, Abs(-1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPmod(t *testing.T) {
|
||||||
|
assert.Equal(t, 0, Pmod(0, 0))
|
||||||
|
assert.Equal(t, 2, Pmod(6, 4))
|
||||||
|
assert.Equal(t, 2, Pmod(-6, 4))
|
||||||
|
assert.Equal(t, 4, Pmod(-6, 10))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMax(t *testing.T) {
|
||||||
|
assert.Equal(t, 500, Max(100, 500))
|
||||||
|
assert.Equal(t, 1, Max(-4, 1))
|
||||||
|
assert.Equal(t, -2, Max(-4, -2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMin(t *testing.T) {
|
||||||
|
assert.Equal(t, 100, Min(100, 500))
|
||||||
|
assert.Equal(t, -4, Min(-4, 1))
|
||||||
|
assert.Equal(t, -4, Min(-4, -2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRoundUp(t *testing.T) {
|
||||||
|
assert.Equal(t, 10, RoundUp(10, 5))
|
||||||
|
assert.Equal(t, 12, RoundUp(10, 4))
|
||||||
|
assert.Equal(t, -8, RoundUp(-8, 4))
|
||||||
|
assert.Equal(t, -4, RoundUp(-7, 4))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRoundDown(t *testing.T) {
|
||||||
|
assert.Equal(t, 10, RoundDown(10, 5))
|
||||||
|
assert.Equal(t, 8, RoundDown(10, 4))
|
||||||
|
assert.Equal(t, -8, RoundDown(-8, 4))
|
||||||
|
assert.Equal(t, -8, RoundDown(-7, 4))
|
||||||
|
|
||||||
|
}
|
119
pkg/maths/vector.go
Normal file
119
pkg/maths/vector.go
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
package maths
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Vector desribes a 3D vector
|
||||||
|
type Vector struct {
|
||||||
|
X int
|
||||||
|
Y int
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds one vector to another
|
||||||
|
func (v *Vector) Add(v2 Vector) {
|
||||||
|
v.X += v2.X
|
||||||
|
v.Y += v2.Y
|
||||||
|
}
|
||||||
|
|
||||||
|
// Added calculates a new vector
|
||||||
|
func (v Vector) Added(v2 Vector) Vector {
|
||||||
|
v.Add(v2)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Negated returns a negated vector
|
||||||
|
func (v Vector) Negated() Vector {
|
||||||
|
return Vector{-v.X, -v.Y}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Length returns the length of the vector
|
||||||
|
func (v Vector) Length() float64 {
|
||||||
|
return math.Sqrt(float64(v.X*v.X + v.Y*v.Y))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Distance returns the distance between two vectors
|
||||||
|
func (v Vector) Distance(v2 Vector) float64 {
|
||||||
|
// Negate the two vectors and calciate the length
|
||||||
|
return v.Added(v2.Negated()).Length()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiplied returns the vector multiplied by an int
|
||||||
|
func (v Vector) Multiplied(val int) Vector {
|
||||||
|
return Vector{v.X * val, v.Y * val}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Divided returns the vector divided by an int
|
||||||
|
func (v Vector) Divided(val int) Vector {
|
||||||
|
return Vector{v.X / val, v.Y / val}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DividedFloor returns the vector divided but floors the value regardless
|
||||||
|
func (v Vector) DividedFloor(val int) Vector {
|
||||||
|
x := float64(v.X) / float64(val)
|
||||||
|
|
||||||
|
if x < 0 {
|
||||||
|
x = math.Floor(x)
|
||||||
|
} else {
|
||||||
|
x = math.Floor(x)
|
||||||
|
}
|
||||||
|
y := float64(v.Y) / float64(val)
|
||||||
|
if y < 0 {
|
||||||
|
y = math.Floor(y)
|
||||||
|
} else {
|
||||||
|
y = math.Floor(y)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Vector{X: int(x), Y: int(y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Abs returns an absolute version of the vector
|
||||||
|
func (v Vector) Abs() Vector {
|
||||||
|
return Vector{Abs(v.X), Abs(v.Y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min2 returns the minimum values in both vectors
|
||||||
|
func Min2(v1 Vector, v2 Vector) Vector {
|
||||||
|
return Vector{Min(v1.X, v2.X), Min(v1.Y, v2.Y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Max2 returns the max values in both vectors
|
||||||
|
func Max2(v1 Vector, v2 Vector) Vector {
|
||||||
|
return Vector{Max(v1.X, v2.X), Max(v1.Y, v2.Y)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BearingToVector converts a bearing to a vector
|
||||||
|
func BearingToVector(b roveapi.Bearing) Vector {
|
||||||
|
switch b {
|
||||||
|
case roveapi.Bearing_North:
|
||||||
|
return Vector{Y: 1}
|
||||||
|
case roveapi.Bearing_NorthEast:
|
||||||
|
return Vector{X: 1, Y: 1}
|
||||||
|
case roveapi.Bearing_East:
|
||||||
|
return Vector{X: 1}
|
||||||
|
case roveapi.Bearing_SouthEast:
|
||||||
|
return Vector{X: 1, Y: -1}
|
||||||
|
case roveapi.Bearing_South:
|
||||||
|
return Vector{Y: -1}
|
||||||
|
case roveapi.Bearing_SouthWest:
|
||||||
|
return Vector{X: -1, Y: -1}
|
||||||
|
case roveapi.Bearing_West:
|
||||||
|
return Vector{X: -1}
|
||||||
|
case roveapi.Bearing_NorthWest:
|
||||||
|
return Vector{X: -1, Y: 1}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Vector{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dot returns the dot product of two vectors
|
||||||
|
func Dot(a Vector, b Vector) int {
|
||||||
|
return a.X*b.X + a.Y*b.Y
|
||||||
|
}
|
||||||
|
|
||||||
|
// AngleCos returns the cosine of the angle between two vectors
|
||||||
|
func AngleCos(a Vector, b Vector) float64 {
|
||||||
|
return float64(Dot(a, b)) / a.Length() * b.Length()
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package game
|
package maths
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
@ -180,15 +180,15 @@ func TestVector_Multiplied(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "Basic multiply 1",
|
name: "Basic multiply 1",
|
||||||
vec: North.Vector(),
|
vec: Vector{0, 1},
|
||||||
arg: 2,
|
arg: 2,
|
||||||
want: Vector{0, 2},
|
want: Vector{0, 2},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Basic multiply 2",
|
name: "Basic multiply 2",
|
||||||
vec: NorthWest.Vector(),
|
vec: Vector{-1, 1},
|
||||||
arg: -1,
|
arg: -1,
|
||||||
want: SouthEast.Vector(),
|
want: Vector{1, -1},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -203,27 +203,3 @@ func TestVector_Multiplied(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDirection(t *testing.T) {
|
|
||||||
dir := North
|
|
||||||
|
|
||||||
assert.Equal(t, "North", dir.String())
|
|
||||||
assert.Equal(t, "N", dir.ShortString())
|
|
||||||
assert.Equal(t, Vector{0, 1}, dir.Vector())
|
|
||||||
|
|
||||||
dir, err := DirectionFromString("N")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, North, dir)
|
|
||||||
|
|
||||||
dir, err = DirectionFromString("n")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, North, dir)
|
|
||||||
|
|
||||||
dir, err = DirectionFromString("north")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, North, dir)
|
|
||||||
|
|
||||||
dir, err = DirectionFromString("NorthWest")
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, NorthWest, dir)
|
|
||||||
}
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
)
|
)
|
||||||
|
@ -12,13 +13,13 @@ import (
|
||||||
var dataPath = os.TempDir()
|
var dataPath = os.TempDir()
|
||||||
|
|
||||||
// SetPath sets the persistent path for the data storage
|
// SetPath sets the persistent path for the data storage
|
||||||
func SetPath(path string) error {
|
func SetPath(p string) error {
|
||||||
if info, err := os.Stat(path); err != nil {
|
if info, err := os.Stat(p); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !info.IsDir() {
|
} else if !info.IsDir() {
|
||||||
return fmt.Errorf("path for persistence is not directory")
|
return fmt.Errorf("path for persistence is not directory")
|
||||||
}
|
}
|
||||||
dataPath = path
|
dataPath = p
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,34 +30,41 @@ func jsonPath(name string) string {
|
||||||
|
|
||||||
// Save will serialise the interface into a json file
|
// Save will serialise the interface into a json file
|
||||||
func Save(name string, data interface{}) error {
|
func Save(name string, data interface{}) error {
|
||||||
if b, err := json.MarshalIndent(data, "", "\t"); err != nil {
|
p := jsonPath(name)
|
||||||
return err
|
b, err := json.MarshalIndent(data, "", " ")
|
||||||
} else {
|
if err != nil {
|
||||||
if err := ioutil.WriteFile(jsonPath(name), b, os.ModePerm); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := ioutil.WriteFile(p, b, os.ModePerm); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Saved %s\n", p)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load will load the interface from the json file
|
// Load will load the interface from the json file
|
||||||
func Load(name string, data interface{}) error {
|
func Load(name string, data interface{}) error {
|
||||||
path := jsonPath(name)
|
p := jsonPath(name)
|
||||||
// Don't load anything if the file doesn't exist
|
// Don't load anything if the file doesn't exist
|
||||||
_, err := os.Stat(path)
|
_, err := os.Stat(p)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
fmt.Printf("File %s didn't exist, loading with fresh data\n", path)
|
log.Printf("File %s didn't exist, loading with fresh data\n", p)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read and unmarshal the json
|
// Read and unmarshal the json
|
||||||
if b, err := ioutil.ReadFile(path); err != nil {
|
if b, err := ioutil.ReadFile(p); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if len(b) == 0 {
|
} else if len(b) == 0 {
|
||||||
fmt.Printf("File %s was empty, loading with fresh data\n", path)
|
log.Printf("File %s was empty, loading with fresh data\n", p)
|
||||||
|
return nil
|
||||||
} else if err := json.Unmarshal(b, data); err != nil {
|
} else if err := json.Unmarshal(b, data); err != nil {
|
||||||
return fmt.Errorf("failed to load file %s error: %s", path, err)
|
return fmt.Errorf("failed to load file %s error: %s", p, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("Loaded %s\n", p)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,7 +78,7 @@ func doAll(f saveLoadFunc, args ...interface{}) error {
|
||||||
var ok bool
|
var ok bool
|
||||||
name, ok = a.(string)
|
name, ok = a.(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Incorrect args")
|
return fmt.Errorf("incorrect args")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := f(name, a); err != nil {
|
if err := f(name, a); err != nil {
|
||||||
|
|
141
pkg/rove/api.go
141
pkg/rove/api.go
|
@ -1,141 +0,0 @@
|
||||||
package rove
|
|
||||||
|
|
||||||
import (
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/mdiluz/rove/pkg/game"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// API: /status method: GET
|
|
||||||
|
|
||||||
// Status queries the status of the server
|
|
||||||
func (s Server) Status() (r StatusResponse, err error) {
|
|
||||||
s.GET("status", &r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// StatusResponse is a struct that contains information on the status of the server
|
|
||||||
type StatusResponse struct {
|
|
||||||
Ready bool `json:"ready"`
|
|
||||||
Version string `json:"version"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// API: /register method: POST
|
|
||||||
|
|
||||||
// Register registers a user by name
|
|
||||||
// Responds with a unique ID for that user to be used in future requests
|
|
||||||
func (s Server) Register(d RegisterData) (r RegisterResponse, err error) {
|
|
||||||
err = s.POST("register", d, &r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterData describes the data to send when registering
|
|
||||||
type RegisterData struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterResponse describes the response to a register request
|
|
||||||
type RegisterResponse struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
Error string `json:"error,omitempty"`
|
|
||||||
|
|
||||||
Id string `json:"id"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// API: /{account}/spawn method: POST
|
|
||||||
|
|
||||||
// Spawn spawns the rover for an account
|
|
||||||
// Responds with the position of said rover
|
|
||||||
func (s Server) Spawn(account string, d SpawnData) (r SpawnResponse, err error) {
|
|
||||||
err = s.POST(path.Join(account, "spawn"), d, &r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// SpawnData is the data to be sent for the spawn command
|
|
||||||
type SpawnData struct {
|
|
||||||
// Empty for now, reserved for data
|
|
||||||
}
|
|
||||||
|
|
||||||
// SpawnResponse is the data to respond with on a spawn command
|
|
||||||
type SpawnResponse struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
Error string `json:"error,omitempty"`
|
|
||||||
|
|
||||||
// The location of the spawned entity
|
|
||||||
Position game.Vector `json:"position"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==============================
|
|
||||||
// API: /{account}/command method: POST
|
|
||||||
|
|
||||||
// Command issues a set of commands from the user
|
|
||||||
func (s Server) Command(account string, d CommandData) (r CommandResponse, err error) {
|
|
||||||
err = s.POST(path.Join(account, "command"), d, &r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommandData is a set of commands to execute in order
|
|
||||||
type CommandData struct {
|
|
||||||
Commands []Command `json:"commands"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommandResponse is the response to be sent back
|
|
||||||
type CommandResponse struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
Error string `json:"error,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
|
||||||
// CommandMove describes a single move command
|
|
||||||
CommandMove = "move"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Command describes a single command to execute
|
|
||||||
// it contains the type, and then any members used for each command type
|
|
||||||
type Command struct {
|
|
||||||
// Command is the main command string
|
|
||||||
Command string `json:"command"`
|
|
||||||
|
|
||||||
// Used for CommandMove
|
|
||||||
Bearing string `json:"bearing"` // The direction to move on a compass in short (NW) or long (NorthWest) form
|
|
||||||
Duration int `json:"duration"` // The duration of the move in ticks
|
|
||||||
}
|
|
||||||
|
|
||||||
// ================
|
|
||||||
// API: /{account}/radar method: GET
|
|
||||||
|
|
||||||
// Radar queries the current radar for the user
|
|
||||||
func (s Server) Radar(account string) (r RadarResponse, err error) {
|
|
||||||
err = s.GET(path.Join(account, "radar"), &r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// RadarResponse describes the response to a /radar call
|
|
||||||
type RadarResponse struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
Error string `json:"error,omitempty"`
|
|
||||||
|
|
||||||
// The set of positions for nearby rovers
|
|
||||||
Rovers []game.Vector `json:"rovers"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// ================
|
|
||||||
// API: /{account}/rover method: GET
|
|
||||||
|
|
||||||
// Rover queries the current state of the rover
|
|
||||||
func (s Server) Rover(account string) (r RoverResponse, err error) {
|
|
||||||
err = s.GET(path.Join(account, "rover"), &r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// RoverResponse includes information about the rover in question
|
|
||||||
type RoverResponse struct {
|
|
||||||
Success bool `json:"success"`
|
|
||||||
Error string `json:"error,omitempty"`
|
|
||||||
|
|
||||||
// The set of positions for nearby rovers
|
|
||||||
Position game.Vector `json:"position"`
|
|
||||||
}
|
|
18
pkg/rove/atlas.go
Normal file
18
pkg/rove/atlas.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Atlas represents a 2D world atlas of tiles and objects
|
||||||
|
type Atlas interface {
|
||||||
|
// SetTile sets a location on the Atlas to a type of tile
|
||||||
|
SetTile(v maths.Vector, tile roveapi.Tile)
|
||||||
|
|
||||||
|
// SetObject will set a location on the Atlas to contain an object
|
||||||
|
SetObject(v maths.Vector, obj Object)
|
||||||
|
|
||||||
|
// QueryPosition queries a position on the atlas
|
||||||
|
QueryPosition(v maths.Vector) (roveapi.Tile, Object)
|
||||||
|
}
|
257
pkg/rove/atlas_test.go
Normal file
257
pkg/rove/atlas_test.go
Normal file
|
@ -0,0 +1,257 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAtlas_NewAtlas(t *testing.T) {
|
||||||
|
a := NewChunkAtlas(1).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
assert.Equal(t, 1, a.ChunkSize)
|
||||||
|
assert.Equal(t, 1, len(a.Chunks)) // Should start empty
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtlas_toChunk(t *testing.T) {
|
||||||
|
a := NewChunkAtlas(1).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
|
||||||
|
// Get a tile to spawn the chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: -1, Y: -1})
|
||||||
|
a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
|
||||||
|
// Chunks should look like:
|
||||||
|
// 2 | 3
|
||||||
|
// -----
|
||||||
|
// 0 | 1
|
||||||
|
chunkID := a.worldSpaceToChunkIndex(maths.Vector{X: 0, Y: 0})
|
||||||
|
assert.Equal(t, 3, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 0, Y: -1})
|
||||||
|
assert.Equal(t, 1, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: -1})
|
||||||
|
assert.Equal(t, 0, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: 0})
|
||||||
|
assert.Equal(t, 2, chunkID)
|
||||||
|
|
||||||
|
a = NewChunkAtlas(2).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
// Get a tile to spawn the chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: -2, Y: -2})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
a.QueryPosition(maths.Vector{X: 1, Y: 1})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
// Chunks should look like:
|
||||||
|
// 2 | 3
|
||||||
|
// -----
|
||||||
|
// 0 | 1
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 1})
|
||||||
|
assert.Equal(t, 3, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: -2})
|
||||||
|
assert.Equal(t, 1, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: -2})
|
||||||
|
assert.Equal(t, 0, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: 1})
|
||||||
|
assert.Equal(t, 2, chunkID)
|
||||||
|
|
||||||
|
a = NewChunkAtlas(2).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
// Get a tile to spawn a 4x4 grid of chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: 3, Y: 3})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
a.QueryPosition(maths.Vector{X: -3, Y: -3})
|
||||||
|
assert.Equal(t, 4*4, len(a.Chunks))
|
||||||
|
|
||||||
|
// Chunks should look like:
|
||||||
|
// 12| 13|| 14| 15
|
||||||
|
// ----------------
|
||||||
|
// 8 | 9 || 10| 11
|
||||||
|
// ================
|
||||||
|
// 4 | 5 || 6 | 7
|
||||||
|
// ----------------
|
||||||
|
// 0 | 1 || 2 | 3
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 3})
|
||||||
|
assert.Equal(t, 14, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: -3})
|
||||||
|
assert.Equal(t, 2, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -1, Y: -1})
|
||||||
|
assert.Equal(t, 5, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: -2, Y: 2})
|
||||||
|
assert.Equal(t, 13, chunkID)
|
||||||
|
|
||||||
|
a = NewChunkAtlas(3).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
// Get a tile to spawn a 4x4 grid of chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: 3, Y: 3})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
|
||||||
|
// Chunks should look like:
|
||||||
|
// || 2| 3
|
||||||
|
// -------
|
||||||
|
// || 0| 1
|
||||||
|
// =======
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 1})
|
||||||
|
assert.Equal(t, 0, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 3, Y: 1})
|
||||||
|
assert.Equal(t, 1, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 1, Y: 4})
|
||||||
|
assert.Equal(t, 2, chunkID)
|
||||||
|
chunkID = a.worldSpaceToChunkIndex(maths.Vector{X: 5, Y: 5})
|
||||||
|
assert.Equal(t, 3, chunkID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtlas_toWorld(t *testing.T) {
|
||||||
|
a := NewChunkAtlas(1).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
|
||||||
|
// Get a tile to spawn some chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: -1, Y: -1})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
|
||||||
|
// Chunks should look like:
|
||||||
|
// 2 | 3
|
||||||
|
// -----
|
||||||
|
// 0 | 1
|
||||||
|
assert.Equal(t, maths.Vector{X: -1, Y: -1}, a.chunkOriginInWorldSpace(0))
|
||||||
|
assert.Equal(t, maths.Vector{X: 0, Y: -1}, a.chunkOriginInWorldSpace(1))
|
||||||
|
|
||||||
|
a = NewChunkAtlas(2).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
// Get a tile to spawn the chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: -2, Y: -2})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
a.QueryPosition(maths.Vector{X: 1, Y: 1})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
// Chunks should look like:
|
||||||
|
// 2 | 3
|
||||||
|
// -----
|
||||||
|
// 0 | 1
|
||||||
|
assert.Equal(t, maths.Vector{X: -2, Y: -2}, a.chunkOriginInWorldSpace(0))
|
||||||
|
assert.Equal(t, maths.Vector{X: -2, Y: 0}, a.chunkOriginInWorldSpace(2))
|
||||||
|
|
||||||
|
a = NewChunkAtlas(2).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
// Get a tile to spawn a 4x4 grid of chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: 3, Y: 3})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
a.QueryPosition(maths.Vector{X: -3, Y: -3})
|
||||||
|
assert.Equal(t, 4*4, len(a.Chunks))
|
||||||
|
|
||||||
|
// Chunks should look like:
|
||||||
|
// 12| 13|| 14| 15
|
||||||
|
// ----------------
|
||||||
|
// 8 | 9 || 10| 11
|
||||||
|
// ================
|
||||||
|
// 4 | 5 || 6 | 7
|
||||||
|
// ----------------
|
||||||
|
// 0 | 1 || 2 | 3
|
||||||
|
assert.Equal(t, maths.Vector{X: -4, Y: -4}, a.chunkOriginInWorldSpace(0))
|
||||||
|
assert.Equal(t, maths.Vector{X: 2, Y: -2}, a.chunkOriginInWorldSpace(7))
|
||||||
|
|
||||||
|
a = NewChunkAtlas(3).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
// Get a tile to spawn a 4x4 grid of chunks
|
||||||
|
a.QueryPosition(maths.Vector{X: 3, Y: 3})
|
||||||
|
assert.Equal(t, 2*2, len(a.Chunks))
|
||||||
|
|
||||||
|
// Chunks should look like:
|
||||||
|
// || 2| 3
|
||||||
|
// -------
|
||||||
|
// || 0| 1
|
||||||
|
// =======
|
||||||
|
assert.Equal(t, maths.Vector{X: 0, Y: 0}, a.chunkOriginInWorldSpace(0))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtlas_GetSetTile(t *testing.T) {
|
||||||
|
a := NewChunkAtlas(10)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
|
||||||
|
// Set the origin tile and test it
|
||||||
|
a.SetTile(maths.Vector{X: 0, Y: 0}, roveapi.Tile_Gravel)
|
||||||
|
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||||
|
assert.Equal(t, roveapi.Tile_Gravel, tile)
|
||||||
|
|
||||||
|
// Set another tile and test it
|
||||||
|
a.SetTile(maths.Vector{X: 5, Y: -2}, roveapi.Tile_Rock)
|
||||||
|
tile, _ = a.QueryPosition(maths.Vector{X: 5, Y: -2})
|
||||||
|
assert.Equal(t, roveapi.Tile_Rock, tile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtlas_GetSetObject(t *testing.T) {
|
||||||
|
a := NewChunkAtlas(10)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
|
||||||
|
// Set the origin tile to 1 and test it
|
||||||
|
a.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_RockLarge})
|
||||||
|
_, obj := a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||||
|
assert.Equal(t, Object{Type: roveapi.Object_RockLarge}, obj)
|
||||||
|
|
||||||
|
// Set another tile to 1 and test it
|
||||||
|
a.SetObject(maths.Vector{X: 5, Y: -2}, Object{Type: roveapi.Object_RockSmall})
|
||||||
|
_, obj = a.QueryPosition(maths.Vector{X: 5, Y: -2})
|
||||||
|
assert.Equal(t, Object{Type: roveapi.Object_RockSmall}, obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtlas_Grown(t *testing.T) {
|
||||||
|
// Start with a small example
|
||||||
|
a := NewChunkAtlas(2).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
assert.Equal(t, 1, len(a.Chunks))
|
||||||
|
|
||||||
|
// Set a few tiles to values
|
||||||
|
a.SetTile(maths.Vector{X: 0, Y: 0}, roveapi.Tile_Gravel)
|
||||||
|
a.SetTile(maths.Vector{X: -1, Y: -1}, roveapi.Tile_Rock)
|
||||||
|
a.SetTile(maths.Vector{X: 1, Y: -2}, roveapi.Tile_Sand)
|
||||||
|
|
||||||
|
// Check tile values
|
||||||
|
tile, _ := a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||||
|
assert.Equal(t, roveapi.Tile_Gravel, tile)
|
||||||
|
|
||||||
|
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
|
||||||
|
assert.Equal(t, roveapi.Tile_Rock, tile)
|
||||||
|
|
||||||
|
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
|
||||||
|
assert.Equal(t, roveapi.Tile_Sand, tile)
|
||||||
|
|
||||||
|
tile, _ = a.QueryPosition(maths.Vector{X: 0, Y: 0})
|
||||||
|
assert.Equal(t, roveapi.Tile_Gravel, tile)
|
||||||
|
|
||||||
|
tile, _ = a.QueryPosition(maths.Vector{X: -1, Y: -1})
|
||||||
|
assert.Equal(t, roveapi.Tile_Rock, tile)
|
||||||
|
|
||||||
|
tile, _ = a.QueryPosition(maths.Vector{X: 1, Y: -2})
|
||||||
|
assert.Equal(t, roveapi.Tile_Sand, tile)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtlas_GetSetCorrect(t *testing.T) {
|
||||||
|
// Big stress test to ensure we do actually properly expand for all reasonable values
|
||||||
|
for i := 1; i <= 4; i++ {
|
||||||
|
|
||||||
|
for x := -i * 2; x < i*2; x++ {
|
||||||
|
for y := -i * 2; y < i*2; y++ {
|
||||||
|
a := NewChunkAtlas(i).(*chunkBasedAtlas)
|
||||||
|
assert.NotNil(t, a)
|
||||||
|
assert.Equal(t, 1, len(a.Chunks))
|
||||||
|
|
||||||
|
pos := maths.Vector{X: x, Y: y}
|
||||||
|
a.SetTile(pos, roveapi.Tile_Rock)
|
||||||
|
a.SetObject(pos, Object{Type: roveapi.Object_RockLarge})
|
||||||
|
tile, obj := a.QueryPosition(pos)
|
||||||
|
|
||||||
|
assert.Equal(t, roveapi.Tile_Rock, roveapi.Tile(tile))
|
||||||
|
assert.Equal(t, Object{Type: roveapi.Object_RockLarge}, obj)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAtlas_WorldGen(t *testing.T) {
|
||||||
|
a := NewChunkAtlas(8)
|
||||||
|
|
||||||
|
// Spawn a large world
|
||||||
|
_, _ = a.QueryPosition(maths.Vector{X: 20, Y: 20})
|
||||||
|
}
|
231
pkg/rove/chunkAtlas.go
Normal file
231
pkg/rove/chunkAtlas.go
Normal file
|
@ -0,0 +1,231 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// chunk represents a fixed square grid of tiles
|
||||||
|
type chunk struct {
|
||||||
|
// Tiles represents the tiles within the chunk
|
||||||
|
Tiles []byte
|
||||||
|
|
||||||
|
// Objects represents the objects within the chunk
|
||||||
|
// only one possible object per tile for now
|
||||||
|
Objects map[int]Object
|
||||||
|
}
|
||||||
|
|
||||||
|
// chunkBasedAtlas represents a grid of Chunks
|
||||||
|
type chunkBasedAtlas struct {
|
||||||
|
// Chunks represents all chunks in the world
|
||||||
|
// This is intentionally not a 2D array so it can be expanded in all directions
|
||||||
|
Chunks []chunk
|
||||||
|
|
||||||
|
// LowerBound is the origin of the bottom left corner of the current chunks in world space (current chunks cover >= this value)
|
||||||
|
LowerBound maths.Vector
|
||||||
|
|
||||||
|
// UpperBound is the top left corner of the current chunks (curent chunks cover < this value)
|
||||||
|
UpperBound maths.Vector
|
||||||
|
|
||||||
|
// ChunkSize is the x/y dimensions of each square chunk
|
||||||
|
ChunkSize int
|
||||||
|
|
||||||
|
// worldGen is the internal world generator
|
||||||
|
worldGen WorldGen
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
noiseSeed = 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewChunkAtlas creates a new empty atlas
|
||||||
|
func NewChunkAtlas(chunkSize int) Atlas {
|
||||||
|
// Start up with one chunk
|
||||||
|
a := chunkBasedAtlas{
|
||||||
|
ChunkSize: chunkSize,
|
||||||
|
Chunks: make([]chunk, 1),
|
||||||
|
LowerBound: maths.Vector{X: 0, Y: 0},
|
||||||
|
UpperBound: maths.Vector{X: chunkSize, Y: chunkSize},
|
||||||
|
worldGen: NewNoiseWorldGen(noiseSeed),
|
||||||
|
}
|
||||||
|
// Initialise the first chunk
|
||||||
|
a.populate(0)
|
||||||
|
return &a
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetTile sets an individual tile's kind
|
||||||
|
func (a *chunkBasedAtlas) SetTile(v maths.Vector, tile roveapi.Tile) {
|
||||||
|
c := a.worldSpaceToChunkWithGrow(v)
|
||||||
|
local := a.worldSpaceToChunkLocal(v)
|
||||||
|
a.setTile(c, local, byte(tile))
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetObject sets the object on a tile
|
||||||
|
func (a *chunkBasedAtlas) SetObject(v maths.Vector, obj Object) {
|
||||||
|
c := a.worldSpaceToChunkWithGrow(v)
|
||||||
|
local := a.worldSpaceToChunkLocal(v)
|
||||||
|
a.setObject(c, local, obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryPosition will return information for a specific position
|
||||||
|
func (a *chunkBasedAtlas) QueryPosition(v maths.Vector) (roveapi.Tile, Object) {
|
||||||
|
c := a.worldSpaceToChunkWithGrow(v)
|
||||||
|
local := a.worldSpaceToChunkLocal(v)
|
||||||
|
a.populate(c)
|
||||||
|
chunk := a.Chunks[c]
|
||||||
|
i := a.chunkTileIndex(local)
|
||||||
|
return roveapi.Tile(chunk.Tiles[i]), chunk.Objects[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// chunkTileID returns the tile index within a chunk
|
||||||
|
func (a *chunkBasedAtlas) chunkTileIndex(local maths.Vector) int {
|
||||||
|
return local.X + local.Y*a.ChunkSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// populate will fill a chunk with data
|
||||||
|
func (a *chunkBasedAtlas) populate(chunk int) {
|
||||||
|
c := a.Chunks[chunk]
|
||||||
|
if c.Tiles != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Tiles = make([]byte, a.ChunkSize*a.ChunkSize)
|
||||||
|
c.Objects = make(map[int]Object)
|
||||||
|
|
||||||
|
origin := a.chunkOriginInWorldSpace(chunk)
|
||||||
|
for i := 0; i < a.ChunkSize; i++ {
|
||||||
|
for j := 0; j < a.ChunkSize; j++ {
|
||||||
|
loc := maths.Vector{X: origin.X + i, Y: origin.Y + j}
|
||||||
|
|
||||||
|
// Set the tile
|
||||||
|
c.Tiles[j*a.ChunkSize+i] = byte(a.worldGen.GetTile(loc))
|
||||||
|
|
||||||
|
// Set the object
|
||||||
|
obj := a.worldGen.GetObject(loc)
|
||||||
|
if obj.Type != roveapi.Object_ObjectUnknown {
|
||||||
|
c.Objects[j*a.ChunkSize+i] = obj
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a.Chunks[chunk] = c
|
||||||
|
}
|
||||||
|
|
||||||
|
// setTile sets a tile in a specific chunk
|
||||||
|
func (a *chunkBasedAtlas) setTile(chunk int, local maths.Vector, tile byte) {
|
||||||
|
a.populate(chunk)
|
||||||
|
c := a.Chunks[chunk]
|
||||||
|
c.Tiles[a.chunkTileIndex(local)] = tile
|
||||||
|
a.Chunks[chunk] = c
|
||||||
|
}
|
||||||
|
|
||||||
|
// setObject sets an object in a specific chunk
|
||||||
|
func (a *chunkBasedAtlas) setObject(chunk int, local maths.Vector, object Object) {
|
||||||
|
a.populate(chunk)
|
||||||
|
|
||||||
|
c := a.Chunks[chunk]
|
||||||
|
i := a.chunkTileIndex(local)
|
||||||
|
if object.Type != roveapi.Object_ObjectUnknown {
|
||||||
|
c.Objects[i] = object
|
||||||
|
} else {
|
||||||
|
delete(c.Objects, i)
|
||||||
|
}
|
||||||
|
a.Chunks[chunk] = c
|
||||||
|
}
|
||||||
|
|
||||||
|
// worldSpaceToChunkLocal gets a chunk local coordinate for a tile
|
||||||
|
func (a *chunkBasedAtlas) worldSpaceToChunkLocal(v maths.Vector) maths.Vector {
|
||||||
|
return maths.Vector{X: maths.Pmod(v.X, a.ChunkSize), Y: maths.Pmod(v.Y, a.ChunkSize)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// worldSpaceToChunkID gets the current chunk ID for a position in the world
|
||||||
|
func (a *chunkBasedAtlas) worldSpaceToChunkIndex(v maths.Vector) int {
|
||||||
|
// Shift the vector by our current min
|
||||||
|
v = v.Added(a.LowerBound.Negated())
|
||||||
|
|
||||||
|
// Divide by the current size and floor, to get chunk-scaled vector from the lower bound
|
||||||
|
v = v.DividedFloor(a.ChunkSize)
|
||||||
|
|
||||||
|
// Calculate the width
|
||||||
|
width := a.UpperBound.X - a.LowerBound.X
|
||||||
|
widthInChunks := width / a.ChunkSize
|
||||||
|
|
||||||
|
// Along the corridor and up the stairs
|
||||||
|
return (v.Y * widthInChunks) + v.X
|
||||||
|
}
|
||||||
|
|
||||||
|
// chunkOriginInWorldSpace returns the origin of the chunk in world space
|
||||||
|
func (a *chunkBasedAtlas) chunkOriginInWorldSpace(chunk int) maths.Vector {
|
||||||
|
// Calculate the width
|
||||||
|
width := a.UpperBound.X - a.LowerBound.X
|
||||||
|
widthInChunks := width / a.ChunkSize
|
||||||
|
|
||||||
|
// Reverse the along the corridor and up the stairs
|
||||||
|
v := maths.Vector{
|
||||||
|
X: chunk % widthInChunks,
|
||||||
|
Y: chunk / widthInChunks,
|
||||||
|
}
|
||||||
|
// Multiply up to world scale
|
||||||
|
v = v.Multiplied(a.ChunkSize)
|
||||||
|
// Shift by the lower bound
|
||||||
|
return v.Added(a.LowerBound)
|
||||||
|
}
|
||||||
|
|
||||||
|
// getNewBounds gets new lower and upper bounds for the world space given a vector
|
||||||
|
func (a *chunkBasedAtlas) getNewBounds(v maths.Vector) (lower maths.Vector, upper maths.Vector) {
|
||||||
|
lower = maths.Min2(v, a.LowerBound)
|
||||||
|
upper = maths.Max2(v.Added(maths.Vector{X: 1, Y: 1}), a.UpperBound)
|
||||||
|
|
||||||
|
lower = maths.Vector{
|
||||||
|
X: maths.RoundDown(lower.X, a.ChunkSize),
|
||||||
|
Y: maths.RoundDown(lower.Y, a.ChunkSize),
|
||||||
|
}
|
||||||
|
upper = maths.Vector{
|
||||||
|
X: maths.RoundUp(upper.X, a.ChunkSize),
|
||||||
|
Y: maths.RoundUp(upper.Y, a.ChunkSize),
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// worldSpaceToTrunkWithGrow will expand the current atlas for a given world space position if needed
|
||||||
|
func (a *chunkBasedAtlas) worldSpaceToChunkWithGrow(v maths.Vector) int {
|
||||||
|
// If we're within bounds, just return the current chunk
|
||||||
|
if v.X >= a.LowerBound.X && v.Y >= a.LowerBound.Y && v.X < a.UpperBound.X && v.Y < a.UpperBound.Y {
|
||||||
|
return a.worldSpaceToChunkIndex(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the new bounds
|
||||||
|
lower, upper := a.getNewBounds(v)
|
||||||
|
size := upper.Added(lower.Negated())
|
||||||
|
size = size.Divided(a.ChunkSize)
|
||||||
|
|
||||||
|
// Create the new empty atlas
|
||||||
|
newAtlas := chunkBasedAtlas{
|
||||||
|
ChunkSize: a.ChunkSize,
|
||||||
|
LowerBound: lower,
|
||||||
|
UpperBound: upper,
|
||||||
|
Chunks: make([]chunk, size.X*size.Y),
|
||||||
|
worldGen: a.worldGen,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log that we're resizing
|
||||||
|
log.Printf("Re-allocating world, old: %+v,%+v new: %+v,%+v\n", a.LowerBound, a.UpperBound, newAtlas.LowerBound, newAtlas.UpperBound)
|
||||||
|
|
||||||
|
// Copy all old chunks into the new atlas
|
||||||
|
for chunk, chunkData := range a.Chunks {
|
||||||
|
|
||||||
|
// Calculate the chunk ID in the new atlas
|
||||||
|
origin := a.chunkOriginInWorldSpace(chunk)
|
||||||
|
newChunk := newAtlas.worldSpaceToChunkIndex(origin)
|
||||||
|
|
||||||
|
// Copy over the old chunk to the new atlas
|
||||||
|
newAtlas.Chunks[newChunk] = chunkData
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overwrite the old atlas with this one
|
||||||
|
*a = newAtlas
|
||||||
|
|
||||||
|
return a.worldSpaceToChunkIndex(v)
|
||||||
|
}
|
305
pkg/rove/command_test.go
Normal file
305
pkg/rove/command_test.go
Normal file
|
@ -0,0 +1,305 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCommand_Invalid(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
name, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_none})
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Toggle(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
a, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
r, err := w.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.SailPosition_SolarCharging, r.SailPosition)
|
||||||
|
|
||||||
|
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_toggle})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
r, err = w.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.SailPosition_CatchingWind, r.SailPosition)
|
||||||
|
|
||||||
|
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_toggle})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
r, err = w.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.SailPosition_SolarCharging, r.SailPosition)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Turn(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
a, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_turn, Bearing: roveapi.Bearing_NorthWest})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
r, err := w.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.Bearing_NorthWest, r.Bearing)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Stash(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
name, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
info, err := w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Empty(t, info.Inventory)
|
||||||
|
|
||||||
|
// Drop a pickup below us
|
||||||
|
w.Atlas.SetObject(info.Pos, Object{Type: roveapi.Object_RockSmall})
|
||||||
|
|
||||||
|
// Try and stash it
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_stash})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
// Check we now have it in the inventory
|
||||||
|
info, err = w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, len(info.Inventory))
|
||||||
|
assert.Equal(t, Object{Type: roveapi.Object_RockSmall}, info.Inventory[0])
|
||||||
|
|
||||||
|
// Check it's no longer on the atlas
|
||||||
|
_, obj := w.Atlas.QueryPosition(info.Pos)
|
||||||
|
assert.Equal(t, Object{Type: roveapi.Object_ObjectUnknown}, obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Repair(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
name, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
info, err := w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, info.MaximumIntegrity, info.Integrity)
|
||||||
|
|
||||||
|
// Put a blocking rock to the north
|
||||||
|
w.Atlas.SetObject(info.Pos.Added(maths.Vector{X: 0, Y: 1}), Object{Type: roveapi.Object_RockLarge})
|
||||||
|
|
||||||
|
// Try and move and make sure we're blocked
|
||||||
|
newpos, err := w.TryMoveRover(name, roveapi.Bearing_North)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, info.Pos, newpos)
|
||||||
|
|
||||||
|
// Check we're damaged
|
||||||
|
info, err = w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, info.MaximumIntegrity-1, info.Integrity)
|
||||||
|
|
||||||
|
// Stash a repair object
|
||||||
|
w.Atlas.SetObject(info.Pos, Object{Type: roveapi.Object_RoverParts})
|
||||||
|
obj, err := w.RoverStash(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.Object_RoverParts, obj)
|
||||||
|
|
||||||
|
// Enqueue the repair and tick
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_repair})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
// Check we're repaired
|
||||||
|
info, err = w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, info.MaximumIntegrity, info.Integrity)
|
||||||
|
assert.Equal(t, 0, len(info.Inventory))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Broadcast(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
name, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Enqueue the broadcast and tick
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_broadcast, Data: []byte("ABC")})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
info, err := w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Contains(t, info.Logs[len(info.Logs)-1].Text, "ABC")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Salvage(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
name, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
info, err := w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
w.Atlas.SetObject(info.Pos, Object{Type: roveapi.Object_RoverDormant})
|
||||||
|
|
||||||
|
// Enqueue the broadcast and tick
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_salvage})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
// Check we now have some rover parts
|
||||||
|
info, err = w.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotEmpty(t, info.Inventory)
|
||||||
|
for _, i := range info.Inventory {
|
||||||
|
assert.Equal(t, roveapi.Object_RoverParts, i.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the dormant rover is gone
|
||||||
|
_, obj := w.Atlas.QueryPosition(info.Pos)
|
||||||
|
assert.Equal(t, roveapi.Object_ObjectUnknown, obj.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Transfer(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
acc, err := w.Accountant.RegisterAccount("tmp")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
nameA, err := w.SpawnRover(acc.Name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
infoA, err := w.GetRover(nameA)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Drop a dormant rover on the current position
|
||||||
|
infoB := DefaultRover()
|
||||||
|
infoB.Name = "abc"
|
||||||
|
infoB.Pos = infoA.Pos
|
||||||
|
data, err := json.Marshal(infoB)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Atlas.SetObject(infoA.Pos, Object{Type: roveapi.Object_RoverDormant, Data: data})
|
||||||
|
|
||||||
|
// Enqueue a transfer as well as a dud command
|
||||||
|
err = w.Enqueue(nameA,
|
||||||
|
&roveapi.Command{Command: roveapi.CommandType_transfer},
|
||||||
|
&roveapi.Command{Command: roveapi.CommandType_broadcast, Data: []byte("xyz")})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
// Ensure both command queues are empty
|
||||||
|
assert.Empty(t, w.CommandQueue[nameA])
|
||||||
|
assert.Empty(t, w.CommandQueue[infoB.Name])
|
||||||
|
|
||||||
|
// Verify the account now controls the new rover
|
||||||
|
accountRover, err := w.Accountant.GetValue(acc.Name, "rover")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, infoB.Name, accountRover)
|
||||||
|
|
||||||
|
// Verify the position now has a dormant rover
|
||||||
|
_, obj := w.Atlas.QueryPosition(infoA.Pos)
|
||||||
|
assert.Equal(t, roveapi.Object_RoverDormant, obj.Type)
|
||||||
|
|
||||||
|
// Verify the stored data matches
|
||||||
|
var stored Rover
|
||||||
|
err = json.Unmarshal(obj.Data, &stored)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, infoA.Name, stored.Name)
|
||||||
|
|
||||||
|
// Verify the new rover data matches what we put in
|
||||||
|
infoB2, err := w.GetRover(infoB.Name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, infoB.Name, infoB2.Name)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Wait(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
a, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
r, err := w.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.SailPosition_SolarCharging, r.SailPosition)
|
||||||
|
|
||||||
|
err = w.Enqueue(a, &roveapi.Command{Command: roveapi.CommandType_wait, Repeat: 4}, &roveapi.Command{Command: roveapi.CommandType_toggle})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Tick 5 times during the wait (1 normal execute + 4)
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
r, err = w.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.SailPosition_SolarCharging, r.SailPosition)
|
||||||
|
}
|
||||||
|
|
||||||
|
// One last tick to do the toggle
|
||||||
|
w.Tick()
|
||||||
|
|
||||||
|
r, err = w.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.SailPosition_CatchingWind, r.SailPosition)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCommand_Upgrade(t *testing.T) {
|
||||||
|
w := NewWorld(8)
|
||||||
|
name, err := w.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
rover, ok := w.Rovers[name]
|
||||||
|
assert.True(t, ok)
|
||||||
|
|
||||||
|
// Try an invalid upgrade
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_upgrade})
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
// Try a valid command but without the parts
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_upgrade, Upgrade: roveapi.RoverUpgrade_Capacity})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Ensure nothing changed and we logged the attempt
|
||||||
|
pre := rover.Capacity
|
||||||
|
w.Tick()
|
||||||
|
assert.Equal(t, pre, rover.Capacity)
|
||||||
|
assert.Contains(t, rover.Logs[len(rover.Logs)-1].Text, "tried")
|
||||||
|
|
||||||
|
// One non-part item
|
||||||
|
rover.Inventory = []Object{
|
||||||
|
{
|
||||||
|
Type: roveapi.Object_RoverParts,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: roveapi.Object_RoverParts,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: roveapi.Object_RockSmall,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: roveapi.Object_RoverParts,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: roveapi.Object_RoverParts,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: roveapi.Object_RoverParts,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try a valid command again
|
||||||
|
err = w.Enqueue(name, &roveapi.Command{Command: roveapi.CommandType_upgrade, Upgrade: roveapi.RoverUpgrade_Capacity})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Check that the capacity increases on the tick and all the parts are used
|
||||||
|
pre = rover.Capacity
|
||||||
|
w.Tick()
|
||||||
|
assert.Equal(t, pre+1, rover.Capacity)
|
||||||
|
assert.Equal(t, 1, len(rover.Inventory))
|
||||||
|
assert.Equal(t, roveapi.Object_RockSmall, rover.Inventory[0].Type)
|
||||||
|
}
|
|
@ -1,64 +0,0 @@
|
||||||
package rove
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Server is a simple wrapper to a server path
|
|
||||||
type Server string
|
|
||||||
|
|
||||||
// GET performs a GET request
|
|
||||||
func (s Server) GET(path string, out interface{}) error {
|
|
||||||
url := url.URL{
|
|
||||||
Scheme: "http",
|
|
||||||
Host: string(s),
|
|
||||||
Path: path,
|
|
||||||
}
|
|
||||||
if resp, err := http.Get(url.String()); err != nil {
|
|
||||||
return err
|
|
||||||
|
|
||||||
} else if resp.StatusCode != http.StatusOK {
|
|
||||||
return fmt.Errorf("http.Get returned status %d: %s", resp.StatusCode, resp.Status)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return json.NewDecoder(resp.Body).Decode(out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// POST performs a POST request
|
|
||||||
func (s Server) POST(path string, in interface{}, out interface{}) error {
|
|
||||||
url := url.URL{
|
|
||||||
Scheme: "http",
|
|
||||||
Host: string(s),
|
|
||||||
Path: path,
|
|
||||||
}
|
|
||||||
client := &http.Client{}
|
|
||||||
|
|
||||||
// Marshal the input
|
|
||||||
marshalled, err := json.Marshal(in)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the request
|
|
||||||
req, err := http.NewRequest("POST", url.String(), bytes.NewReader(marshalled))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do the POST
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
if resp, err := client.Do(req); err != nil {
|
|
||||||
return err
|
|
||||||
|
|
||||||
} else if resp.StatusCode != http.StatusOK {
|
|
||||||
return fmt.Errorf("http returned status %d", resp.StatusCode)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return json.NewDecoder(resp.Body).Decode(out)
|
|
||||||
}
|
|
||||||
}
|
|
44
pkg/rove/objects.go
Normal file
44
pkg/rove/objects.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Object represents an object in the world
|
||||||
|
type Object struct {
|
||||||
|
// The type of the object
|
||||||
|
Type roveapi.Object
|
||||||
|
|
||||||
|
// Data is an internal type used for certain types of object
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsBlocking checks if an object is a blocking object
|
||||||
|
func (o *Object) IsBlocking() bool {
|
||||||
|
var blocking = [...]roveapi.Object{
|
||||||
|
roveapi.Object_RoverLive,
|
||||||
|
roveapi.Object_RockLarge,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, t := range blocking {
|
||||||
|
if o.Type == t {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsStashable checks if an object is stashable
|
||||||
|
func (o *Object) IsStashable() bool {
|
||||||
|
var stashable = [...]roveapi.Object{
|
||||||
|
roveapi.Object_RockSmall,
|
||||||
|
roveapi.Object_RoverParts,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, t := range stashable {
|
||||||
|
if o.Type == t {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
137
pkg/rove/rover.go
Normal file
137
pkg/rove/rover.go
Normal file
|
@ -0,0 +1,137 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxLogEntries = 16
|
||||||
|
)
|
||||||
|
|
||||||
|
// RoverLogEntry describes a single log entry for the rover
|
||||||
|
type RoverLogEntry struct {
|
||||||
|
// Time is the timestamp of the entry
|
||||||
|
Time time.Time
|
||||||
|
|
||||||
|
// Text contains the information in this log entry
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rover describes a single rover in the world
|
||||||
|
type Rover struct {
|
||||||
|
// Unique name of this rover
|
||||||
|
Name string
|
||||||
|
|
||||||
|
// Pos represents where this rover is in the world
|
||||||
|
Pos maths.Vector
|
||||||
|
|
||||||
|
// Bearing is the current direction the rover is facing
|
||||||
|
Bearing roveapi.Bearing
|
||||||
|
|
||||||
|
// Range represents the distance the unit's radar can see
|
||||||
|
Range int
|
||||||
|
|
||||||
|
// Inventory represents any items the rover is carrying
|
||||||
|
Inventory []Object
|
||||||
|
|
||||||
|
// Capacity is the maximum number of inventory items
|
||||||
|
Capacity int
|
||||||
|
|
||||||
|
// Integrity represents current rover health
|
||||||
|
Integrity int
|
||||||
|
|
||||||
|
// MaximumIntegrity is the full integrity of the rover
|
||||||
|
MaximumIntegrity int
|
||||||
|
|
||||||
|
// Charge is the amount of energy the rover has
|
||||||
|
Charge int
|
||||||
|
|
||||||
|
// MaximumCharge is the maximum charge able to be stored
|
||||||
|
MaximumCharge int
|
||||||
|
|
||||||
|
// SailPosition is the current position of the sails
|
||||||
|
SailPosition roveapi.SailPosition
|
||||||
|
|
||||||
|
// Current number of ticks in this move, used for sailing speeds
|
||||||
|
MoveTicks int
|
||||||
|
|
||||||
|
// Logs Stores log of information
|
||||||
|
Logs []RoverLogEntry
|
||||||
|
|
||||||
|
// The account that owns this rover
|
||||||
|
Owner string
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultRover returns a default rover object with default settings
|
||||||
|
func DefaultRover() *Rover {
|
||||||
|
return &Rover{
|
||||||
|
Range: 10,
|
||||||
|
Integrity: 10,
|
||||||
|
MaximumIntegrity: 10,
|
||||||
|
Capacity: 10,
|
||||||
|
Charge: 10,
|
||||||
|
MaximumCharge: 10,
|
||||||
|
Bearing: roveapi.Bearing_North,
|
||||||
|
SailPosition: roveapi.SailPosition_SolarCharging,
|
||||||
|
Name: GenerateRoverName(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddLogEntryf adds an entry to the rovers log
|
||||||
|
func (r *Rover) AddLogEntryf(format string, args ...interface{}) {
|
||||||
|
text := fmt.Sprintf(format, args...)
|
||||||
|
log.Printf("%s log entry: %s", r.Name, text)
|
||||||
|
r.Logs = append(r.Logs,
|
||||||
|
RoverLogEntry{
|
||||||
|
Time: time.Now(),
|
||||||
|
Text: text,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
// Limit the number of logs
|
||||||
|
if len(r.Logs) > maxLogEntries {
|
||||||
|
r.Logs = r.Logs[len(r.Logs)-maxLogEntries:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var wordsFile = os.Getenv("WORDS_FILE")
|
||||||
|
var roverWords []string
|
||||||
|
|
||||||
|
// GenerateRoverName generates a new rover name
|
||||||
|
func GenerateRoverName() string {
|
||||||
|
|
||||||
|
// Try and load the rover words file
|
||||||
|
if len(roverWords) == 0 {
|
||||||
|
// Try and load the words file
|
||||||
|
if file, err := os.Open(wordsFile); err != nil {
|
||||||
|
log.Printf("Couldn't read words file [%s], running without words: %s\n", wordsFile, err)
|
||||||
|
} else {
|
||||||
|
defer file.Close()
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
roverWords = append(roverWords, scanner.Text())
|
||||||
|
}
|
||||||
|
if scanner.Err() != nil {
|
||||||
|
log.Printf("Failure during word file scan: %s\n", scanner.Err())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign a random name if we have words
|
||||||
|
if len(roverWords) > 0 {
|
||||||
|
// Loop until we find a unique name
|
||||||
|
return fmt.Sprintf("%s-%s", roverWords[rand.Intn(len(roverWords))], roverWords[rand.Intn(len(roverWords))])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default to a unique string
|
||||||
|
return uuid.New().String()
|
||||||
|
}
|
840
pkg/rove/world.go
Normal file
840
pkg/rove/world.go
Normal file
|
@ -0,0 +1,840 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math/rand"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/accounts"
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ticksPerNormalMove defines the number of ticks it should take for a "normal" speed move
|
||||||
|
ticksPerNormalMove = 4
|
||||||
|
|
||||||
|
// upgradeCost is the cost in rover parts needed to upgrade a rover specification
|
||||||
|
upgradeCost = 5
|
||||||
|
)
|
||||||
|
|
||||||
|
// CommandStream is a list of commands to execute in order
|
||||||
|
type CommandStream []*roveapi.Command
|
||||||
|
|
||||||
|
// World describes a self contained universe and everything in it
|
||||||
|
type World struct {
|
||||||
|
|
||||||
|
// TicksPerDay is the amount of ticks in a single day
|
||||||
|
TicksPerDay int
|
||||||
|
|
||||||
|
// Current number of ticks from the start
|
||||||
|
CurrentTicks int
|
||||||
|
|
||||||
|
// Rovers is a id->data map of all the rovers in the game
|
||||||
|
Rovers map[string]*Rover
|
||||||
|
|
||||||
|
// Atlas represends the world map of chunks and tiles
|
||||||
|
Atlas Atlas
|
||||||
|
|
||||||
|
// Wind is the current wind direction
|
||||||
|
Wind roveapi.Bearing
|
||||||
|
|
||||||
|
// Commands is the set of currently executing command streams per rover
|
||||||
|
CommandQueue map[string]CommandStream
|
||||||
|
|
||||||
|
// Accountant
|
||||||
|
Accountant accounts.Accountant
|
||||||
|
|
||||||
|
// Mutex to lock around all world operations
|
||||||
|
worldMutex sync.RWMutex
|
||||||
|
// Mutex to lock around command operations
|
||||||
|
cmdMutex sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWorld creates a new world object
|
||||||
|
func NewWorld(chunkSize int) *World {
|
||||||
|
return &World{
|
||||||
|
Rovers: make(map[string]*Rover),
|
||||||
|
CommandQueue: make(map[string]CommandStream),
|
||||||
|
Atlas: NewChunkAtlas(chunkSize),
|
||||||
|
TicksPerDay: 24,
|
||||||
|
CurrentTicks: 0,
|
||||||
|
Accountant: accounts.NewSimpleAccountant(),
|
||||||
|
Wind: roveapi.Bearing_North,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SpawnRover adds an rover to the game (without lock)
|
||||||
|
func (w *World) SpawnRover(account string) (string, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
// Initialise the rover
|
||||||
|
rover := DefaultRover()
|
||||||
|
|
||||||
|
// Assign the owner
|
||||||
|
rover.Owner = account
|
||||||
|
|
||||||
|
// Spawn in a random place near the origin
|
||||||
|
rover.Pos = maths.Vector{
|
||||||
|
X: 10 - rand.Intn(20),
|
||||||
|
Y: 10 - rand.Intn(20),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seach until we error (run out of world)
|
||||||
|
for {
|
||||||
|
_, obj := w.Atlas.QueryPosition(rover.Pos)
|
||||||
|
if !obj.IsBlocking() {
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
// Try and spawn to the east of the blockage
|
||||||
|
rover.Pos.Add(maths.Vector{X: 1, Y: 0})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a log entry for robot creation
|
||||||
|
rover.AddLogEntryf("created at %+v", rover.Pos)
|
||||||
|
|
||||||
|
// Append the rover to the list
|
||||||
|
w.Rovers[rover.Name] = rover
|
||||||
|
|
||||||
|
var err error
|
||||||
|
// Only assign if we've been given an account
|
||||||
|
if len(account) > 0 {
|
||||||
|
err = w.Accountant.AssignData(account, "rover", rover.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rover.Name, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRover gets a specific rover by name
|
||||||
|
func (w *World) GetRover(rover string) (Rover, error) {
|
||||||
|
w.worldMutex.RLock()
|
||||||
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return Rover{}, fmt.Errorf("Failed to find rover with name: %s", rover)
|
||||||
|
}
|
||||||
|
return *i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverRecharge charges up a rover
|
||||||
|
func (w *World) RoverRecharge(rover string) (int, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return 0, fmt.Errorf("Failed to find rover with name: %s", rover)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We can only recharge during the day
|
||||||
|
if !w.Daytime() {
|
||||||
|
return i.Charge, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add one charge
|
||||||
|
if i.Charge < i.MaximumCharge {
|
||||||
|
i.Charge++
|
||||||
|
i.AddLogEntryf("recharged to %d", i.Charge)
|
||||||
|
}
|
||||||
|
|
||||||
|
return i.Charge, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverBroadcast broadcasts a message to nearby rovers
|
||||||
|
func (w *World) RoverBroadcast(rover string, message []byte) (err error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("Failed to find rover with name: %s", rover)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use up a charge as needed, if available
|
||||||
|
if i.Charge == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
i.Charge--
|
||||||
|
|
||||||
|
// Check all rovers
|
||||||
|
for r, rover := range w.Rovers {
|
||||||
|
if rover.Name == i.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if this rover is within range
|
||||||
|
if i.Pos.Distance(rover.Pos) < float64(i.Range) {
|
||||||
|
rover.AddLogEntryf("recieved %s from %s", string(message), i.Name)
|
||||||
|
w.Rovers[r] = rover
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
i.AddLogEntryf("broadcasted %s", string(message))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// DestroyRover Removes an rover from the game
|
||||||
|
func (w *World) DestroyRover(rover string) error {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove this rover from tracked rovers
|
||||||
|
delete(w.Rovers, rover)
|
||||||
|
|
||||||
|
r.Owner = ""
|
||||||
|
r.AddLogEntryf("rover destroyed")
|
||||||
|
|
||||||
|
// Marshal the rover data
|
||||||
|
data, err := json.Marshal(r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Place the dormant rover down
|
||||||
|
w.Atlas.SetObject(r.Pos, Object{Type: roveapi.Object_RoverDormant, Data: data})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverPosition returns the position of the rover
|
||||||
|
func (w *World) RoverPosition(rover string) (maths.Vector, error) {
|
||||||
|
w.worldMutex.RLock()
|
||||||
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return maths.Vector{}, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
return i.Pos, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRoverPosition sets the position of the rover
|
||||||
|
func (w *World) SetRoverPosition(rover string, pos maths.Vector) error {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
i.Pos = pos
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverInventory returns the inventory of a requested rover
|
||||||
|
func (w *World) RoverInventory(rover string) ([]Object, error) {
|
||||||
|
w.worldMutex.RLock()
|
||||||
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
return i.Inventory, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// WarpRover sets an rovers position
|
||||||
|
func (w *World) WarpRover(rover string, pos maths.Vector) error {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
// Nothing to do if these positions match
|
||||||
|
if i.Pos == pos {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the tile is not blocked
|
||||||
|
_, obj := w.Atlas.QueryPosition(pos)
|
||||||
|
if obj.IsBlocking() {
|
||||||
|
return fmt.Errorf("can't warp rover to occupied tile, check before warping")
|
||||||
|
}
|
||||||
|
|
||||||
|
i.Pos = pos
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TryMoveRover attempts to move a rover in a specific direction
|
||||||
|
func (w *World) TryMoveRover(rover string, b roveapi.Bearing) (maths.Vector, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
i, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return maths.Vector{}, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try the new move position
|
||||||
|
newPos := i.Pos.Added(maths.BearingToVector(b))
|
||||||
|
|
||||||
|
// Get the tile and verify it's empty
|
||||||
|
_, obj := w.Atlas.QueryPosition(newPos)
|
||||||
|
if !obj.IsBlocking() {
|
||||||
|
i.AddLogEntryf("moved %s to %+v", b.String(), newPos)
|
||||||
|
// Perform the move
|
||||||
|
i.Pos = newPos
|
||||||
|
} else {
|
||||||
|
// If it is a blocking tile, reduce the rover integrity
|
||||||
|
i.AddLogEntryf("tried to move %s to %+v", b.String(), newPos)
|
||||||
|
i.Integrity = i.Integrity - 1
|
||||||
|
i.AddLogEntryf("had a collision, new integrity %d", i.Integrity)
|
||||||
|
}
|
||||||
|
|
||||||
|
return i.Pos, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverStash will stash an item at the current rovers position
|
||||||
|
func (w *World) RoverStash(rover string) (roveapi.Object, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return roveapi.Object_ObjectUnknown, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can't pick up when full
|
||||||
|
if len(r.Inventory) >= r.Capacity {
|
||||||
|
r.AddLogEntryf("tried to stash object but inventory was full")
|
||||||
|
return roveapi.Object_ObjectUnknown, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the rover has energy
|
||||||
|
if r.Charge <= 0 {
|
||||||
|
r.AddLogEntryf("tried to stash object but had no charge")
|
||||||
|
return roveapi.Object_ObjectUnknown, nil
|
||||||
|
}
|
||||||
|
r.Charge--
|
||||||
|
|
||||||
|
_, obj := w.Atlas.QueryPosition(r.Pos)
|
||||||
|
if !obj.IsStashable() {
|
||||||
|
return roveapi.Object_ObjectUnknown, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r.AddLogEntryf("stashed %c", obj.Type)
|
||||||
|
r.Inventory = append(r.Inventory, obj)
|
||||||
|
w.Atlas.SetObject(r.Pos, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
return obj.Type, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverSalvage will salvage a rover for parts
|
||||||
|
func (w *World) RoverSalvage(rover string) (roveapi.Object, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return roveapi.Object_ObjectUnknown, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can't pick up when full
|
||||||
|
if len(r.Inventory) >= r.Capacity {
|
||||||
|
r.AddLogEntryf("tried to salvage dormant rover but inventory was full")
|
||||||
|
return roveapi.Object_ObjectUnknown, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the rover has energy
|
||||||
|
if r.Charge <= 0 {
|
||||||
|
r.AddLogEntryf("tried to salvage dormant rover but had no charge")
|
||||||
|
return roveapi.Object_ObjectUnknown, nil
|
||||||
|
}
|
||||||
|
r.Charge--
|
||||||
|
|
||||||
|
_, obj := w.Atlas.QueryPosition(r.Pos)
|
||||||
|
if obj.Type != roveapi.Object_RoverDormant {
|
||||||
|
r.AddLogEntryf("tried to salvage dormant rover but found no rover to salvage")
|
||||||
|
return roveapi.Object_ObjectUnknown, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
r.AddLogEntryf("salvaged dormant rover")
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
if len(r.Inventory) == r.Capacity {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r.Inventory = append(r.Inventory, Object{Type: roveapi.Object_RoverParts})
|
||||||
|
}
|
||||||
|
w.Atlas.SetObject(r.Pos, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
return obj.Type, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverTransfer will transfer rover control to dormant rover
|
||||||
|
func (w *World) RoverTransfer(rover string) (string, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
oldRover, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, obj := w.Atlas.QueryPosition(oldRover.Pos)
|
||||||
|
if obj.Type != roveapi.Object_RoverDormant {
|
||||||
|
oldRover.AddLogEntryf("tried to transfer to dormant rover but found no rover")
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal the dormant rover
|
||||||
|
var newRover Rover
|
||||||
|
err := json.Unmarshal(obj.Data, &newRover)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add logs
|
||||||
|
oldRover.AddLogEntryf("transferring to dormant rover %s", newRover.Name)
|
||||||
|
newRover.AddLogEntryf("transferred from rover %s", oldRover.Name)
|
||||||
|
|
||||||
|
// Transfer the ownership
|
||||||
|
err = w.Accountant.AssignData(oldRover.Owner, "rover", newRover.Name)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
newRover.Owner = oldRover.Owner
|
||||||
|
oldRover.Owner = ""
|
||||||
|
|
||||||
|
// Place the old rover in the world
|
||||||
|
oldRoverData, err := json.Marshal(oldRover)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
w.Atlas.SetObject(oldRover.Pos, Object{Type: roveapi.Object_RoverDormant, Data: oldRoverData})
|
||||||
|
|
||||||
|
// Swap the rovers in the tracking
|
||||||
|
w.Rovers[newRover.Name] = &newRover
|
||||||
|
delete(w.Rovers, oldRover.Name)
|
||||||
|
|
||||||
|
// Clear the command queues for both rovers
|
||||||
|
delete(w.CommandQueue, oldRover.Name)
|
||||||
|
delete(w.CommandQueue, newRover.Name)
|
||||||
|
|
||||||
|
return newRover.Name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverToggle will toggle the sail position
|
||||||
|
func (w *World) RoverToggle(rover string) (roveapi.SailPosition, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return roveapi.SailPosition_UnknownSailPosition, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap the sail position
|
||||||
|
switch r.SailPosition {
|
||||||
|
case roveapi.SailPosition_CatchingWind:
|
||||||
|
r.SailPosition = roveapi.SailPosition_SolarCharging
|
||||||
|
case roveapi.SailPosition_SolarCharging:
|
||||||
|
r.SailPosition = roveapi.SailPosition_CatchingWind
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset the movement ticks
|
||||||
|
r.MoveTicks = 0
|
||||||
|
|
||||||
|
return r.SailPosition, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverUpgrade will try to upgrade the rover
|
||||||
|
func (w *World) RoverUpgrade(rover string, upgrade roveapi.RoverUpgrade) (int, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return 0, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
cost := upgradeCost
|
||||||
|
num := 0
|
||||||
|
for i := range r.Inventory {
|
||||||
|
if r.Inventory[i].Type == roveapi.Object_RoverParts {
|
||||||
|
num++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if num < cost {
|
||||||
|
r.AddLogEntryf("tried to upgrade but lacked rover parts")
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply the upgrade
|
||||||
|
var ret int
|
||||||
|
switch upgrade {
|
||||||
|
case roveapi.RoverUpgrade_Capacity:
|
||||||
|
r.Capacity++
|
||||||
|
ret = r.Capacity
|
||||||
|
case roveapi.RoverUpgrade_Range:
|
||||||
|
r.Range++
|
||||||
|
ret = r.Range
|
||||||
|
case roveapi.RoverUpgrade_MaximumCharge:
|
||||||
|
r.MaximumCharge++
|
||||||
|
ret = r.MaximumCharge
|
||||||
|
case roveapi.RoverUpgrade_MaximumIntegrity:
|
||||||
|
r.MaximumIntegrity++
|
||||||
|
ret = r.MaximumIntegrity
|
||||||
|
default:
|
||||||
|
return 0, fmt.Errorf("unknown upgrade: %s", upgrade)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the cost in rover parts
|
||||||
|
var n []Object
|
||||||
|
for _, o := range r.Inventory {
|
||||||
|
if o.Type == roveapi.Object_RoverParts && cost > 0 {
|
||||||
|
cost--
|
||||||
|
} else {
|
||||||
|
n = append(n, o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Assign back the inventory
|
||||||
|
r.Inventory = n
|
||||||
|
|
||||||
|
r.AddLogEntryf("upgraded %s to %d", upgrade, ret)
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverTurn will turn the rover
|
||||||
|
func (w *World) RoverTurn(rover string, bearing roveapi.Bearing) (roveapi.Bearing, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return roveapi.Bearing_BearingUnknown, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the new bearing
|
||||||
|
r.Bearing = bearing
|
||||||
|
// Reset the movement ticks
|
||||||
|
r.MoveTicks = 0
|
||||||
|
|
||||||
|
return r.Bearing, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverRepair will turn the rover
|
||||||
|
func (w *World) RoverRepair(rover string) (int, error) {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
defer w.worldMutex.Unlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
return 0, fmt.Errorf("no rover matching id")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can't repair past max
|
||||||
|
if r.Integrity >= r.MaximumIntegrity {
|
||||||
|
return r.Integrity, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find rover parts in inventory
|
||||||
|
for i, o := range r.Inventory {
|
||||||
|
if o.Type == roveapi.Object_RoverParts {
|
||||||
|
|
||||||
|
// Copy-erase from slice
|
||||||
|
r.Inventory[i] = r.Inventory[len(r.Inventory)-1]
|
||||||
|
r.Inventory = r.Inventory[:len(r.Inventory)-1]
|
||||||
|
|
||||||
|
// Repair
|
||||||
|
r.Integrity = r.Integrity + 1
|
||||||
|
r.AddLogEntryf("repaired self to %d", r.Integrity)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return r.Integrity, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RadarFromRover can be used to query what a rover can currently see
|
||||||
|
func (w *World) RadarFromRover(rover string) (radar []roveapi.Tile, objs []roveapi.Object, err error) {
|
||||||
|
w.worldMutex.RLock()
|
||||||
|
defer w.worldMutex.RUnlock()
|
||||||
|
|
||||||
|
r, ok := w.Rovers[rover]
|
||||||
|
if !ok {
|
||||||
|
err = fmt.Errorf("no rover matching id")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// The radar should span in range direction on each axis, plus the row/column the rover is currently on
|
||||||
|
radarSpan := (r.Range * 2) + 1
|
||||||
|
roverPos := r.Pos
|
||||||
|
|
||||||
|
// Get the radar min and max values
|
||||||
|
radarMin := maths.Vector{
|
||||||
|
X: roverPos.X - r.Range,
|
||||||
|
Y: roverPos.Y - r.Range,
|
||||||
|
}
|
||||||
|
radarMax := maths.Vector{
|
||||||
|
X: roverPos.X + r.Range,
|
||||||
|
Y: roverPos.Y + r.Range,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gather up all tiles within the range
|
||||||
|
radar = make([]roveapi.Tile, radarSpan*radarSpan)
|
||||||
|
objs = make([]roveapi.Object, radarSpan*radarSpan)
|
||||||
|
for j := radarMin.Y; j <= radarMax.Y; j++ {
|
||||||
|
for i := radarMin.X; i <= radarMax.X; i++ {
|
||||||
|
q := maths.Vector{X: i, Y: j}
|
||||||
|
|
||||||
|
tile, obj := w.Atlas.QueryPosition(q)
|
||||||
|
|
||||||
|
// Get the position relative to the bottom left of the radar
|
||||||
|
relative := q.Added(radarMin.Negated())
|
||||||
|
index := relative.X + relative.Y*radarSpan
|
||||||
|
radar[index] = tile
|
||||||
|
objs[index] = obj.Type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add all rovers to the radar
|
||||||
|
for _, r := range w.Rovers {
|
||||||
|
// If the rover is in range
|
||||||
|
dist := r.Pos.Added(roverPos.Negated())
|
||||||
|
dist = dist.Abs()
|
||||||
|
|
||||||
|
if dist.X <= r.Range && dist.Y <= r.Range {
|
||||||
|
relative := r.Pos.Added(radarMin.Negated())
|
||||||
|
index := relative.X + relative.Y*radarSpan
|
||||||
|
objs[index] = roveapi.Object_RoverLive
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return radar, objs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoverCommands returns current commands for the given rover
|
||||||
|
func (w *World) RoverCommands(rover string) (queued CommandStream) {
|
||||||
|
if c, ok := w.CommandQueue[rover]; ok {
|
||||||
|
queued = c
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue will queue the commands given
|
||||||
|
func (w *World) Enqueue(rover string, commands ...*roveapi.Command) error {
|
||||||
|
|
||||||
|
// First validate the commands
|
||||||
|
for _, c := range commands {
|
||||||
|
switch c.Command {
|
||||||
|
case roveapi.CommandType_broadcast:
|
||||||
|
if len(c.GetData()) > 3 {
|
||||||
|
return fmt.Errorf("too many characters in message (limit 3): %d", len(c.GetData()))
|
||||||
|
}
|
||||||
|
for _, b := range c.GetData() {
|
||||||
|
if b < 37 || b > 126 {
|
||||||
|
return fmt.Errorf("invalid message character: %c", b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case roveapi.CommandType_turn:
|
||||||
|
if c.GetBearing() == roveapi.Bearing_BearingUnknown {
|
||||||
|
return fmt.Errorf("turn command given unknown bearing")
|
||||||
|
}
|
||||||
|
case roveapi.CommandType_upgrade:
|
||||||
|
if c.GetUpgrade() == roveapi.RoverUpgrade_RoverUpgradeUnknown {
|
||||||
|
return fmt.Errorf("upgrade command given unknown upgrade")
|
||||||
|
}
|
||||||
|
case roveapi.CommandType_wait:
|
||||||
|
case roveapi.CommandType_toggle:
|
||||||
|
case roveapi.CommandType_stash:
|
||||||
|
case roveapi.CommandType_repair:
|
||||||
|
case roveapi.CommandType_salvage:
|
||||||
|
case roveapi.CommandType_transfer:
|
||||||
|
// Nothing to verify
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown command: %s", c.Command)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lock our commands edit
|
||||||
|
w.cmdMutex.Lock()
|
||||||
|
defer w.cmdMutex.Unlock()
|
||||||
|
|
||||||
|
w.CommandQueue[rover] = commands
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tick will execute any commands in the current command queue and tick the world
|
||||||
|
func (w *World) Tick() {
|
||||||
|
w.cmdMutex.Lock()
|
||||||
|
defer w.cmdMutex.Unlock()
|
||||||
|
|
||||||
|
// Iterate through all the current commands
|
||||||
|
for rover, cmds := range w.CommandQueue {
|
||||||
|
if len(cmds) != 0 {
|
||||||
|
|
||||||
|
// Execute the command
|
||||||
|
if done, err := w.ExecuteCommand(cmds[0], rover); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO: Report this error somehow
|
||||||
|
|
||||||
|
} else if done {
|
||||||
|
// Extract the first command in the queue
|
||||||
|
// Only if the command queue still has entries (the command may have modified this queue)
|
||||||
|
if _, ok := w.CommandQueue[rover]; ok {
|
||||||
|
w.CommandQueue[rover] = cmds[1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Clean out the empty entry
|
||||||
|
delete(w.CommandQueue, rover)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move all the rovers based on current wind and sails
|
||||||
|
for n, r := range w.Rovers {
|
||||||
|
// Skip if we're not catching the wind
|
||||||
|
if r.SailPosition != roveapi.SailPosition_CatchingWind {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment the current move ticks
|
||||||
|
r.MoveTicks++
|
||||||
|
|
||||||
|
// Get the difference between the two bearings
|
||||||
|
// Normalise, we don't care about clockwise/anticlockwise
|
||||||
|
diff := maths.Abs(int(w.Wind - r.Bearing))
|
||||||
|
if diff > 4 {
|
||||||
|
diff = 8 - diff
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the travel "ticks"
|
||||||
|
var ticksToMove int
|
||||||
|
switch diff {
|
||||||
|
case 0:
|
||||||
|
// Going with the wind, travel at base speed of once every 4 ticks
|
||||||
|
ticksToMove = ticksPerNormalMove
|
||||||
|
case 1:
|
||||||
|
// At a slight angle, we can go a little faster
|
||||||
|
ticksToMove = ticksPerNormalMove / 2
|
||||||
|
case 2:
|
||||||
|
// Perpendicular to wind, max speed
|
||||||
|
ticksToMove = 1
|
||||||
|
case 3:
|
||||||
|
// Heading at 45 degrees into the wind, back to min speed
|
||||||
|
ticksToMove = ticksPerNormalMove
|
||||||
|
case 4:
|
||||||
|
// Heading durectly into the wind, no movement at all
|
||||||
|
default:
|
||||||
|
log.Fatalf("bearing difference of %d should be impossible", diff)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we've incremented over the current move ticks on the rover, we can try and make the move
|
||||||
|
if ticksToMove != 0 && r.MoveTicks >= ticksToMove {
|
||||||
|
_, err := w.TryMoveRover(n, r.Bearing)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO: Report this error somehow
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset the move ticks
|
||||||
|
r.MoveTicks = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check all rover integrities
|
||||||
|
for _, r := range w.Rovers {
|
||||||
|
if r.Integrity <= 0 {
|
||||||
|
// The rover has died destroy it
|
||||||
|
err := w.DestroyRover(r.Name)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO: Report this error somehow
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spawn a new one for this account
|
||||||
|
_, err = w.SpawnRover(r.Owner)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
// TODO: Report this error somehow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment the current tick count
|
||||||
|
w.CurrentTicks++
|
||||||
|
|
||||||
|
// Change the wind every day
|
||||||
|
if (w.CurrentTicks % w.TicksPerDay) == 0 {
|
||||||
|
w.Wind = roveapi.Bearing((rand.Int() % 8) + 1) // Random cardinal bearing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecuteCommand will execute a single command
|
||||||
|
func (w *World) ExecuteCommand(c *roveapi.Command, rover string) (done bool, err error) {
|
||||||
|
log.Printf("Executing command: %+v for %s\n", c.Command, rover)
|
||||||
|
|
||||||
|
switch c.Command {
|
||||||
|
case roveapi.CommandType_toggle:
|
||||||
|
_, err = w.RoverToggle(rover)
|
||||||
|
case roveapi.CommandType_stash:
|
||||||
|
_, err = w.RoverStash(rover)
|
||||||
|
case roveapi.CommandType_repair:
|
||||||
|
_, err = w.RoverRepair(rover)
|
||||||
|
case roveapi.CommandType_broadcast:
|
||||||
|
err = w.RoverBroadcast(rover, c.GetData())
|
||||||
|
case roveapi.CommandType_turn:
|
||||||
|
_, err = w.RoverTurn(rover, c.GetBearing())
|
||||||
|
case roveapi.CommandType_salvage:
|
||||||
|
_, err = w.RoverSalvage(rover)
|
||||||
|
case roveapi.CommandType_transfer:
|
||||||
|
_, err = w.RoverTransfer(rover)
|
||||||
|
case roveapi.CommandType_upgrade:
|
||||||
|
_, err = w.RoverUpgrade(rover, c.GetUpgrade())
|
||||||
|
case roveapi.CommandType_wait:
|
||||||
|
// Nothing to do
|
||||||
|
default:
|
||||||
|
return true, fmt.Errorf("unknown command: %s", c.Command)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrement the repeat number
|
||||||
|
c.Repeat--
|
||||||
|
return c.Repeat < 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Daytime returns if it's currently daytime
|
||||||
|
// for simplicity this uses the 1st half of the day as daytime, the 2nd half as nighttime
|
||||||
|
func (w *World) Daytime() bool {
|
||||||
|
tickInDay := w.CurrentTicks % w.TicksPerDay
|
||||||
|
return tickInDay < w.TicksPerDay/2
|
||||||
|
}
|
||||||
|
|
||||||
|
// RLock read locks the world
|
||||||
|
func (w *World) RLock() {
|
||||||
|
w.worldMutex.RLock()
|
||||||
|
w.cmdMutex.RLock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// RUnlock read unlocks the world
|
||||||
|
func (w *World) RUnlock() {
|
||||||
|
w.worldMutex.RUnlock()
|
||||||
|
w.cmdMutex.RUnlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lock locks the world
|
||||||
|
func (w *World) Lock() {
|
||||||
|
w.worldMutex.Lock()
|
||||||
|
w.cmdMutex.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unlock unlocks the world
|
||||||
|
func (w *World) Unlock() {
|
||||||
|
w.worldMutex.Unlock()
|
||||||
|
w.cmdMutex.Unlock()
|
||||||
|
}
|
359
pkg/rove/world_test.go
Normal file
359
pkg/rove/world_test.go
Normal file
|
@ -0,0 +1,359 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewWorld(t *testing.T) {
|
||||||
|
// Very basic for now, nothing to verify
|
||||||
|
world := NewWorld(4)
|
||||||
|
if world == nil {
|
||||||
|
t.Error("Failed to create world")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_CreateRover(t *testing.T) {
|
||||||
|
world := NewWorld(8)
|
||||||
|
a, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
b, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Basic duplicate check
|
||||||
|
if a == b {
|
||||||
|
t.Errorf("Created identical rovers")
|
||||||
|
} else if len(world.Rovers) != 2 {
|
||||||
|
t.Errorf("Incorrect number of rovers created")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_GetRover(t *testing.T) {
|
||||||
|
world := NewWorld(4)
|
||||||
|
a, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
rover, err := world.GetRover(a)
|
||||||
|
assert.NoError(t, err, "Failed to get rover attribs")
|
||||||
|
assert.NotZero(t, rover.Range, "Rover should not be spawned blind")
|
||||||
|
assert.Contains(t, rover.Logs[len(rover.Logs)-1].Text, "created", "Rover logs should contain the creation")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_DestroyRover(t *testing.T) {
|
||||||
|
world := NewWorld(1)
|
||||||
|
a, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
b, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = world.DestroyRover(a)
|
||||||
|
assert.NoError(t, err, "Error returned from rover destroy")
|
||||||
|
|
||||||
|
// Basic duplicate check
|
||||||
|
if len(world.Rovers) != 1 {
|
||||||
|
t.Error("Too many rovers left in world")
|
||||||
|
} else if _, ok := world.Rovers[b]; !ok {
|
||||||
|
t.Error("Remaining rover is incorrect")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_GetSetMovePosition(t *testing.T) {
|
||||||
|
world := NewWorld(4)
|
||||||
|
a, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
pos := maths.Vector{
|
||||||
|
X: 0.0,
|
||||||
|
Y: 0.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = world.WarpRover(a, pos)
|
||||||
|
assert.NoError(t, err, "Failed to set position for rover")
|
||||||
|
|
||||||
|
newPos, err := world.RoverPosition(a)
|
||||||
|
assert.NoError(t, err, "Failed to set position for rover")
|
||||||
|
assert.Equal(t, pos, newPos, "Failed to correctly set position for rover")
|
||||||
|
|
||||||
|
b := roveapi.Bearing_North
|
||||||
|
newPos, err = world.TryMoveRover(a, b)
|
||||||
|
assert.NoError(t, err, "Failed to set position for rover")
|
||||||
|
pos.Add(maths.Vector{X: 0, Y: 1})
|
||||||
|
assert.Equal(t, pos, newPos, "Failed to correctly move position for rover")
|
||||||
|
|
||||||
|
rover, err := world.GetRover(a)
|
||||||
|
assert.NoError(t, err, "Failed to get rover information")
|
||||||
|
assert.Contains(t, rover.Logs[len(rover.Logs)-1].Text, "moved", "Rover logs should contain the move")
|
||||||
|
|
||||||
|
// Place a tile in front of the rover
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 0, Y: 2}, Object{Type: roveapi.Object_RockLarge})
|
||||||
|
newPos, err = world.TryMoveRover(a, b)
|
||||||
|
assert.NoError(t, err, "Failed to move rover")
|
||||||
|
assert.Equal(t, pos, newPos, "Failed to correctly not move position for rover into wall")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_RadarFromRover(t *testing.T) {
|
||||||
|
// Create world that should have visible walls on the radar
|
||||||
|
world := NewWorld(2)
|
||||||
|
a, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
b, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Warp the rovers into position
|
||||||
|
bpos := maths.Vector{X: -3, Y: -3}
|
||||||
|
world.Atlas.SetObject(bpos, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
assert.NoError(t, world.WarpRover(b, bpos), "Failed to warp rover")
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
assert.NoError(t, world.WarpRover(a, maths.Vector{X: 0, Y: 0}), "Failed to warp rover")
|
||||||
|
|
||||||
|
r, err := world.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
radar, objs, err := world.RadarFromRover(a)
|
||||||
|
assert.NoError(t, err, "Failed to get radar from rover")
|
||||||
|
fullRange := r.Range + r.Range + 1
|
||||||
|
assert.Equal(t, fullRange*fullRange, len(radar), "Radar returned wrong length")
|
||||||
|
assert.Equal(t, fullRange*fullRange, len(objs), "Radar returned wrong length")
|
||||||
|
|
||||||
|
// TODO: Verify the other rover is on the radar
|
||||||
|
|
||||||
|
// Check the radar results are stable
|
||||||
|
radar1, objs1, err := world.RadarFromRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
radar2, objs2, err := world.RadarFromRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, radar1, radar2)
|
||||||
|
assert.Equal(t, objs1, objs2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_RoverDamage(t *testing.T) {
|
||||||
|
world := NewWorld(2)
|
||||||
|
acc, err := world.Accountant.RegisterAccount("tmp")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
a, err := world.SpawnRover(acc.Name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
pos := maths.Vector{
|
||||||
|
X: 0.0,
|
||||||
|
Y: 0.0,
|
||||||
|
}
|
||||||
|
|
||||||
|
world.Atlas.SetObject(pos, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
err = world.WarpRover(a, pos)
|
||||||
|
assert.NoError(t, err, "Failed to set position for rover")
|
||||||
|
|
||||||
|
info, err := world.GetRover(a)
|
||||||
|
assert.NoError(t, err, "couldn't get rover info")
|
||||||
|
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 0.0, Y: 1.0}, Object{Type: roveapi.Object_RockLarge})
|
||||||
|
|
||||||
|
vec, err := world.TryMoveRover(a, roveapi.Bearing_North)
|
||||||
|
assert.NoError(t, err, "Failed to move rover")
|
||||||
|
assert.Equal(t, pos, vec, "Rover managed to move into large rock")
|
||||||
|
|
||||||
|
newinfo, err := world.GetRover(a)
|
||||||
|
assert.NoError(t, err, "couldn't get rover info")
|
||||||
|
assert.Equal(t, info.Integrity-1, newinfo.Integrity, "rover should have lost integrity")
|
||||||
|
assert.Contains(t, newinfo.Logs[len(newinfo.Logs)-1].Text, "collision", "Rover logs should contain the collision")
|
||||||
|
|
||||||
|
// Keep moving to damage the rover
|
||||||
|
for i := 0; i < info.Integrity-1; i++ {
|
||||||
|
vec, err := world.TryMoveRover(a, roveapi.Bearing_North)
|
||||||
|
assert.NoError(t, err, "Failed to move rover")
|
||||||
|
assert.Equal(t, pos, vec, "Rover managed to move into large rock")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tick the world to check for rover deaths
|
||||||
|
world.Tick()
|
||||||
|
|
||||||
|
// Rover should have been destroyed now
|
||||||
|
_, err = world.GetRover(a)
|
||||||
|
assert.Error(t, err)
|
||||||
|
|
||||||
|
_, obj := world.Atlas.QueryPosition(info.Pos)
|
||||||
|
assert.Equal(t, roveapi.Object_RoverDormant, obj.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_Daytime(t *testing.T) {
|
||||||
|
world := NewWorld(1)
|
||||||
|
|
||||||
|
a, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Remove rover charge
|
||||||
|
rover := world.Rovers[a]
|
||||||
|
rover.Charge = 0
|
||||||
|
world.Rovers[a] = rover
|
||||||
|
|
||||||
|
// Try and recharge, should work
|
||||||
|
_, err = world.RoverRecharge(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 1, world.Rovers[a].Charge)
|
||||||
|
|
||||||
|
// Loop for half the day
|
||||||
|
for i := 0; i < world.TicksPerDay/2; i++ {
|
||||||
|
assert.True(t, world.Daytime())
|
||||||
|
world.Tick()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove rover charge again
|
||||||
|
rover = world.Rovers[a]
|
||||||
|
rover.Charge = 0
|
||||||
|
world.Rovers[a] = rover
|
||||||
|
|
||||||
|
// Try and recharge, should fail
|
||||||
|
_, err = world.RoverRecharge(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 0, world.Rovers[a].Charge)
|
||||||
|
|
||||||
|
// Loop for half the day
|
||||||
|
for i := 0; i < world.TicksPerDay/2; i++ {
|
||||||
|
assert.False(t, world.Daytime())
|
||||||
|
world.Tick()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_Broadcast(t *testing.T) {
|
||||||
|
world := NewWorld(8)
|
||||||
|
|
||||||
|
a, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
b, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Warp rovers near to eachother
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 1, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
assert.NoError(t, world.WarpRover(a, maths.Vector{X: 0, Y: 0}))
|
||||||
|
assert.NoError(t, world.WarpRover(b, maths.Vector{X: 1, Y: 0}))
|
||||||
|
|
||||||
|
// Broadcast from a
|
||||||
|
assert.NoError(t, world.RoverBroadcast(a, []byte{'A', 'B', 'C'}))
|
||||||
|
|
||||||
|
// Check if b heard it
|
||||||
|
ra, err := world.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, ra.MaximumCharge-1, ra.Charge, "A should have used a charge to broadcast")
|
||||||
|
assert.Contains(t, ra.Logs[len(ra.Logs)-1].Text, "ABC", "Rover B should have heard the broadcast")
|
||||||
|
|
||||||
|
// Check if a logged it
|
||||||
|
rb, err := world.GetRover(b)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Contains(t, rb.Logs[len(rb.Logs)-1].Text, "ABC", "Rover A should have logged it's broadcast")
|
||||||
|
|
||||||
|
// Warp B outside of the range of A
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: ra.Range, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range, Y: 0}))
|
||||||
|
|
||||||
|
// Broadcast from a again
|
||||||
|
assert.NoError(t, world.RoverBroadcast(a, []byte{'X', 'Y', 'Z'}))
|
||||||
|
|
||||||
|
// Check if b heard it
|
||||||
|
ra, err = world.GetRover(b)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotContains(t, ra.Logs[len(ra.Logs)-1].Text, "XYZ", "Rover B should not have heard the broadcast")
|
||||||
|
|
||||||
|
// Check if a logged it
|
||||||
|
rb, err = world.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Contains(t, rb.Logs[len(rb.Logs)-1].Text, "XYZ", "Rover A should have logged it's broadcast")
|
||||||
|
|
||||||
|
// Warp B outside of the range of A
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: ra.Range + 1, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
assert.NoError(t, world.WarpRover(b, maths.Vector{X: ra.Range + 1, Y: 0}))
|
||||||
|
|
||||||
|
// Broadcast from a again
|
||||||
|
assert.NoError(t, world.RoverBroadcast(a, []byte{'H', 'J', 'K'}))
|
||||||
|
|
||||||
|
// Check if b heard it
|
||||||
|
ra, err = world.GetRover(b)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.NotContains(t, ra.Logs[len(ra.Logs)-1].Text, "HJK", "Rover B should have heard the broadcast")
|
||||||
|
|
||||||
|
// Check if a logged it
|
||||||
|
rb, err = world.GetRover(a)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Contains(t, rb.Logs[len(rb.Logs)-1].Text, "HJK", "Rover A should have logged it's broadcast")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWorld_Sailing(t *testing.T) {
|
||||||
|
world := NewWorld(8)
|
||||||
|
world.Tick() // One initial tick to set the wind direction the first time
|
||||||
|
world.Wind = roveapi.Bearing_North // Set the wind direction to north
|
||||||
|
|
||||||
|
name, err := world.SpawnRover("")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Warp the rover to 0,0 after clearing it
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 0, Y: 0}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
assert.NoError(t, world.WarpRover(name, maths.Vector{X: 0, Y: 0}))
|
||||||
|
|
||||||
|
s, err := world.RoverToggle(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.SailPosition_CatchingWind, s)
|
||||||
|
|
||||||
|
b, err := world.RoverTurn(name, roveapi.Bearing_North)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.Bearing_North, b)
|
||||||
|
|
||||||
|
// Clear the space to the north
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 0, Y: 1}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
|
||||||
|
// Tick the world and check we've moved not moved
|
||||||
|
world.Tick()
|
||||||
|
info, err := world.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, maths.Vector{Y: 0}, info.Pos)
|
||||||
|
|
||||||
|
// Loop a few more times
|
||||||
|
for i := 0; i < ticksPerNormalMove-2; i++ {
|
||||||
|
world.Tick()
|
||||||
|
info, err := world.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, maths.Vector{Y: 0}, info.Pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now check we've moved (after the TicksPerNormalMove number of ticks)
|
||||||
|
world.Tick()
|
||||||
|
info, err = world.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, maths.Vector{Y: 1}, info.Pos)
|
||||||
|
|
||||||
|
// Reset the world ticks back to stop any wind changes etc.
|
||||||
|
world.CurrentTicks = 1
|
||||||
|
|
||||||
|
// Face the rover south, into the wind
|
||||||
|
b, err = world.RoverTurn(name, roveapi.Bearing_South)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.Bearing_South, b)
|
||||||
|
|
||||||
|
// Tick a bunch, we should never move
|
||||||
|
for i := 0; i < ticksPerNormalMove*2; i++ {
|
||||||
|
world.Tick()
|
||||||
|
info, err := world.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, maths.Vector{Y: 1}, info.Pos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset the world ticks back to stop any wind changes etc.
|
||||||
|
world.CurrentTicks = 1
|
||||||
|
world.Wind = roveapi.Bearing_SouthEast // Set up a south easternly wind
|
||||||
|
|
||||||
|
// Turn the rover perpendicular
|
||||||
|
b, err = world.RoverTurn(name, roveapi.Bearing_NorthEast)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, roveapi.Bearing_NorthEast, b)
|
||||||
|
|
||||||
|
// Clear a space
|
||||||
|
world.Atlas.SetObject(maths.Vector{X: 1, Y: 2}, Object{Type: roveapi.Object_ObjectUnknown})
|
||||||
|
|
||||||
|
// Now check we've moved immediately
|
||||||
|
world.Tick()
|
||||||
|
info, err = world.GetRover(name)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, maths.Vector{X: 1, Y: 2}, info.Pos)
|
||||||
|
}
|
70
pkg/rove/worldgen.go
Normal file
70
pkg/rove/worldgen.go
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
package rove
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mdiluz/rove/pkg/maths"
|
||||||
|
"github.com/mdiluz/rove/proto/roveapi"
|
||||||
|
"github.com/ojrac/opensimplex-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
// WorldGen describes a world gen algorythm
|
||||||
|
type WorldGen interface {
|
||||||
|
// GetTile generates a tile for a location
|
||||||
|
GetTile(v maths.Vector) roveapi.Tile
|
||||||
|
|
||||||
|
// GetObject generates an object for a location
|
||||||
|
GetObject(v maths.Vector) Object
|
||||||
|
}
|
||||||
|
|
||||||
|
// NoiseWorldGen returns a noise based world generator
|
||||||
|
type NoiseWorldGen struct {
|
||||||
|
// noise describes the noise function
|
||||||
|
noise opensimplex.Noise
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNoiseWorldGen creates a new noise based world generator
|
||||||
|
func NewNoiseWorldGen(seed int64) WorldGen {
|
||||||
|
return &NoiseWorldGen{
|
||||||
|
noise: opensimplex.New(seed),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
terrainNoiseScale = 15
|
||||||
|
rockNoiseScale = 3
|
||||||
|
partsNoiseScale = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetTile returns the chosen tile at a location
|
||||||
|
func (g *NoiseWorldGen) GetTile(v maths.Vector) roveapi.Tile {
|
||||||
|
t := g.noise.Eval2(float64(v.X)/terrainNoiseScale, float64(v.Y)/terrainNoiseScale)
|
||||||
|
switch {
|
||||||
|
case t > 0.5:
|
||||||
|
return roveapi.Tile_Gravel
|
||||||
|
case t > 0.05:
|
||||||
|
return roveapi.Tile_Sand
|
||||||
|
default:
|
||||||
|
return roveapi.Tile_Rock
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetObject returns the chosen object at a location
|
||||||
|
func (g *NoiseWorldGen) GetObject(v maths.Vector) (obj Object) {
|
||||||
|
r := g.noise.Eval2(float64(v.X)/rockNoiseScale, float64(v.Y)/rockNoiseScale)
|
||||||
|
switch {
|
||||||
|
// Prioritise rocks
|
||||||
|
case r > 0.6:
|
||||||
|
obj.Type = roveapi.Object_RockLarge
|
||||||
|
case r > 0.5:
|
||||||
|
obj.Type = roveapi.Object_RockSmall
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Otherwise, try some rover parts
|
||||||
|
p := g.noise.Eval2(float64(v.X)/partsNoiseScale, float64(v.Y)/partsNoiseScale)
|
||||||
|
switch {
|
||||||
|
case p > 0.7:
|
||||||
|
obj.Type = roveapi.Object_RoverParts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj
|
||||||
|
}
|
|
@ -1,134 +0,0 @@
|
||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/mdiluz/rove/pkg/rove"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
var serv rove.Server = "localhost:8080"
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
s := NewServer(OptionPort(8080))
|
|
||||||
s.Initialise()
|
|
||||||
go s.Run()
|
|
||||||
|
|
||||||
code := m.Run()
|
|
||||||
|
|
||||||
s.Close()
|
|
||||||
|
|
||||||
os.Exit(code)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServer_Status(t *testing.T) {
|
|
||||||
status, err := serv.Status()
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, status.Ready)
|
|
||||||
assert.NotZero(t, len(status.Version))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServer_Register(t *testing.T) {
|
|
||||||
d1 := rove.RegisterData{
|
|
||||||
Name: uuid.New().String(),
|
|
||||||
}
|
|
||||||
r1, err := serv.Register(d1)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r1.Success)
|
|
||||||
assert.NotZero(t, len(r1.Id))
|
|
||||||
|
|
||||||
d2 := rove.RegisterData{
|
|
||||||
Name: uuid.New().String(),
|
|
||||||
}
|
|
||||||
r2, err := serv.Register(d2)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r2.Success)
|
|
||||||
assert.NotZero(t, len(r2.Id))
|
|
||||||
|
|
||||||
r3, err := serv.Register(d1)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.False(t, r3.Success)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServer_Spawn(t *testing.T) {
|
|
||||||
d1 := rove.RegisterData{
|
|
||||||
Name: uuid.New().String(),
|
|
||||||
}
|
|
||||||
r1, err := serv.Register(d1)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r1.Success)
|
|
||||||
assert.NotZero(t, len(r1.Id))
|
|
||||||
|
|
||||||
s := rove.SpawnData{}
|
|
||||||
r2, err := serv.Spawn(r1.Id, s)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r2.Success)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServer_Command(t *testing.T) {
|
|
||||||
d1 := rove.RegisterData{
|
|
||||||
Name: uuid.New().String(),
|
|
||||||
}
|
|
||||||
r1, err := serv.Register(d1)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r1.Success)
|
|
||||||
assert.NotZero(t, len(r1.Id))
|
|
||||||
|
|
||||||
s := rove.SpawnData{}
|
|
||||||
r2, err := serv.Spawn(r1.Id, s)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r2.Success)
|
|
||||||
|
|
||||||
c := rove.CommandData{
|
|
||||||
Commands: []rove.Command{
|
|
||||||
{
|
|
||||||
Command: rove.CommandMove,
|
|
||||||
Bearing: "N",
|
|
||||||
Duration: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
r3, err := serv.Command(r1.Id, c)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r3.Success)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServer_Radar(t *testing.T) {
|
|
||||||
d1 := rove.RegisterData{
|
|
||||||
Name: uuid.New().String(),
|
|
||||||
}
|
|
||||||
r1, err := serv.Register(d1)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r1.Success)
|
|
||||||
assert.NotZero(t, len(r1.Id))
|
|
||||||
|
|
||||||
s := rove.SpawnData{}
|
|
||||||
r2, err := serv.Spawn(r1.Id, s)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r2.Success)
|
|
||||||
|
|
||||||
r3, err := serv.Radar(r1.Id)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r3.Success)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServer_Rover(t *testing.T) {
|
|
||||||
d1 := rove.RegisterData{
|
|
||||||
Name: uuid.New().String(),
|
|
||||||
}
|
|
||||||
r1, err := serv.Register(d1)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r1.Success)
|
|
||||||
assert.NotZero(t, len(r1.Id))
|
|
||||||
|
|
||||||
s := rove.SpawnData{}
|
|
||||||
r2, err := serv.Spawn(r1.Id, s)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r2.Success)
|
|
||||||
|
|
||||||
r3, err := serv.Rover(r1.Id)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.True(t, r3.Success)
|
|
||||||
}
|
|
|
@ -1,214 +0,0 @@
|
||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/mdiluz/rove/pkg/rove"
|
|
||||||
"github.com/mdiluz/rove/pkg/version"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Handler describes a function that handles any incoming request and can respond
|
|
||||||
type Handler func(*Server, map[string]string, io.ReadCloser, io.Writer) (interface{}, error)
|
|
||||||
|
|
||||||
// Route defines the information for a single path->function route
|
|
||||||
type Route struct {
|
|
||||||
path string
|
|
||||||
method string
|
|
||||||
handler Handler
|
|
||||||
}
|
|
||||||
|
|
||||||
// Routes is an array of all the Routes
|
|
||||||
var Routes = []Route{
|
|
||||||
{
|
|
||||||
path: "/status",
|
|
||||||
method: http.MethodGet,
|
|
||||||
handler: HandleStatus,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: "/register",
|
|
||||||
method: http.MethodPost,
|
|
||||||
handler: HandleRegister,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: "/{account}/spawn",
|
|
||||||
method: http.MethodPost,
|
|
||||||
handler: HandleSpawn,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: "/{account}/command",
|
|
||||||
method: http.MethodPost,
|
|
||||||
handler: HandleCommand,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: "/{account}/radar",
|
|
||||||
method: http.MethodGet,
|
|
||||||
handler: HandleRadar,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
path: "/{account}/rover",
|
|
||||||
method: http.MethodGet,
|
|
||||||
handler: HandleRover,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleStatus handles the /status request
|
|
||||||
func HandleStatus(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
|
||||||
|
|
||||||
// Simply return the current server status
|
|
||||||
return rove.StatusResponse{
|
|
||||||
Ready: true,
|
|
||||||
Version: version.Version,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleRegister handles /register endpoint
|
|
||||||
func HandleRegister(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
|
||||||
var response = rove.RegisterResponse{
|
|
||||||
Success: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode the registration info, verify it and register the account
|
|
||||||
var data rove.RegisterData
|
|
||||||
err := json.NewDecoder(b).Decode(&data)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
|
||||||
response.Error = err.Error()
|
|
||||||
|
|
||||||
} else if len(data.Name) == 0 {
|
|
||||||
response.Error = "Cannot register empty name"
|
|
||||||
|
|
||||||
} else if acc, err := s.accountant.RegisterAccount(data.Name); err != nil {
|
|
||||||
response.Error = err.Error()
|
|
||||||
|
|
||||||
} else {
|
|
||||||
response.Id = acc.Id.String()
|
|
||||||
response.Success = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleSpawn will spawn the player entity for the associated account
|
|
||||||
func HandleSpawn(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
|
||||||
var response = rove.SpawnResponse{
|
|
||||||
Success: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
id := vars["account"]
|
|
||||||
|
|
||||||
// Decode the spawn info, verify it and spawn the rover for this account
|
|
||||||
var data rove.SpawnData
|
|
||||||
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
|
||||||
response.Error = err.Error()
|
|
||||||
|
|
||||||
} else if len(id) == 0 {
|
|
||||||
response.Error = "No account ID provided"
|
|
||||||
|
|
||||||
} else if id, err := uuid.Parse(id); err != nil {
|
|
||||||
response.Error = "Provided account ID was invalid"
|
|
||||||
|
|
||||||
} else if pos, _, err := s.SpawnRoverForAccount(id); err != nil {
|
|
||||||
response.Error = err.Error()
|
|
||||||
|
|
||||||
} else {
|
|
||||||
response.Success = true
|
|
||||||
response.Position = pos
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleSpawn will spawn the player entity for the associated account
|
|
||||||
func HandleCommand(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
|
||||||
var response = rove.CommandResponse{
|
|
||||||
Success: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
id := vars["account"]
|
|
||||||
|
|
||||||
// Decode the commands, verify them and the account, and execute the commands
|
|
||||||
var data rove.CommandData
|
|
||||||
if err := json.NewDecoder(b).Decode(&data); err != nil {
|
|
||||||
fmt.Printf("Failed to decode json: %s\n", err)
|
|
||||||
response.Error = err.Error()
|
|
||||||
|
|
||||||
} else if len(id) == 0 {
|
|
||||||
response.Error = "No account ID provided"
|
|
||||||
|
|
||||||
} else if id, err := uuid.Parse(id); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Provided account ID was invalid: %s", err)
|
|
||||||
|
|
||||||
} else if inst, err := s.accountant.GetRover(id); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
|
||||||
|
|
||||||
} else if cmds, err := s.ConvertCommands(data.Commands, inst); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Couldn't convert commands: %s", err)
|
|
||||||
|
|
||||||
} else if err := s.world.Execute(cmds...); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Failed to execute commands: %s", err)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
response.Success = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleRadar handles the radar request
|
|
||||||
func HandleRadar(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
|
||||||
var response = rove.RadarResponse{
|
|
||||||
Success: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
id := vars["account"]
|
|
||||||
if len(id) == 0 {
|
|
||||||
response.Error = "No account ID provided"
|
|
||||||
|
|
||||||
} else if id, err := uuid.Parse(id); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Provided account ID was invalid: %s", err)
|
|
||||||
|
|
||||||
} else if inst, err := s.accountant.GetRover(id); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
|
||||||
|
|
||||||
} else if radar, err := s.world.RadarFromRover(inst); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Error getting radar from rover: %s", err)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
response.Rovers = radar.Rovers
|
|
||||||
response.Success = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// HandleRover handles the rover request
|
|
||||||
func HandleRover(s *Server, vars map[string]string, b io.ReadCloser, w io.Writer) (interface{}, error) {
|
|
||||||
var response = rove.RoverResponse{
|
|
||||||
Success: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
id := vars["account"]
|
|
||||||
if len(id) == 0 {
|
|
||||||
response.Error = "No account ID provided"
|
|
||||||
|
|
||||||
} else if id, err := uuid.Parse(id); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Provided account ID was invalid: %s", err)
|
|
||||||
|
|
||||||
} else if inst, err := s.accountant.GetRover(id); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Provided account has no rover: %s", err)
|
|
||||||
|
|
||||||
} else if pos, err := s.world.RoverPosition(inst); err != nil {
|
|
||||||
response.Error = fmt.Sprintf("Error getting radar from rover: %s", err)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
response.Position = pos
|
|
||||||
response.Success = true
|
|
||||||
}
|
|
||||||
|
|
||||||
return response, nil
|
|
||||||
}
|
|
|
@ -1,181 +0,0 @@
|
||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"path"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/mdiluz/rove/pkg/game"
|
|
||||||
"github.com/mdiluz/rove/pkg/rove"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHandleStatus(t *testing.T) {
|
|
||||||
|
|
||||||
request, _ := http.NewRequest(http.MethodGet, "/status", nil)
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
|
|
||||||
s := NewServer()
|
|
||||||
s.Initialise()
|
|
||||||
s.router.ServeHTTP(response, request)
|
|
||||||
assert.Equal(t, http.StatusOK, response.Code)
|
|
||||||
|
|
||||||
var status rove.StatusResponse
|
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
|
||||||
|
|
||||||
if status.Ready != true {
|
|
||||||
t.Errorf("got false for /status")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(status.Version) == 0 {
|
|
||||||
t.Errorf("got empty version info")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleRegister(t *testing.T) {
|
|
||||||
data := rove.RegisterData{Name: "one"}
|
|
||||||
b, err := json.Marshal(data)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
request, _ := http.NewRequest(http.MethodPost, "/register", bytes.NewReader(b))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
|
|
||||||
s := NewServer()
|
|
||||||
s.Initialise()
|
|
||||||
s.router.ServeHTTP(response, request)
|
|
||||||
assert.Equal(t, http.StatusOK, response.Code)
|
|
||||||
|
|
||||||
var status rove.RegisterResponse
|
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
|
||||||
|
|
||||||
if status.Success != true {
|
|
||||||
t.Errorf("got false for /register")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleSpawn(t *testing.T) {
|
|
||||||
s := NewServer()
|
|
||||||
s.Initialise()
|
|
||||||
a, err := s.accountant.RegisterAccount("test")
|
|
||||||
assert.NoError(t, err, "Error registering account")
|
|
||||||
data := rove.SpawnData{}
|
|
||||||
|
|
||||||
b, err := json.Marshal(data)
|
|
||||||
assert.NoError(t, err, "Error marshalling data")
|
|
||||||
|
|
||||||
request, _ := http.NewRequest(http.MethodPost, path.Join("/", a.Id.String(), "/spawn"), bytes.NewReader(b))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
|
|
||||||
s.router.ServeHTTP(response, request)
|
|
||||||
assert.Equal(t, http.StatusOK, response.Code)
|
|
||||||
|
|
||||||
var status rove.SpawnResponse
|
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
|
||||||
assert.Equal(t, http.StatusOK, response.Code)
|
|
||||||
|
|
||||||
if status.Success != true {
|
|
||||||
t.Errorf("got false for /spawn: %s", status.Error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleCommand(t *testing.T) {
|
|
||||||
s := NewServer()
|
|
||||||
s.Initialise()
|
|
||||||
a, err := s.accountant.RegisterAccount("test")
|
|
||||||
assert.NoError(t, err, "Error registering account")
|
|
||||||
|
|
||||||
// Spawn the rover rover for the account
|
|
||||||
_, inst, err := s.SpawnRoverForAccount(a.Id)
|
|
||||||
|
|
||||||
pos, err := s.world.RoverPosition(inst)
|
|
||||||
assert.NoError(t, err, "Couldn't get rover position")
|
|
||||||
|
|
||||||
data := rove.CommandData{
|
|
||||||
Commands: []rove.Command{
|
|
||||||
{
|
|
||||||
Command: rove.CommandMove,
|
|
||||||
Bearing: "N",
|
|
||||||
Duration: 1,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := json.Marshal(data)
|
|
||||||
assert.NoError(t, err, "Error marshalling data")
|
|
||||||
|
|
||||||
request, _ := http.NewRequest(http.MethodPost, path.Join("/", a.Id.String(), "/command"), bytes.NewReader(b))
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
|
|
||||||
s.router.ServeHTTP(response, request)
|
|
||||||
assert.Equal(t, http.StatusOK, response.Code)
|
|
||||||
|
|
||||||
var status rove.CommandResponse
|
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
|
||||||
|
|
||||||
if status.Success != true {
|
|
||||||
t.Errorf("got false for /command: %s", status.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
attrib, err := s.world.RoverAttributes(inst)
|
|
||||||
assert.NoError(t, err, "Couldn't get rover attribs")
|
|
||||||
|
|
||||||
pos2, err := s.world.RoverPosition(inst)
|
|
||||||
assert.NoError(t, err, "Couldn't get rover position")
|
|
||||||
pos.Add(game.Vector{X: 0.0, Y: attrib.Speed * 1}) // Should have moved north by the speed and duration
|
|
||||||
assert.Equal(t, pos, pos2, "Rover should have moved by bearing")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleRadar(t *testing.T) {
|
|
||||||
s := NewServer()
|
|
||||||
s.Initialise()
|
|
||||||
a, err := s.accountant.RegisterAccount("test")
|
|
||||||
assert.NoError(t, err, "Error registering account")
|
|
||||||
|
|
||||||
// Spawn the rover rover for the account
|
|
||||||
_, _, err = s.SpawnRoverForAccount(a.Id)
|
|
||||||
|
|
||||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Id.String(), "/radar"), nil)
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
|
|
||||||
s.router.ServeHTTP(response, request)
|
|
||||||
assert.Equal(t, http.StatusOK, response.Code)
|
|
||||||
|
|
||||||
var status rove.RadarResponse
|
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
|
||||||
|
|
||||||
if status.Success != true {
|
|
||||||
t.Errorf("got false for /radar: %s", status.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Verify the radar information
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleRover(t *testing.T) {
|
|
||||||
s := NewServer()
|
|
||||||
s.Initialise()
|
|
||||||
a, err := s.accountant.RegisterAccount("test")
|
|
||||||
assert.NoError(t, err, "Error registering account")
|
|
||||||
|
|
||||||
// Spawn the rover rover for the account
|
|
||||||
_, _, err = s.SpawnRoverForAccount(a.Id)
|
|
||||||
|
|
||||||
request, _ := http.NewRequest(http.MethodGet, path.Join("/", a.Id.String(), "/rover"), nil)
|
|
||||||
response := httptest.NewRecorder()
|
|
||||||
|
|
||||||
s.router.ServeHTTP(response, request)
|
|
||||||
assert.Equal(t, http.StatusOK, response.Code)
|
|
||||||
|
|
||||||
var status rove.RoverResponse
|
|
||||||
json.NewDecoder(response.Body).Decode(&status)
|
|
||||||
|
|
||||||
if status.Success != true {
|
|
||||||
t.Errorf("got false for /rover: %s", status.Error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Verify the radar information
|
|
||||||
}
|
|
|
@ -1,200 +0,0 @@
|
||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/mdiluz/rove/pkg/accounts"
|
|
||||||
"github.com/mdiluz/rove/pkg/game"
|
|
||||||
"github.com/mdiluz/rove/pkg/persistence"
|
|
||||||
"github.com/mdiluz/rove/pkg/rove"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// PersistentData will allow the server to load and save it's state
|
|
||||||
PersistentData = iota
|
|
||||||
|
|
||||||
// EphemeralData will let the server neither load or save out any of it's data
|
|
||||||
EphemeralData
|
|
||||||
)
|
|
||||||
|
|
||||||
// Server contains the relevant data to run a game server
|
|
||||||
type Server struct {
|
|
||||||
port int
|
|
||||||
|
|
||||||
accountant *accounts.Accountant
|
|
||||||
world *game.World
|
|
||||||
|
|
||||||
server *http.Server
|
|
||||||
router *mux.Router
|
|
||||||
|
|
||||||
persistence int
|
|
||||||
|
|
||||||
sync sync.WaitGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServerOption defines a server creation option
|
|
||||||
type ServerOption func(s *Server)
|
|
||||||
|
|
||||||
// OptionPort sets the server port for hosting
|
|
||||||
func OptionPort(port int) ServerOption {
|
|
||||||
return func(s *Server) {
|
|
||||||
s.port = port
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// OptionPersistentData sets the server data to be persistent
|
|
||||||
func OptionPersistentData() ServerOption {
|
|
||||||
return func(s *Server) {
|
|
||||||
s.persistence = PersistentData
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewServer sets up a new server
|
|
||||||
func NewServer(opts ...ServerOption) *Server {
|
|
||||||
|
|
||||||
router := mux.NewRouter().StrictSlash(true)
|
|
||||||
|
|
||||||
// Set up the default server
|
|
||||||
s := &Server{
|
|
||||||
port: 8080,
|
|
||||||
persistence: EphemeralData,
|
|
||||||
router: router,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply all options
|
|
||||||
for _, o := range opts {
|
|
||||||
o(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the server object
|
|
||||||
s.server = &http.Server{Addr: fmt.Sprintf(":%d", s.port), Handler: router}
|
|
||||||
|
|
||||||
// Create the accountant
|
|
||||||
s.accountant = accounts.NewAccountant()
|
|
||||||
s.world = game.NewWorld()
|
|
||||||
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialise sets up internal state ready to serve
|
|
||||||
func (s *Server) Initialise() error {
|
|
||||||
|
|
||||||
// Load the accounts if requested
|
|
||||||
if s.persistence == PersistentData {
|
|
||||||
if err := persistence.LoadAll("accounts", &s.accountant, "world", &s.world); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up the handlers
|
|
||||||
for _, route := range Routes {
|
|
||||||
s.router.HandleFunc(route.path, s.wrapHandler(route.method, route.handler))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add to our sync
|
|
||||||
s.sync.Add(1)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run executes the server
|
|
||||||
func (s *Server) Run() {
|
|
||||||
defer s.sync.Done()
|
|
||||||
|
|
||||||
// Listen and serve the http requests
|
|
||||||
fmt.Printf("Serving HTTP on port %d\n", s.port)
|
|
||||||
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes up the server
|
|
||||||
func (s *Server) Close() error {
|
|
||||||
// Try and shut down the http server
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
||||||
defer cancel()
|
|
||||||
if err := s.server.Shutdown(ctx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait until the server is shut down
|
|
||||||
s.sync.Wait()
|
|
||||||
|
|
||||||
// Save the accounts if requested
|
|
||||||
if s.persistence == PersistentData {
|
|
||||||
if err := persistence.SaveAll("accounts", s.accountant, "world", s.world); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// wrapHandler wraps a request handler in http checks
|
|
||||||
func (s *Server) wrapHandler(method string, handler Handler) func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
// Log the request
|
|
||||||
fmt.Printf("%s\t%s\n", r.Method, r.RequestURI)
|
|
||||||
|
|
||||||
vars := mux.Vars(r)
|
|
||||||
|
|
||||||
// Verify the method, call the handler, and encode the return
|
|
||||||
if r.Method != method {
|
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
||||||
|
|
||||||
} else if val, err := handler(s, vars, r.Body, w); err != nil {
|
|
||||||
fmt.Printf("Failed to handle http request: %s", err)
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
|
|
||||||
} else if err := json.NewEncoder(w).Encode(val); err != nil {
|
|
||||||
fmt.Printf("Failed to encode return to json: %s", err)
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SpawnRoverForAccount spawns the rover rover for an account
|
|
||||||
func (s *Server) SpawnRoverForAccount(accountid uuid.UUID) (game.Vector, uuid.UUID, error) {
|
|
||||||
inst := s.world.SpawnRover()
|
|
||||||
if pos, err := s.world.RoverPosition(inst); err != nil {
|
|
||||||
return game.Vector{}, uuid.UUID{}, fmt.Errorf("No position found for created rover")
|
|
||||||
|
|
||||||
} else {
|
|
||||||
if err := s.accountant.AssignRover(accountid, inst); err != nil {
|
|
||||||
// Try and clear up the rover
|
|
||||||
if err := s.world.DestroyRover(inst); err != nil {
|
|
||||||
fmt.Printf("Failed to destroy rover after failed rover assign: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return game.Vector{}, uuid.UUID{}, err
|
|
||||||
} else {
|
|
||||||
return pos, inst, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConvertCommands converts server commands to game commands
|
|
||||||
func (s *Server) ConvertCommands(commands []rove.Command, inst uuid.UUID) ([]game.Command, error) {
|
|
||||||
var cmds []game.Command
|
|
||||||
for _, c := range commands {
|
|
||||||
switch c.Command {
|
|
||||||
case rove.CommandMove:
|
|
||||||
if bearing, err := game.DirectionFromString(c.Bearing); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else {
|
|
||||||
cmds = append(cmds, s.world.CommandMove(inst, bearing, c.Duration))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cmds, nil
|
|
||||||
}
|
|
|
@ -1,3 +1,4 @@
|
||||||
package version
|
package version
|
||||||
|
|
||||||
|
// Version represents a version to be overrided with -ldflags
|
||||||
var Version = "undefined"
|
var Version = "undefined"
|
||||||
|
|
2202
proto/roveapi/roveapi.pb.go
Normal file
2202
proto/roveapi/roveapi.pb.go
Normal file
File diff suppressed because it is too large
Load diff
322
proto/roveapi/roveapi.proto
Normal file
322
proto/roveapi/roveapi.proto
Normal file
|
@ -0,0 +1,322 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
// Rove
|
||||||
|
//
|
||||||
|
// Rove is an asychronous nomadic game about exploring a planet as part of a
|
||||||
|
// loose community
|
||||||
|
package roveapi;
|
||||||
|
option go_package = "github.com/mdiluz/rove/proto/roveapi";
|
||||||
|
|
||||||
|
// The Rove server hosts a single game session and world with multiple players
|
||||||
|
service Rove {
|
||||||
|
// Server status
|
||||||
|
// Responds with various details about the current server status
|
||||||
|
rpc ServerStatus(ServerStatusRequest) returns (ServerStatusResponse) {}
|
||||||
|
|
||||||
|
// Register an account
|
||||||
|
// Tries to register an account with the given name
|
||||||
|
rpc Register(RegisterRequest) returns (RegisterResponse) {}
|
||||||
|
|
||||||
|
// Send commands to rover
|
||||||
|
// Sending commands to this endpoint will queue them to be executed during the
|
||||||
|
// following ticks, in the order sent. Commands sent within the same tick will
|
||||||
|
// overwrite until the tick has finished and the commands are queued
|
||||||
|
rpc Command(CommandRequest) returns (CommandResponse) {}
|
||||||
|
|
||||||
|
// Get radar information
|
||||||
|
// Gets the radar output for the given rover
|
||||||
|
rpc Radar(RadarRequest) returns (RadarResponse) {}
|
||||||
|
|
||||||
|
// Get rover information
|
||||||
|
// Gets information for the account's rover
|
||||||
|
rpc Status(StatusRequest) returns (StatusResponse) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// ServerStatus
|
||||||
|
//
|
||||||
|
|
||||||
|
// ServerStatusRequest is an empty placeholder
|
||||||
|
message ServerStatusRequest {}
|
||||||
|
|
||||||
|
// ServerStatusResponse is a response with useful server information
|
||||||
|
message ServerStatusResponse {
|
||||||
|
// The version of the server in v{major}.{minor}-{delta}-{sha} form
|
||||||
|
string version = 1;
|
||||||
|
|
||||||
|
// Whether the server is ready to accept requests
|
||||||
|
bool ready = 2;
|
||||||
|
|
||||||
|
// The tick rate of the server in minutes (how many minutes per tick)
|
||||||
|
int32 tickRate = 3;
|
||||||
|
|
||||||
|
// The current tick of the server
|
||||||
|
int32 currentTick = 4;
|
||||||
|
|
||||||
|
// The time the next tick will occur
|
||||||
|
string next_tick = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Register
|
||||||
|
//
|
||||||
|
|
||||||
|
// RegisterRequest contains data to register an account
|
||||||
|
message RegisterRequest {
|
||||||
|
// The desired account name
|
||||||
|
string name = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Account describes a registered account
|
||||||
|
message Account {
|
||||||
|
// The account name
|
||||||
|
string name = 1;
|
||||||
|
|
||||||
|
// The account secret value, given when creating the account
|
||||||
|
string secret = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// RegisterResponse is the response given to registering an account
|
||||||
|
message RegisterResponse {
|
||||||
|
// The registered account information
|
||||||
|
Account account = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Command
|
||||||
|
//
|
||||||
|
|
||||||
|
// CommandType defines the type of a command to give to the rover
|
||||||
|
enum CommandType {
|
||||||
|
none = 0;
|
||||||
|
// Waits before performing the next command
|
||||||
|
wait = 1;
|
||||||
|
// Toggles the sails, either catching the wind, or charging from the sun
|
||||||
|
toggle = 2;
|
||||||
|
// Turns the rover in the specified bearing (requires bearing)
|
||||||
|
turn = 3;
|
||||||
|
// Stashes item at current location in rover inventory
|
||||||
|
stash = 4;
|
||||||
|
// Repairs the rover using an inventory object
|
||||||
|
repair = 5;
|
||||||
|
// Broadcasts a message to nearby rovers (requires data)
|
||||||
|
broadcast = 6;
|
||||||
|
// Salvages a neighboring dormant rover for parts
|
||||||
|
salvage = 7;
|
||||||
|
// Transfers remote control into dormant rover
|
||||||
|
transfer = 8;
|
||||||
|
// Upgrades a chosen rover specification using 5 rover parts
|
||||||
|
upgrade = 9;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bearing represents a compass direction
|
||||||
|
enum Bearing {
|
||||||
|
// BearingUnknown an unknown invalid bearing
|
||||||
|
BearingUnknown = 0;
|
||||||
|
North = 1;
|
||||||
|
NorthEast = 2;
|
||||||
|
East = 3;
|
||||||
|
SouthEast = 4;
|
||||||
|
South = 5;
|
||||||
|
SouthWest = 6;
|
||||||
|
West = 7;
|
||||||
|
NorthWest = 8;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Describes the type of upgrade
|
||||||
|
enum RoverUpgrade {
|
||||||
|
RoverUpgradeUnknown = 0;
|
||||||
|
Range = 1;
|
||||||
|
Capacity = 2;
|
||||||
|
MaximumIntegrity = 3;
|
||||||
|
MaximumCharge = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command is a single command for a rover
|
||||||
|
message Command {
|
||||||
|
// The command type
|
||||||
|
CommandType command = 1;
|
||||||
|
|
||||||
|
// The number of times to repeat the command after the first
|
||||||
|
int32 repeat = 2;
|
||||||
|
|
||||||
|
// broadcast - a simple message, must be composed of up to 3 printable ASCII
|
||||||
|
// glyphs (32-126)
|
||||||
|
bytes data = 3;
|
||||||
|
|
||||||
|
// move - the bearing for the rover to turn to
|
||||||
|
Bearing bearing = 4;
|
||||||
|
|
||||||
|
// upgrade - the upgrade to apply to the rover
|
||||||
|
RoverUpgrade upgrade = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandRequest describes a set of commands to be requested for the rover
|
||||||
|
message CommandRequest {
|
||||||
|
// The account to execute these commands
|
||||||
|
Account account = 1;
|
||||||
|
|
||||||
|
// The set of desired commands
|
||||||
|
repeated Command commands = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandResponse is an empty placeholder
|
||||||
|
message CommandResponse {}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Radar
|
||||||
|
//
|
||||||
|
|
||||||
|
// Types of objects
|
||||||
|
enum Object {
|
||||||
|
// ObjectUnknown represents no object at all
|
||||||
|
ObjectUnknown = 0;
|
||||||
|
|
||||||
|
// RoverLive represents a live rover
|
||||||
|
RoverLive = 1;
|
||||||
|
|
||||||
|
// RoverDormant describes a dormant rover
|
||||||
|
RoverDormant = 2;
|
||||||
|
|
||||||
|
// RockSmall is a small stashable rock
|
||||||
|
RockSmall = 3;
|
||||||
|
|
||||||
|
// RockLarge is a large blocking rock
|
||||||
|
RockLarge = 4;
|
||||||
|
|
||||||
|
// RoverParts is one unit of rover parts, used for repairing and fixing the
|
||||||
|
// rover
|
||||||
|
RoverParts = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Tile {
|
||||||
|
// TileUnknown is a keyword for nothing
|
||||||
|
TileUnknown = 0;
|
||||||
|
|
||||||
|
// Rock is solid rock ground
|
||||||
|
Rock = 1;
|
||||||
|
|
||||||
|
// Gravel is loose rocks
|
||||||
|
Gravel = 2;
|
||||||
|
|
||||||
|
// Sand is sand
|
||||||
|
Sand = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// RadarRequest is the data needed to request the radar for a rover
|
||||||
|
message RadarRequest {
|
||||||
|
// The account for this request
|
||||||
|
Account account = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// RadarResponse describes radar information
|
||||||
|
message RadarResponse {
|
||||||
|
// The range in tiles from the rover of the radar data
|
||||||
|
int32 range = 1;
|
||||||
|
|
||||||
|
// A 1D array representing range*2 + 1 squared set of tiles, origin bottom
|
||||||
|
// left and in row->column order
|
||||||
|
repeated Tile tiles = 2;
|
||||||
|
|
||||||
|
// A similar array to the tile array, but containing objects
|
||||||
|
repeated Object objects = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Status
|
||||||
|
//
|
||||||
|
|
||||||
|
// StatusRequest is information needed to request rover status
|
||||||
|
message StatusRequest {
|
||||||
|
// The account for this request
|
||||||
|
Account account = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log is a single log item
|
||||||
|
message Log {
|
||||||
|
// The unix timestamp of the log
|
||||||
|
string time = 1;
|
||||||
|
|
||||||
|
// The text of the log
|
||||||
|
string text = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Vector describes a point or vector in 2D space
|
||||||
|
message Vector {
|
||||||
|
int32 x = 1;
|
||||||
|
int32 y = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// SailPosition represents the position of the sola sail
|
||||||
|
enum SailPosition {
|
||||||
|
UnknownSailPosition = 0;
|
||||||
|
|
||||||
|
// CatchingWind means the sail is catching the wind and moving the rover
|
||||||
|
CatchingWind = 1;
|
||||||
|
|
||||||
|
// SolarCharging means the sail is facing the sun and charging
|
||||||
|
SolarCharging = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message RoverSpecifications {
|
||||||
|
|
||||||
|
// The name of the rover
|
||||||
|
string name = 1;
|
||||||
|
|
||||||
|
// The range of this rover's radar and broadcasting
|
||||||
|
int32 range = 2;
|
||||||
|
|
||||||
|
// The capacity of the inventory
|
||||||
|
int32 capacity = 3;
|
||||||
|
|
||||||
|
// The maximum health of the rover
|
||||||
|
int32 maximumIntegrity = 4;
|
||||||
|
|
||||||
|
// The max energy the rover can store
|
||||||
|
int32 maximumCharge = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
message RoverStatus {
|
||||||
|
|
||||||
|
// The current direction of the rover
|
||||||
|
Bearing bearing = 1;
|
||||||
|
|
||||||
|
// The current position of the sails
|
||||||
|
SailPosition sailPosition = 2;
|
||||||
|
|
||||||
|
// The items in the rover inventory
|
||||||
|
bytes inventory = 3;
|
||||||
|
|
||||||
|
// The current health of the rover
|
||||||
|
int32 integrity = 4;
|
||||||
|
|
||||||
|
// The energy stored in the rover
|
||||||
|
int32 charge = 5;
|
||||||
|
|
||||||
|
// The set of currently queued commands
|
||||||
|
repeated Command queuedCommands = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
message RoverReadings {
|
||||||
|
// Position of the rover in world coordinates
|
||||||
|
Vector position = 1;
|
||||||
|
|
||||||
|
// The current wind direction
|
||||||
|
Bearing wind = 2;
|
||||||
|
|
||||||
|
// The most recent logs
|
||||||
|
repeated Log logs = 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// StatusResponse is the response given to a status request
|
||||||
|
message StatusResponse {
|
||||||
|
|
||||||
|
// The static rover information
|
||||||
|
RoverSpecifications spec = 1;
|
||||||
|
|
||||||
|
// Current rover status
|
||||||
|
RoverStatus status = 2;
|
||||||
|
|
||||||
|
// Current rover readings
|
||||||
|
RoverReadings readings = 3;
|
||||||
|
}
|
|
@ -1,19 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
cd "$(dirname "$0")"
|
|
||||||
cd ..
|
|
||||||
set -x
|
|
||||||
|
|
||||||
# Check we can build everything
|
|
||||||
go mod download
|
|
||||||
go build ./...
|
|
||||||
|
|
||||||
# Run the server and shut it down again to ensure our docker-compose works
|
|
||||||
docker-compose up --detach --build
|
|
||||||
docker-compose down
|
|
||||||
|
|
||||||
# Run tests with coverage
|
|
||||||
go test -v ./... -cover -coverprofile=/tmp/c.out
|
|
||||||
|
|
||||||
# Convert the coverage data to html
|
|
||||||
go tool cover -html=/tmp/c.out -o /tmp/coverage.html
|
|
202
script/wait-for-it.sh
Executable file
202
script/wait-for-it.sh
Executable file
|
@ -0,0 +1,202 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
# The MIT License (MIT)
|
||||||
|
# Copyright (c) 2016 Giles Hall
|
||||||
|
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
# this software and associated documentation files (the "Software"), to deal in
|
||||||
|
# the Software without restriction, including without limitation the rights to
|
||||||
|
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
# of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
# so, subject to the following conditions:
|
||||||
|
|
||||||
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
|
# copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
# Use this script to test if a given TCP host/port are available
|
||||||
|
|
||||||
|
WAITFORIT_cmdname=${0##*/}
|
||||||
|
|
||||||
|
echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi }
|
||||||
|
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
cat << USAGE >&2
|
||||||
|
Usage:
|
||||||
|
$WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args]
|
||||||
|
-h HOST | --host=HOST Host or IP under test
|
||||||
|
-p PORT | --port=PORT TCP port under test
|
||||||
|
Alternatively, you specify the host and port as host:port
|
||||||
|
-s | --strict Only execute subcommand if the test succeeds
|
||||||
|
-q | --quiet Don't output any status messages
|
||||||
|
-t TIMEOUT | --timeout=TIMEOUT
|
||||||
|
Timeout in seconds, zero for no timeout
|
||||||
|
-- COMMAND ARGS Execute command with args after the test finishes
|
||||||
|
USAGE
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_for()
|
||||||
|
{
|
||||||
|
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
|
||||||
|
echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
|
||||||
|
else
|
||||||
|
echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout"
|
||||||
|
fi
|
||||||
|
WAITFORIT_start_ts=$(date +%s)
|
||||||
|
while :
|
||||||
|
do
|
||||||
|
if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then
|
||||||
|
nc -z $WAITFORIT_HOST $WAITFORIT_PORT
|
||||||
|
WAITFORIT_result=$?
|
||||||
|
else
|
||||||
|
(echo > /dev/tcp/$WAITFORIT_HOST/$WAITFORIT_PORT) >/dev/null 2>&1
|
||||||
|
WAITFORIT_result=$?
|
||||||
|
fi
|
||||||
|
if [[ $WAITFORIT_result -eq 0 ]]; then
|
||||||
|
WAITFORIT_end_ts=$(date +%s)
|
||||||
|
echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((WAITFORIT_end_ts - WAITFORIT_start_ts)) seconds"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
return $WAITFORIT_result
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_for_wrapper()
|
||||||
|
{
|
||||||
|
# In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692
|
||||||
|
if [[ $WAITFORIT_QUIET -eq 1 ]]; then
|
||||||
|
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --quiet --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
|
||||||
|
else
|
||||||
|
timeout $WAITFORIT_BUSYTIMEFLAG $WAITFORIT_TIMEOUT $0 --child --host=$WAITFORIT_HOST --port=$WAITFORIT_PORT --timeout=$WAITFORIT_TIMEOUT &
|
||||||
|
fi
|
||||||
|
WAITFORIT_PID=$!
|
||||||
|
trap "kill -INT -$WAITFORIT_PID" INT
|
||||||
|
wait $WAITFORIT_PID
|
||||||
|
WAITFORIT_RESULT=$?
|
||||||
|
if [[ $WAITFORIT_RESULT -ne 0 ]]; then
|
||||||
|
echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT"
|
||||||
|
fi
|
||||||
|
return $WAITFORIT_RESULT
|
||||||
|
}
|
||||||
|
|
||||||
|
# process arguments
|
||||||
|
while [[ $# -gt 0 ]]
|
||||||
|
do
|
||||||
|
case "$1" in
|
||||||
|
*:* )
|
||||||
|
WAITFORIT_hostport=(${1//:/ })
|
||||||
|
WAITFORIT_HOST=${WAITFORIT_hostport[0]}
|
||||||
|
WAITFORIT_PORT=${WAITFORIT_hostport[1]}
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
--child)
|
||||||
|
WAITFORIT_CHILD=1
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
-q | --quiet)
|
||||||
|
WAITFORIT_QUIET=1
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
-s | --strict)
|
||||||
|
WAITFORIT_STRICT=1
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
-h)
|
||||||
|
WAITFORIT_HOST="$2"
|
||||||
|
if [[ $WAITFORIT_HOST == "" ]]; then break; fi
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
--host=*)
|
||||||
|
WAITFORIT_HOST="${1#*=}"
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
-p)
|
||||||
|
WAITFORIT_PORT="$2"
|
||||||
|
if [[ $WAITFORIT_PORT == "" ]]; then break; fi
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
--port=*)
|
||||||
|
WAITFORIT_PORT="${1#*=}"
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
-t)
|
||||||
|
WAITFORIT_TIMEOUT="$2"
|
||||||
|
if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
--timeout=*)
|
||||||
|
WAITFORIT_TIMEOUT="${1#*=}"
|
||||||
|
shift 1
|
||||||
|
;;
|
||||||
|
--)
|
||||||
|
shift
|
||||||
|
WAITFORIT_CLI=("$@")
|
||||||
|
break
|
||||||
|
;;
|
||||||
|
--help)
|
||||||
|
usage
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echoerr "Unknown argument: $1"
|
||||||
|
usage
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then
|
||||||
|
echoerr "Error: you need to provide a host and port to test."
|
||||||
|
usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15}
|
||||||
|
WAITFORIT_STRICT=${WAITFORIT_STRICT:-0}
|
||||||
|
WAITFORIT_CHILD=${WAITFORIT_CHILD:-0}
|
||||||
|
WAITFORIT_QUIET=${WAITFORIT_QUIET:-0}
|
||||||
|
|
||||||
|
# Check to see if timeout is from busybox?
|
||||||
|
WAITFORIT_TIMEOUT_PATH=$(type -p timeout)
|
||||||
|
WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH)
|
||||||
|
|
||||||
|
WAITFORIT_BUSYTIMEFLAG=""
|
||||||
|
if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then
|
||||||
|
WAITFORIT_ISBUSY=1
|
||||||
|
# Check if busybox timeout uses -t flag
|
||||||
|
# (recent Alpine versions don't support -t anymore)
|
||||||
|
if timeout &>/dev/stdout | grep -q -e '-t '; then
|
||||||
|
WAITFORIT_BUSYTIMEFLAG="-t"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
WAITFORIT_ISBUSY=0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $WAITFORIT_CHILD -gt 0 ]]; then
|
||||||
|
wait_for
|
||||||
|
WAITFORIT_RESULT=$?
|
||||||
|
exit $WAITFORIT_RESULT
|
||||||
|
else
|
||||||
|
if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then
|
||||||
|
wait_for_wrapper
|
||||||
|
WAITFORIT_RESULT=$?
|
||||||
|
else
|
||||||
|
wait_for
|
||||||
|
WAITFORIT_RESULT=$?
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ $WAITFORIT_CLI != "" ]]; then
|
||||||
|
if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then
|
||||||
|
echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess"
|
||||||
|
exit $WAITFORIT_RESULT
|
||||||
|
fi
|
||||||
|
exec "${WAITFORIT_CLI[@]}"
|
||||||
|
else
|
||||||
|
exit $WAITFORIT_RESULT
|
||||||
|
fi
|
50
snap/snapcraft.yaml
Normal file
50
snap/snapcraft.yaml
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
name: rove
|
||||||
|
title: Rove
|
||||||
|
base: core20
|
||||||
|
license: MIT
|
||||||
|
architectures: [ amd64 ]
|
||||||
|
icon: data/icon.svg
|
||||||
|
summary: An asynchronous nomadic game about exploring as part of a loose community.
|
||||||
|
# TODO: Add more to the description
|
||||||
|
description: |
|
||||||
|
An asynchronous nomadic game about exploring as part of a loose community.
|
||||||
|
confinement: strict
|
||||||
|
adopt-info: go-rove
|
||||||
|
|
||||||
|
apps:
|
||||||
|
rove:
|
||||||
|
command: bin/rove
|
||||||
|
plugs:
|
||||||
|
- network
|
||||||
|
environment:
|
||||||
|
ROVE_USER_DATA: $SNAP_USER_DATA
|
||||||
|
|
||||||
|
rove-server:
|
||||||
|
command: bin/rove-server
|
||||||
|
plugs:
|
||||||
|
- network
|
||||||
|
- network-bind
|
||||||
|
environment:
|
||||||
|
WORDS_FILE : "$SNAP/data/words_alpha.txt"
|
||||||
|
DATA_PATH : $SNAP_USER_DATA
|
||||||
|
|
||||||
|
parts:
|
||||||
|
go-rove:
|
||||||
|
plugin: go
|
||||||
|
source-type: local
|
||||||
|
source: .
|
||||||
|
build-packages:
|
||||||
|
- gcc-multilib
|
||||||
|
override-pull: |
|
||||||
|
snapcraftctl pull
|
||||||
|
version=$(git describe --always --long --dirty --tags)
|
||||||
|
sed -i "s/undefined/$version/" pkg/version/version.go
|
||||||
|
snapcraftctl set-version $version
|
||||||
|
git describe --exact-match --tags HEAD 2> /dev/null && snapcraftctl set-grade "stable" || snapcraftctl set-grade "devel"
|
||||||
|
|
||||||
|
copy-data:
|
||||||
|
plugin: dump
|
||||||
|
source-type: local
|
||||||
|
source: data
|
||||||
|
organize:
|
||||||
|
'*.txt' : data/
|
9
tools/tools.go
Normal file
9
tools/tools.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
// +build tools
|
||||||
|
|
||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "github.com/golang/protobuf/protoc-gen-go"
|
||||||
|
_ "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway"
|
||||||
|
_ "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger"
|
||||||
|
)
|
Loading…
Add table
Reference in a new issue