Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jochen Bauer
spn-relayserver
Commits
c2c6743c
Commit
c2c6743c
authored
May 06, 2018
by
Hubert Denkmair
Browse files
implement new BotMoveHead and BotStats messages
parent
a4d6a681
Changes
5
Hide whitespace changes
Inline
Side-by-side
relayserver/JsonProtocol.cpp
View file @
c2c6743c
...
...
@@ -22,6 +22,12 @@ void MsgPackProtocol::to_json(nlohmann::json &j, const MsgPackProtocol::Message
case
MESSAGE_TYPE_BOT_MOVE
:
to_json
(
j
,
*
static_cast
<
const
BotMoveMessage
*>
(
&
msg
));
break
;
case
MESSAGE_TYPE_BOT_STATS
:
to_json
(
j
,
*
static_cast
<
const
BotStatsMessage
*>
(
&
msg
));
break
;
case
MESSAGE_TYPE_BOT_MOVE_HEAD
:
to_json
(
j
,
*
static_cast
<
const
BotMoveHeadMessage
*>
(
&
msg
));
break
;
case
MESSAGE_TYPE_FOOD_SPAWN
:
to_json
(
j
,
*
static_cast
<
const
FoodSpawnMessage
*>
(
&
msg
));
break
;
...
...
@@ -146,6 +152,46 @@ void MsgPackProtocol::to_json(nlohmann::json &j, const MsgPackProtocol::BotMoveI
};
}
void
MsgPackProtocol
::
to_json
(
nlohmann
::
json
&
j
,
const
MsgPackProtocol
::
BotStatsMessage
&
msg
)
{
json
data
=
json
::
object
();
for
(
auto
&
item
:
msg
.
items
)
{
data
[
std
::
to_string
(
item
.
bot_id
)]
=
{
{
"n"
,
item
.
natural_food_consumed
},
{
"c"
,
item
.
carrison_food_consumed
},
{
"h"
,
item
.
hunted_food_consumed
}
};
}
j
=
json
{
{
"t"
,
"BotStats"
},
{
"data"
,
data
}
};
}
void
MsgPackProtocol
::
to_json
(
nlohmann
::
json
&
j
,
const
MsgPackProtocol
::
BotMoveHeadMessage
&
msg
)
{
j
=
json
{
{
"t"
,
"BotMoveHead"
},
{
"items"
,
msg
.
items
}
};
}
void
MsgPackProtocol
::
to_json
(
nlohmann
::
json
&
j
,
const
MsgPackProtocol
::
BotMoveHeadItem
&
item
)
{
auto
positions
=
json
::
array
();
for
(
auto
&
pos
:
item
.
new_head_positions
)
{
positions
.
push_back
(
json
::
array_t
{
pos
.
x
(),
pos
.
y
()
});
}
j
=
json
{
{
"bot_id"
,
item
.
bot_id
},
{
"m"
,
item
.
mass
},
{
"p"
,
positions
}
};
}
void
MsgPackProtocol
::
to_json
(
nlohmann
::
json
&
j
,
const
MsgPackProtocol
::
FoodSpawnMessage
&
msg
)
{
j
=
json
{
...
...
relayserver/JsonProtocol.h
View file @
c2c6743c
...
...
@@ -18,6 +18,9 @@ namespace MsgPackProtocol
void
to_json
(
json
&
j
,
const
BotKillMessage
&
msg
);
void
to_json
(
json
&
j
,
const
BotMoveMessage
&
msg
);
void
to_json
(
json
&
j
,
const
BotMoveItem
&
item
);
void
to_json
(
json
&
j
,
const
BotStatsMessage
&
msg
);
void
to_json
(
json
&
j
,
const
BotMoveHeadMessage
&
msg
);
void
to_json
(
json
&
j
,
const
BotMoveHeadItem
&
item
);
void
to_json
(
json
&
j
,
const
FoodSpawnMessage
&
msg
);
void
to_json
(
json
&
j
,
const
FoodConsumeMessage
&
msg
);
void
to_json
(
json
&
j
,
const
FoodConsumeItem
&
item
);
...
...
relayserver/MsgPackProtocol.h
View file @
c2c6743c
...
...
@@ -17,6 +17,8 @@ namespace MsgPackProtocol
MESSAGE_TYPE_BOT_KILL
=
0x21
,
MESSAGE_TYPE_BOT_MOVE
=
0x22
,
MESSAGE_TYPE_BOT_LOG
=
0x23
,
MESSAGE_TYPE_BOT_STATS
=
0x24
,
MESSAGE_TYPE_BOT_MOVE_HEAD
=
0x25
,
MESSAGE_TYPE_FOOD_SPAWN
=
0x30
,
MESSAGE_TYPE_FOOD_CONSUME
=
0x31
,
...
...
@@ -59,6 +61,22 @@ namespace MsgPackProtocol
std
::
string
message
;
};
struct
BotMoveHeadItem
{
guid_t
bot_id
;
double
mass
;
// one head position for each step moved in this frame, in temporal order
std
::
vector
<
Vector2D
>
new_head_positions
;
};
struct
BotStatsItem
{
guid_t
bot_id
;
double
natural_food_consumed
;
double
carrison_food_consumed
;
double
hunted_food_consumed
;
};
struct
Message
{
MessageType
messageType
;
...
...
@@ -130,6 +148,18 @@ namespace MsgPackProtocol
BotLogMessage
()
:
Message
(
MESSAGE_TYPE_BOT_LOG
)
{}
};
struct
BotStatsMessage
:
public
Message
{
std
::
vector
<
BotStatsItem
>
items
;
BotStatsMessage
()
:
Message
(
MESSAGE_TYPE_BOT_STATS
)
{}
};
struct
BotMoveHeadMessage
:
public
Message
{
std
::
vector
<
BotMoveHeadItem
>
items
;
BotMoveHeadMessage
()
:
Message
(
MESSAGE_TYPE_BOT_MOVE_HEAD
)
{}
};
struct
FoodSpawnMessage
:
public
Message
{
std
::
vector
<
FoodItem
>
new_food
;
...
...
@@ -343,6 +373,79 @@ namespace msgpack {
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
BotMoveHeadMessage
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
BotMoveHeadMessage
const
&
v
)
const
{
o
.
pack_array
(
3
);
o
.
pack
(
MsgPackProtocol
::
PROTOCOL_VERSION
);
o
.
pack
(
static_cast
<
int
>
(
MsgPackProtocol
::
MESSAGE_TYPE_BOT_MOVE_HEAD
));
o
.
pack
(
v
.
items
);
return
o
;
}
};
template
<
>
struct
convert
<
MsgPackProtocol
::
BotMoveHeadMessage
>
{
msgpack
::
object
const
&
operator
()(
msgpack
::
object
const
&
o
,
MsgPackProtocol
::
BotMoveHeadMessage
&
v
)
const
{
if
(
o
.
type
!=
msgpack
::
type
::
ARRAY
)
throw
msgpack
::
type_error
();
if
(
o
.
via
.
array
.
size
!=
3
)
throw
msgpack
::
type_error
();
o
.
via
.
array
.
ptr
[
2
]
>>
v
.
items
;
return
o
;
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
BotMoveHeadItem
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
BotMoveHeadItem
const
&
v
)
const
{
o
.
pack_array
(
3
);
o
.
pack
(
v
.
bot_id
);
o
.
pack
(
v
.
mass
);
o
.
pack
(
v
.
new_head_positions
);
return
o
;
}
};
template
<
>
struct
convert
<
MsgPackProtocol
::
BotMoveHeadItem
>
{
msgpack
::
object
const
&
operator
()(
msgpack
::
object
const
&
o
,
MsgPackProtocol
::
BotMoveHeadItem
&
v
)
const
{
if
(
o
.
type
!=
msgpack
::
type
::
ARRAY
)
throw
msgpack
::
type_error
();
if
(
o
.
via
.
array
.
size
!=
3
)
throw
msgpack
::
type_error
();
o
.
via
.
array
.
ptr
[
0
]
>>
v
.
bot_id
;
o
.
via
.
array
.
ptr
[
1
]
>>
v
.
mass
;
o
.
via
.
array
.
ptr
[
2
]
>>
v
.
new_head_positions
;
return
o
;
}
};
template
<
>
struct
pack
<
Vector2D
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
Vector2D
const
&
v
)
const
{
o
.
pack_array
(
2
);
o
.
pack
(
v
.
x
());
o
.
pack
(
v
.
y
());
return
o
;
}
};
template
<
>
struct
convert
<
Vector2D
>
{
msgpack
::
object
const
&
operator
()(
msgpack
::
object
const
&
o
,
Vector2D
&
v
)
const
{
if
(
o
.
type
!=
msgpack
::
type
::
ARRAY
)
throw
msgpack
::
type_error
();
if
(
o
.
via
.
array
.
size
!=
2
)
throw
msgpack
::
type_error
();
v
=
{
o
.
via
.
array
.
ptr
[
0
].
via
.
f64
,
o
.
via
.
array
.
ptr
[
1
].
via
.
f64
};
return
o
;
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
BotKillMessage
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
BotKillMessage
const
&
v
)
const
...
...
@@ -414,6 +517,56 @@ namespace msgpack {
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
BotStatsMessage
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
BotStatsMessage
const
&
v
)
const
{
o
.
pack_array
(
3
);
o
.
pack
(
MsgPackProtocol
::
PROTOCOL_VERSION
);
o
.
pack
(
static_cast
<
int
>
(
MsgPackProtocol
::
MESSAGE_TYPE_BOT_STATS
));
o
.
pack
(
v
.
items
);
return
o
;
}
};
template
<
>
struct
convert
<
MsgPackProtocol
::
BotStatsMessage
>
{
msgpack
::
object
const
&
operator
()(
msgpack
::
object
const
&
o
,
MsgPackProtocol
::
BotStatsMessage
&
v
)
const
{
if
(
o
.
type
!=
msgpack
::
type
::
ARRAY
)
throw
msgpack
::
type_error
();
if
(
o
.
via
.
array
.
size
!=
3
)
throw
msgpack
::
type_error
();
o
.
via
.
array
.
ptr
[
2
]
>>
v
.
items
;
return
o
;
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
BotStatsItem
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
BotStatsItem
const
&
v
)
const
{
o
.
pack_array
(
4
);
o
.
pack
(
v
.
bot_id
);
o
.
pack
(
v
.
natural_food_consumed
);
o
.
pack
(
v
.
carrison_food_consumed
);
o
.
pack
(
v
.
hunted_food_consumed
);
return
o
;
}
};
template
<
>
struct
convert
<
MsgPackProtocol
::
BotStatsItem
>
{
msgpack
::
object
const
&
operator
()(
msgpack
::
object
const
&
o
,
MsgPackProtocol
::
BotStatsItem
&
v
)
const
{
if
(
o
.
type
!=
msgpack
::
type
::
ARRAY
)
throw
msgpack
::
type_error
();
if
(
o
.
via
.
array
.
size
!=
4
)
throw
msgpack
::
type_error
();
o
.
via
.
array
.
ptr
[
0
]
>>
v
.
bot_id
;
o
.
via
.
array
.
ptr
[
1
]
>>
v
.
natural_food_consumed
;
o
.
via
.
array
.
ptr
[
2
]
>>
v
.
carrison_food_consumed
;
o
.
via
.
array
.
ptr
[
3
]
>>
v
.
hunted_food_consumed
;
return
o
;
}
};
template
<
>
struct
pack
<
MsgPackProtocol
::
FoodSpawnMessage
>
{
template
<
typename
Stream
>
msgpack
::
packer
<
Stream
>&
operator
()(
msgpack
::
packer
<
Stream
>&
o
,
MsgPackProtocol
::
FoodSpawnMessage
const
&
v
)
const
...
...
relayserver/TcpProtocol.cpp
View file @
c2c6743c
...
...
@@ -114,6 +114,22 @@ void TcpProtocol::OnMessageReceived(const char* data, size_t count)
break
;
}
case
MsgPackProtocol
::
MESSAGE_TYPE_BOT_STATS
:
{
auto
msg
=
std
::
make_unique
<
MsgPackProtocol
::
BotStatsMessage
>
();
obj
.
get
().
convert
(
*
msg
);
OnBotStatsReceived
(
std
::
move
(
msg
));
break
;
}
case
MsgPackProtocol
::
MESSAGE_TYPE_BOT_MOVE_HEAD
:
{
auto
msg
=
std
::
make_unique
<
MsgPackProtocol
::
BotMoveHeadMessage
>
();
obj
.
get
().
convert
(
*
msg
);
OnBotMoveHeadReceived
(
std
::
move
(
msg
));
break
;
}
case
MsgPackProtocol
::
MESSAGE_TYPE_BOT_LOG
:
{
auto
msg
=
std
::
make_unique
<
MsgPackProtocol
::
BotLogMessage
>
();
...
...
@@ -227,3 +243,13 @@ void TcpProtocol::OnBotLogReceived(std::unique_ptr<MsgPackProtocol::BotLogMessag
_pendingLogItems
[
item
.
viewer_key
].
emplace_back
(
item
);
}
}
void
TcpProtocol
::
OnBotStatsReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotStatsMessage
>
msg
)
{
_pendingMessages
.
push_back
(
std
::
move
(
msg
));
}
void
TcpProtocol
::
OnBotMoveHeadReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotMoveHeadMessage
>
msg
)
{
_pendingMessages
.
push_back
(
std
::
move
(
msg
));
}
relayserver/TcpProtocol.h
View file @
c2c6743c
...
...
@@ -57,4 +57,6 @@ class TcpProtocol
void
OnBotKillReceived
(
const
MsgPackProtocol
::
BotKillMessage
&
msg
);
void
OnBotMoveReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotMoveMessage
>
msg
);
void
OnBotLogReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotLogMessage
>
msg
);
void
OnBotStatsReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotStatsMessage
>
msg
);
void
OnBotMoveHeadReceived
(
std
::
unique_ptr
<
MsgPackProtocol
::
BotMoveHeadMessage
>
msg
);
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment