tablesink_impl.go 116 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896
  1. package gamelogic
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math/rand"
  6. "os"
  7. "path/filepath"
  8. "sort"
  9. "time"
  10. "bet24.com/servers/games/baloot/config"
  11. robotmanager "bet24.com/servers/insecureframe/robot"
  12. badge "bet24.com/servers/micros/badge/proto"
  13. cardlibrary "bet24.com/servers/micros/cardlibrary/proto"
  14. ladder "bet24.com/servers/micros/ladderservice/proto"
  15. task "bet24.com/servers/micros/task/proto"
  16. userservices "bet24.com/servers/micros/userservices/proto"
  17. waterpool "bet24.com/servers/micros/waterpool/proto"
  18. "bet24.com/servers/user"
  19. "github.com/google/uuid"
  20. excelize "github.com/xuri/excelize/v2"
  21. )
  22. func (ts *tablesink) checkAndStartGame() {
  23. if ts.gameScene.Phase != Phase_Free &&
  24. ts.gameScene.Phase != Phase_End &&
  25. ts.gameScene.Phase != Phase_GameEnd {
  26. return
  27. }
  28. // 查看是否所有人都ready
  29. readyCount := 0
  30. for i := 0; i < CHAIR_COUNT; i++ {
  31. usr := ts.table.GetUserByChair(i)
  32. if usr == nil {
  33. continue
  34. }
  35. userStatus := usr.GetUserStatus()
  36. // 旁观不判断
  37. if userStatus <= user.UserStatus_Free ||
  38. userStatus == user.UserStatus_Watch {
  39. continue
  40. }
  41. if usr.GetUserStatus() != user.UserStatus_Ready && userStatus != user.UserStatus_Offline {
  42. ts.table.LogWithTableId("-----checkAndStartGame: chair[%d]: %d, status: %d", i, usr.GetUserId(), usr.GetUserStatus())
  43. return
  44. }
  45. readyCount++
  46. }
  47. ts.table.LogWithTableId("-----checkAndStartGame : %d ", readyCount)
  48. if readyCount >= CHAIR_COUNT {
  49. ts.startGame()
  50. }
  51. }
  52. func (ts *tablesink) checkUserReadyStatus(chair int) {
  53. if ts.gameScene.Phase != Phase_Free && ts.gameScene.Phase != Phase_End && ts.gameScene.Phase != Phase_GameEnd {
  54. ts.table.LogWithTableId("tablesink.checkUserReadyStatus status not right %d", ts.gameScene.Phase)
  55. return
  56. }
  57. usr := ts.table.GetUserByChair(chair)
  58. if usr == nil {
  59. ts.table.LogWithTableId("tablesink.checkUserReadyStatus user not exist %d", chair)
  60. return
  61. }
  62. if ts.gameScene.Phase == Phase_GameEnd {
  63. ts.table.SetUserReadyStatus(usr.GetUserIndex(), true)
  64. return
  65. }
  66. if !usr.IsRobot() {
  67. ts.table.LogWithTableId("tablesink.checkUserReadyStatus kicking [%d]: %d ", chair, usr.GetUserId())
  68. ts.table.UserWatch(usr.GetUserIndex())
  69. return
  70. }
  71. //桌上还有没有真人,有就准备,没有就踢走
  72. count := 0
  73. for i := 0; i < CHAIR_COUNT; i++ {
  74. u := ts.table.GetUserByChair(i)
  75. if u == nil {
  76. continue
  77. }
  78. if u.IsRobot() {
  79. continue
  80. }
  81. if u.GetUserStatus() <= user.UserStatus_Free {
  82. continue
  83. }
  84. count++
  85. }
  86. if count >= 1 || (ts.roomInfo.Test && count >= 0) {
  87. ts.table.SetUserReadyStatus(usr.GetUserIndex(), true)
  88. } else {
  89. ts.table.LogWithTableId("tablesink.checkUserReadyStatus: %d ", usr.GetUserId())
  90. if usr.IsRobot() {
  91. //ts.table.UserWatch(usr.GetUserIndex())
  92. ts.table.KickUser(usr.GetUserIndex(), false)
  93. } else {
  94. ts.table.UserWatch(usr.GetUserIndex())
  95. }
  96. }
  97. }
  98. func (ts *tablesink) onPlayerFirstOutCard() {
  99. ts.gameScene.WhoseTurn = getPreviousChair(ts.gameScene.Banker)
  100. ts.gameScene.RoundIndex++
  101. ts.gameScene.LastTurn = -1
  102. ts.gameScene.Phase = Phase_Play
  103. ts.gameScene.PhaseIndex = 0
  104. ts.allPassCount = 0
  105. // 从初始化模拟场景数据
  106. ts.simulator = newSimulatorScene()
  107. ts.simulator.initData(ts.gameScene.TrumpType, ts.gameScene.FinalClub, ts.gameScene.BuyChair, ts.gameScene.BaseTimes,
  108. ts.gameScene.IsBO1, ts.gameScene.IsClose)
  109. delaySec := 0
  110. for i := 0; i < CHAIR_COUNT; i++ {
  111. ts.simulator.fakers[i].handCards = append(ts.simulator.fakers[i].handCards, ts.gameScene.Players[i].HandCards...)
  112. ts.simulator.fakers[i].actionProb = ts.gameScene.Players[i].robotActionProb
  113. cardList := make([]int, len(ts.gameScene.Players[i].HandCards))
  114. copy(cardList, ts.gameScene.Players[i].HandCards)
  115. ts.gameScene.Players[i].ProjectTips = ts.logic.getBestProject(cardList)
  116. if len(ts.gameScene.Players[i].ProjectTips) > 0 {
  117. if ts.gameScene.Players[i].ProjectTips[0] >= PROJECT_FIFTY && isMostOverKInHandCard(ts.gameScene.Players[i].HandCards, ts.gameScene.TrumpType) {
  118. ts.sendRobotChatAction(RobotChatAction_GoodHandCard, ts.gameScene.Players[i].userID, -1, delaySec)
  119. delaySec += 2500
  120. }
  121. }
  122. if isAllBelow10InHandCard(ts.gameScene.Players[i].HandCards) {
  123. ts.sendRobotChatAction(RobotChatAction_BadHandCard, ts.gameScene.Players[i].userID, -1, delaySec)
  124. delaySec += 2500
  125. }
  126. }
  127. ts.table.NotifySceneChanged(-1)
  128. ts.resetGameTimer()
  129. }
  130. func (ts *tablesink) isMeetTheStorageConditions() bool {
  131. if !ts.gameScene.isBuyChairWin() {
  132. return false
  133. }
  134. buyWinCardScore := 0
  135. playerWinCardScore := 0
  136. for i := 0; i < CHAIR_COUNT; i++ {
  137. if isSameTeam(i, ts.gameScene.BuyChair) {
  138. buyWinCardScore += ts.gameScene.Players[i].winCardScore
  139. } else {
  140. playerWinCardScore += ts.gameScene.Players[i].winCardScore
  141. }
  142. }
  143. differ := buyWinCardScore - playerWinCardScore
  144. if differ < 30 {
  145. return false
  146. }
  147. percent := 0
  148. winScore := ts.gameScene.Players[ts.gameScene.BuyChair].winCardScore
  149. friendWinScore := ts.gameScene.Players[getFriendChair(ts.gameScene.BuyChair)].winCardScore
  150. percent = (winScore * 100) / (winScore + friendWinScore)
  151. return percent >= 60
  152. }
  153. func (ts *tablesink) isSatisfyTheCondition() bool {
  154. if ts.gameScene.FinalClub != Suit_Sun {
  155. return false
  156. }
  157. cardList := []int{}
  158. for i := 0; i < BUY_PHRSE_HOLD_CARD; i++ {
  159. cardList = append(cardList, ts.gameScene.Players[ts.gameScene.BuyChair].bakHandCards[i])
  160. }
  161. cardList = append(cardList, ts.gameScene.PublicCard)
  162. sortedCards := sortCards(cardList)
  163. // if haveFourA(cardList) {
  164. // return false
  165. // }
  166. maxCardNum := 0
  167. if ts.gameScene.FinalClub == Suit_Sun {
  168. for i := CardType_Diamond; i <= CardType_Spade; i++ {
  169. maxCardNum += getScoreCardNum(sortedCards[i], i, false)
  170. }
  171. if maxCardNum >= 4 {
  172. return true
  173. }
  174. }
  175. if ts.gameScene.FinalClub != Suit_Hokum {
  176. return false
  177. }
  178. if len(sortedCards[ts.gameScene.TrumpType]) < 3 {
  179. return false
  180. }
  181. if len(sortedCards[ts.gameScene.TrumpType]) >= 5 {
  182. return true
  183. }
  184. maxCardNum = 0
  185. trumpBigCardNum := getScoreCardNum(sortedCards[ts.gameScene.TrumpType], ts.gameScene.TrumpType, true)
  186. if len(sortedCards[ts.gameScene.TrumpType]) >= 3 && trumpBigCardNum >= 1 {
  187. for j := CardType_Diamond; j <= CardType_Spade; j++ {
  188. if ts.gameScene.TrumpType == j {
  189. continue
  190. }
  191. maxCardNum += getScoreCardNum(sortedCards[j], j, false)
  192. }
  193. if maxCardNum >= 1 {
  194. return true
  195. }
  196. }
  197. return false
  198. }
  199. func (ts *tablesink) statisRobotChatCount() {
  200. if !ts.roomInfo.Test {
  201. return
  202. }
  203. var f *excelize.File
  204. var err error
  205. sheetName := "Sheet1"
  206. // 先查看名为excelName的表格是否存在
  207. dirName := "./baloot_excel/robotchat"
  208. fileName := "baloot" + ts.uniqueId + ".xlsx"
  209. _, err1 := os.Stat(fileName)
  210. if err1 != nil {
  211. // 创建一个新的 Excel
  212. f = excelize.NewFile()
  213. // 在工作簿中添加一个名为 "Sheet1" 的工作表
  214. f.NewSheet(sheetName)
  215. // 设置列宽
  216. f.SetColWidth(sheetName, "A", "A", 5)
  217. f.SetColWidth(sheetName, "B", "B", 5)
  218. f.SetColWidth(sheetName, "C", "C", 5)
  219. f.SetColWidth(sheetName, "D", "D", 200)
  220. } else {
  221. // 打开一个现有的 Excel 文件
  222. f, err = excelize.OpenFile(fileName)
  223. if err != nil {
  224. return
  225. }
  226. }
  227. // 设置对齐方式
  228. // 水平对齐方式 center left right fill(填充) justify(两端对齐) centerContinuous(跨列居中) distributed(分散对齐)
  229. // 垂直对齐方式 center top justify distributed
  230. style, _ := f.NewStyle(&excelize.Style{
  231. Alignment: &excelize.Alignment{
  232. Horizontal: "center",
  233. Vertical: "center",
  234. },
  235. })
  236. // 获取已有数据的行数
  237. rows, err := f.GetRows(sheetName)
  238. if err != nil {
  239. return
  240. }
  241. rowIndex := len(rows)
  242. data := [][]string{}
  243. for i := 0; i < len(ts.gameScene.statics); i++ {
  244. str := []string{
  245. fmt.Sprintf("%d", ts.gameScene.ScoreToWin),
  246. fmt.Sprintf("%d", ts.gameScene.statics[i].round),
  247. fmt.Sprintf("%d", len(ts.gameScene.statics[i].action)),
  248. getRobotChatDesc(ts.gameScene.statics[i].action)}
  249. data = append(data, str)
  250. }
  251. for _, row := range data {
  252. for colIndex, col := range row {
  253. cellName, _ := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
  254. f.SetCellValue(sheetName, cellName, col)
  255. f.SetCellStyle(sheetName, cellName, cellName, style)
  256. }
  257. rowIndex++
  258. }
  259. f.SaveAs(filepath.Join(dirName, fileName))
  260. }
  261. func (ts *tablesink) statisticsScoreHistoryToExcel() {
  262. if !ts.roomInfo.Test {
  263. return
  264. }
  265. var f *excelize.File
  266. var err error
  267. sheetName := "Sheet1"
  268. // 先查看名为excelName的表格是否存在
  269. dirName := "./baloot_excel/scorehistory"
  270. fileName := "baloot" + ts.uniqueId + ".xlsx"
  271. _, err1 := os.Stat(fileName)
  272. if err1 != nil {
  273. // 创建一个新的 Excel
  274. f = excelize.NewFile()
  275. // 在工作簿中添加一个名为 "Sheet1" 的工作表
  276. f.NewSheet(sheetName)
  277. // 设置列宽
  278. f.SetColWidth(sheetName, "A", "A", 5)
  279. f.SetColWidth(sheetName, "B", "B", 100)
  280. } else {
  281. // 打开一个现有的 Excel 文件
  282. f, err = excelize.OpenFile(fileName)
  283. if err != nil {
  284. return
  285. }
  286. }
  287. // 设置对齐方式
  288. // 水平对齐方式 center left right fill(填充) justify(两端对齐) centerContinuous(跨列居中) distributed(分散对齐)
  289. // 垂直对齐方式 center top justify distributed
  290. style, _ := f.NewStyle(&excelize.Style{
  291. Alignment: &excelize.Alignment{
  292. Horizontal: "center",
  293. Vertical: "center",
  294. },
  295. })
  296. // 获取已有数据的行数
  297. rows, err := f.GetRows(sheetName)
  298. if err != nil {
  299. return
  300. }
  301. rowIndex := len(rows)
  302. data := [][]string{
  303. {fmt.Sprintf("%d", ts.gameScene.GameIndex), getScoreHistoryDesc(ts.gameScene.scoreHistory)},
  304. }
  305. for _, row := range data {
  306. for colIndex, col := range row {
  307. cellName, _ := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
  308. f.SetCellValue(sheetName, cellName, col)
  309. f.SetCellStyle(sheetName, cellName, cellName, style)
  310. }
  311. rowIndex++
  312. }
  313. f.SaveAs(filepath.Join(dirName, fileName))
  314. }
  315. func (ts *tablesink) genCardLibrary() {
  316. var f *excelize.File
  317. var err error
  318. sheetName := "Sheet1"
  319. // 先查看名为excelName的表格是否存在
  320. dirName := "./baloot_excel/cardlibrary"
  321. fileName := "baloot" + ts.uniqueId + ".xlsx"
  322. _, err1 := os.Stat(fileName)
  323. if err1 != nil {
  324. // 创建一个新的 Excel
  325. f = excelize.NewFile()
  326. // 在工作簿中添加一个名为 "Sheet1" 的工作表
  327. f.NewSheet(sheetName)
  328. // 设置列宽
  329. f.SetColWidth(sheetName, "A", "AI", 5)
  330. } else {
  331. // 打开一个现有的 Excel 文件
  332. f, err = excelize.OpenFile(fileName)
  333. if err != nil {
  334. return
  335. }
  336. }
  337. // 获取已有数据的行数
  338. rows, err := f.GetRows(sheetName)
  339. if err != nil {
  340. return
  341. }
  342. rowIndex := len(rows)
  343. // 追加数据到现有工作表
  344. data := [][]string{
  345. {fmt.Sprintf("%d", ts.gameScene.FirstActionChair), fmt.Sprintf("%d", ts.gameScene.BuyChair),
  346. fmt.Sprintf("%d", ts.logic.cards[0]), fmt.Sprintf("%d", ts.logic.cards[1]), fmt.Sprintf("%d", ts.logic.cards[2]),
  347. fmt.Sprintf("%d", ts.logic.cards[3]), fmt.Sprintf("%d", ts.logic.cards[4]), fmt.Sprintf("%d", ts.logic.cards[5]),
  348. fmt.Sprintf("%d", ts.logic.cards[6]), fmt.Sprintf("%d", ts.logic.cards[7]), fmt.Sprintf("%d", ts.logic.cards[8]),
  349. fmt.Sprintf("%d", ts.logic.cards[9]), fmt.Sprintf("%d", ts.logic.cards[10]), fmt.Sprintf("%d", ts.logic.cards[11]),
  350. fmt.Sprintf("%d", ts.logic.cards[12]), fmt.Sprintf("%d", ts.logic.cards[13]), fmt.Sprintf("%d", ts.logic.cards[14]),
  351. fmt.Sprintf("%d", ts.logic.cards[15]), fmt.Sprintf("%d", ts.logic.cards[16]), fmt.Sprintf("%d", ts.logic.cards[17]),
  352. fmt.Sprintf("%d", ts.logic.cards[18]), fmt.Sprintf("%d", ts.logic.cards[19]), fmt.Sprintf("%d", ts.logic.cards[20]),
  353. fmt.Sprintf("%d", ts.logic.cards[21]), fmt.Sprintf("%d", ts.logic.cards[22]), fmt.Sprintf("%d", ts.logic.cards[23]),
  354. fmt.Sprintf("%d", ts.logic.cards[24]), fmt.Sprintf("%d", ts.logic.cards[25]), fmt.Sprintf("%d", ts.logic.cards[26]),
  355. fmt.Sprintf("%d", ts.logic.cards[27]), fmt.Sprintf("%d", ts.logic.cards[28]), fmt.Sprintf("%d", ts.logic.cards[29]),
  356. fmt.Sprintf("%d", ts.logic.cards[30]), fmt.Sprintf("%d", ts.logic.cards[31]),
  357. fmt.Sprintf("%d", ts.gameScene.currentRoundDoubling)},
  358. }
  359. // 设置对齐方式
  360. // 水平对齐方式 center left right fill(填充) justify(两端对齐) centerContinuous(跨列居中) distributed(分散对齐)
  361. // 垂直对齐方式 center top justify distributed
  362. style, _ := f.NewStyle(&excelize.Style{
  363. Alignment: &excelize.Alignment{
  364. Horizontal: "center",
  365. Vertical: "center",
  366. },
  367. })
  368. for _, row := range data {
  369. for colIndex, col := range row {
  370. cellName, _ := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
  371. f.SetCellValue(sheetName, cellName, col)
  372. f.SetCellStyle(sheetName, cellName, cellName, style)
  373. }
  374. rowIndex++
  375. }
  376. // 保存到Excel文件中
  377. f.SaveAs(filepath.Join(dirName, fileName))
  378. }
  379. func (ts *tablesink) haveSmallProject() bool {
  380. for i := 0; i < CHAIR_COUNT; i++ {
  381. if len(ts.gameScene.Players[i].Projects) == 0 {
  382. continue
  383. }
  384. for j := 0; j < len(ts.gameScene.Players[i].Projects); j++ {
  385. if ts.gameScene.Players[i].bakProjects[j].Type == PROJECT_FOURHUNDRED {
  386. return false
  387. }
  388. if isBothInclude9J(ts.gameScene.Players[i].bakProjects[j].Cards) {
  389. return false
  390. }
  391. }
  392. return true
  393. }
  394. return false
  395. }
  396. func (ts *tablesink) statisticsRobotAction() {
  397. if !ts.roomInfo.Test {
  398. return
  399. }
  400. var f *excelize.File
  401. var err error
  402. sheetName := "Sheet1"
  403. // 先查看名为excelName的表格是否存在
  404. dirName := "./baloot_excel/robotaction"
  405. fileName := "baloot" + ts.uniqueId + ".xlsx"
  406. _, err1 := os.Stat(fileName)
  407. if err1 != nil {
  408. // 创建一个新的 Excel
  409. f = excelize.NewFile()
  410. // 在工作簿中添加一个名为 "Sheet1" 的工作表
  411. f.NewSheet(sheetName)
  412. // 设置列宽
  413. f.SetColWidth(sheetName, "A", "D", 10)
  414. } else {
  415. // 打开一个现有的 Excel 文件
  416. f, err = excelize.OpenFile(fileName)
  417. if err != nil {
  418. return
  419. }
  420. }
  421. // 获取已有数据的行数
  422. rows, err := f.GetRows(sheetName)
  423. if err != nil {
  424. return
  425. }
  426. rowIndex := len(rows)
  427. // 追加数据到现有工作表
  428. data := [][]string{}
  429. perStr := []string{}
  430. perStr = append(perStr, fmt.Sprintf("%d", ts.gameScene.FinalClub))
  431. for i := 0; i < CHAIR_COUNT; i++ {
  432. str := ts.getRobotTypeAction(i)
  433. perStr = append(perStr, str)
  434. }
  435. data = append(data, perStr)
  436. // 设置对齐方式
  437. // 水平对齐方式 center left right fill(填充) justify(两端对齐) centerContinuous(跨列居中) distributed(分散对齐)
  438. // 垂直对齐方式 center top justify distributed
  439. style, _ := f.NewStyle(&excelize.Style{
  440. Alignment: &excelize.Alignment{
  441. Horizontal: "center",
  442. Vertical: "center",
  443. },
  444. })
  445. for _, row := range data {
  446. for colIndex, col := range row {
  447. cellName, _ := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
  448. f.SetCellValue(sheetName, cellName, col)
  449. f.SetCellStyle(sheetName, cellName, cellName, style)
  450. }
  451. rowIndex++
  452. }
  453. // 保存到Excel文件中
  454. e := f.SaveAs(filepath.Join(dirName, fileName))
  455. if e != nil {
  456. ts.importDataToExcel()
  457. }
  458. }
  459. func (ts *tablesink) getRobotTypeAction(chairId int) string {
  460. if !ts.gameScene.Players[chairId].isRobot {
  461. return fmt.Sprintf("%d", 40)
  462. }
  463. if ts.gameScene.isBuyChairWin() {
  464. if isSameTeam(ts.gameScene.BuyChair, chairId) {
  465. return fmt.Sprintf("%d", ts.gameScene.Players[chairId].robotType*10+1)
  466. }
  467. return fmt.Sprintf("%d", ts.gameScene.Players[chairId].robotType*10)
  468. }
  469. if isSameTeam(ts.gameScene.BuyChair, chairId) {
  470. return fmt.Sprintf("%d", ts.gameScene.Players[chairId].robotType*10)
  471. }
  472. return fmt.Sprintf("%d", ts.gameScene.Players[chairId].robotType*10+1)
  473. }
  474. func (ts *tablesink) importDataToExcel() {
  475. if !ts.roomInfo.Test || !ts.roomInfo.IsDoublingMode {
  476. return
  477. }
  478. if !ts.isMeetTheStorageConditions() {
  479. return
  480. }
  481. if !ts.isSatisfyTheCondition() {
  482. return
  483. }
  484. if !ts.haveSmallProject() {
  485. return
  486. }
  487. ts.genCardLibrary()
  488. var f *excelize.File
  489. var err error
  490. sheetName := "Sheet1"
  491. // 先查看名为excelName的表格是否存在
  492. dirName := "./baloot_excel/cardlibrarydesc"
  493. fileName := "baloot" + ts.uniqueId + ".xlsx"
  494. _, err1 := os.Stat(fileName)
  495. if err1 != nil {
  496. // 创建一个新的 Excel
  497. f = excelize.NewFile()
  498. // 在工作簿中添加一个名为 "Sheet1" 的工作表
  499. f.NewSheet(sheetName)
  500. // 设置列宽
  501. f.SetColWidth(sheetName, "A", "A", 72)
  502. f.SetColWidth(sheetName, "B", "E", 40)
  503. f.SetColWidth(sheetName, "F", "F", 18)
  504. f.SetColWidth(sheetName, "G", "G", 15)
  505. f.SetColWidth(sheetName, "H", "H", 10)
  506. // 设置对齐方式
  507. // 水平对齐方式 center left right fill(填充) justify(两端对齐) centerContinuous(跨列居中) distributed(分散对齐)
  508. // 垂直对齐方式 center top justify distributed
  509. style, _ := f.NewStyle(&excelize.Style{
  510. Alignment: &excelize.Alignment{
  511. Horizontal: "center",
  512. Vertical: "center",
  513. },
  514. })
  515. f.SetCellStyle(sheetName, "A1", "H1", style)
  516. // 设置工作表的单元格值
  517. f.SetCellValue(sheetName, "A1", "[公共牌], [买牌玩家], [模式], [加倍倍数], [主牌花色], [BO1], [开关], [首出玩家], [买牌阶段]")
  518. f.SetCellValue(sheetName, "B1", "0号椅子玩家[全部手牌][剩余三张牌]")
  519. f.SetCellValue(sheetName, "C1", "1号椅子玩家[全部手牌][剩余三张牌]")
  520. f.SetCellValue(sheetName, "D1", "2号椅子玩家[全部手牌][剩余三张牌]")
  521. f.SetCellValue(sheetName, "E1", "3号椅子玩家[全部手牌][剩余三张牌]")
  522. f.SetCellValue(sheetName, "F1", "[0号, 1号, 2号, 3号]")
  523. f.SetCellValue(sheetName, "G1", "[0号队: 1号队]")
  524. f.SetCellValue(sheetName, "H1", "加倍倍数")
  525. } else {
  526. // 打开一个现有的 Excel 文件
  527. f, err = excelize.OpenFile(fileName)
  528. if err != nil {
  529. return
  530. }
  531. }
  532. // 获取已有数据的行数
  533. rows, err := f.GetRows(sheetName)
  534. if err != nil {
  535. return
  536. }
  537. rowIndex := len(rows)
  538. // 追加数据到现有工作表
  539. data := [][]string{
  540. {fmt.Sprintf("[%s], [%d], [%s], [%d], [%d], [%v], [%v], [%d], [%s]", getCardHex(ts.gameScene.PublicCard),
  541. ts.gameScene.BuyChair, getSuitDesc(ts.gameScene.FinalClub), ts.gameScene.BaseTimes,
  542. ts.gameScene.TrumpType, ts.gameScene.IsBO1, ts.gameScene.IsClose, ts.gameScene.FirstActionChair,
  543. getPhaseDesc(ts.gameScene.buyPhase)),
  544. getCardsHex(ts.gameScene.Players[0].bakHandCards) + getCardsHex(ts.gameScene.Players[0].LeftSendCards),
  545. getCardsHex(ts.gameScene.Players[1].bakHandCards) + getCardsHex(ts.gameScene.Players[1].LeftSendCards),
  546. getCardsHex(ts.gameScene.Players[2].bakHandCards) + getCardsHex(ts.gameScene.Players[2].LeftSendCards),
  547. getCardsHex(ts.gameScene.Players[3].bakHandCards) + getCardsHex(ts.gameScene.Players[3].LeftSendCards),
  548. fmt.Sprintf("[%d, %d, %d, %d]", ts.gameScene.Players[0].winCardScore, ts.gameScene.Players[1].winCardScore,
  549. ts.gameScene.Players[2].winCardScore, ts.gameScene.Players[3].winCardScore),
  550. fmt.Sprintf("[%d : %d]", ts.gameScene.Players[0].TotalChangeScore, ts.gameScene.Players[1].TotalChangeScore),
  551. fmt.Sprintf("%d", ts.gameScene.currentRoundDoubling)},
  552. }
  553. bigChair := 0
  554. bigScore := -1
  555. for i := 0; i < CHAIR_COUNT; i++ {
  556. if ts.gameScene.Players[i].winCardScore > bigScore {
  557. bigChair = i
  558. bigScore = ts.gameScene.Players[i].winCardScore
  559. }
  560. }
  561. // 设置对齐方式
  562. // 水平对齐方式 center left right fill(填充) justify(两端对齐) centerContinuous(跨列居中) distributed(分散对齐)
  563. // 垂直对齐方式 center top justify distributed
  564. style, _ := f.NewStyle(&excelize.Style{
  565. Alignment: &excelize.Alignment{
  566. Horizontal: "center",
  567. Vertical: "center",
  568. },
  569. })
  570. style1, _ := f.NewStyle(&excelize.Style{
  571. Alignment: &excelize.Alignment{
  572. Horizontal: "center",
  573. Vertical: "center",
  574. }, Fill: excelize.Fill{
  575. Type: "pattern", // gradient 渐变色 pattern 填充图案
  576. Pattern: 1,
  577. Color: []string{"#FFFF00"},
  578. Shading: 1, // 类型是 gradient 使用 0-5 横向(每种颜色横向分布) 纵向 对角向上 对角向下 有外向内 由内向外
  579. },
  580. })
  581. for _, row := range data {
  582. for colIndex, col := range row {
  583. cellName, _ := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
  584. f.SetCellValue(sheetName, cellName, col)
  585. if (bigChair + 1) == colIndex {
  586. f.SetCellStyle(sheetName, cellName, cellName, style1)
  587. } else {
  588. f.SetCellStyle(sheetName, cellName, cellName, style)
  589. }
  590. }
  591. rowIndex++
  592. }
  593. // 保存到Excel文件中
  594. f.SaveAs(filepath.Join(dirName, fileName))
  595. }
  596. func (ts *tablesink) dealGameTimeOut() {
  597. if ts.gameScene.Phase == Phase_End {
  598. return
  599. }
  600. switch ts.gameScene.Phase {
  601. case Phase_Start:
  602. ts.dealGamePlay()
  603. return
  604. case Phase_GameEnd:
  605. isSurrenderFinish := ts.surrenderInfo.isSurrenderEnd()
  606. if ts.gameScene.nextGame(isSurrenderFinish) {
  607. // 比赛场流程特殊处理
  608. if ts.table.GetOwner() == -1 && config.Server.IsLadderRoom == 0 {
  609. ts.gameScene.Phase = Phase_Free
  610. ts.table.NotifySceneChanged(-1)
  611. ts.table.SetTimer(TIMER_GAME, 3000)
  612. } else {
  613. ts.gameStart()
  614. diff := ts.gameScene.Players[0].TotalScore - ts.gameScene.Players[1].TotalScore
  615. win := 0
  616. if diff <= -80 || diff >= 80 {
  617. if diff >= 80 {
  618. win = 1
  619. }
  620. start := rand.Intn(4)
  621. for i := 0; i < CHAIR_COUNT; i++ {
  622. chairId := (start + i) % CHAIR_COUNT
  623. if !isSameTeam(chairId, win) && ts.gameScene.Players[chairId].isRobot {
  624. ts.sendRobotChatAction(RobotChatAction_LoseOverEighty, ts.gameScene.Players[chairId].userID, -1, 0)
  625. break
  626. }
  627. }
  628. }
  629. }
  630. } else {
  631. if isSurrenderFinish {
  632. ts.enterSurrenderEndPhase()
  633. } else {
  634. ts.enterEndPhase()
  635. }
  636. }
  637. return
  638. case Phase_Correct:
  639. ts.onTimerCorrectStart()
  640. return
  641. case Phase_Free:
  642. ts.gameStart()
  643. return
  644. case Phase_ChooseTrump:
  645. if !isValidChair(ts.gameScene.WhoseTurn) {
  646. ts.table.LogWithTableId("tablesink.dealGameTimeOut invalid whoseturn")
  647. return
  648. }
  649. if !ts.gameScene.Players[ts.gameScene.WhoseTurn].isRobot {
  650. ts.gameScene.Players[ts.gameScene.WhoseTurn].AutoOut = true
  651. }
  652. cardList := make([]int, len(ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards))
  653. copy(cardList, ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards)
  654. cardList = append(cardList, ts.gameScene.PublicCard)
  655. t := getBestTrumpType(cardList, ts.gameScene.PublicCard)
  656. ts.dealChooseTrump(t)
  657. return
  658. case Phase_Reshuffle:
  659. for i := 0; i < CHAIR_COUNT; i++ {
  660. if ts.gameScene.Players[i].ReshuffleAction == Reshuffle_None && ts.gameScene.Players[i].CanDoReshuffle {
  661. if !ts.gameScene.Players[i].isRobot {
  662. ts.gameScene.Players[i].AutoOut = true
  663. }
  664. ts.dealReshuffle(i, Reshuffle_Pass)
  665. }
  666. }
  667. return
  668. case Phase_Double:
  669. for i := 0; i < CHAIR_COUNT; i++ {
  670. action := ts.gameScene.Players[i].getAutoBuyAction()
  671. if action != Action_Buy_Invaild && ts.gameScene.Players[i].canAction() {
  672. ts.gameScene.Players[i].AutoOut = true
  673. ts.dealDouble(i, action)
  674. }
  675. }
  676. return
  677. case Phase_Play:
  678. if !isValidChair(ts.gameScene.WhoseTurn) {
  679. ts.table.LogWithTableId("tablesink.dealGameTimeOut invalid whoseturn")
  680. return
  681. }
  682. projects := []int{}
  683. if len(ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards) == 1 {
  684. ts.dealOutCard(ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards[0], projects)
  685. return
  686. }
  687. if !ts.gameScene.Players[ts.gameScene.WhoseTurn].isRobot {
  688. ts.gameScene.Players[ts.gameScene.WhoseTurn].AutoOut = true
  689. }
  690. if ts.gameScene.Players[ts.gameScene.WhoseTurn].CanGawah {
  691. ts.dealGawah()
  692. return
  693. }
  694. outCard := CARD_COUNT
  695. if len(ts.gameScene.Players[ts.gameScene.WhoseTurn].BigCards) > 0 {
  696. outCard = ts.gameScene.Players[ts.gameScene.WhoseTurn].BigCards[0]
  697. } else {
  698. outCard = ts.logic.worstCard(ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards, ts.gameScene.RoundType)
  699. }
  700. ts.dealOutCard(outCard, projects)
  701. return
  702. case Phase_FirstBuy:
  703. fallthrough
  704. case Phase_SecondBuy:
  705. if ts.gameScene.WhoseTurn != -1 {
  706. action := ts.gameScene.Players[ts.gameScene.WhoseTurn].getAutoBuyAction()
  707. if action != Action_Buy_Invaild && ts.gameScene.Players[ts.gameScene.WhoseTurn].canAction() {
  708. if !ts.gameScene.Players[ts.gameScene.WhoseTurn].isRobot {
  709. ts.gameScene.Players[ts.gameScene.WhoseTurn].AutoOut = true
  710. }
  711. ts.dealBuy(ts.gameScene.WhoseTurn, action)
  712. }
  713. } else {
  714. for i := 0; i < CHAIR_COUNT; i++ {
  715. action := ts.gameScene.Players[i].getAutoBuyAction()
  716. if action != Action_Buy_Invaild && ts.gameScene.Players[i].canAction() {
  717. if !ts.gameScene.Players[i].isRobot {
  718. ts.gameScene.Players[i].AutoOut = true
  719. }
  720. ts.dealBuy(i, action)
  721. }
  722. }
  723. }
  724. return
  725. case Phase_CloseOpen:
  726. if !ts.gameScene.Players[ts.gameScene.WhoseTurn].isRobot {
  727. ts.gameScene.Players[ts.gameScene.WhoseTurn].AutoOut = true
  728. }
  729. ts.dealCloseOrOpen(Action_Buy_Open)
  730. return
  731. }
  732. ts.table.LogWithTableId("tablesink.dealGameTimeOut unreachable")
  733. }
  734. func (ts *tablesink) dealGamePlay() {
  735. ts.table.LogWithTableId("------dealGamePlay -----")
  736. ts.gameScene.Phase = Phase_FirstBuy
  737. ts.gameScene.PhaseIndex = 0
  738. for i := 0; i < CHAIR_COUNT; i++ {
  739. u := ts.table.GetUserByChair(i)
  740. if u == nil {
  741. continue
  742. }
  743. buyList := []int{}
  744. if i == ts.gameScene.WhoseTurn {
  745. buyList = append(buyList, Action_Buy_Pass, Action_Buy_Hokum, Action_Buy_Sun)
  746. }
  747. ts.gameScene.Players[i].setPlayerOperators(buyList)
  748. }
  749. ts.table.NotifySceneChanged(-1)
  750. ts.resetGameTimer()
  751. }
  752. func (ts *tablesink) startGame() {
  753. ts.uniqueId = uuid.New().String()
  754. ts.gameScene.initData(ts.table.GetTableID(), ts.roomInfo.ScoreToWin, ts.roomInfo.MaxSurrenderCount)
  755. if ts.canStartGame(true) {
  756. delayTime := 1500
  757. // 比赛场特殊处理
  758. if ts.table.GetOwner() == -1 {
  759. delayTime = 2000
  760. usr := ts.table.GetUserByChair(0)
  761. if usr.GetSetCount() == 1 {
  762. delayTime = 5000
  763. }
  764. } else if ts.table.GetOwner() > 0 {
  765. ts.table.NotifySceneChanged(-1)
  766. delayTime = 3000
  767. }
  768. ts.table.SetTimer(TIMER_START_GAME, delayTime)
  769. }
  770. }
  771. func (ts *tablesink) canStartGame(needCost bool) bool {
  772. count := 0
  773. for i := 0; i < CHAIR_COUNT; i++ {
  774. usr := ts.table.GetUserByChair(i)
  775. if usr == nil {
  776. break
  777. }
  778. userStatus := usr.GetUserStatus()
  779. if userStatus != user.UserStatus_Ready && userStatus != user.UserStatus_Offline {
  780. ts.table.LogWithTableId("ts.startGame chair[%d] userStatue = %d", i, userStatus)
  781. break
  782. }
  783. if !needCost {
  784. count++
  785. continue
  786. }
  787. userGold := ts.table.GetUserChipOrGoldByUser(usr)
  788. if userGold < ts.roomInfo.MinGold || (ts.roomInfo.MaxGold > 0 && userGold > ts.roomInfo.MaxGold) {
  789. ts.table.LogWithTableId("tablesink.startGame %d 金币不足,站起", usr.GetUserId(),userGold,ts.roomInfo.MinGold,ts.roomInfo.MaxGold)
  790. if usr.IsRobot() {
  791. go ts.table.KickUser(usr.GetUserIndex(), false)
  792. } else {
  793. go ts.table.UserWatch(usr.GetUserIndex())
  794. }
  795. break
  796. }
  797. // 先投注
  798. ok, _ := ts.writeScore(usr.GetUserId(), -ts.roomInfo.BaseScore, 0, 0, ScoreType_Bet, ts.roomInfo.RoomName, usr.IsRobot())
  799. if !ok {
  800. break
  801. }
  802. ts.gameScene.Players[i].initData(usr.GetUserId(), userGold, 0)
  803. ts.gameScene.Players[i].IsValid = true
  804. ts.gameScene.Players[i].userID = usr.GetUserId()
  805. ts.gameScene.Players[i].bet = ts.roomInfo.BaseScore
  806. ts.gameScene.Players[i].isRobot = usr.IsRobot()
  807. count++
  808. }
  809. if count < CHAIR_COUNT {
  810. ts.table.LogWithTableId("游戏开始,用户不足")
  811. if !needCost {
  812. return false
  813. }
  814. ts.gameScene.Phase = Phase_End
  815. ts.gameScene.PhaseIndex = 0
  816. // 分数还原
  817. for i := 0; i < CHAIR_COUNT; i++ {
  818. if !ts.gameScene.Players[i].IsValid {
  819. continue
  820. }
  821. if ts.gameScene.Players[i].bet > 0 {
  822. ts.writeScore(ts.gameScene.Players[i].userID, ts.gameScene.Players[i].bet, 0, 0,
  823. ScoreType_Return, ts.roomInfo.RoomName, ts.gameScene.Players[i].isRobot)
  824. }
  825. }
  826. if ts.table.IsPrivate() {
  827. ts.table.PrivateRoomSetWinners([]int{})
  828. }
  829. ts.endGame()
  830. ts.uniqueId = uuid.New().String()
  831. ts.gameScene.initData(ts.table.GetTableID(), ts.roomInfo.ScoreToWin, ts.roomInfo.MaxSurrenderCount)
  832. return false
  833. }
  834. return true
  835. }
  836. func (ts *tablesink) checkNeedControlPlayer() int {
  837. if !ts.roomInfo.IsOpenCardLibrary {
  838. return waterpool.PoolControl_Normal
  839. }
  840. if ts.gameScene.getUserCount() != 1 {
  841. return waterpool.PoolControl_Normal
  842. }
  843. controlType := waterpool.PoolControl_Normal
  844. gameType := waterpool.GameType_Normal
  845. if ts.roomInfo.IsDoublingMode {
  846. gameType = waterpool.GameType_Doubling
  847. }
  848. if ts.roomInfo.IsCorrectionMode {
  849. gameType = waterpool.GameType_Correct
  850. }
  851. isMatch := false
  852. if ts.table.GetOwner() == -1 {
  853. isMatch = true
  854. }
  855. baseScore := ts.roomInfo.BaseScore
  856. for i := 0; i < CHAIR_COUNT; i++ {
  857. usr := ts.table.GetUserByChair(i)
  858. if usr == nil || usr.IsRobot() {
  859. continue
  860. }
  861. if isMatch {
  862. baseScore = usr.GetBaseScore()
  863. }
  864. userId := usr.GetUserId()
  865. poolType, _ := waterpool.GetUserPoolType(userId, GAMEID, gameType, baseScore, usr.GetUserGold(), isMatch)
  866. if poolType == waterpool.PoolControl_Normal {
  867. continue
  868. }
  869. ts.gameScene.Players[i].isControl = true
  870. ts.gameScene.Players[i].controlType = poolType
  871. poolValue := waterpool.GetUserWaterPool(userId)
  872. if poolValue < 0 {
  873. poolValue = -poolValue
  874. }
  875. ts.gameScene.Players[i].controlDoubling = poolValue / baseScore / 2
  876. if ts.gameScene.Players[i].controlDoubling > MAX_DOUBLING_LIMIT {
  877. ts.gameScene.Players[i].controlDoubling = MAX_DOUBLING_LIMIT
  878. }
  879. r := 6 + rand.Intn(4)
  880. ts.gameScene.Players[i].averageDoubling = ts.gameScene.Players[i].controlDoubling / r
  881. if ts.gameScene.ScoreToWin == -1 {
  882. ts.gameScene.Players[i].averageDoubling = ts.gameScene.Players[i].controlDoubling
  883. }
  884. controlType = poolType
  885. }
  886. return controlType
  887. }
  888. func (ts *tablesink) checkNeedControlRobot() {
  889. if !ts.roomInfo.IsOpenCardLibrary {
  890. return
  891. }
  892. controlRobot := ts.gameScene.getControlRobot()
  893. if !isValidChair(controlRobot) {
  894. return
  895. }
  896. controlType := waterpool.GetInventoryType(GAMEID, ts.roomInfo.RoomName)
  897. if controlType > waterpool.PoolControl_Normal {
  898. ts.gameScene.Players[controlRobot].controlType = controlType
  899. ts.gameScene.Players[controlRobot].isControl = true
  900. }
  901. }
  902. func (ts *tablesink) isPlayerEnterChasing(chairId int) bool {
  903. if !ts.gameScene.Players[chairId].isControl {
  904. return false
  905. }
  906. if ts.gameScene.Players[chairId].controlType == waterpool.PoolControl_Lose &&
  907. ts.gameScene.Players[chairId].TotalScore > ts.gameScene.Players[getNextChair(chairId)].TotalScore {
  908. return true
  909. }
  910. if ts.gameScene.Players[chairId].controlType == waterpool.PoolControl_Win &&
  911. ts.gameScene.Players[chairId].TotalScore < ts.gameScene.Players[getNextChair(chairId)].TotalScore {
  912. return true
  913. }
  914. return false
  915. }
  916. func (ts *tablesink) isEnterWindUp() bool {
  917. for i := 0; i < CHAIR_COUNT; i++ {
  918. if ts.gameScene.Players[i].TotalScore >= WINDUP_SCORE {
  919. return true
  920. }
  921. }
  922. return false
  923. }
  924. func (ts *tablesink) getControlChair() (int, int, bool) {
  925. controlChair := -1
  926. realControlChair := -1
  927. if !ts.roomInfo.IsOpenCardLibrary {
  928. return controlChair, realControlChair, false
  929. }
  930. for i := 0; i < CHAIR_COUNT; i++ {
  931. usr := ts.table.GetUserByChair(i)
  932. if usr == nil {
  933. continue
  934. }
  935. if !ts.gameScene.Players[i].isControl {
  936. continue
  937. }
  938. isNeedControl := false
  939. if !ts.roomInfo.IsDoublingMode {
  940. if ts.gameScene.GameIndex == 0 {
  941. isNeedControl = true
  942. } else {
  943. if ts.isPlayerEnterChasing(i) {
  944. isNeedControl = true
  945. }
  946. if ts.isEnterWindUp() {
  947. isNeedControl = true
  948. }
  949. }
  950. } else {
  951. isNeedControl = true
  952. }
  953. if !isNeedControl {
  954. continue
  955. }
  956. controType := ts.gameScene.Players[i].controlType
  957. if controType == waterpool.PoolControl_Win {
  958. controlChair = i
  959. } else {
  960. r := rand.Intn(10000)
  961. if r%2 == 1 {
  962. controlChair = getNextChair(i)
  963. } else {
  964. controlChair = getPreviousChair(i)
  965. }
  966. }
  967. realControlChair = i
  968. }
  969. return controlChair, realControlChair, false
  970. }
  971. func (ts *tablesink) getDoubling(controlChair int) int {
  972. if !isValidChair(controlChair) || !ts.roomInfo.IsDoublingMode {
  973. return -1
  974. }
  975. controlDoubling := ts.gameScene.Players[controlChair].controlDoubling
  976. if controlDoubling <= 0 {
  977. return controlDoubling
  978. }
  979. leftDoubling := 0
  980. if ts.gameScene.Players[controlChair].controlType == waterpool.PoolControl_Win {
  981. leftDoubling = controlDoubling - ts.gameScene.Players[controlChair].getPlayerCurrentTotalDoublings()
  982. } else {
  983. leftDoubling = controlDoubling - ts.gameScene.Players[getNextChair(controlChair)].getPlayerCurrentTotalDoublings()
  984. }
  985. if leftDoubling < ts.gameScene.Players[controlChair].averageDoubling {
  986. return 0
  987. }
  988. return ts.gameScene.Players[controlChair].averageDoubling
  989. }
  990. func (ts *tablesink) gameStart() {
  991. // 发牌
  992. controlChair, realControlChair, bNewUser := ts.getControlChair()
  993. doubling := ts.getDoubling(realControlChair)
  994. newChairId := -1
  995. if ts.roomInfo.RoomID == 0 && !ts.table.IsPrivate() {
  996. bNewUser = true
  997. for i := 0; i < CHAIR_COUNT; i++ {
  998. usr := ts.table.GetUserByChair(i)
  999. if usr == nil {
  1000. continue
  1001. }
  1002. if !usr.IsRobot() {
  1003. newChairId = i
  1004. break
  1005. }
  1006. }
  1007. }
  1008. if bNewUser {
  1009. controlChair = newChairId
  1010. }
  1011. ts.logic.shuffle(ts.roomInfo.Test, controlChair, ts.gameScene.FirstActionChair, doubling, bNewUser)
  1012. if bNewUser || ts.roomInfo.IsOpenCardLibrary {
  1013. for i := 0; i < CHAIR_COUNT; i++ {
  1014. usr := ts.table.GetUserByChair(i)
  1015. if usr == nil {
  1016. continue
  1017. }
  1018. userStatus := usr.GetUserStatus()
  1019. if userStatus <= user.UserStatus_Free ||
  1020. userStatus == user.UserStatus_Watch {
  1021. continue
  1022. }
  1023. ts.gameScene.Players[i].HandCards = ts.logic.getNormalCards()
  1024. ts.gameScene.Players[i].bakHandCards = append(ts.gameScene.Players[i].bakHandCards, ts.gameScene.Players[i].HandCards...)
  1025. }
  1026. } else {
  1027. var list []weightInfo
  1028. for i := 0; i < CHAIR_COUNT; i++ {
  1029. value := cardlibrary.GetUserCardLibraryrWeightValue(ts.gameScene.Players[i].userID)
  1030. if ts.gameScene.Players[i].isRobot {
  1031. value = 0
  1032. }
  1033. list = append(list, weightInfo{
  1034. chair: i,
  1035. weight: value,
  1036. })
  1037. }
  1038. sort.Slice(list, func(i, j int) bool {
  1039. return list[i].weight > list[j].weight
  1040. })
  1041. for i := 0; i < len(list); i++ {
  1042. usr := ts.table.GetUserByChair(list[i].chair)
  1043. if usr == nil {
  1044. continue
  1045. }
  1046. userStatus := usr.GetUserStatus()
  1047. if userStatus <= user.UserStatus_Free ||
  1048. userStatus == user.UserStatus_Watch {
  1049. continue
  1050. }
  1051. ts.gameScene.Players[list[i].chair].HandCards = ts.logic.getNormalCardsInControl(ts.gameScene.Players[list[i].chair].userID)
  1052. ts.gameScene.Players[list[i].chair].bakHandCards = append(ts.gameScene.Players[list[i].chair].bakHandCards, ts.gameScene.Players[list[i].chair].HandCards...)
  1053. }
  1054. }
  1055. ts.gameScene.PublicCard = ts.logic.getOneCard()
  1056. ts.gameScene.Phase = Phase_Start
  1057. ts.gameScene.PhaseIndex = 0
  1058. ts.gameScene.LastTurn = -1
  1059. ts.gameScene.Index++
  1060. if ts.roomInfo.IsDoublingMode && getCardValue(ts.gameScene.PublicCard) == CardValueJ {
  1061. ts.table.SetTimer(TIMER_GAME, SEC_START)
  1062. } else {
  1063. ts.table.SetTimer(TIMER_GAME, 2000)
  1064. }
  1065. ts.table.LogWithTableId("tablesink.gameStart[%d]", ts.table.GetTableID())
  1066. if ts.gameScene.GameIndex == 0 {
  1067. ts.gameScene.dump(true)
  1068. } else {
  1069. ts.table.NotifySceneChanged(-1)
  1070. if ts.roomInfo.IsDoublingMode {
  1071. if getCardValue(ts.gameScene.PublicCard) == CardValueJ {
  1072. ts.addDoublingData(Doubling_PublicCard_J)
  1073. ts.sendShowDoublingMsg([]int{Doubling_PublicCard_J})
  1074. }
  1075. }
  1076. }
  1077. }
  1078. func (ts *tablesink) addDoublingData(doubleType int) {
  1079. bFound := false
  1080. valueDouble := []int{}
  1081. if len(ts.roomInfo.ValueDoubling) != VALUE_DOUBLING_LEN {
  1082. valueDouble = append(valueDouble, value_doubling...)
  1083. } else {
  1084. valueDouble = append(valueDouble, ts.roomInfo.ValueDoubling...)
  1085. }
  1086. for i := 0; i < len(ts.gameScene.DoublingDetails); i++ {
  1087. if doubleType == ts.gameScene.DoublingDetails[i].DoublingType {
  1088. ts.gameScene.DoublingDetails[i].DoublingTimes *= valueDouble[doubleType]
  1089. bFound = true
  1090. break
  1091. }
  1092. }
  1093. if !bFound {
  1094. ts.gameScene.DoublingDetails = append(ts.gameScene.DoublingDetails, DoublingData{
  1095. DoublingType: doubleType,
  1096. DoublingTimes: valueDouble[doubleType],
  1097. })
  1098. }
  1099. }
  1100. func (ts *tablesink) sendShowDoublingMsg(doublingTypes []int) {
  1101. var data ShowDoubling
  1102. data.Index = ts.gameScene.Index
  1103. data.Times = 1
  1104. valueDouble := []int{}
  1105. if len(ts.roomInfo.ValueDoubling) != VALUE_DOUBLING_LEN {
  1106. valueDouble = append(valueDouble, value_doubling...)
  1107. } else {
  1108. valueDouble = append(valueDouble, ts.roomInfo.ValueDoubling...)
  1109. }
  1110. for i := 0; i < len(doublingTypes); i++ {
  1111. data.Times *= valueDouble[doublingTypes[i]]
  1112. }
  1113. d, _ := json.Marshal(data)
  1114. ts.table.SendGameData(-1, CMD_SHOWDOUBLING, string(d))
  1115. }
  1116. func (ts *tablesink) recvChatMsg(userIndex int32, data string) {
  1117. usr := ts.table.GetPlayer(userIndex)
  1118. if usr == nil {
  1119. ts.table.LogWithTableId("tablesink.recvChatMsg,user[%d] not exist", userIndex)
  1120. return
  1121. }
  1122. if ts.gameScene.Phase != Phase_Play {
  1123. return
  1124. }
  1125. ts.table.SendGameData(-1, CMD_TABLECHAT, data)
  1126. }
  1127. func (ts *tablesink) recvCancleAutoMsg(userIndex int32, data string) {
  1128. usr := ts.table.GetPlayer(userIndex)
  1129. if usr == nil {
  1130. ts.table.LogWithTableId("tablesink.recvCancleAutoMsg,user[%d] not exist", userIndex)
  1131. return
  1132. }
  1133. chairId := usr.GetUserChairId()
  1134. if chairId == -1 {
  1135. return
  1136. }
  1137. if !ts.gameScene.Players[chairId].AutoOut {
  1138. return
  1139. }
  1140. ts.gameScene.Players[chairId].AutoOut = false
  1141. // 重启倒计时
  1142. if ts.gameScene.WhoseTurn != chairId {
  1143. return
  1144. }
  1145. if ts.gameScene.Phase == Phase_Play {
  1146. ts.table.SetTimer(TIMER_GAME, ts.getSecond(ts.gameScene.Phase))
  1147. }
  1148. }
  1149. func (ts *tablesink) recvAutoMsg(userIndex int32, data string) {
  1150. usr := ts.table.GetPlayer(userIndex)
  1151. if usr == nil {
  1152. ts.table.LogWithTableId("tablesink.recvAutoMsg,user[%d] not exist", userIndex)
  1153. return
  1154. }
  1155. chairId := usr.GetUserChairId()
  1156. if chairId == -1 {
  1157. return
  1158. }
  1159. ts.gameScene.Players[chairId].AutoOut = true
  1160. // 重启倒计时
  1161. if ts.gameScene.WhoseTurn != chairId {
  1162. return
  1163. }
  1164. if ts.gameScene.Phase == Phase_Play {
  1165. ts.table.SetTimer(TIMER_GAME, SEC_AUTO)
  1166. }
  1167. }
  1168. func (ts *tablesink) recvSyncData(userIndex int32, data string) bool {
  1169. usr := ts.table.GetPlayer(userIndex)
  1170. if usr == nil {
  1171. ts.table.LogWithTableId("tablesink.recvSyncData,user[%d] not exist", userIndex)
  1172. return false
  1173. }
  1174. chairId := usr.GetUserChairId()
  1175. if !isValidChair(chairId) {
  1176. ts.table.LogWithTableId("tablesink.recvSyncData invalid chair %d", chairId)
  1177. return false
  1178. }
  1179. if !ts.gameScene.Players[chairId].IsValid {
  1180. ts.table.LogWithTableId("tablesink.recvSyncData invalid player [%d]", chairId)
  1181. return false
  1182. }
  1183. if chairId != ts.gameScene.WhoseTurn {
  1184. ts.table.LogWithTableId("tablesink.recvSyncData not WhoseTurn %d != WhoseTurn(%d)", chairId, ts.gameScene.WhoseTurn)
  1185. return false
  1186. }
  1187. if ts.gameScene.Phase != Phase_Correct {
  1188. ts.table.LogWithTableId("tablesink.recvSyncData not CorrectPhase %d", ts.gameScene.Phase)
  1189. return false
  1190. }
  1191. ts.table.SendGameData(-1, CMD_SYNCDATA, data)
  1192. return true
  1193. }
  1194. func (ts *tablesink) recvAction(userIndex int32, data string) bool {
  1195. usr := ts.table.GetPlayer(userIndex)
  1196. if usr == nil {
  1197. ts.table.LogWithTableId("tablesink.recvAction,user[%d] not exist", userIndex)
  1198. return false
  1199. }
  1200. chairId := usr.GetUserChairId()
  1201. if !isValidChair(chairId) {
  1202. ts.table.LogWithTableId("tablesink.recvAction invalid chair %d", chairId)
  1203. return false
  1204. }
  1205. ts.gameScene.Players[chairId].AutoOut = false
  1206. if !ts.gameScene.Players[chairId].IsValid {
  1207. ts.table.LogWithTableId("tablesink.recvAction invalid player [%d]", chairId)
  1208. return false
  1209. }
  1210. var cmd CmdAction
  1211. e := json.Unmarshal([]byte(data), &cmd)
  1212. if e != nil {
  1213. ts.table.LogWithTableId("tablesink.recvAction Unmarshal failed %s", data)
  1214. return false
  1215. }
  1216. // 重洗阶段不用判断WhoseTurn,只需知道自己是否CanDoReshuffle即可
  1217. if cmd.Action != Action_CorrectionStart {
  1218. if chairId != ts.gameScene.WhoseTurn && ts.gameScene.WhoseTurn != -1 && ts.gameScene.Phase != Phase_Reshuffle {
  1219. ts.table.LogWithTableId("tablesink.recvAction not WhoseTurn %d != WhoseTurn(%d) action[%s] ",
  1220. chairId, ts.gameScene.WhoseTurn, getActionDesc(cmd.Action))
  1221. return false
  1222. }
  1223. }
  1224. ret := false
  1225. errMsg := ""
  1226. switch cmd.Action {
  1227. case Action_Buy:
  1228. ret, errMsg = ts.dealBuy(chairId, cmd.Param)
  1229. case Action_ChooseTrump:
  1230. ret, errMsg = ts.dealChooseTrump(cmd.Param)
  1231. case Action_OutCard:
  1232. ret, errMsg = ts.dealOutCard(cmd.Param, cmd.Projects)
  1233. case Action_CloseOrOpen:
  1234. ret, errMsg = ts.dealCloseOrOpen(cmd.Param)
  1235. case Action_Gawah:
  1236. ret, errMsg = ts.dealGawah()
  1237. case Action_Double:
  1238. ret, errMsg = ts.dealDouble(chairId, cmd.Param)
  1239. case Action_Reshuffle:
  1240. ret, errMsg = ts.dealReshuffle(chairId, cmd.Param)
  1241. case Action_CorrectionStart:
  1242. ret, errMsg = ts.dealCorrectionStart(chairId, cmd.Param)
  1243. case Action_CorrectionFinish:
  1244. ret, errMsg = ts.dealCorrectionFinish(cmd.Param, cmd.CorrectCards)
  1245. case Action_Sawa:
  1246. ret, errMsg = ts.dealSawa()
  1247. }
  1248. if !ret {
  1249. var cmdFailed CmdActionFailed
  1250. cmdFailed.CmdAction = cmd
  1251. cmdFailed.ErrMsg = errMsg
  1252. d, _ := json.Marshal(cmdFailed)
  1253. ts.table.SendGameData(userIndex, CMD_ACTION_FAILED, string(d))
  1254. }
  1255. return ret
  1256. }
  1257. func (ts *tablesink) recvSurrender(userIndex int32, data string) bool {
  1258. usr := ts.table.GetPlayer(userIndex)
  1259. if usr == nil {
  1260. ts.table.LogWithTableId("tablesink.recvSurrender,user[%d] not exist", userIndex)
  1261. return false
  1262. }
  1263. chairId := usr.GetUserChairId()
  1264. if !isValidChair(chairId) {
  1265. ts.table.LogWithTableId("tablesink.recvSurrender invalid chair %d", chairId)
  1266. return false
  1267. }
  1268. if ts.surrenderInfo.isSurrendering {
  1269. ts.table.LogWithTableId("tablesink.recvSurrender isSurrendering")
  1270. return false
  1271. }
  1272. if ts.gameScene.LeftSurrenderCount <= 0 {
  1273. ts.table.LogWithTableId("tablesink.recvSurrender max operatorCount:%d", ts.gameScene.LeftSurrenderCount)
  1274. return false
  1275. }
  1276. ts.surrenderInfo.isSurrendering = true
  1277. ts.gameScene.LeftSurrenderCount--
  1278. ts.surrenderInfo.WhoseSurrender = chairId
  1279. ts.surrenderInfo.SurrenderResult = Surrender_None
  1280. d := ts.surrenderInfo.getSurrenderInfo()
  1281. ts.table.SendGameData(userIndex, CMD_SURRENDER_RESULT, d)
  1282. friendIndex := ts.table.GetUserByChair(getFriendChair(chairId)).GetUserIndex()
  1283. ts.table.SendGameData(friendIndex, CMD_SURRENDER_RESULT, d)
  1284. // 玩家设置5s倒计时
  1285. if ts.gameScene.Players[getFriendChair(chairId)].isRobot {
  1286. ts.table.SetTimer(TIMER_ROBOT_SURRENDER, 1000+rand.Intn(2000))
  1287. } else {
  1288. ts.table.SetTimer(TIMER_SURRENDER, 5000)
  1289. }
  1290. return true
  1291. }
  1292. func (ts *tablesink) recvSurrenderResult(userIndex int32, data string) bool {
  1293. usr := ts.table.GetPlayer(userIndex)
  1294. if usr == nil {
  1295. ts.table.LogWithTableId("tablesink.recvSurrenderResult,user[%d] not exist", userIndex)
  1296. return false
  1297. }
  1298. chairId := usr.GetUserChairId()
  1299. if !isValidChair(chairId) {
  1300. ts.table.LogWithTableId("tablesink.recvSurrenderResult invalid chair %d", chairId)
  1301. return false
  1302. }
  1303. if !ts.surrenderInfo.isSurrendering {
  1304. ts.table.LogWithTableId("tablesink.recvSurrenderResult not isSurrendering")
  1305. return false
  1306. }
  1307. if getFriendChair(chairId) != ts.surrenderInfo.WhoseSurrender {
  1308. ts.table.LogWithTableId("tablesink.recvSurrenderResult not CorrentChair:%d", chairId)
  1309. return false
  1310. }
  1311. var cmd int
  1312. e := json.Unmarshal([]byte(data), &cmd)
  1313. if e != nil {
  1314. ts.table.LogWithTableId("tablesink.recvSurrenderResult Unmarshal failed %s", data)
  1315. return false
  1316. }
  1317. if cmd != Surrender_Failed && cmd != Surrender_Success {
  1318. ts.table.LogWithTableId("tablesink.recvSurrenderResult cmd ERROR %d", cmd)
  1319. return false
  1320. }
  1321. ts.surrenderInfo.SurrenderResult = cmd
  1322. ts.table.KillTimer(TIMER_SURRENDER)
  1323. d := ts.surrenderInfo.getSurrenderInfo()
  1324. if cmd == Surrender_Success {
  1325. ts.gameScene.Players[ts.surrenderInfo.WhoseSurrender].IsSurrender = true
  1326. ts.table.SendGameData(-1, CMD_SURRENDER_RESULT, d)
  1327. } else {
  1328. ts.surrenderInfo.isSurrendering = false
  1329. friendIndex := ts.table.GetUserByChair(getFriendChair(chairId)).GetUserIndex()
  1330. ts.table.SendGameData(friendIndex, CMD_SURRENDER_RESULT, d)
  1331. }
  1332. return true
  1333. }
  1334. func (ts *tablesink) enterSurrenderEndPhase() {
  1335. // 结算
  1336. winners := []int{}
  1337. surrendChair := ts.surrenderInfo.WhoseSurrender
  1338. winners = append(winners, getNextChair(surrendChair), getPreviousChair(surrendChair))
  1339. totalBet := 0
  1340. if ts.gameScene.Players[winners[0]].bet > 0 {
  1341. totalBet = ts.gameScene.getTotalBet(winners[0])
  1342. }
  1343. // 平分
  1344. endScore := totalBet / len(winners)
  1345. if endScore != 0 {
  1346. for _, v := range winners {
  1347. ts.gameScene.Players[v].EndScore = endScore
  1348. }
  1349. }
  1350. // 写分
  1351. for i := 0; i < CHAIR_COUNT; i++ {
  1352. p := &ts.gameScene.Players[i]
  1353. usr := ts.table.GetUserByChair(i)
  1354. if !p.IsValid || usr == nil {
  1355. continue
  1356. }
  1357. if isSameTeam(i, surrendChair) {
  1358. ts.gameScene.Players[i].TotalScore = 0
  1359. } else {
  1360. ts.gameScene.Players[i].TotalScore = SCORE_TO_WIN
  1361. }
  1362. realWin := p.EndScore - p.bet
  1363. if p.bet == 0 {
  1364. realWin = 0
  1365. }
  1366. tax := 0
  1367. if realWin > 0 {
  1368. tax = realWin * ts.roomInfo.TaxRate / 100
  1369. }
  1370. wScore := p.EndScore - tax
  1371. if p.bet == 0 {
  1372. wScore = 0
  1373. }
  1374. ts.writeScore(p.userID, wScore, tax, 0, scoreType_Surrender, ts.roomInfo.RoomName, p.isRobot)
  1375. if wScore > 0 {
  1376. userservices.DoRecord(p.userID, userservices.Record_MaxWin, wScore)
  1377. }
  1378. if !p.isRobot {
  1379. roomType := waterpool.RoomType_Normal
  1380. if ts.roomInfo.IsDoublingMode {
  1381. roomType = waterpool.RoomType_Doubling
  1382. }
  1383. if ts.roomInfo.RoomType == 3 {
  1384. roomType = waterpool.RoomType_Quick
  1385. }
  1386. if config.Server.IsLadderRoom > 0 {
  1387. roomType = waterpool.RoomType_Ladder
  1388. }
  1389. if p.isControl {
  1390. go waterpool.UpdataUserWaterPool(p.userID, realWin, GAME_NAME, roomType, ts.roomInfo.RoomID)
  1391. } else {
  1392. go waterpool.ReduceInventoryValue(GAMEID, ts.roomInfo.RoomName, realWin-tax, roomType)
  1393. }
  1394. }
  1395. // 写记录
  1396. if ts.table.IsPrivate() {
  1397. } else {
  1398. if p.bet != 0 {
  1399. go ts.table.WriteBetRecordWithSetcount(p.userID, p.bet, wScore, 1.0, "SurrenderGold", "Surrender", ts.roomInfo.RoomName, 1)
  1400. }
  1401. if config.Server.IsLadderRoom > 0 {
  1402. ts.gameScene.Players[i].LastWinCount = ladder.GetUserConsecutiveWinCount(p.userID, GAMEID)
  1403. ts.gameScene.Players[i].LadderChangeValue, _ = ladder.AddUserLadderScore(p.userID, GAMEID, ts.roomInfo.RoomName, wScore-p.bet)
  1404. if ts.gameScene.Players[i].LadderChangeValue > 0 {
  1405. ts.gameScene.Players[i].LastWinCount++
  1406. }
  1407. d := ladder.GetUserLadderInfo(p.userID, GAMEID)
  1408. if d != nil {
  1409. ts.gameScene.Players[i].LadderInfo = *d
  1410. } else {
  1411. ts.table.LogWithTableId("User:%d, LadderInfo is nil", p.userID)
  1412. }
  1413. }
  1414. }
  1415. }
  1416. ts.gameScene.Phase = Phase_End
  1417. ts.table.NotifySceneChanged(-1)
  1418. if ts.table.IsPrivate() {
  1419. winnerUsers := make([]int, len(winners))
  1420. for i := 0; i < len(winners); i++ {
  1421. winnerUsers[i] = ts.gameScene.Players[winners[i]].userID
  1422. }
  1423. ts.table.PrivateRoomSetWinners(winnerUsers)
  1424. ts.writePrivateBetRecords()
  1425. }
  1426. ts.endGame()
  1427. ts.uniqueId = uuid.New().String()
  1428. ts.gameScene.initData(ts.table.GetTableID(), ts.roomInfo.ScoreToWin, ts.roomInfo.MaxSurrenderCount)
  1429. }
  1430. func (ts *tablesink) dealCloseOrOpen(action int) (bool, string) {
  1431. if ts.gameScene.Phase != Phase_CloseOpen {
  1432. ts.table.LogWithTableId("tablesink.dealCloseOrOpen incorrect phase %d", ts.gameScene.Phase)
  1433. return false, "Wrong Phase"
  1434. }
  1435. if action != Action_Buy_Open && action != Action_Buy_Close {
  1436. ts.table.LogWithTableId("tablesink.dealCloseOrOpen incorrect action %d", action)
  1437. return false, "Wrong Action"
  1438. }
  1439. if !isValidCard(ts.gameScene.WhoseTurn) {
  1440. ts.table.LogWithTableId("tablesink.dealCloseOrOpen Wrong WhoseTurn:%d", ts.gameScene.WhoseTurn)
  1441. return false, "Wrong ChairId"
  1442. }
  1443. if ts.gameScene.Players[ts.gameScene.WhoseTurn].LastAction == Action_CloseOrOpen {
  1444. ts.table.LogWithTableId("tablesink.dealCloseOrOpen already do CloseOrOpen")
  1445. return false, "Wrong Done Action"
  1446. }
  1447. if action == Action_Buy_Close {
  1448. ts.gameScene.IsClose = true
  1449. ts.logic.setIsFirstOutCardClose(true)
  1450. } else {
  1451. ts.gameScene.IsClose = false
  1452. ts.logic.setIsFirstOutCardClose(false)
  1453. }
  1454. // 记录玩家操作
  1455. ts.gameScene.Players[ts.gameScene.WhoseTurn].LastAction = Action_CloseOrOpen
  1456. ts.gameScene.Players[ts.gameScene.WhoseTurn].LastActionData = action
  1457. ts.gameScene.LastTurn = ts.gameScene.WhoseTurn
  1458. ts.gameScene.addAction(ts.gameScene.WhoseTurn, Action_CloseOrOpen, action)
  1459. ts.gameScene.WhoseTurn = -1
  1460. ts.gameScene.PhaseIndex++
  1461. ts.table.NotifySceneChanged(-1)
  1462. buyAction := Action_Buy_Double + ts.gameScene.BaseTimes - 1
  1463. for i := 0; i < CHAIR_COUNT; i++ {
  1464. buyList := []int{}
  1465. if isSameTeam(ts.gameScene.LastTurn, i) {
  1466. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1467. continue
  1468. }
  1469. buyList = append(buyList, Action_Buy_Pass, buyAction)
  1470. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1471. }
  1472. ts.gameScene.Phase = Phase_Double
  1473. ts.gameScene.PhaseIndex = 0
  1474. ts.table.SetTimer(TIMER_DOUBLE_CHANGE, 600)
  1475. return true, ""
  1476. }
  1477. func (ts *tablesink) dealCorrectionStart(chairId int, mode int) (bool, string) {
  1478. if ts.gameScene.Phase != Phase_Play {
  1479. ts.table.LogWithTableId("tablesink.dealCorrectionStart incorrect phase %d", ts.gameScene.Phase)
  1480. return false, "Wrong Phase"
  1481. }
  1482. if ts.gameScene.isCorrectEnd {
  1483. return false, "have Corrected"
  1484. }
  1485. if !ts.roomInfo.IsCorrectionMode {
  1486. return false, "not CorrectionMode"
  1487. }
  1488. if isValidChair(ts.gameScene.SawaChair) && chairId == ts.gameScene.SawaChair {
  1489. return false, "SawaChair"
  1490. }
  1491. if !ts.gameScene.CanCorrect {
  1492. return false, "Can Not Operator"
  1493. }
  1494. ts.table.KillAllTimer()
  1495. ts.gameScene.isCorrectEnd = true
  1496. ts.gameScene.CorrectMode = mode
  1497. ts.gameScene.Players[chairId].LastAction = Action_CorrectionStart
  1498. ts.gameScene.LastTurn = chairId
  1499. ts.gameScene.WhoseTurn = chairId
  1500. ts.gameScene.addAction(chairId, Action_CorrectionStart, mode)
  1501. ts.gameScene.PhaseIndex = 0
  1502. ts.gameScene.ShowAllCards = true
  1503. ts.gameScene.Phase = Phase_Correct
  1504. if !ts.simulator.haveWrongAction(ts.gameScene.CorrectMode, ts.gameScene.SawaChair) ||
  1505. (isSameTeam(ts.gameScene.SawaChair, chairId) && ts.gameScene.CorrectMode == Correct_Sawa) {
  1506. ts.gameScene.CorrectResult = 0
  1507. ts.gameScene.PhaseIndex++
  1508. ts.table.NotifySceneChanged(-1)
  1509. ts.table.SetTimer(TIMER_CORRECTFINISH, SEC_CORRECTFINISH)
  1510. return true, ""
  1511. }
  1512. ts.table.NotifySceneChanged(-1)
  1513. ts.DumpScene()
  1514. ts.resetGameTimer()
  1515. return true, ""
  1516. }
  1517. func (ts *tablesink) onTimerCorrectStart() {
  1518. ts.table.KillAllTimer()
  1519. ts.gameScene.CorrectResult = 0
  1520. ts.gameScene.ShowAllCards = true
  1521. ts.gameScene.Index++
  1522. ts.gameScene.PhaseIndex++
  1523. ts.table.NotifySceneChanged(-1)
  1524. ts.table.SetTimer(TIMER_CORRECTFINISH, SEC_CORRECTFINISH)
  1525. }
  1526. func (ts *tablesink) dealCorrectionFinish(correctType int, cards []int) (bool, string) {
  1527. if ts.gameScene.Phase != Phase_Correct {
  1528. ts.table.LogWithTableId("tablesink.dealReshuffle incorrect phase %d", ts.gameScene.Phase)
  1529. return false, "Wrong Phase"
  1530. }
  1531. if !ts.roomInfo.IsCorrectionMode {
  1532. return false, "not CorrectionMode"
  1533. }
  1534. if ts.gameScene.CorrectMode == Correct_Invaild {
  1535. return false, "Invaild CorrectMode"
  1536. }
  1537. if len(cards) != 2 {
  1538. return false, "Wrong Cards"
  1539. }
  1540. if ts.gameScene.PhaseIndex != 0 {
  1541. return false, "Wrong Phase Index"
  1542. }
  1543. ts.table.KillAllTimer()
  1544. ts.gameScene.Players[ts.gameScene.WhoseTurn].LastAction = Action_CorrectionFinish
  1545. ts.gameScene.LastTurn = ts.gameScene.WhoseTurn
  1546. ts.gameScene.addCorrectAction(ts.gameScene.WhoseTurn, Action_CorrectionFinish, correctType, cards)
  1547. ts.gameScene.PhaseIndex++
  1548. ts.gameScene.CorrectCards = append(ts.gameScene.CorrectCards, cards...)
  1549. if ts.gameScene.CorrectMode == Correct_Sawa {
  1550. ts.gameScene.CorrectResult = ts.simulator.checkWrongActionInSawa(ts.gameScene.SawaChair, cards)
  1551. } else {
  1552. ts.gameScene.CorrectResult = ts.simulator.checkWrongActionInNormal(ts.gameScene.WhoseTurn, correctType, cards)
  1553. }
  1554. ts.gameScene.ShowAllCards = true
  1555. ts.gameScene.CorrectType = correctType
  1556. ts.table.NotifySceneChanged(-1)
  1557. ts.table.SetTimer(TIMER_CORRECTFINISH, SEC_CORRECTFINISH)
  1558. return true, ""
  1559. }
  1560. func (ts *tablesink) onTimerCorrectFinish() {
  1561. winChair := ts.gameScene.WhoseTurn
  1562. if ts.gameScene.CorrectResult == 0 {
  1563. winChair = getNextChair(winChair)
  1564. }
  1565. for i := 0; i < CHAIR_COUNT; i++ {
  1566. if ts.gameScene.Players[i].HaveBaloot && !ts.gameScene.Players[i].isIncludeBaloot() &&
  1567. ts.gameScene.CorrectMode == Correct_Normal {
  1568. ts.gameScene.Players[i].Projects = append(ts.gameScene.Players[i].Projects, SingleProject{
  1569. Type: PROJECT_BALOOT,
  1570. Score: value_project[PROJECT_BALOOT],
  1571. Cards: []int{genACard(ts.gameScene.TrumpType, CardValueQ), genACard(ts.gameScene.TrumpType, CardValueK)},
  1572. })
  1573. if ts.roomInfo.IsDoublingMode {
  1574. ts.addDoublingData(Doubling_Project_Baloot)
  1575. }
  1576. }
  1577. if isSameTeam(i, winChair) {
  1578. if ts.gameScene.FinalClub == Suit_Hokum {
  1579. ts.gameScene.Players[i].WinCardScore = HOKUM_TOTAL_SCORE
  1580. } else {
  1581. ts.gameScene.Players[i].WinCardScore = SUN_TOTAL_SCORE
  1582. }
  1583. } else {
  1584. ts.gameScene.Players[i].WinCardScore = 0
  1585. }
  1586. }
  1587. ts.gameScene.Players[winChair].WinCardNumber += ts.gameScene.getLeftCardNumber()
  1588. for i := 0; i < CHAIR_COUNT; i++ {
  1589. ts.gameScene.Players[i].clearHandCard()
  1590. }
  1591. ts.gameScene.RoundWinner = winChair
  1592. ts.onTimerNewRound()
  1593. }
  1594. func (ts *tablesink) dealReshuffle(chairId int, action int) (bool, string) {
  1595. if ts.gameScene.Phase != Phase_Reshuffle {
  1596. ts.table.LogWithTableId("tablesink.dealReshuffle incorrect phase %d", ts.gameScene.Phase)
  1597. return false, "Wrong Phase"
  1598. }
  1599. p := &ts.gameScene.Players[chairId]
  1600. if !p.CanDoReshuffle || p.ReshuffleAction != Reshuffle_None {
  1601. ts.table.LogWithTableId("tablesink.dealReshuffle Can not do the action %d", action)
  1602. return false, "Wrong Action"
  1603. }
  1604. if ts.gameScene.havePlayerConfirmReshuffle(chairId) {
  1605. ts.table.LogWithTableId("tablesink.dealReshuffle Can not do the action %d", action)
  1606. return false, "Wrong Action"
  1607. }
  1608. // 记录玩家操作
  1609. ts.gameScene.Players[chairId].LastAction = Action_Reshuffle
  1610. ts.gameScene.Players[chairId].LastActionData = action
  1611. ts.gameScene.Players[chairId].ReshuffleAction = action
  1612. ts.gameScene.LastTurn = chairId
  1613. ts.gameScene.addAction(chairId, Action_Reshuffle, action)
  1614. ts.gameScene.PhaseIndex++
  1615. if action == Reshuffle_Confirm {
  1616. ts.gameScene.WhoseTurn = -1
  1617. ts.gameScene.reshuffleResult = -1
  1618. ts.table.NotifySceneChanged(-1)
  1619. ts.table.SetTimer(TIMER_ALL_PASS, SEC_ALL_PASS*30)
  1620. return true, ""
  1621. }
  1622. if ts.gameScene.isAllCanDoReshufflePlayerPass() {
  1623. ts.gameScene.Phase = ts.gameScene.reshufflePhase
  1624. ts.gameScene.PhaseIndex = 0
  1625. ts.gameScene.LastTurn = ts.gameScene.BuyChair
  1626. if ts.gameScene.reshuffleResult == 0 {
  1627. ts.resetGameTimer()
  1628. } else {
  1629. ts.table.SetTimer(TIMER_SEND_LEFT_CARD, SEC_SEND_LEFT_CARD)
  1630. }
  1631. }
  1632. ts.table.NotifySceneChanged(-1)
  1633. return true, ""
  1634. }
  1635. func (ts *tablesink) dealDouble(chairId int, buyAction int) (bool, string) {
  1636. if ts.gameScene.Phase != Phase_Double {
  1637. ts.table.LogWithTableId("tablesink.dealDouble incorrect phase %d", ts.gameScene.Phase)
  1638. return false, "Wrong Phase"
  1639. }
  1640. p := &ts.gameScene.Players[chairId]
  1641. if !p.canDoBuyAction(buyAction) {
  1642. ts.table.LogWithTableId("tablesink.dealDouble Can not do the buy action %d", buyAction)
  1643. return false, "Wrong Action"
  1644. }
  1645. // 记录玩家操作
  1646. ts.gameScene.Players[chairId].LastAction = Action_Double
  1647. ts.gameScene.Players[chairId].LastActionData = buyAction
  1648. ts.gameScene.LastTurn = chairId
  1649. ts.gameScene.PhaseIndex++
  1650. ts.gameScene.addAction(chairId, Action_Double, buyAction)
  1651. ts.gameScene.initAllPlayerTime()
  1652. if buyAction != Action_Buy_Pass && ts.roomInfo.IsDoublingMode {
  1653. ts.addDoublingData(buyAction)
  1654. ts.sendShowDoublingMsg([]int{buyAction})
  1655. }
  1656. if buyAction == Action_Buy_Double && ts.gameScene.FinalClub != Suit_Hokum {
  1657. for i := 0; i < CHAIR_COUNT; i++ {
  1658. ts.gameScene.Players[i].setPlayerOperators([]int{})
  1659. }
  1660. ts.gameScene.setBaseTimes(buyAction)
  1661. ts.gameScene.BuyChair = chairId
  1662. ts.table.SetTimer(TIMER_SEND_LEFT_CARD, SEC_SEND_LEFT_CARD)
  1663. ts.table.NotifySceneChanged(-1)
  1664. return true, ""
  1665. }
  1666. if ts.gameScene.isAllPlayerPass() {
  1667. for i := 0; i < CHAIR_COUNT; i++ {
  1668. ts.gameScene.Players[i].setPlayerOperators([]int{})
  1669. }
  1670. ts.table.SetTimer(TIMER_SEND_LEFT_CARD, SEC_SEND_LEFT_CARD)
  1671. ts.table.NotifySceneChanged(-1)
  1672. return true, ""
  1673. }
  1674. ts.gameScene.Players[chairId].setPlayerOperators([]int{})
  1675. if buyAction == Action_Buy_Coffee {
  1676. ts.gameScene.IsClose = false
  1677. for i := 0; i < CHAIR_COUNT; i++ {
  1678. ts.gameScene.Players[i].setPlayerOperators([]int{})
  1679. }
  1680. ts.gameScene.BaseTimes = 1
  1681. ts.gameScene.BuyChair = chairId
  1682. ts.gameScene.IsBO1 = true
  1683. ts.table.SetTimer(TIMER_SEND_LEFT_CARD, SEC_SEND_LEFT_CARD)
  1684. ts.table.NotifySceneChanged(-1)
  1685. return true, ""
  1686. }
  1687. if buyAction != Action_Buy_Pass {
  1688. ts.gameScene.setBaseTimes(buyAction)
  1689. ts.gameScene.BuyChair = chairId
  1690. for i := 0; i < CHAIR_COUNT; i++ {
  1691. buyList := []int{}
  1692. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1693. }
  1694. ts.table.NotifySceneChanged(-1)
  1695. if ts.gameScene.BaseTimes%2 != 0 {
  1696. ts.gameScene.WhoseTurn = -1
  1697. ts.gameScene.IsClose = false
  1698. for i := 0; i < CHAIR_COUNT; i++ {
  1699. buyList := []int{}
  1700. if isSameTeam(chairId, i) {
  1701. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1702. continue
  1703. }
  1704. buyList = append(buyList, Action_Buy_Pass, buyAction+1)
  1705. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1706. }
  1707. } else {
  1708. ts.gameScene.WhoseTurn = ts.gameScene.BuyChair
  1709. ts.gameScene.Phase = Phase_CloseOpen
  1710. ts.gameScene.PhaseIndex = 0
  1711. }
  1712. ts.table.SetTimer(TIMER_DOUBLE_CHANGE, 100)
  1713. return true, ""
  1714. }
  1715. ts.table.NotifySceneChanged(-1)
  1716. return true, ""
  1717. }
  1718. func (ts *tablesink) isFirstBuyEnterSecondBuy(chairId, buyAction int) bool {
  1719. return (chairId == ts.gameScene.Banker && ts.gameScene.Phase == Phase_FirstBuy &&
  1720. buyAction == Action_Buy_Pass && ts.gameScene.BuyAction <= Action_Buy_Pass)
  1721. }
  1722. func (ts *tablesink) dealBuy(chairId int, buyAction int) (bool, string) {
  1723. if ts.gameScene.Phase != Phase_FirstBuy && ts.gameScene.Phase != Phase_SecondBuy {
  1724. ts.table.LogWithTableId("tablesink.dealBuy incorrect phase %d", ts.gameScene.Phase)
  1725. return false, "Wrong Phase"
  1726. }
  1727. p := &ts.gameScene.Players[chairId]
  1728. if !p.canDoBuyAction(buyAction) {
  1729. ts.table.LogWithTableId("tablesink.dealBuy Chair:%d Can not do the buy action %d", chairId, buyAction)
  1730. return false, "Wrong Action"
  1731. }
  1732. if ts.gameScene.WhoseTurn != -1 {
  1733. if chairId != ts.gameScene.WhoseTurn {
  1734. ts.table.LogWithTableId("tablesink.dealBuy not the chairId %d", chairId)
  1735. return false, "Wrong chairId"
  1736. }
  1737. }
  1738. // 记录玩家操作
  1739. ts.gameScene.Players[chairId].LastAction = Action_Buy
  1740. ts.gameScene.Players[chairId].LastActionData = buyAction
  1741. ts.gameScene.LastTurn = chairId
  1742. ts.gameScene.initAllPlayerTime()
  1743. ts.gameScene.Players[chairId].setPlayerOperators([]int{})
  1744. ts.gameScene.addAction(chairId, Action_Buy, buyAction)
  1745. ts.gameScene.buyPhase = ts.gameScene.Phase
  1746. // 查看是否触发加倍
  1747. if ts.roomInfo.IsDoublingMode && !isSameTeam(ts.gameScene.BuyChair, chairId) {
  1748. if ts.gameScene.BuyAction == Action_Buy_Hokum && buyAction == Action_Buy_Ashkal {
  1749. ts.addDoublingData(Doubling_Hokum_to_Ashkal)
  1750. ts.sendShowDoublingMsg([]int{Doubling_Hokum_to_Ashkal})
  1751. }
  1752. if ts.gameScene.BuyAction == Action_Buy_Hokum && buyAction == Action_Buy_Sun {
  1753. ts.addDoublingData(Doubling_Hokum_to_Sun)
  1754. ts.sendShowDoublingMsg([]int{Doubling_Hokum_to_Sun})
  1755. }
  1756. if ts.gameScene.BuyAction == Action_Buy_Ashkal && buyAction == Action_Buy_Sun {
  1757. ts.addDoublingData(Doubling_Ashkal_to_Sun)
  1758. ts.sendShowDoublingMsg([]int{Doubling_Ashkal_to_Sun})
  1759. }
  1760. if ts.gameScene.BuyAction == Action_Buy_Sun && buyAction == Action_Buy_Sun {
  1761. ts.addDoublingData(Doubling_Sun_to_Sun)
  1762. ts.sendShowDoublingMsg([]int{Doubling_Sun_to_Sun})
  1763. }
  1764. }
  1765. if buyAction == Action_Buy_ConfirmHokum || buyAction == Action_Buy_SwitchSun ||
  1766. ts.gameScene.isBuySunEnterDouble(buyAction, chairId) ||
  1767. ts.gameScene.canReshuffle(buyAction, chairId) || ts.isFirstBuyEnterSecondBuy(chairId, buyAction) {
  1768. ts.gameScene.PhaseIndex++
  1769. ts.table.NotifySceneChanged(-1)
  1770. }
  1771. // 判断买牌玩家是否有变化
  1772. buyChairIsChange := false
  1773. if (buyAction > Action_Buy_Pass && buyAction <= Action_Buy_Ashkal) || ts.gameScene.BuyAction <= Action_Buy_Pass {
  1774. buyChairIsChange = true
  1775. }
  1776. // 两轮都不买重洗
  1777. if ts.gameScene.WhoseTurn == ts.gameScene.Banker &&
  1778. ts.gameScene.BuyAction <= Action_Buy_Pass &&
  1779. ts.gameScene.Phase == Phase_SecondBuy && buyAction == Action_Buy_Pass {
  1780. ts.gameScene.WhoseTurn = -1
  1781. ts.gameScene.PhaseIndex++
  1782. ts.table.NotifySceneChanged(-1)
  1783. ts.allPassCount++
  1784. ts.table.SetTimer(TIMER_ALL_PASS, SEC_ALL_PASS*5)
  1785. return true, ""
  1786. }
  1787. result := 0
  1788. prePhase := ts.gameScene.Phase
  1789. if buyChairIsChange {
  1790. result = ts.gameScene.resetBuyActionOfChangeBuyChair(buyAction, chairId)
  1791. if ts.gameScene.isEnterReshuffle(buyAction) {
  1792. ts.gameScene.reshuffleResult = result
  1793. ts.resetGameTimer()
  1794. } else {
  1795. if result == 0 {
  1796. ts.resetGameTimer()
  1797. } else {
  1798. ts.table.SetTimer(TIMER_SEND_LEFT_CARD, SEC_SEND_LEFT_CARD)
  1799. }
  1800. }
  1801. if prePhase != ts.gameScene.Phase {
  1802. ts.gameScene.initAllPlayerLastAction()
  1803. } else {
  1804. ts.gameScene.PhaseIndex++
  1805. }
  1806. // 特殊要求First_Buy切换到Second_Buy阶段,做延迟处理
  1807. if prePhase == Phase_FirstBuy && ts.gameScene.Phase == Phase_SecondBuy {
  1808. ts.table.SetTimer(TIMER_BUY_CHANGE, 100)
  1809. return true, ""
  1810. }
  1811. ts.table.NotifySceneChanged(-1)
  1812. return true, ""
  1813. }
  1814. result = ts.gameScene.resetBuyActionOfNotChangeBuyChair(chairId, buyAction)
  1815. isConfirmHokum := false
  1816. if buyAction == Action_Buy_ConfirmHokum && prePhase == Phase_FirstBuy {
  1817. isConfirmHokum = true
  1818. data := ShowCardType{Type: ts.gameScene.TrumpType, Chair: chairId}
  1819. d, _ := json.Marshal(data)
  1820. ts.table.SendGameData(-1, CMD_SHOW_CARDTYPE, string(d))
  1821. for i := 0; i < CHAIR_COUNT; i++ {
  1822. buyList := []int{}
  1823. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1824. }
  1825. }
  1826. ts.gameScene.Index++
  1827. ts.table.NotifySceneChanged(-1)
  1828. if result == 1 {
  1829. if !isConfirmHokum {
  1830. ts.resetGameTimer()
  1831. } else {
  1832. for i := 0; i < CHAIR_COUNT; i++ {
  1833. buyList := []int{}
  1834. if isSameTeam(i, ts.gameScene.BuyChair) {
  1835. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1836. continue
  1837. }
  1838. buyList = append(buyList, Action_Buy_Pass, Action_Buy_Double)
  1839. ts.gameScene.Players[i].setPlayerOperators(buyList)
  1840. }
  1841. ts.table.SetTimer(TIMER_DOUBLE_CHANGE, 2200)
  1842. }
  1843. } else if result == 2 {
  1844. ts.table.SetTimer(TIMER_SEND_LEFT_CARD, SEC_SEND_LEFT_CARD)
  1845. }
  1846. if prePhase != ts.gameScene.Phase {
  1847. ts.gameScene.initAllPlayerLastAction()
  1848. }
  1849. return true, ""
  1850. }
  1851. func (ts *tablesink) killAutoTimer() {
  1852. for i := 0; i < CHAIR_COUNT; i++ {
  1853. ts.table.KillTimer(TIMER_AUTO_ACTION_0 + i)
  1854. }
  1855. }
  1856. func (ts *tablesink) resetGameTimer() {
  1857. ts.LastOpraTime = time.Now()
  1858. ts.killAutoTimer()
  1859. switch ts.gameScene.Phase {
  1860. case Phase_Start:
  1861. ts.table.SetTimer(TIMER_GAME, SEC_START)
  1862. case Phase_GameEnd:
  1863. if ts.gameScene.ScoreToWin == -1 && !ts.table.IsPrivate() {
  1864. ts.table.SetTimer(TIMER_GAME, 500)
  1865. } else {
  1866. if config.Server.IsLadderRoom == 1 && (ts.gameScene.ScoreToWin+ts.gameScene.GameIndex) < -1 {
  1867. ts.table.SetTimer(TIMER_GAME, 6000)
  1868. } else {
  1869. if config.Server.IsLadderRoom == 1 && (ts.gameScene.ScoreToWin+ts.gameScene.GameIndex) == -1 {
  1870. ts.table.SetTimer(TIMER_GAME, 500)
  1871. } else {
  1872. ts.table.SetTimer(TIMER_GAME, 500)
  1873. }
  1874. }
  1875. }
  1876. case Phase_Correct:
  1877. ts.table.SetTimer(TIMER_GAME, 60000)
  1878. case Phase_Reshuffle:
  1879. for i := 0; i < CHAIR_COUNT; i++ {
  1880. usr := ts.table.GetUserByChair(i)
  1881. if usr == nil || usr.IsRobot() {
  1882. continue
  1883. }
  1884. if !ts.gameScene.Players[i].AutoOut {
  1885. continue
  1886. }
  1887. if !ts.gameScene.Players[i].canReshuffle() {
  1888. continue
  1889. }
  1890. ts.table.SetTimer(TIMER_AUTO_ACTION_0+i, SEC_AUTO)
  1891. }
  1892. ts.table.SetTimer(TIMER_GAME, SEC_RESHUFFLE)
  1893. default:
  1894. if ts.gameScene.WhoseTurn == -1 {
  1895. ts.checkAutoPlayerAction()
  1896. ts.table.SetTimer(TIMER_GAME, ts.getSecond(ts.gameScene.Phase))
  1897. } else {
  1898. if !isValidChair(ts.gameScene.WhoseTurn) {
  1899. return
  1900. }
  1901. chairId := ts.gameScene.WhoseTurn
  1902. usr := ts.table.GetUserByChair(chairId)
  1903. if usr == nil {
  1904. ts.table.LogWithTableId("resetGameTimer Phase:%d ChairId:%d nnot exist", ts.gameScene.Phase, chairId)
  1905. ts.gameScene.Phase = Phase_End
  1906. ts.gameScene.PhaseIndex = 0
  1907. // 分数还原
  1908. for i := 0; i < CHAIR_COUNT; i++ {
  1909. if !ts.gameScene.Players[i].IsValid {
  1910. continue
  1911. }
  1912. if ts.gameScene.Players[i].bet > 0 {
  1913. ts.writeScore(ts.gameScene.Players[i].userID, ts.gameScene.Players[i].bet, 0, 0,
  1914. ScoreType_Return, ts.roomInfo.RoomName, ts.gameScene.Players[i].isRobot)
  1915. }
  1916. }
  1917. if ts.table.IsPrivate() {
  1918. ts.table.PrivateRoomSetWinners([]int{})
  1919. }
  1920. ts.endGame()
  1921. ts.uniqueId = uuid.New().String()
  1922. ts.gameScene.initData(ts.table.GetTableID(), ts.roomInfo.ScoreToWin, ts.roomInfo.MaxSurrenderCount)
  1923. return
  1924. }
  1925. if isValidChair(chairId) {
  1926. if (ts.gameScene.Players[chairId].AutoOut || usr.GetUserStatus() == user.UserStatus_Offline) &&
  1927. !usr.IsRobot() {
  1928. ts.table.SetTimer(TIMER_GAME, SEC_AUTO)
  1929. } else {
  1930. ts.table.SetTimer(TIMER_GAME, ts.getSecond(ts.gameScene.Phase))
  1931. }
  1932. } else {
  1933. ts.table.SetTimer(TIMER_GAME, ts.getSecond(ts.gameScene.Phase))
  1934. }
  1935. }
  1936. }
  1937. if ts.gameScene.Phase == Phase_Play {
  1938. for i := 0; i < CHAIR_COUNT; i++ {
  1939. ts.table.KillTimer(TIMER_ROBOT_SEND_CHAT_0 + i)
  1940. }
  1941. ts.table.SetTimer(TIMER_ROBOT_SEND_CHAT_0+ts.gameScene.WhoseTurn, 5000)
  1942. }
  1943. ts.checkRobotAction()
  1944. }
  1945. func (ts *tablesink) getSecond(phase int) int {
  1946. ret := 0
  1947. switch phase {
  1948. case Phase_FirstBuy:
  1949. fallthrough
  1950. case Phase_SecondBuy:
  1951. ret = ts.roomInfo.SecBuy
  1952. case Phase_ChooseTrump:
  1953. ret = ts.roomInfo.SecChooseTrump
  1954. case Phase_Double:
  1955. ret = ts.roomInfo.SecDouble
  1956. case Phase_CloseOpen:
  1957. ret = ts.roomInfo.SecCloseOpen
  1958. case Phase_Reshuffle:
  1959. ret = SEC_RESHUFFLE
  1960. case Phase_Play:
  1961. if isValidChair(ts.gameScene.WhoseTurn) && len(ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards) == 1 {
  1962. ret = 700
  1963. } else {
  1964. ret = ts.roomInfo.SecPlay
  1965. }
  1966. }
  1967. if ret < 100 {
  1968. ret *= 1000
  1969. }
  1970. if ret == 0 {
  1971. ret = 20000
  1972. }
  1973. return ret
  1974. }
  1975. func (ts *tablesink) checkAutoPlayerAction() {
  1976. if ts.gameScene.WhoseTurn != -1 {
  1977. return
  1978. }
  1979. for i := 0; i < CHAIR_COUNT; i++ {
  1980. usr := ts.table.GetUserByChair(i)
  1981. if usr == nil || usr.IsRobot() || !ts.gameScene.Players[i].canAction() {
  1982. continue
  1983. }
  1984. if !ts.gameScene.Players[i].AutoOut {
  1985. continue
  1986. }
  1987. ts.table.SetTimer(TIMER_AUTO_ACTION_0+i, SEC_AUTO)
  1988. }
  1989. }
  1990. func (ts *tablesink) onTimerBuyFailed() {
  1991. if ts.gameScene.Phase == Phase_End {
  1992. return
  1993. }
  1994. if ts.surrenderInfo.isSurrenderEnd() {
  1995. ts.gameScene.BuyChair = getNextChair(ts.surrenderInfo.WhoseSurrender)
  1996. ts.gameScene.FinalClub = Suit_Sun
  1997. ts.gameScene.LastWinChair = ts.gameScene.BuyChair
  1998. ts.gameScene.Players[ts.gameScene.BuyChair].FinalCalcScore = 26
  1999. ts.gameScene.Players[ts.gameScene.BuyChair].TotalScore += 26
  2000. ts.gameScene.Players[getFriendChair(ts.gameScene.BuyChair)].FinalCalcScore = 26
  2001. ts.gameScene.Players[getFriendChair(ts.gameScene.BuyChair)].TotalScore += 26
  2002. ts.enterGameEndPhase(false)
  2003. return
  2004. }
  2005. ts.gameScene.setNextBanker()
  2006. ts.gameScene.gameInit(getPreviousChair(ts.gameScene.Banker))
  2007. ts.gameStart()
  2008. if ts.gameScene.GameIndex == 0 {
  2009. ts.table.NotifySceneChanged(-1)
  2010. if ts.roomInfo.IsDoublingMode {
  2011. if getCardValue(ts.gameScene.PublicCard) == CardValueJ {
  2012. ts.addDoublingData(Doubling_PublicCard_J)
  2013. ts.sendShowDoublingMsg([]int{Doubling_PublicCard_J})
  2014. }
  2015. }
  2016. }
  2017. }
  2018. func (ts *tablesink) sendLeftCard() {
  2019. ts.logic.setTrumpAndSuit(ts.gameScene.TrumpType, ts.gameScene.FinalClub)
  2020. getPublicChairId := ts.gameScene.BuyChair
  2021. ts.gameScene.Phase = Phase_SendLeftCard
  2022. ts.gameScene.PhaseIndex = 0
  2023. if ts.gameScene.FinalClub == Suit_Ashkal {
  2024. getPublicChairId = (getPublicChairId + 2) % CHAIR_COUNT
  2025. }
  2026. count := 0
  2027. for i := 0; i < CHAIR_COUNT; i++ {
  2028. usr := ts.table.GetUserByChair(i)
  2029. if usr == nil {
  2030. continue
  2031. }
  2032. userStatus := usr.GetUserStatus()
  2033. if userStatus <= user.UserStatus_Free ||
  2034. userStatus == user.UserStatus_Watch {
  2035. continue
  2036. }
  2037. count++
  2038. leftCards := ts.logic.getLeftCards(getPublicChairId == i)
  2039. ts.gameScene.Players[i].HandCards = append(ts.gameScene.Players[i].HandCards, leftCards...)
  2040. ts.gameScene.Players[i].LeftSendCards = append(ts.gameScene.Players[i].LeftSendCards, leftCards...)
  2041. if getPublicChairId == i {
  2042. ts.gameScene.Players[i].HandCards = append(ts.gameScene.Players[i].HandCards, ts.gameScene.PublicCard)
  2043. ts.gameScene.Players[i].LeftSendCards = append(ts.gameScene.Players[i].LeftSendCards, ts.gameScene.PublicCard)
  2044. }
  2045. if ts.gameScene.FinalClub == Suit_Hokum {
  2046. ts.gameScene.Players[i].checkBaloot(ts.gameScene.TrumpType)
  2047. }
  2048. p := &ts.gameScene.Players[i]
  2049. sort.Slice(p.HandCards, func(m, n int) bool {
  2050. return getCardSortValue(p.HandCards[m], ts.gameScene.TrumpType) <
  2051. getCardSortValue(p.HandCards[n], ts.gameScene.TrumpType)
  2052. })
  2053. }
  2054. if count < CHAIR_COUNT {
  2055. ts.table.LogWithTableId("sendLeftCard 用户不足")
  2056. ts.gameScene.Phase = Phase_End
  2057. ts.gameScene.PhaseIndex = 0
  2058. // 分数还原
  2059. for i := 0; i < CHAIR_COUNT; i++ {
  2060. if !ts.gameScene.Players[i].IsValid {
  2061. continue
  2062. }
  2063. if ts.gameScene.Players[i].bet > 0 {
  2064. ts.writeScore(ts.gameScene.Players[i].userID, ts.gameScene.Players[i].bet, 0, 0,
  2065. ScoreType_Return, ts.roomInfo.RoomName, ts.gameScene.Players[i].isRobot)
  2066. }
  2067. }
  2068. if ts.table.IsPrivate() {
  2069. ts.table.PrivateRoomSetWinners([]int{})
  2070. }
  2071. ts.endGame()
  2072. ts.uniqueId = uuid.New().String()
  2073. ts.gameScene.initData(ts.table.GetTableID(), ts.roomInfo.ScoreToWin, ts.roomInfo.MaxSurrenderCount)
  2074. return
  2075. }
  2076. ts.gameScene.onAllCardsSent()
  2077. ts.table.SetTimer(TIMER_START_OUT_CARD, SEC_START_OUT_CARD)
  2078. ts.table.NotifySceneChanged(-1)
  2079. }
  2080. func (ts *tablesink) dealChooseTrump(t int) (bool, string) {
  2081. if ts.gameScene.Phase != Phase_ChooseTrump {
  2082. ts.table.LogWithTableId("tablesink.dealChooseTrump incorrect phase %d", ts.gameScene.Phase)
  2083. return false, "Wrong Phase"
  2084. }
  2085. if t == getCardType(ts.gameScene.PublicCard) || t >= CardType_Invalid {
  2086. ts.table.LogWithTableId("tablesink.dealChooseTrump incorrect Type %d", t)
  2087. return false, "Wrong Type"
  2088. }
  2089. ts.gameScene.FinalClub = Suit_Hokum
  2090. ts.gameScene.TrumpType = t
  2091. data := ShowCardType{Type: t, Chair: ts.gameScene.WhoseTurn}
  2092. d, _ := json.Marshal(data)
  2093. ts.table.SendGameData(-1, CMD_SHOW_CARDTYPE, string(d))
  2094. chair := ts.gameScene.WhoseTurn
  2095. ts.gameScene.LastTurn = chair
  2096. ts.gameScene.Players[chair].LastAction = Action_ChooseTrump
  2097. ts.gameScene.Players[chair].LastActionData = t
  2098. ts.gameScene.addAction(chair, Action_ChooseTrump, t)
  2099. ts.gameScene.PhaseIndex++
  2100. ts.gameScene.WhoseTurn = -1
  2101. ts.table.NotifySceneChanged(-1)
  2102. for i := 0; i < CHAIR_COUNT; i++ {
  2103. p := &ts.gameScene.Players[i]
  2104. sort.Slice(p.HandCards, func(m, n int) bool {
  2105. return getCardSortValue(p.HandCards[m], ts.gameScene.TrumpType) <
  2106. getCardSortValue(p.HandCards[n], ts.gameScene.TrumpType)
  2107. })
  2108. buyList := []int{}
  2109. if isSameTeam(i, ts.gameScene.BuyChair) {
  2110. ts.gameScene.Players[i].setPlayerOperators(buyList)
  2111. continue
  2112. }
  2113. buyList = append(buyList, Action_Buy_Pass, Action_Buy_Double)
  2114. ts.gameScene.Players[i].setPlayerOperators(buyList)
  2115. }
  2116. ts.gameScene.Phase = Phase_Double
  2117. ts.gameScene.PhaseIndex = 0
  2118. ts.table.SetTimer(TIMER_DOUBLE_CHANGE, 2200)
  2119. return true, ""
  2120. }
  2121. func (ts *tablesink) dealSawa() (bool, string) {
  2122. if ts.gameScene.Phase != Phase_Play {
  2123. ts.table.LogWithTableId("tablesink.dealSawa incorrect phase %d", ts.gameScene.Phase)
  2124. return false, "Wrong Phase"
  2125. }
  2126. if ts.gameScene.WhoseTurn == -1 {
  2127. ts.table.LogWithTableId("tablesink.dealSawa not correct WhoseTurn %d", ts.gameScene.WhoseTurn)
  2128. return false, "Wrong WhoseTurn"
  2129. }
  2130. if ts.gameScene.RoundType != CardType_Invalid {
  2131. ts.table.LogWithTableId("tablesink.dealSawa Not First OutCard")
  2132. return false, "Not First OutCard"
  2133. }
  2134. if isValidChair(ts.gameScene.SawaChair) {
  2135. ts.table.LogWithTableId("tablesink.dealSawa Already Sawa")
  2136. return false, "Already Sawa"
  2137. }
  2138. for i := 0; i < CHAIR_COUNT; i++ {
  2139. if ts.gameScene.Players[i].HaveBaloot && !ts.gameScene.Players[i].isIncludeBaloot() {
  2140. ts.gameScene.Players[i].Projects = append(ts.gameScene.Players[i].Projects, SingleProject{
  2141. Type: PROJECT_BALOOT,
  2142. Score: value_project[PROJECT_BALOOT],
  2143. Cards: []int{genACard(ts.gameScene.TrumpType, CardValueQ), genACard(ts.gameScene.TrumpType, CardValueK)},
  2144. })
  2145. d, _ := json.Marshal(i)
  2146. ts.table.SendGameData(-1, CMD_CALL_BALOOT, string(d))
  2147. if ts.roomInfo.IsDoublingMode {
  2148. ts.addDoublingData(Doubling_Project_Baloot)
  2149. ts.sendShowDoublingMsg([]int{Doubling_Project_Baloot})
  2150. }
  2151. }
  2152. }
  2153. if ts.roomInfo.IsCorrectionMode {
  2154. ts.gameScene.CanCorrect = true
  2155. }
  2156. ts.table.KillAllTimer()
  2157. p := &ts.gameScene.Players[ts.gameScene.WhoseTurn]
  2158. ts.gameScene.LastTurn = ts.gameScene.WhoseTurn
  2159. p.LastAction = Action_Sawa
  2160. ts.gameScene.addAction(ts.gameScene.WhoseTurn, Action_Sawa, ts.gameScene.WhoseTurn)
  2161. ts.gameScene.PhaseIndex++
  2162. ts.gameScene.ShowAllCards = true
  2163. ts.gameScene.SawaChair = ts.gameScene.WhoseTurn
  2164. ts.table.NotifySceneChanged(-1)
  2165. if ts.roomInfo.IsCorrectionMode {
  2166. ts.table.SetTimer(TIMER_SAWA, SEC_SAWA)
  2167. } else {
  2168. ts.table.SetTimer(TIMER_SAWA, 2200)
  2169. }
  2170. return true, ""
  2171. }
  2172. func (ts *tablesink) onTimerSawa() {
  2173. if ts.gameScene.Phase != Phase_Play {
  2174. ts.table.LogWithTableId("tablesink.dealSawa incorrect phase %d", ts.gameScene.Phase)
  2175. return
  2176. }
  2177. chair := ts.gameScene.WhoseTurn
  2178. winChair := chair
  2179. if !ts.roomInfo.IsCorrectionMode {
  2180. for n := 0; n < CHAIR_COUNT; n++ {
  2181. for i := 0; i <= len(ts.gameScene.Players[chair].HandCards); i++ {
  2182. card := ts.gameScene.Players[chair].HandCards[i]
  2183. if !ts.simulator.isBiggerThanAll(card, getCardType(card), ts.gameScene.Players[n].HandCards) {
  2184. winChair = n
  2185. break
  2186. }
  2187. }
  2188. if winChair != chair {
  2189. if isSameTeam(winChair, chair) {
  2190. winChair = getNextChair(chair)
  2191. }
  2192. break
  2193. }
  2194. }
  2195. }
  2196. leftScore := 0
  2197. getScore := ts.gameScene.Players[winChair].WinCardScore + ts.gameScene.Players[getNextChair(winChair)].WinCardScore
  2198. if ts.gameScene.FinalClub == Suit_Hokum {
  2199. leftScore = HOKUM_TOTAL_SCORE - getScore
  2200. } else {
  2201. leftScore = SUN_TOTAL_SCORE - getScore
  2202. }
  2203. for i := 0; i < CHAIR_COUNT; i++ {
  2204. if isSameTeam(i, winChair) {
  2205. ts.gameScene.Players[i].WinCardScore += leftScore
  2206. }
  2207. }
  2208. ts.gameScene.ShowAllCards = true
  2209. ts.gameScene.CanCorrect = false
  2210. ts.gameScene.Players[winChair].WinCardNumber += ts.gameScene.getLeftCardNumber()
  2211. for i := 0; i < CHAIR_COUNT; i++ {
  2212. ts.gameScene.Players[i].clearHandCard()
  2213. }
  2214. ts.gameScene.RoundWinner = winChair
  2215. ts.onTimerNewRound()
  2216. }
  2217. func (ts *tablesink) dealGawah() (bool, string) {
  2218. if ts.gameScene.Phase != Phase_Play {
  2219. ts.table.LogWithTableId("tablesink.dealGawah incorrect phase %d", ts.gameScene.Phase)
  2220. return false, "Wrong Phase"
  2221. }
  2222. if ts.gameScene.WhoseTurn == -1 {
  2223. ts.table.LogWithTableId("tablesink.dealGawah not correct WhoseTurn %d", ts.gameScene.WhoseTurn)
  2224. return false, "Wrong WhoseTurn"
  2225. }
  2226. chair := ts.gameScene.WhoseTurn
  2227. p := &ts.gameScene.Players[chair]
  2228. if !p.CanGawah {
  2229. ts.table.LogWithTableId("tablesink.dealGawah can not Gawah")
  2230. return false, "Wrong Action"
  2231. }
  2232. ts.gameScene.LastTurn = chair
  2233. p.LastAction = Action_Gawah
  2234. p.CanGawah = false
  2235. ts.gameScene.addAction(chair, Action_Gawah, chair)
  2236. ts.gameScene.PhaseIndex++
  2237. ts.table.KillTimer(TIMER_GAME)
  2238. leftScore := ts.gameScene.getLeftScore()
  2239. ts.gameScene.Players[chair].winCardScore += leftScore
  2240. doublingData := []int{}
  2241. for i := 0; i < CHAIR_COUNT; i++ {
  2242. if isSameTeam(i, chair) {
  2243. ts.gameScene.Players[i].addScore(leftScore)
  2244. } else {
  2245. ts.gameScene.Players[i].addScore(0)
  2246. }
  2247. if ts.gameScene.Players[i].HaveBaloot && !ts.gameScene.Players[i].isIncludeBaloot() {
  2248. ts.gameScene.Players[i].Projects = append(ts.gameScene.Players[i].Projects, SingleProject{
  2249. Type: PROJECT_BALOOT,
  2250. Score: value_project[PROJECT_BALOOT],
  2251. Cards: []int{genACard(ts.gameScene.TrumpType, CardValueQ), genACard(ts.gameScene.TrumpType, CardValueK)},
  2252. })
  2253. d, _ := json.Marshal(i)
  2254. ts.table.SendGameData(-1, CMD_CALL_BALOOT, string(d))
  2255. if ts.roomInfo.IsDoublingMode {
  2256. ts.addDoublingData(Doubling_Project_Baloot)
  2257. doublingData = append(doublingData, Doubling_Project_Baloot)
  2258. }
  2259. }
  2260. }
  2261. ts.gameScene.ShowAllCards = true
  2262. winCardNum := ts.gameScene.getLeftCardNumber()
  2263. preWinCount := ts.gameScene.Players[chair].consecutiveWinCardCount
  2264. ts.gameScene.Players[chair].consecutiveWinCardCount += winCardNum / CHAIR_COUNT
  2265. ts.gameScene.Players[getFriendChair(chair)].consecutiveWinCardCount += winCardNum / CHAIR_COUNT
  2266. ts.gameScene.Players[chair].WinCardNumber += winCardNum
  2267. if ts.gameScene.Players[chair].consecutiveWinCardCount >= 5 && ts.roomInfo.IsDoublingMode {
  2268. leftCount := 0
  2269. if preWinCount >= 5 {
  2270. leftCount = ts.gameScene.Players[chair].consecutiveWinCardCount - preWinCount
  2271. } else {
  2272. leftCount = ts.gameScene.Players[chair].consecutiveWinCardCount - 4
  2273. }
  2274. winData := []int{}
  2275. for i := 0; i < leftCount; i++ {
  2276. ts.addDoublingData(Doubling_ConsecutiveWinOverFour)
  2277. winData = append(winData, Doubling_ConsecutiveWinOverFour)
  2278. }
  2279. doublingData = append(doublingData, winData...)
  2280. }
  2281. if len(doublingData) > 0 {
  2282. ts.sendShowDoublingMsg(doublingData)
  2283. }
  2284. ts.table.NotifySceneChanged(-1)
  2285. for i := 0; i < CHAIR_COUNT; i++ {
  2286. ts.gameScene.Players[i].clearHandCard()
  2287. }
  2288. ts.gameScene.RoundWinner = chair
  2289. ts.table.SetTimer(TIMER_NEWROUND, 2200)
  2290. return true, ""
  2291. }
  2292. func (ts *tablesink) dealOutCard(card int, projects []int) (bool, string) {
  2293. if ts.gameScene.Phase != Phase_Play {
  2294. ts.table.LogWithTableId("tablesink.dealOutCard incorrect phase %d", ts.gameScene.Phase)
  2295. return false, "Wrong Phase"
  2296. }
  2297. if ts.gameScene.WhoseTurn == -1 {
  2298. ts.table.LogWithTableId("tablesink.dealOutCard incorrect WhoseTurn %d", ts.gameScene.WhoseTurn)
  2299. return false, "Wrong WhoseTurn"
  2300. }
  2301. chair := ts.gameScene.WhoseTurn
  2302. p := &ts.gameScene.Players[chair]
  2303. if ts.gameScene.isFirstOutCardInCloseMode() && !ts.roomInfo.IsCorrectionMode {
  2304. if getCardType(card) == ts.gameScene.TrumpType && !p.IsAllTrumpCard(ts.gameScene.TrumpType) {
  2305. ts.table.LogWithTableId("tablesink.dealOutCard OutCard Fail")
  2306. return false, "Wrong Card"
  2307. }
  2308. }
  2309. if isValidChair(ts.gameScene.SawaChair) {
  2310. return false, "is In Sawa"
  2311. }
  2312. // 检查牌是否能出
  2313. if !ts.simulator.fakers[chair].isOutBigCards(card) && !ts.roomInfo.IsCorrectionMode {
  2314. ts.table.LogWithTableId("tablesink.dealOutCard not Out BigCard %d, BigCard%s", card, getCardsHex(p.BigCards))
  2315. return false, "Wrong BigCard"
  2316. }
  2317. // 检查牌是否能出
  2318. if !ts.logic.canOut(card, ts.gameScene.RoundType, p.HandCards) && !ts.roomInfo.IsCorrectionMode {
  2319. ts.table.LogWithTableId("tablesink.dealOutCard removeCard failed %d", card)
  2320. return false, "Wrong Card Type"
  2321. }
  2322. handCards := make([]int, len(p.HandCards))
  2323. copy(handCards, p.HandCards)
  2324. // 牌在不在?
  2325. if !p.removeCard(card) {
  2326. ts.table.LogWithTableId("tablesink.dealOutCard removeCard failed %d", card)
  2327. return false, "Card Not Exist"
  2328. }
  2329. // 纠错模式,记录玩家出牌是否正确
  2330. if ts.roomInfo.IsCorrectionMode {
  2331. flag := -1
  2332. correctType := CorrectType_Invaild
  2333. shouldOutCards := []int{}
  2334. if ts.gameScene.isFirstOutCardInCloseMode() {
  2335. if getCardType(card) == ts.gameScene.TrumpType && !p.IsAllTrumpCard(ts.gameScene.TrumpType) {
  2336. flag = card
  2337. correctType = CorrectType_Close
  2338. for n := 0; n < len(handCards); n++ {
  2339. if getCardType(handCards[n]) != ts.gameScene.TrumpType {
  2340. shouldOutCards = append(shouldOutCards, handCards[n])
  2341. }
  2342. }
  2343. }
  2344. }
  2345. if !ts.simulator.fakers[chair].isOutBigCards(card) {
  2346. flag = card
  2347. for n := 0; n < len(handCards); n++ {
  2348. if ts.simulator.fakers[chair].isOutBigCards(handCards[n]) {
  2349. shouldOutCards = append(shouldOutCards, handCards[n])
  2350. }
  2351. }
  2352. if ts.gameScene.RoundType == ts.gameScene.TrumpType {
  2353. correctType = CorrectType_BigCardInHokum
  2354. } else {
  2355. correctType = CorrectType_TrumpCard
  2356. }
  2357. }
  2358. if !ts.logic.canOut(card, ts.gameScene.RoundType, handCards) {
  2359. flag = card
  2360. correctType = CorrectType_SameType
  2361. for n := 0; n < len(handCards); n++ {
  2362. if ts.logic.canOut(handCards[n], ts.gameScene.RoundType, handCards) {
  2363. shouldOutCards = append(shouldOutCards, handCards[n])
  2364. }
  2365. }
  2366. }
  2367. ts.simulator.fakers[chair].correctionList = append(ts.simulator.fakers[chair].correctionList, correction{
  2368. correctCard: flag,
  2369. correctType: correctType,
  2370. suggestCards: shouldOutCards,
  2371. })
  2372. }
  2373. ts.simulator.fakers[chair].removeCard(card)
  2374. if ts.gameScene.RoundType != CardType_Invalid && getCardType(card) != ts.gameScene.RoundType {
  2375. ts.simulator.fakers[chair].updataACardTypeToNotExist(ts.gameScene.RoundType)
  2376. }
  2377. if p.CanGawah {
  2378. p.CanGawah = false
  2379. }
  2380. ts.gameScene.LastTurn = chair
  2381. p.LastAction = Action_OutCard
  2382. p.LastActionData = card
  2383. p.IsCallBiggest = false
  2384. ts.gameScene.addOutCardAction(chair, Action_OutCard, card, projects)
  2385. doublingData := []int{}
  2386. if getCardType(card) == ts.gameScene.TrumpType &&
  2387. (getCardValue(card) == CardValueQ || getCardValue(card) == CardValueK) &&
  2388. p.outAllBalootCard(ts.gameScene.TrumpType) {
  2389. p.Projects = append(p.Projects, SingleProject{
  2390. Type: PROJECT_BALOOT,
  2391. Score: value_project[PROJECT_BALOOT],
  2392. Cards: []int{genACard(ts.gameScene.TrumpType, CardValueQ), genACard(ts.gameScene.TrumpType, CardValueK)},
  2393. })
  2394. d, _ := json.Marshal(ts.gameScene.WhoseTurn)
  2395. ts.table.SendGameData(-1, CMD_CALL_BALOOT, string(d))
  2396. if ts.roomInfo.IsDoublingMode {
  2397. ts.addDoublingData(Doubling_Project_Baloot)
  2398. doublingData = append(doublingData, Doubling_Project_Baloot)
  2399. }
  2400. }
  2401. if ts.gameScene.RoundIndex == 1 && len(projects) == 4 {
  2402. ts.gameScene.checkProject(chair, projects)
  2403. for n := 0; n < len(ts.gameScene.Players[chair].Projects); n++ {
  2404. //if ts.gameScene.Players[chair].CallProject == PROJECT_FOURHUNDRED {
  2405. var param badge.Scope
  2406. param.GameName = GAME_NAME
  2407. param.RankInfo.Score = ts.gameScene.Players[chair].Projects[n].taskScore
  2408. userId := ts.gameScene.Players[chair].userID
  2409. badge.DoAction(userId, badge.Action_Game_CallPoints, 1, param)
  2410. }
  2411. }
  2412. // 加倍模式项目加倍
  2413. if ts.roomInfo.IsDoublingMode && ts.gameScene.RoundIndex == 1 && len(p.Projects) > 0 {
  2414. projectData := []int{}
  2415. for i := 0; i < len(p.Projects); i++ {
  2416. ts.addDoublingData(p.Projects[i].Type)
  2417. projectData = append(projectData, p.Projects[i].Type)
  2418. }
  2419. doublingData = append(doublingData, projectData...)
  2420. }
  2421. delaySce := 0
  2422. if ts.gameScene.RoundIndex == 2 {
  2423. if len(ts.gameScene.Players[chair].Projects) > 0 {
  2424. userservices.DoRecord(ts.gameScene.Players[chair].userID, userservices.Record_Project, 1)
  2425. if ts.gameScene.Players[chair].Projects[0].Type >= PROJECT_FIFTY && ts.gameScene.Players[getFriendChair(chair)].isRobot {
  2426. delaySce += 3500
  2427. ts.sendRobotChatAction(RobotChatAction_FriendGetBigProject, ts.gameScene.Players[getFriendChair(chair)].userID, ts.gameScene.Players[chair].userID, delaySce)
  2428. delaySce += 2500
  2429. }
  2430. if ts.gameScene.Players[chair].Projects[0].Type >= PROJECT_HUNDRED {
  2431. start := rand.Intn(4)
  2432. for i := 0; i < CHAIR_COUNT; i++ {
  2433. chairId := (start + i) % CHAIR_COUNT
  2434. if !isSameTeam(chairId, chair) && ts.gameScene.Players[chairId].isRobot {
  2435. ts.sendRobotChatAction(RobotChatAction_EmenyGetBigProject, ts.gameScene.Players[chairId].userID, -1, delaySce)
  2436. delaySce += 2500
  2437. break
  2438. }
  2439. }
  2440. }
  2441. }
  2442. }
  2443. if ts.gameScene.FinalClub == Suit_Hokum && ts.gameScene.RoundType == CardType_Invalid &&
  2444. getCardType(card) != ts.gameScene.TrumpType && !isBiggestCard(card, ts.gameScene.TrumpType) {
  2445. if isScoreCard(card, []int{}, ts.gameScene.GameOutCardHistory, ts.gameScene.TrumpType) {
  2446. p.IsCallBiggest = true
  2447. }
  2448. }
  2449. if ts.gameScene.RoundType == CardType_Invalid {
  2450. ts.gameScene.RoundType = getCardType(card)
  2451. ts.simulator.roundType = ts.gameScene.RoundType
  2452. if ts.roomInfo.IsCorrectionMode && len(ts.gameScene.Players[chair].HandCards) == NORMAL_HOLD_CARD-2 {
  2453. ts.gameScene.CanCorrect = true
  2454. }
  2455. }
  2456. ts.gameScene.GameOutCardHistory = append(ts.gameScene.GameOutCardHistory, card)
  2457. if !ts.gameScene.isRoundEnd() {
  2458. ts.gameScene.nextChair()
  2459. if ts.gameScene.FinalClub == Suit_Hokum {
  2460. isFriendCallBiggest := false
  2461. friendOutCard := ts.gameScene.Players[getFriendChair(ts.gameScene.WhoseTurn)].CurrentCard
  2462. if isValidCard(friendOutCard) {
  2463. isFriendCallBiggest = ts.gameScene.Players[getFriendChair(ts.gameScene.WhoseTurn)].IsCallBiggest
  2464. if isBiggestCard(friendOutCard, ts.gameScene.TrumpType) {
  2465. isFriendCallBiggest = true
  2466. }
  2467. }
  2468. bigCards := ts.simulator.getCanOutCards(ts.gameScene.WhoseTurn, isFriendCallBiggest)
  2469. if !ts.roomInfo.IsCorrectionMode {
  2470. ts.gameScene.Players[ts.gameScene.WhoseTurn].initBigCards(bigCards)
  2471. }
  2472. ts.simulator.fakers[ts.gameScene.WhoseTurn].initBigCards(bigCards)
  2473. }
  2474. if len(doublingData) > 0 {
  2475. ts.sendShowDoublingMsg(doublingData)
  2476. }
  2477. ts.resetGameTimer()
  2478. ts.table.NotifySceneChanged(-1)
  2479. return true, ""
  2480. }
  2481. ts.table.KillTimer(TIMER_GAME)
  2482. for i := 0; i < CHAIR_COUNT; i++ {
  2483. ts.table.KillTimer(TIMER_ROBOT_SEND_CHAT_0 + i)
  2484. }
  2485. // 一圈打完了,需要延迟开新的一圈
  2486. outCards := ts.gameScene.getRoundCards()
  2487. winner := ts.logic.getWinner(outCards, ts.gameScene.RoundType)
  2488. ts.table.LogWithTableId("end a round outCards = %s roundType = %d,winner = %d",
  2489. getCardsHex(outCards), ts.gameScene.RoundType, winner)
  2490. for i := 0; i < CHAIR_COUNT; i++ {
  2491. if !isSameTeam(i, winner) {
  2492. ts.gameScene.Players[i].consecutiveWinCardCount = 0
  2493. ts.simulator.fakers[i].consecutiveWinCardCount = 0
  2494. } else {
  2495. ts.gameScene.Players[i].consecutiveWinCardCount++
  2496. ts.simulator.fakers[i].consecutiveWinCardCount++
  2497. }
  2498. if i == winner {
  2499. ts.gameScene.Players[i].selfConsecutiveWinCardCount = 0
  2500. } else {
  2501. ts.gameScene.Players[i].selfConsecutiveWinCardCount++
  2502. }
  2503. }
  2504. if ts.gameScene.Players[winner].consecutiveWinCardCount >= 5 && ts.roomInfo.IsDoublingMode {
  2505. ts.addDoublingData(Doubling_ConsecutiveWinOverFour)
  2506. doublingData = append(doublingData, Doubling_ConsecutiveWinOverFour)
  2507. start := rand.Intn(4)
  2508. for i := 0; i < CHAIR_COUNT; i++ {
  2509. chairId := (start + i) % CHAIR_COUNT
  2510. if !isSameTeam(chairId, winner) && ts.gameScene.Players[chairId].isRobot && ts.gameScene.Players[winner].consecutiveWinCardCount == 5 {
  2511. ts.sendRobotChatAction(RobotChatAction_LoseOverFive, ts.gameScene.Players[chairId].userID, -1, delaySce)
  2512. delaySce += 2500
  2513. break
  2514. }
  2515. }
  2516. }
  2517. if ts.gameScene.Players[winner].selfConsecutiveWinCardCount == 5 {
  2518. if ts.gameScene.Players[winner].isRobot {
  2519. ts.sendRobotChatAction(RobotChatAction_SelfWinOverFive, ts.gameScene.Players[winner].userID, -1, delaySce)
  2520. delaySce += 2500
  2521. }
  2522. if ts.gameScene.Players[winner].isRobot && ts.gameScene.Players[getFriendChair(winner)].isRobot {
  2523. ts.sendRobotChatAction(RobotChatAction_FriendWinOverFive, ts.gameScene.Players[getFriendChair(winner)].userID, -1, delaySce)
  2524. }
  2525. }
  2526. winScore := ts.gameScene.endAOutCardRound(winner)
  2527. for i := 0; i < CHAIR_COUNT; i++ {
  2528. if isSameTeam(i, winner) {
  2529. ts.simulator.fakers[i].addScore(winScore)
  2530. }
  2531. }
  2532. for i := 0; i < CHAIR_COUNT; i++ {
  2533. ts.simulator.gameOutCardHistory = append(ts.simulator.gameOutCardHistory, ts.simulator.fakers[i].currentCard)
  2534. ts.simulator.fakers[i].currentCard = CARD_COUNT
  2535. ts.gameScene.Players[i].initBigCards([]int{})
  2536. ts.simulator.fakers[i].initBigCards([]int{})
  2537. }
  2538. if len(doublingData) > 0 {
  2539. ts.sendShowDoublingMsg(doublingData)
  2540. }
  2541. ts.table.NotifySceneChanged(-1)
  2542. if ts.gameScene.isGameEnd() {
  2543. ts.table.SetTimer(TIMER_NEWROUND, 1800)
  2544. } else {
  2545. ts.table.SetTimer(TIMER_NEWROUND, SEC_NEWROUND)
  2546. }
  2547. return true, ""
  2548. }
  2549. func (ts *tablesink) onTimerNewRound() {
  2550. if ts.gameScene.Phase == Phase_End {
  2551. return
  2552. }
  2553. if ts.gameScene.isGameEnd() {
  2554. if ts.roomInfo.IsCorrectionMode {
  2555. ts.table.SetTimer(TIMER_CORRECTDELAY, SCE_CORRECT_DELAY)
  2556. return
  2557. }
  2558. // 轮次结束也要清理收牌信息
  2559. ts.gameScene.newOutCardRound()
  2560. // 进入SetEnd阶段
  2561. ts.enterGameEndPhase(true)
  2562. return
  2563. }
  2564. // 继续
  2565. ts.gameScene.newOutCardRound()
  2566. if ts.gameScene.RoundIndex == 2 {
  2567. for i := 0; i < CHAIR_COUNT; i++ {
  2568. if len(ts.gameScene.Players[i].Projects) == 0 {
  2569. continue
  2570. }
  2571. ts.simulator.fakers[i].projects = append(ts.simulator.fakers[i].projects, ts.gameScene.Players[i].Projects...)
  2572. }
  2573. }
  2574. if !ts.roomInfo.IsCorrectionMode {
  2575. ts.gameScene.checkCanGawah()
  2576. }
  2577. ts.simulator.roundType = ts.gameScene.RoundType
  2578. ts.resetGameTimer()
  2579. ts.gameScene.Index++
  2580. ts.table.NotifySceneChanged(-1)
  2581. }
  2582. func (ts *tablesink) enterGameEndPhase(isCalc bool) {
  2583. if isCalc {
  2584. ts.gameScene.gameEnd()
  2585. } else {
  2586. ts.gameScene.PhaseIndex = 0
  2587. ts.gameScene.Phase = Phase_GameEnd
  2588. }
  2589. if !ts.table.IsPrivate() && ts.roomInfo.LevelParam >= 100 {
  2590. for i := 0; i < CHAIR_COUNT; i++ {
  2591. ts.gameScene.Players[i].LevelValue = 0
  2592. if ts.gameScene.Players[i].FinalCalcScore == 0 {
  2593. continue
  2594. }
  2595. value := (10 * ts.roomInfo.LevelParam * ts.gameScene.Players[i].FinalCalcScore) / 100
  2596. value = ts.table.AddExperience(ts.gameScene.Players[i].userID, value)
  2597. ts.gameScene.Players[i].LevelValue = value
  2598. }
  2599. }
  2600. var scores perRoundScores
  2601. scores.positionScore0 = ts.gameScene.Players[0].FinalCalcScore
  2602. scores.positionScore1 = ts.gameScene.Players[1].FinalCalcScore
  2603. ts.gameScene.scoreHistory = append(ts.gameScene.scoreHistory, scores)
  2604. goal := -1
  2605. if scores.positionScore0 == 0 {
  2606. goal = 1
  2607. } else if scores.positionScore1 == 0 {
  2608. goal = 1
  2609. }
  2610. if goal != -1 {
  2611. for i := 0; i < CHAIR_COUNT; i++ {
  2612. if isSameTeam(i, goal) {
  2613. userservices.DoRecord(ts.gameScene.Players[i].userID, userservices.Record_BalootWinAllScore, 1)
  2614. }
  2615. }
  2616. }
  2617. ts.statisticsRobotAction()
  2618. ts.gameScene.statics = append(ts.gameScene.statics, robotChatActionList{
  2619. round: ts.gameScene.GameIndex,
  2620. action: ts.gameScene.robotChatList,
  2621. })
  2622. ts.gameScene.robotChatList = []robotChatAction{}
  2623. ts.gameScene.robotSendChatCount = 0
  2624. ts.resetGameTimer()
  2625. ts.reportTaskAndUpdateGameScore()
  2626. //ts.table.NotifySceneChanged(-1)
  2627. if !ts.gameScene.isGameFinish() && !ts.surrenderInfo.isSurrenderEnd() {
  2628. ts.table.SendGameData(-1, CMD_SMALL_RESULT, ts.gameScene.getScene(-1, true, false))
  2629. }
  2630. // 为比赛场服务,最后一局EndScore不清零
  2631. if (ts.gameScene.ScoreToWin < -1 && (ts.gameScene.GameIndex+ts.gameScene.ScoreToWin) < -1) ||
  2632. (!ts.table.IsPrivate() && ts.gameScene.ScoreToWin == -1) {
  2633. for i := 0; i < CHAIR_COUNT; i++ {
  2634. ts.gameScene.Players[i].EndScore = 0
  2635. }
  2636. }
  2637. if ts.gameScene.ScoreToWin < -1 && (ts.gameScene.GameIndex+ts.gameScene.ScoreToWin) < -1 && ts.table.GetOwner() == -1 {
  2638. for i := 0; i < CHAIR_COUNT; i++ {
  2639. ts.gameScene.Players[i].Doublings = []DoublingDetail{}
  2640. ts.gameScene.Players[i].TotalScore = 0
  2641. }
  2642. }
  2643. }
  2644. func (ts *tablesink) reportTaskAndUpdateGameScore() {
  2645. for i := 0; i < CHAIR_COUNT; i++ {
  2646. if !ts.gameScene.Players[i].IsValid {
  2647. continue
  2648. }
  2649. usr := ts.table.GetUserByChair(i)
  2650. if usr == nil {
  2651. continue
  2652. }
  2653. score := 0
  2654. // 比赛场,需要按玩家底分来上报分数,注意上报是本局变化的分数
  2655. if ts.table.GetOwner() == -1 {
  2656. oldScore := usr.GetScore()
  2657. ts.table.LogWithTableId("reportTaskAndUpdateGameScore,%d,%d,%d", oldScore, usr.GetBaseScore(), usr.GetSetCount())
  2658. // 上报完成后,修改自身分数
  2659. score = ts.gameScene.getDoublingTotalTimes() * usr.GetBaseScore()
  2660. if ts.gameScene.Players[i].FinalCalcScore < ts.gameScene.Players[getNextChair(i)].FinalCalcScore {
  2661. score = -score
  2662. } else if ts.gameScene.Players[i].FinalCalcScore == ts.gameScene.Players[getNextChair(i)].FinalCalcScore {
  2663. if !isSameTeam(i, ts.gameScene.BuyChair) {
  2664. score = -score
  2665. }
  2666. }
  2667. usr.SetScore(oldScore + score)
  2668. ts.table.UpdateGameScore(usr.GetUserId(), score)
  2669. ts.gameScene.Players[i].EndScore = score
  2670. ts.gameScene.Players[i].MatchScore += score
  2671. } else {
  2672. if !ts.table.IsPrivate() && ts.gameScene.ScoreToWin == -1 {
  2673. score = ts.gameScene.getDoublingTotalTimes() * ts.gameScene.Players[i].bet
  2674. if !ts.gameScene.isPlayerWinInQuickGame(i) {
  2675. score = -score
  2676. }
  2677. ts.gameScene.Players[i].EndScore = score + ts.gameScene.Players[i].bet
  2678. }
  2679. ts.table.UpdateGameScore(usr.GetUserId(), ts.gameScene.Players[i].getTotalScore())
  2680. }
  2681. if ts.table.IsPrivate() && config.Server.IsLadderRoom > 0 {
  2682. p := &ts.gameScene.Players[i]
  2683. ts.gameScene.Players[i].LastWinCount = ladder.GetUserConsecutiveWinCount(p.userID, GAMEID)
  2684. ts.gameScene.Players[i].LadderChangeValue, _ = ladder.AddUserLadderScore(p.userID, GAMEID, ts.roomInfo.RoomName, score)
  2685. if ts.gameScene.Players[i].LadderChangeValue > 0 {
  2686. ts.gameScene.Players[i].LastWinCount++
  2687. }
  2688. d := ladder.GetUserLadderInfo(p.userID, GAMEID)
  2689. if d != nil {
  2690. ts.gameScene.Players[i].LadderInfo = *d
  2691. } else {
  2692. ts.table.LogWithTableId("User:%d, LadderInfo is nil", p.userID)
  2693. }
  2694. }
  2695. if ts.gameScene.FinalClub == Suit_Sun {
  2696. task.DoTaskAction(usr.GetUserId(), TaskAction_Baloot_Sun, 1, task.TaskScope{GameName: GAME_NAME})
  2697. }
  2698. if ts.gameScene.Players[i].TotalChangeScore >= 80 {
  2699. task.DoTaskAction(usr.GetUserId(), TaskAction_Baloot80, 1, task.TaskScope{GameName: GAME_NAME})
  2700. }
  2701. if ts.gameScene.Players[i].TotalChangeScore >= 100 {
  2702. task.DoTaskAction(usr.GetUserId(), TaskAction_Baloot100, 1, task.TaskScope{GameName: GAME_NAME})
  2703. }
  2704. if ts.gameScene.Players[i].TotalChangeScore >= 120 {
  2705. task.DoTaskAction(usr.GetUserId(), TaskAction_Baloot120, 1, task.TaskScope{GameName: GAME_NAME})
  2706. }
  2707. }
  2708. }
  2709. func (ts *tablesink) enterEndPhase() {
  2710. // 结算
  2711. winners := ts.gameScene.getWinner()
  2712. ts.table.LogWithTableId("tablesink.enterEndPhase getWinners:%v", winners)
  2713. if len(winners) == 0 {
  2714. ts.table.LogWithTableId("tablesink.enterEndPhase getWinners = 0")
  2715. if ts.table.IsPrivate() {
  2716. ts.table.PrivateRoomSetWinners([]int{})
  2717. }
  2718. ts.endGame()
  2719. ts.uniqueId = uuid.New().String()
  2720. ts.gameScene.initData(ts.table.GetTableID(), ts.roomInfo.ScoreToWin, ts.roomInfo.MaxSurrenderCount)
  2721. return
  2722. }
  2723. totalBet := 0
  2724. if ts.gameScene.Players[winners[0]].bet > 0 {
  2725. totalBet = ts.gameScene.getTotalBet(winners[0])
  2726. }
  2727. // 平分
  2728. endScore := totalBet / len(winners)
  2729. if endScore != 0 {
  2730. for _, v := range winners {
  2731. ts.gameScene.Players[v].EndScore = endScore
  2732. }
  2733. }
  2734. // 写分
  2735. for i := 0; i < CHAIR_COUNT; i++ {
  2736. p := &ts.gameScene.Players[i]
  2737. usr := ts.table.GetUserByChair(i)
  2738. if !p.IsValid || usr == nil {
  2739. continue
  2740. }
  2741. realWin := p.EndScore - p.bet
  2742. if p.bet == 0 {
  2743. realWin = 0
  2744. }
  2745. tax := 0
  2746. if realWin > 0 {
  2747. tax = realWin * ts.roomInfo.TaxRate / 100
  2748. }
  2749. wScore := p.EndScore - tax
  2750. if p.bet == 0 {
  2751. wScore = 0
  2752. }
  2753. ts.writeScore(p.userID, wScore, tax, 0, ScoreType_End, ts.roomInfo.RoomName, p.isRobot)
  2754. if wScore > 0 {
  2755. userservices.DoRecord(p.userID, userservices.Record_MaxWin, wScore)
  2756. }
  2757. if !p.isRobot {
  2758. roomType := waterpool.RoomType_Normal
  2759. if ts.roomInfo.IsDoublingMode {
  2760. roomType = waterpool.RoomType_Doubling
  2761. }
  2762. if ts.roomInfo.RoomType == 3 {
  2763. roomType = waterpool.RoomType_Quick
  2764. }
  2765. if config.Server.IsLadderRoom > 0 {
  2766. roomType = waterpool.RoomType_Ladder
  2767. }
  2768. if p.isControl {
  2769. go waterpool.UpdataUserWaterPool(p.userID, realWin, GAME_NAME, roomType, ts.roomInfo.RoomID)
  2770. } else {
  2771. go waterpool.ReduceInventoryValue(GAMEID, ts.roomInfo.RoomName, realWin-tax, roomType)
  2772. }
  2773. }
  2774. // 写记录
  2775. if ts.table.IsPrivate() {
  2776. } else {
  2777. if p.bet != 0 {
  2778. go ts.table.WriteBetRecordWithSetcount(p.userID, p.bet, wScore, 1.0, "normal",
  2779. fmt.Sprintf("totalpoits:[%v]", p.roundScores), ts.roomInfo.RoomName, len(p.roundScores))
  2780. }
  2781. if config.Server.IsLadderRoom > 0 {
  2782. ts.gameScene.Players[i].LastWinCount = ladder.GetUserConsecutiveWinCount(p.userID, GAMEID)
  2783. ts.gameScene.Players[i].LadderChangeValue, _ = ladder.AddUserLadderScore(p.userID, GAMEID, ts.roomInfo.RoomName, wScore-p.bet)
  2784. if ts.gameScene.Players[i].LadderChangeValue > 0 {
  2785. ts.gameScene.Players[i].LastWinCount++
  2786. }
  2787. d := ladder.GetUserLadderInfo(p.userID, GAMEID)
  2788. if d != nil {
  2789. ts.gameScene.Players[i].LadderInfo = *d
  2790. } else {
  2791. ts.table.LogWithTableId("User:%d, LadderInfo is nil", p.userID)
  2792. }
  2793. }
  2794. }
  2795. }
  2796. if ts.roomInfo.RoomID == 0 && !ts.table.IsPrivate() {
  2797. for i := 0; i < CHAIR_COUNT; i++ {
  2798. ts.gameScene.Players[i].LevelValue = 0
  2799. if ts.gameScene.Players[i].isRobot {
  2800. continue
  2801. }
  2802. value := ts.table.AddExperience(ts.gameScene.Players[i].userID, 100)
  2803. ts.gameScene.Players[i].LevelValue = value
  2804. }
  2805. }
  2806. ts.gameScene.Phase = Phase_End
  2807. ts.table.NotifySceneChanged(-1)
  2808. if ts.table.IsPrivate() {
  2809. winnerUsers := make([]int, len(winners))
  2810. for i := 0; i < len(winners); i++ {
  2811. winnerUsers[i] = ts.gameScene.Players[winners[i]].userID
  2812. }
  2813. ts.table.PrivateRoomSetWinners(winnerUsers)
  2814. ts.writePrivateBetRecords()
  2815. }
  2816. ts.endGame()
  2817. ts.uniqueId = uuid.New().String()
  2818. ts.gameScene.initData(ts.table.GetTableID(), ts.roomInfo.ScoreToWin, ts.roomInfo.MaxSurrenderCount)
  2819. }
  2820. func (ts *tablesink) writePrivateBetRecords() {
  2821. for i := 0; i < CHAIR_COUNT; i++ {
  2822. p := &ts.gameScene.Players[i]
  2823. usr := ts.table.GetUserByChair(i)
  2824. if !p.IsValid || usr == nil {
  2825. continue
  2826. }
  2827. fee, prize := ts.table.PrivateRoomGetFeeAndPrize(p.userID)
  2828. ts.table.LogWithTableId("writePrivateBetRecords user[%d] fee[%d],prize[%d]", p.userID, fee, prize)
  2829. ts.table.WriteBetRecordWithSetcount(p.userID, fee, prize, 1.0, ts.roomType,
  2830. fmt.Sprintf("totalpoits:%v", p.roundScores), ts.roomInfo.RoomName, len(p.roundScores))
  2831. }
  2832. }
  2833. func (ts *tablesink) endGame() {
  2834. ts.table.LogWithTableId("--------endGame-----------")
  2835. ts.gameScene.dump(true)
  2836. ts.statisticsScoreHistoryToExcel()
  2837. ts.statisRobotChatCount()
  2838. ts.gameScene.PhaseIndex = 0
  2839. ts.gameScene.GameIndex = 0
  2840. ts.gameScene.Index = 0
  2841. ts.surrenderInfo.isSurrendering = false
  2842. ts.table.KillAllTimer()
  2843. if ts.table.IsPrivate() {
  2844. ts.table.SetTimer(TIMER_DELAY_END, 1000)
  2845. // 私人场,不继续下一把
  2846. if ts.roomInfo.BaseScore == 0 {
  2847. ts.gameScene.pointsToEndScore()
  2848. }
  2849. return
  2850. }
  2851. ts.table.EndGame()
  2852. //realUserCount := 0
  2853. robotCount := 0
  2854. for i := 0; i < CHAIR_COUNT; i++ {
  2855. usr := ts.table.GetUserByChair(i)
  2856. if usr == nil {
  2857. continue
  2858. }
  2859. // 机器人,随机3-5秒准备
  2860. if !usr.IsRobot() {
  2861. ts.table.SetTimer(TIMER_READY_0+i, SEC_READY)
  2862. ts.gameScene.Players[i].isEndToStart = true
  2863. //realUserCount++
  2864. } else {
  2865. //ts.table.SetTimer(TIMER_READY_0+i, 100)
  2866. robotCount++
  2867. }
  2868. }
  2869. if robotCount > 0 {
  2870. ts.table.SetTimer(TIMER_REMOVE_ROBOT, 1000+rand.Intn(1000))
  2871. }
  2872. }
  2873. func (ts *tablesink) writeScore(userId int, amount, tax int, status, scoreType int, sourceName string, isRobot bool) (bool, int) {
  2874. return ts.table.WriteUserMoney(userId, amount, tax, status, scoreType, sourceName)
  2875. }
  2876. func (ts *tablesink) checkIsNeedRobot() {
  2877. if ts.gameScene.Phase != Phase_Free && ts.gameScene.Phase != Phase_End {
  2878. fmt.Print("游戏阶段=====ts.gameScene.Phase",ts.gameScene.Phase)
  2879. return
  2880. }
  2881. if ts.roomInfo.RobotCount == 0 {
  2882. fmt.Print("游戏阶段=====ts.roomInfo.RobotCount",ts.roomInfo.RobotCount)
  2883. return
  2884. }
  2885. if ts.table.IsPrivate() {
  2886. fmt.Print("游戏阶段=====ts.table.IsPrivate")
  2887. return
  2888. }
  2889. count := 0
  2890. playerCount := 0
  2891. for i := 0; i < CHAIR_COUNT; i++ {
  2892. usr := ts.table.GetUserByChair(i)
  2893. if usr == nil {
  2894. continue
  2895. }
  2896. if !usr.IsRobot() {
  2897. playerCount++
  2898. }
  2899. if usr.GetUserStatus() <= user.UserStatus_Free {
  2900. continue
  2901. }
  2902. count++
  2903. }
  2904. chairCount := CHAIR_COUNT
  2905. if count >= chairCount {
  2906. fmt.Print("游戏阶段=====玩家数大于椅子数=====")
  2907. return
  2908. }
  2909. robotCount := chairCount - count
  2910. ts.table.LogWithTableId("-------checkIsNeedRobot. need: %d ------", robotCount)
  2911. if robotCount > 0 && playerCount > 0 {
  2912. sec := ts.roomInfo.RobotEnterRoomTime*1000 - 500
  2913. time.AfterFunc(time.Duration(sec)*time.Millisecond, func() {
  2914. if ts.gameScene.Phase != Phase_Free && ts.gameScene.Phase != Phase_End {
  2915. fmt.Print("游戏阶段=====ts.gameScene.Phase",ts.gameScene.Phase)
  2916. return
  2917. }
  2918. tableID := ts.table.GetTableID()
  2919. robotmanager.GetOneRobotEnterTable(tableID, ts.roomInfo.MinGold, ts.roomInfo.MaxGold)
  2920. })
  2921. }
  2922. }
  2923. func (ts *tablesink) removeOneRobot() {
  2924. if ts.gameScene.Phase != Phase_End && ts.gameScene.Phase != Phase_Free {
  2925. return
  2926. }
  2927. for i := 0; i < CHAIR_COUNT; i++ {
  2928. usr := ts.table.GetUserByChair(i)
  2929. if usr == nil {
  2930. continue
  2931. }
  2932. if !usr.IsRobot() {
  2933. continue
  2934. }
  2935. ts.table.KickUserByChair(i, false)
  2936. //return
  2937. }
  2938. }
  2939. func (ts *tablesink) getRobotActionTime(chairId int, isNeedDiminishing bool) int {
  2940. if ts.gameScene.Phase == Phase_FirstBuy || ts.gameScene.Phase == Phase_SecondBuy {
  2941. return 1000 + rand.Intn(2000)
  2942. }
  2943. if !isNeedDiminishing {
  2944. diff := ts.roomInfo.MaxRobotNotFirstOutMS - ts.roomInfo.MinRobotNotFirstOutMS
  2945. if diff <= 0 {
  2946. diff = MS_MaxRobotNotFirstOut - MS_MinRobotNotFirstOut
  2947. }
  2948. minMs := ts.roomInfo.MinRobotNotFirstOutMS
  2949. if minMs < MIN_OUT_MS {
  2950. minMs = MS_MinRobotNotFirstOut
  2951. }
  2952. return minMs + rand.Intn(diff)
  2953. }
  2954. ms := 0
  2955. if ts.gameScene.getRoundOutedPlayerCount() > 0 {
  2956. diff := ts.roomInfo.MaxRobotNotFirstOutMS - ts.roomInfo.MinRobotNotFirstOutMS
  2957. if diff <= 0 {
  2958. diff = MS_MaxRobotNotFirstOut - MS_MinRobotNotFirstOut
  2959. }
  2960. minMs := ts.roomInfo.MinRobotNotFirstOutMS
  2961. if minMs <= MIN_OUT_MS {
  2962. minMs = MS_MinRobotNotFirstOut
  2963. }
  2964. leftCardCount := NORMAL_HOLD_CARD - len(ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards)
  2965. deMs := ts.roomInfo.DiminishingNotFirstOutMS * leftCardCount
  2966. if deMs <= 0 || (minMs-deMs) < MIN_OUT_MS {
  2967. deMs = MS_DiminishingNotFirstOut * leftCardCount
  2968. }
  2969. ms = minMs + rand.Intn(diff) - deMs
  2970. } else {
  2971. diff := ts.roomInfo.MaxRobotFirstOutMS - ts.roomInfo.MinRobotFirstOutMS
  2972. if diff <= 0 {
  2973. diff = MS_MaxRobotFirstOut - MS_MinRobotFirstOut
  2974. }
  2975. minMs := ts.roomInfo.MinRobotFirstOutMS
  2976. if minMs < MIN_OUT_MS {
  2977. minMs = MS_MinRobotFirstOut
  2978. }
  2979. leftCardCount := NORMAL_HOLD_CARD - len(ts.gameScene.Players[ts.gameScene.WhoseTurn].HandCards)
  2980. deMs := ts.roomInfo.DiminishingFirstOutMS * leftCardCount
  2981. if deMs <= 0 || (minMs-deMs) < MIN_OUT_MS {
  2982. deMs = MS_DiminishingFirstOut * leftCardCount
  2983. }
  2984. ms = minMs + rand.Intn(diff) - deMs
  2985. }
  2986. return ms
  2987. }
  2988. func (ts *tablesink) checkRobotAction() {
  2989. if ts.gameScene.Phase == Phase_GameEnd || ts.gameScene.Phase == Phase_Start {
  2990. return
  2991. }
  2992. if ts.gameScene.Phase == Phase_Reshuffle {
  2993. for i := 0; i < CHAIR_COUNT; i++ {
  2994. usr := ts.table.GetUserByChair(i)
  2995. if usr == nil || !usr.IsRobot() {
  2996. continue
  2997. }
  2998. if ts.gameScene.Players[i].canReshuffle() {
  2999. ms := rand.Intn(500) + 500
  3000. ts.table.SetTimer(TIMER_ROBOT_ACTION_0+i, ms)
  3001. break
  3002. }
  3003. }
  3004. return
  3005. }
  3006. if ts.gameScene.WhoseTurn != -1 {
  3007. usr := ts.table.GetUserByChair(ts.gameScene.WhoseTurn)
  3008. if usr == nil || !usr.IsRobot() {
  3009. return
  3010. }
  3011. ms := ts.getRobotActionTime(ts.gameScene.WhoseTurn, ts.gameScene.Phase == Phase_Play)
  3012. ts.table.SetTimer(TIMER_ROBOT_ACTION_0+ts.gameScene.WhoseTurn, ms)
  3013. } else {
  3014. start := rand.Intn(CHAIR_COUNT)
  3015. ms := 0
  3016. for i := 0; i < CHAIR_COUNT; i++ {
  3017. iChair := (i + start) % CHAIR_COUNT
  3018. usr := ts.table.GetUserByChair(iChair)
  3019. if usr == nil || !usr.IsRobot() || !ts.gameScene.Players[iChair].canAction() {
  3020. continue
  3021. }
  3022. if ms == 0 {
  3023. ms = ts.getRobotActionTime(iChair, false)
  3024. } else {
  3025. ms += 1000
  3026. }
  3027. ts.table.SetTimer(TIMER_ROBOT_ACTION_0+iChair, ms)
  3028. }
  3029. }
  3030. }
  3031. func (ts *tablesink) onTimerAutoAction(chairId int) {
  3032. if ts.gameScene.WhoseTurn != -1 && ts.gameScene.Phase != Phase_Reshuffle {
  3033. ts.table.LogWithTableId("tablesink.onTimerAutoAction WhoseTurn != -1")
  3034. return
  3035. }
  3036. if !ts.gameScene.Players[chairId].AutoOut {
  3037. ts.table.LogWithTableId("tablesink.onTimerAutoAction not Auto")
  3038. return
  3039. }
  3040. if ts.gameScene.Phase != Phase_FirstBuy &&
  3041. ts.gameScene.Phase != Phase_SecondBuy &&
  3042. ts.gameScene.Phase != Phase_Double &&
  3043. ts.gameScene.Phase != Phase_Reshuffle {
  3044. ts.table.LogWithTableId("tablesink.onTimerAutoAction not Correct Phase")
  3045. return
  3046. }
  3047. if ts.gameScene.Phase == Phase_Reshuffle {
  3048. ts.dealReshuffle(chairId, Reshuffle_Pass)
  3049. return
  3050. }
  3051. action := ts.gameScene.Players[chairId].getAutoBuyAction()
  3052. if action != Action_Buy_Invaild && ts.gameScene.Players[chairId].canAction() {
  3053. if ts.gameScene.Phase == Phase_Double {
  3054. ts.dealDouble(chairId, action)
  3055. }
  3056. if ts.gameScene.Phase == Phase_FirstBuy || ts.gameScene.Phase == Phase_SecondBuy {
  3057. ts.dealBuy(chairId, action)
  3058. }
  3059. }
  3060. }
  3061. func (ts *tablesink) onTimerRobotAction(chairId int) {
  3062. if ts.gameScene.WhoseTurn != -1 {
  3063. if ts.gameScene.WhoseTurn != chairId && ts.gameScene.Phase != Phase_Reshuffle {
  3064. ts.table.LogWithTableId("tablesink.robotAction whoseturn changed")
  3065. return
  3066. }
  3067. } else {
  3068. if !ts.gameScene.Players[chairId].canAction() {
  3069. ts.table.LogWithTableId("tablesink.robotAction canAction changed")
  3070. return
  3071. }
  3072. }
  3073. if ts.gameScene.Phase == Phase_Play {
  3074. if len(ts.gameScene.Players[chairId].HandCards) == 0 {
  3075. ts.table.LogWithTableId("tablesink.robotAction chair:%d HandCard == 0", chairId)
  3076. return
  3077. }
  3078. var projects []int
  3079. if ts.gameScene.RoundIndex == 1 {
  3080. cardList := make([]int, len(ts.gameScene.Players[chairId].HandCards))
  3081. copy(cardList, ts.gameScene.Players[chairId].HandCards)
  3082. projects = ts.logic.getBestProject(cardList)
  3083. }
  3084. if ts.gameScene.Players[chairId].CanGawah {
  3085. ts.dealGawah()
  3086. return
  3087. }
  3088. outCard := CARD_COUNT
  3089. isQuickOrLadder := false
  3090. if ts.roomInfo.RoomType == 3 || config.Server.IsLadderRoom == 1 {
  3091. isQuickOrLadder = true
  3092. }
  3093. outCard = ts.simulator.getBestOutCard(chairId, isQuickOrLadder)
  3094. if !isValidCard(outCard) {
  3095. if len(ts.gameScene.Players[chairId].BigCards) > 0 {
  3096. outCard = ts.gameScene.Players[chairId].BigCards[0]
  3097. } else {
  3098. outCard = ts.logic.worstCard(ts.gameScene.Players[chairId].HandCards, ts.gameScene.RoundType)
  3099. }
  3100. }
  3101. ts.dealOutCard(outCard, projects)
  3102. } else {
  3103. action := ts.getAction(chairId)
  3104. switch ts.gameScene.Phase {
  3105. case Phase_FirstBuy:
  3106. fallthrough
  3107. case Phase_SecondBuy:
  3108. if ts.roomInfo.RoomID == 0 && !ts.table.IsPrivate() {
  3109. action = Action_Buy_Pass
  3110. if ts.allPassCount >= 2 {
  3111. action = Action_Buy_Sun
  3112. }
  3113. }
  3114. ts.dealBuy(chairId, action)
  3115. case Phase_ChooseTrump:
  3116. ts.dealChooseTrump(action)
  3117. case Phase_CloseOpen:
  3118. ts.dealCloseOrOpen(action)
  3119. case Phase_Double:
  3120. ts.dealDouble(chairId, action)
  3121. case Phase_Reshuffle:
  3122. if ts.gameScene.Players[chairId].ReshuffleAction == Reshuffle_None && ts.gameScene.Players[chairId].CanDoReshuffle {
  3123. ts.dealReshuffle(chairId, Reshuffle_Confirm)
  3124. }
  3125. default:
  3126. ts.table.LogWithTableId("tablesink.robotAction invalid phase %d", ts.gameScene.Phase)
  3127. }
  3128. }
  3129. }
  3130. func (ts *tablesink) getAction(chairId int) int {
  3131. buyWinScore := 0
  3132. action, optionButtons := ts.getInitActionAndOperationList(chairId)
  3133. // 不抢自己队友的买牌
  3134. if isValidChair(ts.gameScene.BuyChair) && isFriendChair(ts.gameScene.BuyChair, chairId) &&
  3135. (ts.gameScene.Phase == Phase_FirstBuy || ts.gameScene.Phase == Phase_SecondBuy) {
  3136. return action
  3137. }
  3138. // 做模拟场景分析,并获取最大赢分的操作
  3139. for i := 0; i < len(optionButtons); i++ {
  3140. // 过滤掉Action_Buy_Pass的解析
  3141. if optionButtons[i] == Action_Buy_Pass &&
  3142. ts.gameScene.Phase != Phase_ChooseTrump &&
  3143. ts.gameScene.Phase != Phase_CloseOpen {
  3144. continue
  3145. }
  3146. trumpType := CardType_Invalid
  3147. suit := Suit_Hokum
  3148. baseTimes := 1
  3149. isBo1 := false
  3150. isClose := false
  3151. score := -1000
  3152. switch ts.gameScene.Phase {
  3153. case Phase_FirstBuy:
  3154. if optionButtons[i] == Action_Buy_Hokum || optionButtons[i] == Action_Buy_ConfirmHokum {
  3155. trumpType = getCardType(ts.gameScene.PublicCard)
  3156. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious && optionButtons[i] == Action_Buy_Hokum {
  3157. sortedCards := sortCards(ts.gameScene.Players[chairId].HandCards)
  3158. if len(sortedCards[trumpType]) >= 2 {
  3159. r := rand.Intn(100)
  3160. if r < ts.gameScene.Players[chairId].robotType*50 {
  3161. ts.gameScene.Players[chairId].robotActionType = ts.gameScene.Players[chairId].robotType
  3162. return Action_Buy_Hokum
  3163. } else {
  3164. return action
  3165. }
  3166. }
  3167. }
  3168. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious &&
  3169. optionButtons[i] == Action_Buy_ConfirmHokum &&
  3170. ts.gameScene.Players[chairId].robotActionType != RobotType_Cautious {
  3171. return Action_Buy_ConfirmHokum
  3172. }
  3173. } else if optionButtons[i] == Action_Buy_Ashkal {
  3174. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious {
  3175. totalPoint := 0
  3176. for j := 0; j < len(ts.gameScene.Players[chairId].HandCards); j++ {
  3177. totalPoint += getCardPoint(ts.gameScene.Players[chairId].HandCards[j], CardType_Invalid)
  3178. }
  3179. if totalPoint >= 25 {
  3180. r := rand.Intn(100)
  3181. if r < ts.gameScene.Players[chairId].robotType*50 {
  3182. return Action_Buy_Ashkal
  3183. } else {
  3184. return action
  3185. }
  3186. }
  3187. }
  3188. suit = Suit_Ashkal
  3189. } else {
  3190. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious {
  3191. totalPoint := 0
  3192. for j := 0; j < len(ts.gameScene.Players[chairId].HandCards); j++ {
  3193. totalPoint += getCardPoint(ts.gameScene.Players[chairId].HandCards[j], CardType_Invalid)
  3194. }
  3195. if totalPoint >= 22 && getCardPoint(ts.gameScene.PublicCard, CardType_Invalid) >= 3 {
  3196. r := rand.Intn(100)
  3197. if r < ts.gameScene.Players[chairId].robotType*50 {
  3198. return optionButtons[i]
  3199. } else {
  3200. return action
  3201. }
  3202. }
  3203. }
  3204. suit = Suit_Sun
  3205. }
  3206. case Phase_SecondBuy:
  3207. if optionButtons[i] == Action_Buy_Hokum || optionButtons[i] == Action_Buy_ConfirmHokum {
  3208. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious && optionButtons[i] == Action_Buy_Hokum {
  3209. t := getLAGsBestTrumpType(ts.gameScene.Players[chairId].HandCards, ts.gameScene.PublicCard)
  3210. if t != CardType_Invalid {
  3211. r := rand.Intn(100)
  3212. if r < ts.gameScene.Players[chairId].robotType*50 {
  3213. ts.gameScene.Players[chairId].robotActionType = ts.gameScene.Players[chairId].robotType
  3214. return Action_Buy_Hokum
  3215. } else {
  3216. return action
  3217. }
  3218. }
  3219. }
  3220. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious &&
  3221. optionButtons[i] == Action_Buy_ConfirmHokum &&
  3222. ts.gameScene.Players[chairId].robotActionType != RobotType_Cautious {
  3223. return Action_Buy_ConfirmHokum
  3224. }
  3225. for n := CardType_Diamond; n <= CardType_Spade; n++ {
  3226. if n == getCardType(ts.gameScene.PublicCard) {
  3227. continue
  3228. }
  3229. maxScore := ts.getSimulatorSceneBuyWinScore(n, suit, chairId, baseTimes, isBo1, isClose)
  3230. if maxScore > score {
  3231. score = maxScore
  3232. trumpType = n
  3233. }
  3234. }
  3235. } else if optionButtons[i] == Action_Buy_Ashkal {
  3236. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious {
  3237. totalPoint := 0
  3238. for j := 0; j < len(ts.gameScene.Players[chairId].HandCards); j++ {
  3239. totalPoint += getCardPoint(ts.gameScene.Players[chairId].HandCards[j], CardType_Invalid)
  3240. }
  3241. if totalPoint >= 25 {
  3242. r := rand.Intn(100)
  3243. if r < ts.gameScene.Players[chairId].robotType*50 {
  3244. return optionButtons[i]
  3245. } else {
  3246. return action
  3247. }
  3248. }
  3249. }
  3250. suit = Suit_Ashkal
  3251. } else {
  3252. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious {
  3253. totalPoint := 0
  3254. for j := 0; j < len(ts.gameScene.Players[chairId].HandCards); j++ {
  3255. totalPoint += getCardPoint(ts.gameScene.Players[chairId].HandCards[j], CardType_Invalid)
  3256. }
  3257. if totalPoint >= 22 && getCardPoint(ts.gameScene.PublicCard, CardType_Invalid) >= 3 {
  3258. r := rand.Intn(100)
  3259. if r < ts.gameScene.Players[chairId].robotType*50 {
  3260. return optionButtons[i]
  3261. } else {
  3262. return action
  3263. }
  3264. }
  3265. }
  3266. suit = Suit_Sun
  3267. }
  3268. case Phase_Double:
  3269. trumpType = ts.gameScene.TrumpType
  3270. suit = ts.gameScene.FinalClub
  3271. baseTimes = optionButtons[i] - Action_Buy_Double + 2
  3272. if optionButtons[i] == Action_Buy_Coffee {
  3273. baseTimes = 1
  3274. isBo1 = true
  3275. }
  3276. case Phase_CloseOpen:
  3277. isClose = (optionButtons[i] == Action_Buy_Close)
  3278. trumpType = ts.gameScene.TrumpType
  3279. case Phase_ChooseTrump:
  3280. if ts.gameScene.Players[chairId].robotType != RobotType_Cautious &&
  3281. ts.gameScene.Players[chairId].robotActionType != RobotType_Cautious {
  3282. return getLAGsBestTrumpType(ts.gameScene.Players[chairId].HandCards, ts.gameScene.PublicCard)
  3283. }
  3284. trumpType = optionButtons[i]
  3285. }
  3286. score = ts.getSimulatorSceneBuyWinScore(trumpType, suit, chairId, baseTimes, isBo1, isClose)
  3287. if baseTimes >= 2 {
  3288. score = score / baseTimes
  3289. }
  3290. if score >= buyWinScore {
  3291. buyWinScore = score
  3292. if ts.gameScene.Phase == Phase_FirstBuy || ts.gameScene.Phase == Phase_SecondBuy {
  3293. // 这里是避免机器人买牌过于频繁
  3294. if buyWinScore >= 10 {
  3295. action = optionButtons[i]
  3296. }
  3297. } else if ts.gameScene.Phase == Phase_Double {
  3298. if buyWinScore >= 30 {
  3299. action = optionButtons[i]
  3300. }
  3301. } else {
  3302. action = optionButtons[i]
  3303. }
  3304. }
  3305. }
  3306. return action
  3307. }
  3308. func (ts *tablesink) getInitActionAndOperationList(chairId int) (int, []int) {
  3309. optionButtons := []int{}
  3310. action := 0
  3311. switch ts.gameScene.Phase {
  3312. case Phase_FirstBuy:
  3313. fallthrough
  3314. case Phase_SecondBuy:
  3315. action = Action_Buy_Pass
  3316. optionButtons = ts.gameScene.Players[chairId].CanAction
  3317. if !ts.gameScene.Players[chairId].canDoBuyAction(Action_Buy_Pass) {
  3318. action = ts.gameScene.Players[chairId].getAutoBuyAction()
  3319. }
  3320. case Phase_Double:
  3321. optionButtons = ts.gameScene.Players[chairId].CanAction
  3322. action = Action_Buy_Pass
  3323. case Phase_CloseOpen:
  3324. optionButtons = append(optionButtons, Action_Buy_Close, Action_Buy_Open)
  3325. action = Action_Buy_Open
  3326. case Phase_ChooseTrump:
  3327. for i := CardType_Diamond; i <= CardType_Spade; i++ {
  3328. if i == getCardType(ts.gameScene.PublicCard) {
  3329. continue
  3330. }
  3331. optionButtons = append(optionButtons, i)
  3332. }
  3333. cardList := make([]int, len(ts.gameScene.Players[chairId].HandCards))
  3334. copy(cardList, ts.gameScene.Players[chairId].HandCards)
  3335. cardList = append(cardList, ts.gameScene.PublicCard)
  3336. currentCardIndex := ts.logic.currentCardIndex
  3337. for j := 0; j < CHAIR_COUNT; j++ {
  3338. if j == chairId {
  3339. leftCards := ts.logic.getLeftCards(true)
  3340. cardList = append(cardList, leftCards...)
  3341. break
  3342. }
  3343. ts.logic.currentCardIndex += SURPLUS_SEND_CARD
  3344. }
  3345. ts.logic.currentCardIndex = currentCardIndex
  3346. action = getBestTrumpType(cardList, ts.gameScene.PublicCard)
  3347. }
  3348. return action, optionButtons
  3349. }
  3350. func (ts *tablesink) getSimulatorSceneBuyWinScore(trumpType, suit, chairId, baseTimes int, isBo1, isClose bool) int {
  3351. getPublicCardChairId := chairId
  3352. if suit == Suit_Ashkal {
  3353. getPublicCardChairId = getFriendChair(chairId)
  3354. }
  3355. ss := newSimulatorScene()
  3356. ss.initData(trumpType, suit, chairId, baseTimes, isBo1, isClose)
  3357. currentCardIndex := ts.logic.currentCardIndex
  3358. for j := 0; j < CHAIR_COUNT; j++ {
  3359. leftCards := ts.logic.getLeftCards(getPublicCardChairId == j)
  3360. ss.fakers[j].handCards = append(ss.fakers[j].handCards, ts.gameScene.Players[j].HandCards...)
  3361. ss.fakers[j].handCards = append(ss.fakers[j].handCards, leftCards...)
  3362. if getPublicCardChairId == j {
  3363. ss.fakers[j].handCards = append(ss.fakers[j].handCards, ts.gameScene.PublicCard)
  3364. }
  3365. f := &ss.fakers[j]
  3366. sort.Slice(f.handCards, func(m, n int) bool {
  3367. return getCardSortValue(f.handCards[m], trumpType) < getCardSortValue(f.handCards[n], trumpType)
  3368. })
  3369. }
  3370. ts.logic.currentCardIndex = currentCardIndex
  3371. isQuickOrLadder := false
  3372. if ts.roomInfo.RoomType == 3 || config.Server.IsLadderRoom == 1 {
  3373. isQuickOrLadder = true
  3374. }
  3375. ss.startSimulatorScene(getPreviousChair(ts.gameScene.Banker), isQuickOrLadder)
  3376. buyWinScore := ss.buyWinScore
  3377. if ss.suit != Suit_Hokum {
  3378. buyWinScore /= 2
  3379. }
  3380. if !ss.isWinScoreBigThanFriend() {
  3381. buyWinScore = -1
  3382. }
  3383. return buyWinScore
  3384. }
  3385. func (ts *tablesink) updateRobotChatAction(action int) {
  3386. bFound := false
  3387. for i := 0; i < len(ts.gameScene.robotChatList); i++ {
  3388. if ts.gameScene.robotChatList[i].chatType == action {
  3389. bFound = true
  3390. ts.gameScene.robotChatList[i].count++
  3391. }
  3392. }
  3393. if !bFound {
  3394. ts.gameScene.robotChatList = append(ts.gameScene.robotChatList, robotChatAction{count: 1, chatType: action})
  3395. }
  3396. }
  3397. func (ts *tablesink) sendRobotChatAction(action, sender, receiver, delaySec int) {
  3398. if ts.gameScene.robotSendChatCount >= ts.gameScene.robotSendChatMaxCount {
  3399. return
  3400. }
  3401. if ts.roomInfo.ScoreToWin == 1 && ts.gameScene.robotSendChatCount >= (ts.gameScene.RoundIndex/4+1)*2 {
  3402. return
  3403. }
  3404. if ts.roomInfo.ScoreToWin != 1 && ts.gameScene.robotSendChatCount >= ts.gameScene.GameIndex+1 {
  3405. return
  3406. }
  3407. if ts.roomInfo.ScoreToWin != 1 {
  3408. if ts.gameScene.isSendChat {
  3409. return
  3410. }
  3411. ts.gameScene.isSendChat = true
  3412. }
  3413. usr := ts.table.GetUserByUserId(sender)
  3414. if usr == nil {
  3415. return
  3416. }
  3417. if !usr.IsRobot() {
  3418. return
  3419. }
  3420. timer := time.NewTimer(time.Duration(delaySec) * time.Millisecond)
  3421. go func() {
  3422. <-timer.C
  3423. ts.updateRobotChatAction(action)
  3424. userIndex := usr.GetUserIndex()
  3425. var data string
  3426. switch action {
  3427. case RobotChatAction_MuiltyTimeOut:
  3428. data = fmt.Sprintf("1/7/%d", sender)
  3429. case RobotChatAction_LoseOverFive:
  3430. r := rand.Intn(100)
  3431. if r < 50 {
  3432. data = fmt.Sprintf("1/5/%d", sender)
  3433. } else {
  3434. data = fmt.Sprintf("2/7/%d", sender)
  3435. }
  3436. case RobotChatAction_LoseOverEighty:
  3437. r := rand.Intn(100)
  3438. if r < 50 {
  3439. data = fmt.Sprintf("1/5/%d", sender)
  3440. } else {
  3441. data = fmt.Sprintf("2/8/%d", sender)
  3442. }
  3443. case RobotChatAction_FriendGetBigProject:
  3444. r := rand.Intn(90)
  3445. if r < 30 {
  3446. chatIds := []int{1, 6, 7}
  3447. chatId := chatIds[rand.Intn(3)]
  3448. data = fmt.Sprintf("3/%d/%d/%d", chatId, sender, receiver)
  3449. } else if r < 60 && r >= 30 {
  3450. data = fmt.Sprintf("1/9/%d", sender)
  3451. } else {
  3452. chatIds := []int{0, 3, 15}
  3453. chatId := chatIds[rand.Intn(3)]
  3454. data = fmt.Sprintf("2/%d/%d", chatId, usr.GetUserId())
  3455. }
  3456. case RobotChatAction_SelfWinOverFive:
  3457. r := rand.Intn(100)
  3458. if r < 50 {
  3459. data = fmt.Sprintf("1/8/%d", sender)
  3460. } else {
  3461. chatIds := []int{16, 17}
  3462. chatId := chatIds[rand.Intn(2)]
  3463. data = fmt.Sprintf("2/%d/%d", chatId, sender)
  3464. }
  3465. case RobotChatAction_GoodHandCard:
  3466. r := rand.Intn(100)
  3467. if r < 50 {
  3468. data = fmt.Sprintf("1/9/%d", sender)
  3469. } else {
  3470. chatIds := []int{0, 16, 12, 6}
  3471. chatId := chatIds[rand.Intn(4)]
  3472. data = fmt.Sprintf("2/%d/%d", chatId, sender)
  3473. }
  3474. case RobotChatAction_BadHandCard:
  3475. r := rand.Intn(100)
  3476. if r < 50 {
  3477. data = fmt.Sprintf("1/5/%d", sender)
  3478. } else {
  3479. chatIds := []int{1, 5, 8}
  3480. chatId := chatIds[rand.Intn(3)]
  3481. data = fmt.Sprintf("2/%d/%d", chatId, sender)
  3482. }
  3483. case RobotChatAction_EmenyGetBigProject:
  3484. r := rand.Intn(100)
  3485. if r < 50 {
  3486. data = fmt.Sprintf("1/5/%d", sender)
  3487. } else {
  3488. chatIds := []int{8, 14, 2}
  3489. chatId := chatIds[rand.Intn(3)]
  3490. data = fmt.Sprintf("2/%d/%d", chatId, sender)
  3491. }
  3492. case RobotChatAction_EnterRoom:
  3493. data = fmt.Sprintf("1/0/%d", sender)
  3494. case RobotChatAction_Reply:
  3495. data = fmt.Sprintf("1/1/%d", sender)
  3496. case RobotChatAction_FriendWinOverFive:
  3497. data = fmt.Sprintf("1/2/%d", sender)
  3498. }
  3499. ts.recvChatMsg(userIndex, data)
  3500. ts.gameScene.robotSendChatCount++
  3501. timer.Stop()
  3502. }()
  3503. }