-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,023 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package db | ||
|
||
// Transact contains transaction control methods | ||
type Transact interface { | ||
Commit() error | ||
Rollback() error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package player | ||
|
||
import ( | ||
"github.com/takama/backer/db" | ||
"github.com/takama/backer/model" | ||
) | ||
|
||
// Controller defines DB interface for Player Entry | ||
type Controller interface { | ||
Transaction() (db.Transact, error) | ||
NewPlayer(ID string, tx db.Transact) error | ||
FindPlayer(ID string, tx db.Transact) (*model.Player, error) | ||
SavePlayer(player *model.Player, tx db.Transact) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package player | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/takama/backer" | ||
"github.com/takama/backer/helper" | ||
"github.com/takama/backer/model" | ||
) | ||
|
||
var ( | ||
// ErrInsufficientPoints appears if player has not enough points | ||
ErrInsufficientPoints = errors.New("Insufficient points") | ||
) | ||
|
||
// Entry implements Player interface | ||
type Entry struct { | ||
Controller | ||
mutex sync.RWMutex | ||
model.Player | ||
} | ||
|
||
// New returns new Entry which implement Player interface | ||
func New(id string, ctrl Controller) (*Entry, error) { | ||
tx, err := ctrl.Transaction() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
entry := &Entry{Controller: ctrl} | ||
|
||
player, err := ctrl.FindPlayer(id, tx) | ||
if err != nil { | ||
err = ctrl.NewPlayer(id, tx) | ||
if err != nil { | ||
tx.Rollback() | ||
return nil, err | ||
} | ||
player = &model.Player{ID: id} | ||
} | ||
entry.Player = *player | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return entry, nil | ||
} | ||
|
||
// Fund funds (add to balance) player with amount | ||
func (entry *Entry) Fund(amount backer.Points) error { | ||
tx, err := entry.Controller.Transaction() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
player, err := entry.Controller.FindPlayer(entry.Player.ID, tx) | ||
if err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
player.Balance = backer.Points( | ||
helper.RoundPrice(float32(player.Balance) + helper.TruncatePrice(float32(amount)))) | ||
err = entry.Controller.SavePlayer(player, tx) | ||
if err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entry.mutex.Lock() | ||
defer entry.mutex.Unlock() | ||
entry.Player.Balance = player.Balance | ||
|
||
return nil | ||
} | ||
|
||
// Take takes points from player account | ||
func (entry *Entry) Take(amount backer.Points) error { | ||
tx, err := entry.Controller.Transaction() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
player, err := entry.Controller.FindPlayer(entry.Player.ID, tx) | ||
if err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
if player.Balance < amount { | ||
return ErrInsufficientPoints | ||
} | ||
|
||
player.Balance = backer.Points( | ||
helper.RoundPrice(float32(player.Balance) - helper.TruncatePrice(float32(amount)))) | ||
err = entry.Controller.SavePlayer(player, tx) | ||
if err != nil { | ||
tx.Rollback() | ||
return err | ||
} | ||
|
||
err = tx.Commit() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
entry.mutex.Lock() | ||
defer entry.mutex.Unlock() | ||
entry.Player.Balance = player.Balance | ||
|
||
return nil | ||
} | ||
|
||
// Balance gets current points | ||
func (entry *Entry) Balance() (backer.Points, error) { | ||
player, err := entry.Controller.FindPlayer(entry.Player.ID, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
entry.mutex.Lock() | ||
defer entry.mutex.Unlock() | ||
entry.Player.Balance = player.Balance | ||
|
||
return player.Balance, nil | ||
} | ||
|
||
// ID returns player ID | ||
func (entry *Entry) ID() string { | ||
entry.mutex.RLock() | ||
defer entry.mutex.RUnlock() | ||
return entry.Player.ID | ||
} |
Oops, something went wrong.