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

Commit

Permalink
wip: fix small differences between game versions, updating MailBox ha…
Browse files Browse the repository at this point in the history
…ndlers to new version
  • Loading branch information
ggmolly committed Jul 14, 2024
1 parent 5b25fb0 commit 245f876
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 111 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
40 changes: 20 additions & 20 deletions answer/delete_all_mails.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package answer

import (
"github.com/ggmolly/belfast/connection"
"github.com/ggmolly/belfast/orm"
// import (
// "github.com/ggmolly/belfast/connection"
// "github.com/ggmolly/belfast/orm"

"github.com/ggmolly/belfast/protobuf"
)
// "github.com/ggmolly/belfast/protobuf"
// )

func DeleteAllMails(buffer *[]byte, client *connection.Client) (int, int, error) {
mailIds := make([]uint32, len(client.Commander.Mails))
for i, mail := range client.Commander.Mails {
mailIds[i] = mail.ID
}
response := protobuf.SC_30007{
IdList: mailIds,
}
if err := client.Commander.CleanMailbox(); err != nil {
return 0, 30007, err
}
client.Commander.Mails = make([]orm.Mail, 0)
client.Commander.MailsMap = make(map[uint32]*orm.Mail)
return client.SendMessage(30007, &response)
}
// func DeleteAllMails(buffer *[]byte, client *connection.Client) (int, int, error) {
// mailIds := make([]uint32, len(client.Commander.Mails))
// for i, mail := range client.Commander.Mails {
// mailIds[i] = mail.ID
// }
// response := protobuf.SC_30007{
// IdList: mailIds,
// }
// if err := client.Commander.CleanMailbox(); err != nil {
// return 0, 30007, err
// }
// client.Commander.Mails = make([]orm.Mail, 0)
// client.Commander.MailsMap = make(map[uint32]*orm.Mail)
// return client.SendMessage(30007, &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)
}
56 changes: 28 additions & 28 deletions answer/give_mail_attachments.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package answer

import (
"github.com/ggmolly/belfast/connection"
// import (
// "github.com/ggmolly/belfast/connection"

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

func GiveMailAttachments(buffer *[]byte, client *connection.Client) (int, int, error) {
var data protobuf.CS_30004
err := proto.Unmarshal(*buffer, &data)
if err != nil {
return 0, 30005, err
}
var attachments []*protobuf.ATTACHMENT
for _, mailId := range data.GetId() {
mail, ok := client.Commander.MailsMap[mailId]
if !ok {
return 0, 30005, nil
}
mailAttachments, err := mail.CollectAttachments(client.Commander)
if err != nil {
return 0, 30005, err
}
attachments = append(attachments, mailAttachments...)
}
response := protobuf.SC_30005{
AttachmentList: attachments,
}
return client.SendMessage(30005, &response)
}
// func GiveMailAttachments(buffer *[]byte, client *connection.Client) (int, int, error) {
// var data protobuf.CS_30004
// err := proto.Unmarshal(*buffer, &data)
// if err != nil {
// return 0, 30005, err
// }
// var attachments []*protobuf.ATTACHMENT
// for _, mailId := range data.GetId() {
// mail, ok := client.Commander.MailsMap[mailId]
// if !ok {
// return 0, 30005, nil
// }
// mailAttachments, err := mail.CollectAttachments(client.Commander)
// if err != nil {
// return 0, 30005, err
// }
// attachments = append(attachments, mailAttachments...)
// }
// response := protobuf.SC_30005{
// AttachmentList: attachments,
// }
// return client.SendMessage(30005, &response)
// }
117 changes: 117 additions & 0 deletions answer/handle_mail_deal_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package answer

import (
"fmt"
"log"

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

type MailDealCmdHandler func(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error

func handleMailDealCmdRead(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error {
return mail.SetRead(true)
}

func handleMailDealCmdImportant(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error {
return mail.SetImportant(true)
}

func handleMailDealCmdUnimportant(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error {
return mail.SetImportant(false)
}

func handleMailDealCmdDelete(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error {
if err := orm.GormDB.Delete(&orm.Mail{}, "read = ?", true).Error; err != nil {
return err
}

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

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

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

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

func handleMailDealCmdMove(client *connection.Client, payload *protobuf.CS_30006, response *protobuf.SC_30007, mail *orm.Mail) error {
return mail.SetArchived(true)
}

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
}

logger.LogEvent("Server", "MailCmd", payload.String(), logger.LOG_LEVEL_DEBUG)

response := protobuf.SC_30007{
Result: proto.Uint32(0),
}
_, ok := cmdHandlers[payload.GetCmd()]
if !ok {
return 0, 30006, fmt.Errorf("unknown mail deal cmd: %d", payload.GetCmd())
}
var mailIndex uint32
if len(payload.GetMatchList()) > 0 {
mailIndex = payload.GetMatchList()[0].GetType() - 1
}
if mailIndex >= uint32(len(client.Commander.Mails)) {
response.Result = proto.Uint32(1)
return client.SendMessage(30007, &response)
}
fn := cmdHandlers[payload.GetCmd()]
mail := client.Commander.Mails[mailIndex]
if err := fn(client, &payload, &response, &mail); 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
var mailIds []uint32
for _, mail := range client.Commander.Mails {
if !mail.IsArchived {
mailIds = append(mailIds, mail.ID)
}
}
response.MailIdList = mailIds
log.Println(response.String())
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 245f876

Please sign in to comment.