-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.go
126 lines (106 loc) · 3.43 KB
/
player.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package dctycoon
import (
"fmt"
"github.com/nzin/dctycoon/firewall"
"github.com/nzin/dctycoon/accounting"
"github.com/nzin/dctycoon/supplier"
"github.com/nzin/dctycoon/timer"
log "github.com/sirupsen/logrus"
)
type Player struct {
supplier.ActorAbstract
reputation *supplier.Reputation
firewall *firewall.Firewall
companyname string
maplevel int32 // from 0 (3x4 map), to 2 (32x32 map)
}
//
// GetReputationScore is part of the Actor interface
func (p *Player) GetReputationScore() float64 {
return p.reputation.GetScore()
}
func (p *Player) GetFirewall() *firewall.Firewall {
return p.firewall
}
func (p *Player) GetReputation() *supplier.Reputation {
return p.reputation
}
func (p *Player) GetCompanyName() string {
return p.companyname
}
func (p *Player) GetMapLevel() int32 {
return p.maplevel
}
func (p *Player) SetMapLevel(maplevel int32) {
p.maplevel = maplevel
}
//
// NewPlayer create a new player representation
func NewPlayer() *Player {
log.Debug("NewPlayer()")
actorabstract := supplier.NewActorAbstract()
p := &Player{
ActorAbstract: *actorabstract,
reputation: nil,
companyname: "noname",
maplevel: 0,
firewall: firewall.NewFirewall(),
}
return p
}
func (self *Player) Init(timer *timer.GameTimer, initialcapital float64, locationname, companyname string, maplevel int32) {
log.Debug("Player::Init(", timer, ",", initialcapital, ",", locationname, ")")
self.ActorAbstract.Init(timer, locationname, "you")
self.reputation = supplier.NewReputation()
self.companyname = companyname
self.maplevel = maplevel
self.firewall = firewall.NewFirewall()
// add some equity
self.GetLedger().AddMovement(accounting.LedgerMovement{
Description: "initial capital",
Amount: initialcapital,
AccountFrom: "4561",
AccountTo: "5121",
Date: timer.CurrentTime,
})
}
func (self *Player) LoadGame(timer *timer.GameTimer, v map[string]interface{}) {
log.Debug("Player::LoadGame(", timer, ",", v, ")")
locationname := v["location"].(string)
self.ActorAbstract.Init(timer, locationname, "you")
self.reputation = supplier.NewReputation()
self.companyname = v["companyname"].(string)
self.maplevel = int32(v["maplevel"].(float64))
self.firewall = firewall.NewFirewall()
self.GetLedger().Load(v["ledger"].(map[string]interface{}), self.GetLocation().Taxrate, self.GetLocation().Bankinterestrate)
self.GetInventory().Load(v["inventory"].(map[string]interface{}))
if offersinterface, ok := v["offers"]; ok {
offers := offersinterface.([]interface{})
for _, offer := range offers {
self.LoadOffer(offer.(map[string]interface{}))
}
}
self.reputation.Load(v["reputation"].(map[string]interface{}))
self.firewall.Load(v["firewall"].(map[string]interface{}))
}
func (self *Player) Save() string {
save := fmt.Sprintf(`"location": "%s",`, self.GetLocationName()) + "\n"
save += fmt.Sprintf(`"inventory": %s,`, self.GetInventory().Save()) + "\n"
save += `"offers":[`
firstitem := true
for _, offer := range self.GetOffers() {
if firstitem == true {
firstitem = false
} else {
save += ",\n"
}
save += offer.Save()
}
save += "],"
save += fmt.Sprintf(`"companyname": "%s",`, self.companyname) + "\n"
save += fmt.Sprintf(`"maplevel": %d,`, self.maplevel) + "\n"
save += fmt.Sprintf(`"reputation": %s,`, self.reputation.Save()) + "\n"
save += fmt.Sprintf(`"firewall": %s,`, self.firewall.Save()) + "\n"
save += fmt.Sprintf(`"ledger": %s`, self.GetLedger().Save()) + "\n"
return save
}