
 [ AI AVALON CommunicationFormat ]
                          Copyright (c) 2022 Hidetoshi NAGAI
                          Copyright (c) 2022 Kyutech AI AVALON Team

Supported Protocol Version: 0.9.0 and above


Defines the binary format for communication between the server and clients.
This format is also used when selecting binary communication between the
client and agent. (Refer to a separate document [AI AVALON CommunicationString]
for text-based communication format selection.)

":8" represents 8-bit, and ":16" represents 16-bit data in network byte order.
While 4 bits are sufficient for some information, it is processed in byte
units for simplicity.
Specific details and numerical values like "code" are referenced in [AI AVALON
Protocol] and [ ... KeyCode].

Each message format begins with an 8-bit information header.
However, the data at the beginning represents the "message key" and is
defined with an unsigned 7-bit ([AI AVALON KeyCode] for reference), making
the actual initial byte information 7 bits.
For messages with assigned IDs, the first information of protocol's data
list is the 16-bit ID. To maintain consistency with an 8-bit data header,
a non-conflicting 8-bit data is assigned as the header ([AI AVALON KeyCode]
for reference).

The communication format encapsulates a byte sequence constructed based on
the message format, enclosed with header and footer data.


*********************************************
Communication Format
*********************************************
In communication, the server and client exchange roles between sending and
receiving, repeatedly performing the process of "sending a message" followed
by "returning a status in response to the message."

Message Sending:
[0xFF:8][size:16][message][0xFE:8]
# The size represents the total byte count of the message.


*********************************************
Message Format
*********************************************
The structure of the data embedded in [message] in the sending format is as
follows.

Receiving Status: Fixed Length: send/receive
[code:8][status:8]
# status == 0x00: Unknown error
#        == 0x01: Transmission format error (header or footer invalid)
#        == 0x02: Message size error
#        == 0x03: Message error: Unknown code
#        == 0x04: Message error: Inappropriate structure (element count)
#        == 0x05: Message error: Abnormal parameter value
#        == 0x0C: Timeout (communication interrupted before reading the
#                          specified data size)
#        == 0x0D: Timeout (expected message not received)
#        == 0x0E: QUERY received
#        == 0x0F: Successfully received (OK)
#
# Except for "Successfully received" and "QUERY received," if the server
# receives a status from the client, it needs to retry the last message sent.
# Conversely, if the client receives a status from the server, except for
# Timeout, it needs to retry the last message sent. In the case of Timeout,
# it indicates that the communication was interrupted before the data could
# be submitted, so no retransmission is necessary due to default handling
# with no message.

=============================================
exit command: fixed length: receive
[code:8][status:8]
# Upon receiving this command from the server, send the "Successfully
# received" status, then close the communication channel and terminate
# the process with the specified status value.

=============================================
"role" message: fixed length: receive
[code:8][pnum:8][role:8][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"construct" message: fixed length: receive
[code:8][quest:8][cnt:8][trial:8][leader:8]

"require"+"team" message: fixed length: receive
[code:8][code:8][leader:8][cnt:8]

"team" message: fixed length: send
[code:8][leader:8][pow:8][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"team" information message: fixed length: receive
[IDhead:8][ID:16][code:8][quest:8][trial:8][leader:8][pow:8][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"require"+"vote" message: fixed length: receive
[code:8][code:8][pnum:8][ID:16]

"vote" message: fixed length: send
[code:8][pnum:8][ID:16][code:8]

"vote" information message: fixed length: receive
[IDhead:8][ID:16][code:8][pnum:8][ID:16][code:8]

"victroy" message: fixed length: receive
[code:8][code:8]

"quest" message: fixed length: receive
[code:8][quest:8][ID:16][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"require"+"action" message: fixed length: receive
[code:8][code:8][pnum:8][to_fail:8]

"action" message: fixed length: send
[code:8][pnum:8][code:8]

"result" information message: fixed length: send
[IDhead:8][ID:16][code:8][code:8][res:8][quest:8][ID:16][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"assassination" message: fixed length: send
[code:8][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"require"+"assassinate" message: fixed length: receive
[code:8][code:8][pnum:8]

"assassinate" message: fixed length: send
[code:8][pnum:8][pnum:8]

"assassinate" information message: fixed length: receive
[IDhead:8][ID:16][code:8][pnum:8][pnum:8][res:8]

"require"+"quit" message: fixed length: receive
[code:8][code:8][pnum:8]

"quit" message: fixed length: send
[code:8][pnum:8][pow:8]

"quit" information message: fixed length: receive
[IDhead:8][ID:16][code:8][pnum:8][pow:8]

"require"+"claim" message: fixed length: receive
[code:8][code:8][pnum:8][ID:16]

"claim" message: semi-fixed length: send
[code:8][pnum:8][pow:8][ID:16][content:*]
# The number of [content:*] is fixed to one, but the content itself is
# variable length.

"claim" information message: semi-fixed length: receive
[IDhead:8][ID:16][type:8][pnum:8][pow:8][ID:16][content:*]
# The number of [content:*] is fixed to one, but the content itself is
# variable length.

"require"+"request" message: fixed length: receive
[code:8][code:8][pnum:8]

"request" message: fixed length: send
[code:8][pnum:8][pow:8][code:8][ID:16][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"request" information message: fixed length: receive
[IDhead:8][ID:16][code:8][pnum:8][pow:8][code:8][ID:16][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"require"+"inquiry" message: fixed length: receive
[code:8][code:8][pnum:8]

"inquiry" message: fixed length: send
[code:8][pnum:8][pow:8][pnum:8][ID:16]

"inquiry" information message: fixed length: receive
[IDhead:8][ID:16][code:8][pnum:8][pow:8][pnum:8][ID:16]

"require"+"reason" message: fixed length: receive
[code:8][code:8][pnum:8][ID:16]

"reason" message: semi-fixed length: send
[code:8][pnum:8][pow:8][ID:16][content:*]
# The number of [content:*] is fixed to one, but the content itself is
# variable length.

"reason" information message: semi-fixed length: receive
[IDhead:8][ID:16][code:8][pnum:8][pow:8][ID:16][content:*]
# The number of [content:*] is fixed to one, but the content itself is
# variable length.

"voice" message: fixed length: receive
[code:8][pnum:8][status:8][remain:8][depth:8][point:8]

"require"+"interrupt" message: fixed length: receive
[code:8][code:8][pnum:8][rights:8][point:8][increment:8][exchange:8]

"interrupt" message: fixed length: send
[code:8][pnum:8][point:8]

"interrupt" information message: semi-fixed length: receive
[IDhead:8][ID:16][code:8][cnt:8][point:8]...[point:8]
# The number of [point:8] depends on the number of players ([cnt:8]),
# so the message is fixed length per play.

"finish" message: fixed length: send
[code:8][quest:8][trial:8][pnum:8]


=============================================
>> Format of [content:*] in messages such as "claim" message
  (Need to recursively parse the embedded structure)
---------------------------------------------
>> Claim component

"hunch","omit": fixed length
[code:8]

"agree","disagree": fixed length
[code:8][pow:8][ID:16]

"justice","evil",role-name: fixed length
[code:8][pow:8][pnum:8]

"trust","distrust","include","exclude": fixed length
[code:8][pow:8][pnum:8]

"confidence","nonconfidence": fixed length
[code:8][pow:8][pnum:8]

"consistent","inconsistent": fixed length
[code:8][pow:8][ID:16][ID:16]

"succeed","fail","win","lose": fixed length
[code:8][pow:8][ID:16]

"servants","minions": fixed length
[code:8][pow:8][min:8][max:8][pinfo:16]
# [pinfo:16] specifies multiple players and is data with the corresponding
# player number bit set to 1.

"endeavor","obstruct": fixed length
[code:8][pow:8][pnum:8][ID:16]

---------------------------------------------
>> Logical structure

"AT": semi-fixed length
[code:8][ID:16][content:*]
# The number of [content:*] is fixed to one, but the content itself is
# variable length.

"OR","AND": variable length
[code:8][content:*]...[content:*][0x00:8]

"IF"(with else clause): semi-fixed length
[code:8][content:*][content:*][content:*]
# The number of [content:*] is fixed to three, but the length of each
# content itself is variable.

"IF"(without else clause)：semi-fixed length
[code:8][content:*][content:*][0x00:8]
# The number of [content:*] is fixed to two, but the length of each
# content itself is variable.

---------------------------------------------
>> Direct reference

"FACT": fixed length
[code:8][ID:16]

---------------------------------------------
>> Presume structure

"PRESUME": fixed length
[code:8][pow:8][pnum:8][content:*]
# The number of [content:*] is fixed to one, but the content itself is
# variable length.


=============================================
>> Format related to "query"

(to_server) "query"+"get": fixed length
[code:8][code:8][ID:16]

(from_server) "query"+"get": semi-fixed length
[code:8][code:8][quest:8][trial:8][message:*]
# [message:*] is passed the data of the target ID as it is.

(to_server) "query"+"range": fixed length
[code:8][code:8][quest:8][trial:8]
# If you want to omit trial specification, specify a value greater than 5.

(from_server) "query"+"range": fixed length
[code:8][code:8][minID:16][maxID:16]

(to_server) "query"+"last_trial": fixed length
[code:8][code:8][quest:8]

(from_server) "query"+"last_trial": fixed length
[code:8][code:8][trial:8]

(to_server) "query"+"team": fixed length
[code:8][code:8][quest:8][trial:8]

(from_server) "query"+"team": semi-fixed length
[code:8][code:8][quest:8][trial:8][message:*]
# [message:*] is passed the data of the target ID as it is.

(to_server) "query"+"vote": fixed length
[code:8][code:8][quest:8][trial:8][pnum:8]

(from_server) "query"+"vote": semi-fixed length
[code:8][code:8][quest:8][trial:8][message:*]

(to_server) "query"+"result": fixed length
[code:8][code:8][quest:8]

(from_server) "query"+"result": semi-fixed length
[code:8][code:8][quest:8][trial:8][message:*]

(to_server) "query"+"point": fixed length
[code:8][code:8]

(from_server) "query"+"point": fixed length
[code:8][code:8][count:8][point:8][increment:8][exchange:8]

-----------------------------------------------------------------------------
end of document
-----------------------------------------------------------------------------
