123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- package qznn
- import (
- "reflect"
- "testing"
- "time"
- "github.com/bwmarrin/snowflake"
- "github.com/shopspring/decimal"
- "github.com/spf13/viper"
- "gogs.daxia.dev/huanan/pkg.daxia.dev/db"
- "gorm.io/gorm"
- "nn.daxia.dev/model"
- )
- func TestGame_payout(t *testing.T) {
- viper.Set("gormlog", true)
- viper.Set("db.username", "root")
- viper.Set("db.password", "123456~")
- viper.Set("db.addr", "10.42.50.148:3306")
- viper.Set("db.name", "qznn")
- db.Init()
- db.GetDB().Model(model.User{}).Where("id = 22").Update("credits", 10000)
- db.GetDB().Model(model.User{}).Where("id = 23").Update("credits", 10000)
- type fields struct {
- RoomMap map[int32]Room
- PlayerMap map[int32]Player
- IDGener *snowflake.Node
- LogID int64
- }
- type args struct {
- tx *gorm.DB
- roomBaseAmount decimal.Decimal
- roomID int32
- }
- tests := []struct {
- name string
- fields fields
- args args
- want map[int32]PayoutInfo
- wantErr bool
- }{
- {
- name: "normal",
- fields: fields{
- RoomMap: map[int32]Room{
- 1: Room{
- ID: 1,
- Name: "test",
- RoomType: int32(model.RoomTypeFree),
- ChairList: []Chair{
- Chair{
- ID: 0,
- PlayerID: 22,
- },
- Chair{
- ID: 1,
- PlayerID: 23,
- },
- },
- BaseAmount: decimal.NewFromInt(3),
- Issue: "123",
- Status: RoomStatusOpen,
- StatusStartTime: time.Now().Unix(),
- ReadyCh: make(chan bool),
- ChooseMasterCh: make(chan bool),
- ChooseMulCh: make(chan bool),
- OpenCh: make(chan bool),
- },
- },
- PlayerMap: map[int32]Player{
- 22: Player{
- ID: 22,
- Name: "test_player22",
- Balance: decimal.NewFromInt(200),
- Credits: decimal.NewFromInt(200),
- RoomID: 1,
- IsMaster: true,
- MasterMul: 3,
- Mul: 3,
- Status: PlayerStatusOpen,
- CardList: NNCardList{
- Card1: Card{
- Suit: 1,
- Num: 5,
- },
- Card2: Card{
- Suit: 2,
- Num: 7,
- },
- Card3: Card{
- Suit: 3,
- Num: 8,
- },
- Card4: Card{
- Suit: 1,
- Num: 10,
- },
- Card5: Card{
- Suit: 4,
- Num: 11,
- },
- },
- },
- 23: Player{
- ID: 23,
- Name: "test_player23",
- Balance: decimal.NewFromInt(200),
- Credits: decimal.NewFromInt(200),
- RoomID: 1,
- IsMaster: false,
- MasterMul: 3,
- Mul: 3,
- Status: PlayerStatusOpen,
- CardList: NNCardList{
- Card1: Card{
- Suit: 1,
- Num: 13,
- },
- Card2: Card{
- Suit: 2,
- Num: 6,
- },
- Card3: Card{
- Suit: 3,
- Num: 9,
- },
- Card4: Card{
- Suit: 1,
- Num: 7,
- },
- Card5: Card{
- Suit: 4,
- Num: 3,
- },
- },
- },
- },
- },
- args: args{
- tx: db.GetDB(),
- roomBaseAmount: decimal.NewFromInt(3),
- roomID: 1,
- },
- want: map[int32]PayoutInfo{
- 22: {
- PlayerID: 22,
- Amount: decimal.NewFromInt(81),
- IsMaster: true,
- },
- 23: {
- PlayerID: 23,
- Amount: decimal.NewFromInt(-81),
- IsMaster: false,
- },
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- p := &Game{
- RoomMap: tt.fields.RoomMap,
- PlayerMap: tt.fields.PlayerMap,
- }
- got, err := p.payout(tt.args.tx, tt.args.roomBaseAmount, tt.args.roomID)
- if (err != nil) != tt.wantErr {
- t.Errorf("Game.payout() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Game.payout() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestGame_payout_庄没钱(t *testing.T) {
- viper.Set("gormlog", true)
- viper.Set("db.username", "root")
- viper.Set("db.password", "123456~")
- viper.Set("db.addr", "10.42.50.148:3306")
- viper.Set("db.name", "qznn")
- db.Init()
- db.GetDB().Model(model.User{}).Where("id = 22").Update("credits", 0)
- db.GetDB().Model(model.User{}).Where("id = 23").Update("credits", 10000)
- type fields struct {
- RoomMap map[int32]Room
- PlayerMap map[int32]Player
- IDGener *snowflake.Node
- LogID int64
- }
- type args struct {
- tx *gorm.DB
- roomBaseAmount decimal.Decimal
- roomID int32
- }
- tests := []struct {
- name string
- fields fields
- args args
- want map[int32]PayoutInfo
- wantErr bool
- }{
- {
- name: "normal",
- fields: fields{
- RoomMap: map[int32]Room{
- 1: Room{
- ID: 1,
- Name: "test",
- RoomType: int32(model.RoomTypeFree),
- ChairList: []Chair{
- Chair{
- ID: 0,
- PlayerID: 22,
- },
- Chair{
- ID: 1,
- PlayerID: 23,
- },
- },
- BaseAmount: decimal.NewFromInt(3),
- Issue: "123",
- Status: RoomStatusOpen,
- StatusStartTime: time.Now().Unix(),
- ReadyCh: make(chan bool),
- ChooseMasterCh: make(chan bool),
- ChooseMulCh: make(chan bool),
- OpenCh: make(chan bool),
- },
- },
- PlayerMap: map[int32]Player{
- 22: Player{
- ID: 22,
- Name: "test_player22",
- Balance: decimal.NewFromInt(200),
- Credits: decimal.NewFromInt(200),
- RoomID: 1,
- IsMaster: true,
- MasterMul: 3,
- Mul: 3,
- Status: PlayerStatusOpen,
- CardList: NNCardList{
- Card1: Card{
- Suit: 1,
- Num: 5,
- },
- Card2: Card{
- Suit: 2,
- Num: 7,
- },
- Card3: Card{
- Suit: 3,
- Num: 9,
- },
- Card4: Card{
- Suit: 1,
- Num: 10,
- },
- Card5: Card{
- Suit: 4,
- Num: 11,
- },
- },
- },
- 23: Player{
- ID: 23,
- Name: "test_player23",
- Balance: decimal.NewFromInt(200),
- Credits: decimal.NewFromInt(200),
- RoomID: 1,
- IsMaster: false,
- MasterMul: 3,
- Mul: 3,
- Status: PlayerStatusOpen,
- CardList: NNCardList{
- Card1: Card{
- Suit: 1,
- Num: 5,
- },
- Card2: Card{
- Suit: 2,
- Num: 7,
- },
- Card3: Card{
- Suit: 3,
- Num: 8,
- },
- Card4: Card{
- Suit: 1,
- Num: 10,
- },
- Card5: Card{
- Suit: 4,
- Num: 11,
- },
- },
- },
- },
- },
- args: args{
- tx: db.GetDB(),
- roomBaseAmount: decimal.NewFromInt(3),
- roomID: 1,
- },
- want: map[int32]PayoutInfo{
- 22: {
- PlayerID: 22,
- Amount: decimal.Zero,
- IsMaster: true,
- },
- 23: {
- PlayerID: 23,
- Amount: decimal.Zero,
- IsMaster: false,
- },
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- p := &Game{
- RoomMap: tt.fields.RoomMap,
- PlayerMap: tt.fields.PlayerMap,
- }
- got, err := p.payout(tt.args.tx, tt.args.roomBaseAmount, tt.args.roomID)
- if (err != nil) != tt.wantErr {
- t.Errorf("Game.payout() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !got[22].Amount.Equal(tt.want[22].Amount) {
- t.Errorf("Game.payout() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestGame_payout_庄没钱2(t *testing.T) {
- viper.Set("gormlog", true)
- viper.Set("db.username", "root")
- viper.Set("db.password", "123456~")
- viper.Set("db.addr", "10.42.50.148:3306")
- viper.Set("db.name", "qznn")
- db.Init()
- db.GetDB().Model(model.User{}).Where("id = 22").Update("credits_qznn", 72)
- db.GetDB().Model(model.User{}).Where("id = 23").Update("credits_qznn", 10000)
- db.GetDB().Model(model.User{}).Where("id = 24").Update("credits_qznn", 10000)
- type fields struct {
- RoomMap map[int32]Room
- PlayerMap map[int32]Player
- IDGener *snowflake.Node
- LogID int64
- }
- type args struct {
- tx *gorm.DB
- roomBaseAmount decimal.Decimal
- roomID int32
- }
- tests := []struct {
- name string
- fields fields
- args args
- want map[int32]PayoutInfo
- wantErr bool
- }{
- {
- name: "normal",
- fields: fields{
- RoomMap: map[int32]Room{
- 1: Room{
- ID: 1,
- Name: "test",
- RoomType: int32(model.RoomTypeFree),
- ChairList: []Chair{
- Chair{
- ID: 0,
- PlayerID: 22,
- },
- Chair{
- ID: 1,
- PlayerID: 23,
- },
- Chair{
- ID: 1,
- PlayerID: 24,
- },
- },
- BaseAmount: decimal.NewFromInt(3),
- Issue: "123",
- Status: RoomStatusOpen,
- StatusStartTime: time.Now().Unix(),
- ReadyCh: make(chan bool),
- ChooseMasterCh: make(chan bool),
- ChooseMulCh: make(chan bool),
- OpenCh: make(chan bool),
- },
- },
- PlayerMap: map[int32]Player{
- 22: Player{
- ID: 22,
- Name: "test_player22",
- Balance: decimal.NewFromInt(0),
- Credits: decimal.NewFromInt(72),
- RoomID: 1,
- IsMaster: true,
- MasterMul: 3,
- Mul: 1,
- Status: PlayerStatusOpen,
- CardList: CardList2NNCardList(getOpenNumList("48,10,37,22,0")),
- },
- 23: Player{
- ID: 23,
- Name: "test_player23",
- Balance: decimal.NewFromInt(0),
- Credits: decimal.NewFromInt(10000),
- RoomID: 1,
- IsMaster: false,
- MasterMul: 2,
- Mul: 4,
- Status: PlayerStatusOpen,
- CardList: CardList2NNCardList(getOpenNumList("49,19,50,5,32")),
- },
- 24: Player{
- ID: 24,
- Name: "test_player24",
- Balance: decimal.NewFromInt(0),
- Credits: decimal.NewFromInt(10000),
- RoomID: 1,
- IsMaster: false,
- MasterMul: 1,
- Mul: 1,
- Status: PlayerStatusOpen,
- CardList: CardList2NNCardList(getOpenNumList("14,4,45,21,51")),
- },
- },
- },
- args: args{
- tx: db.GetDB(),
- roomBaseAmount: decimal.NewFromInt(3),
- roomID: 1,
- },
- want: map[int32]PayoutInfo{
- 22: {
- PlayerID: 22,
- Amount: decimal.NewFromInt(-72),
- IsMaster: true,
- },
- 23: {
- PlayerID: 23,
- Amount: decimal.NewFromInt(81),
- IsMaster: false,
- },
- 24: {
- PlayerID: 24,
- Amount: decimal.NewFromInt(-9),
- IsMaster: false,
- },
- },
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- p := &Game{
- RoomMap: tt.fields.RoomMap,
- PlayerMap: tt.fields.PlayerMap,
- }
- got, err := p.payout(tt.args.tx, tt.args.roomBaseAmount, tt.args.roomID)
- if (err != nil) != tt.wantErr {
- t.Errorf("Game.payout() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !got[22].Amount.Equal(tt.want[22].Amount) {
- t.Errorf("Game.payout() = %v, want %v", got, tt.want)
- }
- if !got[23].Amount.Equal(tt.want[23].Amount) {
- t.Errorf("Game.payout() = %v, want %v", got, tt.want)
- }
- if !got[24].Amount.Equal(tt.want[24].Amount) {
- t.Errorf("Game.payout() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|