RoomList.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { useContext } from 'react'
  2. import './RoomList.css'
  3. import { ROOM } from '../Constant';
  4. import RevMsgContext from '../provider/RevMsgContext';
  5. import GlobalSocketContext from '../provider/GlobalSocketContext';
  6. const enuString = {
  7. 0 :'空位',
  8. 1 :'落座',
  9. 2 :'下棋中',
  10. }
  11. const RoomList = function () {
  12. const roomList = useContext(RevMsgContext)
  13. const socket = useContext(GlobalSocketContext)
  14. const comeIn = function(item){
  15. socket.send(JSON.stringify({
  16. instruct: ROOM,
  17. roomId: item.roomId
  18. }))
  19. }
  20. return (
  21. <>
  22. <div className='list'>
  23. <h3 className='list-title'>在线博弈五子棋</h3>
  24. <div className='grid-list'>
  25. {(() => {
  26. if ( roomList ) {
  27. return roomList.data.map((item) => {
  28. return (<div onClick={()=>{
  29. comeIn(item)
  30. }} key={item.roomId} className='room-list'>
  31. <div className='room-list-item' style={{
  32. border: (item.backState === 2 && item.whiteState === 2) ?
  33. "3px solid green" : "1px solid black"}}>
  34. <span className='room-list-title'>黑方: {(()=>{
  35. if ( item.backState !== 0 ) {
  36. return item.backUsername
  37. } else {
  38. return enuString[item.backState]
  39. }
  40. })()}</span>
  41. <span className='room-list-title'>白方: {(()=>{
  42. if ( item.whiteState !== 0 ) {
  43. return item.whiteUsername
  44. } else {
  45. return enuString[item.whiteState]
  46. }
  47. })()}</span>
  48. <span className='room-list-title' >状态: {(()=>{
  49. if ( item.backState === 2 && item.whiteState === 2 ) {
  50. return "博弈中"
  51. } else if (item.backState === 0 && item.whiteState === 0 ) {
  52. return "未开始"
  53. } else if (item.backState === 1 && item.whiteState === 1 ) {
  54. return "已就位"
  55. } else if (item.backState === 0 && item.whiteState === 1 ) {
  56. return "二缺一"
  57. } else if (item.backState === 2 && item.whiteState !== 2 ) {
  58. return "待开始"
  59. } else if (item.backState !== 2 && item.whiteState === 2 ) {
  60. return "待开始"
  61. } else if (item.backState === 0 && item.whiteState === 1 ) {
  62. return "二缺一"
  63. } else {
  64. return "未知"
  65. }
  66. })()}</span>
  67. </div>
  68. </div> )
  69. })
  70. }
  71. })()}
  72. </div>
  73. </div>
  74. </>
  75. )
  76. }
  77. export default RoomList