Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #13 from ggmolly/fix/netcode-upgrades
Browse files Browse the repository at this point in the history
fix: upgrade netcode to latest game version
  • Loading branch information
Molly authored Jul 20, 2024
2 parents 258ab9f + 442b2b8 commit cb90b35
Show file tree
Hide file tree
Showing 1,171 changed files with 10,912 additions and 4,852 deletions.
11 changes: 5 additions & 6 deletions answer/ask_mail_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ func AskMailBody(buffer *[]byte, client *connection.Client) (int, int, error) {
if err != nil {
return 0, 30009, err
}
mail, ok := client.Commander.MailsMap[data.GetId()]
mail, ok := client.Commander.MailsMap[data.GetMailId()]
if !ok {
return 0, 30009, nil
}
body := protobuf.SC_30009{
DetailInfo: &protobuf.MAIL_DETAIL{
Id: proto.Uint32(mail.ID),
Content: proto.String(mail.Body),
},
Result: proto.Uint32(0),
}
if err := mail.SetRead(true); err != nil {
body.Result = proto.Uint32(1)
}
mail.SetRead(true)
return client.SendMessage(30009, &body)
}
1 change: 0 additions & 1 deletion answer/commander_fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func CommanderFleet(buffer *[]byte, client *connection.Client) (int, int, error)
response.GroupList = append(response.GroupList, &protobuf.GROUPINFO{
Id: proto.Uint32(fleet.GameID),
ShipList: shipList,
Name: proto.String(fleet.Name),
Commanders: []*protobuf.COMMANDERSINFO{},
})
}
Expand Down
24 changes: 0 additions & 24 deletions answer/delete_all_mails.go

This file was deleted.

24 changes: 24 additions & 0 deletions answer/delete_archived_mail.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package answer

import (
"github.com/ggmolly/belfast/connection"
"github.com/ggmolly/belfast/protobuf"
"google.golang.org/protobuf/proto"
)

func DeleteArchivedMail(buffer *[]byte, client *connection.Client) (int, int, error) {
var payload protobuf.CS_30008
if err := proto.Unmarshal(*buffer, &payload); err != nil {
return 0, 30008, err
}
response := protobuf.SC_30009{
Result: proto.Uint32(0),
}
mail, ok := client.Commander.MailsMap[payload.GetMailId()]
if !ok {
response.Result = proto.Uint32(1)
} else if err := mail.Delete(); err != nil {
response.Result = proto.Uint32(1)
}
return client.SendMessage(30009, &response)
}
54 changes: 54 additions & 0 deletions answer/get_collection_mail_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package answer

import (
"github.com/ggmolly/belfast/connection"
"github.com/ggmolly/belfast/orm"
"github.com/ggmolly/belfast/protobuf"
"google.golang.org/protobuf/proto"
)

func mailToSimpleMailInfo(mail *orm.Mail) *protobuf.MAIL_SIMPLE_INFO {
attachments := make([]*protobuf.DROPINFO, len(mail.Attachments))
for i, attachment := range mail.Attachments {
attachments[i] = &protobuf.DROPINFO{
Type: proto.Uint32(attachment.Type),
Id: proto.Uint32(attachment.ItemID),
Number: proto.Uint32(attachment.Quantity),
}
}
return &protobuf.MAIL_SIMPLE_INFO{
Id: proto.Uint32(mail.ID),
Date: proto.Uint32(uint32(mail.Date.Unix())),
Title: proto.String(mail.Title),
Content: proto.String(mail.Body),
AttachmentList: attachments,
}
}

// Returns archived mails
func GetCollectionMailList(buffer *[]byte, client *connection.Client) (int, int, error) {
var payload protobuf.CS_30004
if err := proto.Unmarshal(*buffer, &payload); err != nil {
return 0, 30002, err
}
var response protobuf.SC_30005
commanderMailsCount := uint32(len(client.Commander.Mails))
if commanderMailsCount == 0 {
return client.SendMessage(30003, &response)
}

// If end range is 0, it means we want to send all the mails
if payload.GetIndexEnd() == 0 {
payload.IndexEnd = proto.Uint32(commanderMailsCount + 1)
}

// Lua range starts at 1, so we will compensate for that
payload.IndexBegin = proto.Uint32(payload.GetIndexBegin() - 1)

for i := payload.GetIndexBegin(); i < commanderMailsCount && i < payload.GetIndexEnd(); i++ {
if client.Commander.Mails[i].IsArchived {
response.MailList = append(response.MailList, mailToSimpleMailInfo(&client.Commander.Mails[i]))
}
}
return client.SendMessage(30005, &response)
}
32 changes: 0 additions & 32 deletions answer/give_mail_attachments.go

This file was deleted.

157 changes: 157 additions & 0 deletions answer/handle_mail_deal_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package answer

import (
"fmt"

"github.com/ggmolly/belfast/connection"
"github.com/ggmolly/belfast/consts"
"github.com/ggmolly/belfast/orm"
"github.com/ggmolly/belfast/protobuf"
"google.golang.org/protobuf/proto"
)

// The bool is whether the function has modified the response or not
type MailDealCmdHandler func(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error)

func handleMailDealCmdRead(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error) {
err := mail.SetRead(true)
// Put all read mails in the mailIdList
for _, commanderMail := range client.Commander.Mails {
if commanderMail.Read || mail.ID == commanderMail.ID {
response.MailIdList = append(response.MailIdList, commanderMail.ID)
}
}
return true, err
}

func handleMailDealCmdImportant(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error) {
// Put all important mails in the mailIdList
err := mail.SetImportant(true)
for _, commanderMail := range client.Commander.Mails {
if commanderMail.IsImportant || mail.ID == commanderMail.ID {
response.MailIdList = append(response.MailIdList, commanderMail.ID)
}
}
return true, err
}

func handleMailDealCmdUnimportant(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error) {
// Put all unimportant mails in the mailIdList
err := mail.SetImportant(false)
for _, commanderMail := range client.Commander.Mails {
if !commanderMail.IsImportant || mail.ID == commanderMail.ID {
response.MailIdList = append(response.MailIdList, commanderMail.ID)
}
}
return true, err
}

func handleMailDealCmdDelete(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error) {
for _, mail := range client.Commander.Mails {
if mail.Read && (mail.AttachmentsCollected && len(mail.Attachments) > 0) {
response.MailIdList = append(response.MailIdList, mail.ID)
}
}

// TODO: Update the ORM query to delete all mails that are read, and that have collected attachments (if any)
if err := orm.GormDB.Delete(&orm.Mail{}, "read = ?", true, true).Error; err != nil {
return false, err
}

// Reload mails
if err := orm.GormDB.Preload("Attachments").Find(&client.Commander.Mails).Error; err != nil {
return false, err
}

// load MailsMap
client.Commander.MailsMap = make(map[uint32]*orm.Mail)
for i, mail := range client.Commander.Mails {
client.Commander.MailsMap[mail.ID] = &client.Commander.Mails[i]
}

return true, nil
}

func handleMailDealCmdAttachment(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error) {
panic("not implemented")
}

func handleMailDealCmdOverflow(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error) {
panic("not implemented")
}

func handleMailDealCmdMove(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) (bool, error) {
// Put all archived mails in the mailIdList -- TODO: check if commander has enough space to archive the mail
for _, commanderMail := range client.Commander.Mails {
if commanderMail.IsArchived || mail.ID == commanderMail.ID {
response.MailIdList = append(response.MailIdList, commanderMail.ID)
}
}
err := mail.SetArchived(true)
return true, err
}

var cmdHandlers = map[uint32]MailDealCmdHandler{
consts.MAIL_DEAL_CMDS_READ: handleMailDealCmdRead,
consts.MAIL_DEAL_CMDS_IMPORTANT: handleMailDealCmdImportant,
consts.MAIL_DEAL_CMDS_UNIMPORTANT: handleMailDealCmdUnimportant,
consts.MAIL_DEAL_CMDS_DELETE: handleMailDealCmdDelete,
consts.MAIL_DEAL_CMDS_ATTACHMENT: handleMailDealCmdAttachment,
consts.MAIL_DEAL_CMDS_OVERFLOW: handleMailDealCmdOverflow,
consts.MAIL_DEAL_CMDS_MOVE: handleMailDealCmdMove,
}

func HandleMailDealCmd(buffer *[]byte, client *connection.Client) (int, int, error) {
var payload protobuf.CS_30006
if err := proto.Unmarshal(*buffer, &payload); err != nil {
return 0, 30006, err
}

response := protobuf.SC_30007{
Result: proto.Uint32(0),
UnreadNumber: proto.Uint32(0),
}
fn, ok := cmdHandlers[payload.GetCmd()]
if !ok {
return 0, 30006, fmt.Errorf("unknown mail deal cmd: %d", payload.GetCmd())
}
var mailId uint32
matchList := payload.GetMatchList()
if len(matchList) == 0 { // the action doesn't specifically target one / many mails
mailId = 0
} else if matchList[0].GetType() == 0 {
return 0, 30006, fmt.Errorf("unhandled case: matchList[0].GetType() != 1, got %d", matchList[0].GetType())
} else if len(matchList[0].GetArgList()) == 0 {
return 0, 30006, fmt.Errorf("unhandled case: matchList[0].GetArgList() is empty")
} else {
mailId = matchList[0].GetArgList()[0]
}
mail, ok := client.Commander.MailsMap[mailId]
if !ok && mailId != 0 { // 0 represents a specific case where the action doesn't target any mail
return 0, 30006, fmt.Errorf("mail #%d not found", mailId)
}
dirty, err := fn(client, &payload, &response, mail)
if err != nil {
return 0, 30006, err
}

var unreadCount uint32
for _, mail := range client.Commander.Mails {
if !mail.Read {
unreadCount++
}
}
response.UnreadNumber = proto.Uint32(unreadCount)

// Copy mail ids if the handler didn't do it
if !dirty {
var mailIds []uint32
for _, mail := range client.Commander.Mails {
if !mail.IsArchived {
mailIds = append(mailIds, mail.ID)
}
}
response.MailIdList = mailIds
}
return client.SendMessage(30007, &response)
}
3 changes: 3 additions & 0 deletions answer/mailbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func GameMailbox(buffer *[]byte, client *connection.Client) (int, int, error) {
var unread uint32
var total uint32
for _, mail := range client.Commander.Mails {
if mail.IsArchived {
continue
}
if !mail.Read {
unread++
}
Expand Down
1 change: 1 addition & 0 deletions answer/player_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func PlayerInfo(buffer *[]byte, client *connection.Client) (int, int, error) {
ThemeUploadNotAllowedTime: proto.Uint32(0),
RandomShipMode: proto.Uint32(0),
MarryShip: proto.Uint32(0),
MailStoreroomLv: proto.Uint32(0),
}

// Get user's secretaries
Expand Down
Loading

0 comments on commit cb90b35

Please sign in to comment.