Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Majorly refactored decode.go
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Sep 8, 2017
1 parent 7cbe818 commit 789ef3c
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 109 deletions.
119 changes: 42 additions & 77 deletions client/decode.go
Expand Up @@ -9,127 +9,90 @@ import (
"github.com/regner/albiondata-client/log"
)

func decodeRequest(params map[string]interface{}) operation {
func decodeRequest(params map[string]interface{}) (operation operation, err error) {
if _, ok := params["253"]; !ok {
return nil
return nil, nil
}

code := params["253"].(int16)

switch code {
case 10:
operation := operationGetGameServerByCluster{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationGetGameServerByCluster{}
case 67:
operation := operationAuctionGetOffers{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationAuctionGetOffers{}
case 166:
operation := operationGetClusterMapInfo{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationGetClusterMapInfo{}
case 217:
operation := operationGoldMarketGetAverageInfo{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationGoldMarketGetAverageInfo{}
case 232:
operation := operationRealEstateGetAuctionData{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationRealEstateGetAuctionData{}
case 233:
operation := operationRealEstateBidOnAuction{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationRealEstateBidOnAuction{}
default:
return nil, nil
}

return nil
err = decodeParams(params, operation)

return operation, err
}

func decodeResponse(params map[string]interface{}) operation {
func decodeResponse(params map[string]interface{}) (operation operation, err error) {
if _, ok := params["253"]; !ok {
return nil
return nil, nil
}

code := params["253"].(int16)

switch code {
case 2:
operation := operationJoinResponse{}
decodeParams(params, &operation)
return operation
operation = &operationJoinResponse{}
case 67:
operation := operationAuctionGetOffersResponse{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationAuctionGetOffersResponse{}
case 68:
operation := operationAuctionGetRequestsResponse{}
mapstructure.Decode(params, &operation)

return operation

operation = &operationAuctionGetRequestsResponse{}
case 147:
operation := operationReadMail{}
mapstructure.Decode(params, &operation)
return operation

operation = &operationReadMail{}
case 166:
operation := operationGetClusterMapInfoResponse{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationGetClusterMapInfoResponse{}
case 217:
operation := operationGoldMarketGetAverageInfoResponse{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationGoldMarketGetAverageInfoResponse{}
case 232:
operation := operationRealEstateGetAuctionDataResponse{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationRealEstateGetAuctionDataResponse{}
case 233:
operation := operationRealEstateBidOnAuctionResponse{}
mapstructure.Decode(params, &operation)

return operation
operation = &operationRealEstateBidOnAuctionResponse{}
default:
return nil, nil
}

return nil
err = decodeParams(params, operation)

return operation, err
}

func decodeEvent(params map[string]interface{}) operation {
func decodeEvent(params map[string]interface{}) (event operation, err error) {
if _, ok := params["252"]; !ok {
return nil
return nil, nil
}

eventType := params["252"].(int16)

switch eventType {
case 77:
event := eventPlayerOnlineStatus{}
err := decodeParams(params, &event)
log.Debug(err)

return event
event = &eventPlayerOnlineStatus{}
case 114:
event := eventSkillData{}
mapstructure.Decode(params, &event)

return event
event = &eventSkillData{}
default:
return nil, nil
}

return nil
err = decodeParams(params, event)

return event, err
}

func decodeParams(params interface{}, out interface{}) error {
func decodeParams(params interface{}, operation operation) error {
convertGameObjects := func(from reflect.Type, to reflect.Type, v interface{}) (interface{}, error) {
if from == reflect.TypeOf([]int8{}) && to == reflect.TypeOf(lib.CharacterID("")) {
log.Debug("Parsing character ID from mixed-endian UUID")
Expand All @@ -142,15 +105,17 @@ func decodeParams(params interface{}, out interface{}) error {

config := mapstructure.DecoderConfig{
DecodeHook: convertGameObjects,
Result: out,
Result: operation,
}

decoder, err := mapstructure.NewDecoder(&config)
if err != nil {
return err
}

return decoder.Decode(params)
err = decoder.Decode(params)

return err
}

func decodeCharacterID(array []int8) lib.CharacterID {
Expand Down
3 changes: 0 additions & 3 deletions client/event_player_online_status.go
Expand Up @@ -13,7 +13,4 @@ type eventPlayerOnlineStatus struct {

func (event eventPlayerOnlineStatus) Process(state *albionState) {
log.Debug("Got player online status event...")

log.Debug(event)

}
28 changes: 14 additions & 14 deletions client/listener.go
Expand Up @@ -119,26 +119,26 @@ func (l *listener) onReliableCommand(command *photon.PhotonCommand) {
params, err := photon.DecodeReliableMessage(msg)
if err != nil {
log.Debugf("Error while decoding parameters: %v", err)
// reset error message
err = nil
}

var operation operation

switch msg.Type {
case photon.OperationRequest:
operation := decodeRequest(params)

if operation != nil {
l.router.newOperation <- operation
}
operation, err = decodeRequest(params)
case photon.OperationResponse:
operation := decodeResponse(params)

if operation != nil {
l.router.newOperation <- operation
}
operation, err = decodeResponse(params)
case photon.EventDataType:
operation := decodeEvent(params)
operation, err = decodeEvent(params)
}

if operation != nil {
l.router.newOperation <- operation
}
if err != nil {
log.Debugf("Error while decoding an event or operation: %v", err)
}

if operation != nil {
l.router.newOperation <- operation
}
}
61 changes: 46 additions & 15 deletions client/operation_read_mail.go
Expand Up @@ -16,41 +16,72 @@ type operationReadMail struct {
func (op operationReadMail) Process(state *albionState) {
log.Debug("Got ReadMail operation...")

// split the mailbody
array := strings.Split(op.Body, "|")
// split the mail body
body := strings.Split(op.Body, "|")

// looks like this is not a market sell notification. for now we're only interested in those
if len(array) != 5 {
var notification lib.MarketNotification

// looks like this is a market sell notification.
switch len(body) {
case 5:
notification = decodeSellNotification(op, body)
case 3:
notification = decodeExpiryNotification(op, body)
// this is a normal mail or something else we're not interested in
default:
return
}

upload := lib.MarketNotificationUpload{
Type: notification.Type(),
Notification: notification,
}

log.Info("Sending a market notification to private ingest")
sendMsgToPrivateUploaders(&upload, lib.NatsMarketNotifications, state)
}

func decodeSellNotification(op operationReadMail, body []string) lib.MarketNotification {
notification := &lib.MarketSellNotification{}
notification.MailID = op.ID
notification.BuyerName = array[0]
notification.BuyerName = body[0]

amount, err := strconv.Atoi(array[1])
amount, err := strconv.Atoi(body[1])
if err != nil {
log.Error("Could not parse amount in market sell notification ", err)
return

return nil
}

notification.Amount = amount
notification.ItemID = array[2]
notification.ItemID = body[2]

price, err := strconv.Atoi(array[3])
price, err := strconv.Atoi(body[3])
if err != nil {
log.Error("Could not parse price in market sell notification ", err)
return

return nil
}

notification.Price = price / 10000
notification.TotalAfterTaxes = float32(float32(notification.Price) * float32(notification.Amount) * (1.0 - lib.SalesTax))

upload := lib.MarketNotificationUpload{
Type: notification.Type(),
Notification: notification,
return notification
}

func decodeExpiryNotification(op operationReadMail, body []string) lib.MarketNotification {
notification := &lib.MarketExpiryNotification{}
notification.MailID = op.ID

amount, err := strconv.Atoi(body[0])
if err != nil {
log.Error("Could not parse amount in market sell notification ", err)

return nil
}

log.Info("Sending a market notification to private ingest")
sendMsgToPrivateUploaders(&upload, lib.NatsMarketNotifications, state)
notification.Amount = amount
notification.ItemID = body[1]

return notification
}
10 changes: 10 additions & 0 deletions lib/market.go
Expand Up @@ -54,10 +54,20 @@ type MarketSellNotification struct {
TotalAfterTaxes float32 `json:"TotalAfterTaxes"`
}

type MarketExpiryNotification struct {
MailID int `json:"Id"`
ItemID string `json:"ItemTypeId"`
Amount int `json:"Amount"`
}

func (m *MarketSellNotification) Type() MarketNotificationType {
return SalesNotification
}

func (m *MarketExpiryNotification) Type() MarketNotificationType {
return ExpiryNotification
}

type MarketNotificationUpload struct {
PrivateUpload
Type MarketNotificationType `json: "NotificationType"`
Expand Down

0 comments on commit 789ef3c

Please sign in to comment.