payout_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. package qznn
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/bwmarrin/snowflake"
  7. "github.com/shopspring/decimal"
  8. "github.com/spf13/viper"
  9. "gogs.daxia.dev/huanan/pkg.daxia.dev/db"
  10. "gorm.io/gorm"
  11. "nn.daxia.dev/model"
  12. )
  13. func TestGame_payout(t *testing.T) {
  14. viper.Set("gormlog", true)
  15. viper.Set("db.username", "root")
  16. viper.Set("db.password", "123456~")
  17. viper.Set("db.addr", "10.42.50.148:3306")
  18. viper.Set("db.name", "qznn")
  19. db.Init()
  20. db.GetDB().Model(model.User{}).Where("id = 22").Update("credits", 10000)
  21. db.GetDB().Model(model.User{}).Where("id = 23").Update("credits", 10000)
  22. type fields struct {
  23. RoomMap map[int32]Room
  24. PlayerMap map[int32]Player
  25. IDGener *snowflake.Node
  26. LogID int64
  27. }
  28. type args struct {
  29. tx *gorm.DB
  30. roomBaseAmount decimal.Decimal
  31. roomID int32
  32. }
  33. tests := []struct {
  34. name string
  35. fields fields
  36. args args
  37. want map[int32]PayoutInfo
  38. wantErr bool
  39. }{
  40. {
  41. name: "normal",
  42. fields: fields{
  43. RoomMap: map[int32]Room{
  44. 1: Room{
  45. ID: 1,
  46. Name: "test",
  47. RoomType: int32(model.RoomTypeFree),
  48. ChairList: []Chair{
  49. Chair{
  50. ID: 0,
  51. PlayerID: 22,
  52. },
  53. Chair{
  54. ID: 1,
  55. PlayerID: 23,
  56. },
  57. },
  58. BaseAmount: decimal.NewFromInt(3),
  59. Issue: "123",
  60. Status: RoomStatusOpen,
  61. StatusStartTime: time.Now().Unix(),
  62. ReadyCh: make(chan bool),
  63. ChooseMasterCh: make(chan bool),
  64. ChooseMulCh: make(chan bool),
  65. OpenCh: make(chan bool),
  66. },
  67. },
  68. PlayerMap: map[int32]Player{
  69. 22: Player{
  70. ID: 22,
  71. Name: "test_player22",
  72. Balance: decimal.NewFromInt(200),
  73. Credits: decimal.NewFromInt(200),
  74. RoomID: 1,
  75. IsMaster: true,
  76. MasterMul: 3,
  77. Mul: 3,
  78. Status: PlayerStatusOpen,
  79. CardList: NNCardList{
  80. Card1: Card{
  81. Suit: 1,
  82. Num: 5,
  83. },
  84. Card2: Card{
  85. Suit: 2,
  86. Num: 7,
  87. },
  88. Card3: Card{
  89. Suit: 3,
  90. Num: 8,
  91. },
  92. Card4: Card{
  93. Suit: 1,
  94. Num: 10,
  95. },
  96. Card5: Card{
  97. Suit: 4,
  98. Num: 11,
  99. },
  100. },
  101. },
  102. 23: Player{
  103. ID: 23,
  104. Name: "test_player23",
  105. Balance: decimal.NewFromInt(200),
  106. Credits: decimal.NewFromInt(200),
  107. RoomID: 1,
  108. IsMaster: false,
  109. MasterMul: 3,
  110. Mul: 3,
  111. Status: PlayerStatusOpen,
  112. CardList: NNCardList{
  113. Card1: Card{
  114. Suit: 1,
  115. Num: 13,
  116. },
  117. Card2: Card{
  118. Suit: 2,
  119. Num: 6,
  120. },
  121. Card3: Card{
  122. Suit: 3,
  123. Num: 9,
  124. },
  125. Card4: Card{
  126. Suit: 1,
  127. Num: 7,
  128. },
  129. Card5: Card{
  130. Suit: 4,
  131. Num: 3,
  132. },
  133. },
  134. },
  135. },
  136. },
  137. args: args{
  138. tx: db.GetDB(),
  139. roomBaseAmount: decimal.NewFromInt(3),
  140. roomID: 1,
  141. },
  142. want: map[int32]PayoutInfo{
  143. 22: {
  144. PlayerID: 22,
  145. Amount: decimal.NewFromInt(81),
  146. IsMaster: true,
  147. },
  148. 23: {
  149. PlayerID: 23,
  150. Amount: decimal.NewFromInt(-81),
  151. IsMaster: false,
  152. },
  153. },
  154. wantErr: false,
  155. },
  156. }
  157. for _, tt := range tests {
  158. t.Run(tt.name, func(t *testing.T) {
  159. p := &Game{
  160. RoomMap: tt.fields.RoomMap,
  161. PlayerMap: tt.fields.PlayerMap,
  162. }
  163. got, err := p.payout(tt.args.tx, tt.args.roomBaseAmount, tt.args.roomID)
  164. if (err != nil) != tt.wantErr {
  165. t.Errorf("Game.payout() error = %v, wantErr %v", err, tt.wantErr)
  166. return
  167. }
  168. if !reflect.DeepEqual(got, tt.want) {
  169. t.Errorf("Game.payout() = %v, want %v", got, tt.want)
  170. }
  171. })
  172. }
  173. }
  174. func TestGame_payout_庄没钱(t *testing.T) {
  175. viper.Set("gormlog", true)
  176. viper.Set("db.username", "root")
  177. viper.Set("db.password", "123456~")
  178. viper.Set("db.addr", "10.42.50.148:3306")
  179. viper.Set("db.name", "qznn")
  180. db.Init()
  181. db.GetDB().Model(model.User{}).Where("id = 22").Update("credits", 0)
  182. db.GetDB().Model(model.User{}).Where("id = 23").Update("credits", 10000)
  183. type fields struct {
  184. RoomMap map[int32]Room
  185. PlayerMap map[int32]Player
  186. IDGener *snowflake.Node
  187. LogID int64
  188. }
  189. type args struct {
  190. tx *gorm.DB
  191. roomBaseAmount decimal.Decimal
  192. roomID int32
  193. }
  194. tests := []struct {
  195. name string
  196. fields fields
  197. args args
  198. want map[int32]PayoutInfo
  199. wantErr bool
  200. }{
  201. {
  202. name: "normal",
  203. fields: fields{
  204. RoomMap: map[int32]Room{
  205. 1: Room{
  206. ID: 1,
  207. Name: "test",
  208. RoomType: int32(model.RoomTypeFree),
  209. ChairList: []Chair{
  210. Chair{
  211. ID: 0,
  212. PlayerID: 22,
  213. },
  214. Chair{
  215. ID: 1,
  216. PlayerID: 23,
  217. },
  218. },
  219. BaseAmount: decimal.NewFromInt(3),
  220. Issue: "123",
  221. Status: RoomStatusOpen,
  222. StatusStartTime: time.Now().Unix(),
  223. ReadyCh: make(chan bool),
  224. ChooseMasterCh: make(chan bool),
  225. ChooseMulCh: make(chan bool),
  226. OpenCh: make(chan bool),
  227. },
  228. },
  229. PlayerMap: map[int32]Player{
  230. 22: Player{
  231. ID: 22,
  232. Name: "test_player22",
  233. Balance: decimal.NewFromInt(200),
  234. Credits: decimal.NewFromInt(200),
  235. RoomID: 1,
  236. IsMaster: true,
  237. MasterMul: 3,
  238. Mul: 3,
  239. Status: PlayerStatusOpen,
  240. CardList: NNCardList{
  241. Card1: Card{
  242. Suit: 1,
  243. Num: 5,
  244. },
  245. Card2: Card{
  246. Suit: 2,
  247. Num: 7,
  248. },
  249. Card3: Card{
  250. Suit: 3,
  251. Num: 9,
  252. },
  253. Card4: Card{
  254. Suit: 1,
  255. Num: 10,
  256. },
  257. Card5: Card{
  258. Suit: 4,
  259. Num: 11,
  260. },
  261. },
  262. },
  263. 23: Player{
  264. ID: 23,
  265. Name: "test_player23",
  266. Balance: decimal.NewFromInt(200),
  267. Credits: decimal.NewFromInt(200),
  268. RoomID: 1,
  269. IsMaster: false,
  270. MasterMul: 3,
  271. Mul: 3,
  272. Status: PlayerStatusOpen,
  273. CardList: NNCardList{
  274. Card1: Card{
  275. Suit: 1,
  276. Num: 5,
  277. },
  278. Card2: Card{
  279. Suit: 2,
  280. Num: 7,
  281. },
  282. Card3: Card{
  283. Suit: 3,
  284. Num: 8,
  285. },
  286. Card4: Card{
  287. Suit: 1,
  288. Num: 10,
  289. },
  290. Card5: Card{
  291. Suit: 4,
  292. Num: 11,
  293. },
  294. },
  295. },
  296. },
  297. },
  298. args: args{
  299. tx: db.GetDB(),
  300. roomBaseAmount: decimal.NewFromInt(3),
  301. roomID: 1,
  302. },
  303. want: map[int32]PayoutInfo{
  304. 22: {
  305. PlayerID: 22,
  306. Amount: decimal.Zero,
  307. IsMaster: true,
  308. },
  309. 23: {
  310. PlayerID: 23,
  311. Amount: decimal.Zero,
  312. IsMaster: false,
  313. },
  314. },
  315. wantErr: false,
  316. },
  317. }
  318. for _, tt := range tests {
  319. t.Run(tt.name, func(t *testing.T) {
  320. p := &Game{
  321. RoomMap: tt.fields.RoomMap,
  322. PlayerMap: tt.fields.PlayerMap,
  323. }
  324. got, err := p.payout(tt.args.tx, tt.args.roomBaseAmount, tt.args.roomID)
  325. if (err != nil) != tt.wantErr {
  326. t.Errorf("Game.payout() error = %v, wantErr %v", err, tt.wantErr)
  327. return
  328. }
  329. if !got[22].Amount.Equal(tt.want[22].Amount) {
  330. t.Errorf("Game.payout() = %v, want %v", got, tt.want)
  331. }
  332. })
  333. }
  334. }
  335. func TestGame_payout_庄没钱2(t *testing.T) {
  336. viper.Set("gormlog", true)
  337. viper.Set("db.username", "root")
  338. viper.Set("db.password", "123456~")
  339. viper.Set("db.addr", "10.42.50.148:3306")
  340. viper.Set("db.name", "qznn")
  341. db.Init()
  342. db.GetDB().Model(model.User{}).Where("id = 22").Update("credits_qznn", 72)
  343. db.GetDB().Model(model.User{}).Where("id = 23").Update("credits_qznn", 10000)
  344. db.GetDB().Model(model.User{}).Where("id = 24").Update("credits_qznn", 10000)
  345. type fields struct {
  346. RoomMap map[int32]Room
  347. PlayerMap map[int32]Player
  348. IDGener *snowflake.Node
  349. LogID int64
  350. }
  351. type args struct {
  352. tx *gorm.DB
  353. roomBaseAmount decimal.Decimal
  354. roomID int32
  355. }
  356. tests := []struct {
  357. name string
  358. fields fields
  359. args args
  360. want map[int32]PayoutInfo
  361. wantErr bool
  362. }{
  363. {
  364. name: "normal",
  365. fields: fields{
  366. RoomMap: map[int32]Room{
  367. 1: Room{
  368. ID: 1,
  369. Name: "test",
  370. RoomType: int32(model.RoomTypeFree),
  371. ChairList: []Chair{
  372. Chair{
  373. ID: 0,
  374. PlayerID: 22,
  375. },
  376. Chair{
  377. ID: 1,
  378. PlayerID: 23,
  379. },
  380. Chair{
  381. ID: 1,
  382. PlayerID: 24,
  383. },
  384. },
  385. BaseAmount: decimal.NewFromInt(3),
  386. Issue: "123",
  387. Status: RoomStatusOpen,
  388. StatusStartTime: time.Now().Unix(),
  389. ReadyCh: make(chan bool),
  390. ChooseMasterCh: make(chan bool),
  391. ChooseMulCh: make(chan bool),
  392. OpenCh: make(chan bool),
  393. },
  394. },
  395. PlayerMap: map[int32]Player{
  396. 22: Player{
  397. ID: 22,
  398. Name: "test_player22",
  399. Balance: decimal.NewFromInt(0),
  400. Credits: decimal.NewFromInt(72),
  401. RoomID: 1,
  402. IsMaster: true,
  403. MasterMul: 3,
  404. Mul: 1,
  405. Status: PlayerStatusOpen,
  406. CardList: CardList2NNCardList(getOpenNumList("48,10,37,22,0")),
  407. },
  408. 23: Player{
  409. ID: 23,
  410. Name: "test_player23",
  411. Balance: decimal.NewFromInt(0),
  412. Credits: decimal.NewFromInt(10000),
  413. RoomID: 1,
  414. IsMaster: false,
  415. MasterMul: 2,
  416. Mul: 4,
  417. Status: PlayerStatusOpen,
  418. CardList: CardList2NNCardList(getOpenNumList("49,19,50,5,32")),
  419. },
  420. 24: Player{
  421. ID: 24,
  422. Name: "test_player24",
  423. Balance: decimal.NewFromInt(0),
  424. Credits: decimal.NewFromInt(10000),
  425. RoomID: 1,
  426. IsMaster: false,
  427. MasterMul: 1,
  428. Mul: 1,
  429. Status: PlayerStatusOpen,
  430. CardList: CardList2NNCardList(getOpenNumList("14,4,45,21,51")),
  431. },
  432. },
  433. },
  434. args: args{
  435. tx: db.GetDB(),
  436. roomBaseAmount: decimal.NewFromInt(3),
  437. roomID: 1,
  438. },
  439. want: map[int32]PayoutInfo{
  440. 22: {
  441. PlayerID: 22,
  442. Amount: decimal.NewFromInt(-72),
  443. IsMaster: true,
  444. },
  445. 23: {
  446. PlayerID: 23,
  447. Amount: decimal.NewFromInt(81),
  448. IsMaster: false,
  449. },
  450. 24: {
  451. PlayerID: 24,
  452. Amount: decimal.NewFromInt(-9),
  453. IsMaster: false,
  454. },
  455. },
  456. wantErr: false,
  457. },
  458. }
  459. for _, tt := range tests {
  460. t.Run(tt.name, func(t *testing.T) {
  461. p := &Game{
  462. RoomMap: tt.fields.RoomMap,
  463. PlayerMap: tt.fields.PlayerMap,
  464. }
  465. got, err := p.payout(tt.args.tx, tt.args.roomBaseAmount, tt.args.roomID)
  466. if (err != nil) != tt.wantErr {
  467. t.Errorf("Game.payout() error = %v, wantErr %v", err, tt.wantErr)
  468. return
  469. }
  470. if !got[22].Amount.Equal(tt.want[22].Amount) {
  471. t.Errorf("Game.payout() = %v, want %v", got, tt.want)
  472. }
  473. if !got[23].Amount.Equal(tt.want[23].Amount) {
  474. t.Errorf("Game.payout() = %v, want %v", got, tt.want)
  475. }
  476. if !got[24].Amount.Equal(tt.want[24].Amount) {
  477. t.Errorf("Game.payout() = %v, want %v", got, tt.want)
  478. }
  479. })
  480. }
  481. }