You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

run.sh 7.1 kB

2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/usr/local
  2. python_dir=/usr/local/PlayerCode/CAPI/python/PyAPI
  3. python_main_dir=/usr/local/PlayerCode/CAPI/python
  4. playback_dir=/usr/local/output
  5. map_dir=/usr/local/map
  6. mkdir -p $playback_dir
  7. # initialize
  8. if [[ "${MODE}" == "ARENA" ]]; then
  9. MODE_NUM=0
  10. elif [[ "${MODE}" == "COMPETITION" ]]; then
  11. MODE_NUM=1
  12. fi
  13. # set default value
  14. : "${TEAM_SEQ_ID:=0}"
  15. : "${TEAM_LABELS:=Student:Tricker}"
  16. : "${TEAM_LABEL:=Student}"
  17. : "${EXPOSED=1}"
  18. : "${MODE_NUM=0}"
  19. : "${GAME_TIME=10}"
  20. : "${CONNECT_IP=172.17.0.1}"
  21. get_current_team_label() {
  22. if [ $TEAM_SEQ_ID -eq $2 ]; then
  23. echo "find current team label: $1"
  24. current_team_label=$1
  25. fi
  26. }
  27. read_array() {
  28. callback=$1
  29. echo "read array: set callback command: $callback"
  30. IFS=':' read -r -a fields <<< "$2"
  31. count=0 # loop count
  32. for field in "${fields[@]}"
  33. do
  34. echo "parse field: $field"
  35. param0=$field
  36. # call command
  37. run_command="$callback $param0 $count"
  38. echo "Call Command: $run_command"
  39. $run_command
  40. count=$((count+1))
  41. done
  42. }
  43. function retry_command {
  44. local command="$1"
  45. local max_attempts=10
  46. local attempt_num=1
  47. local sleep_seconds=5
  48. while [ $attempt_num -le $max_attempts ]; do
  49. echo "Attempt $attempt_num / $max_attempts to run command: $command"
  50. eval $command &
  51. local PID=$!
  52. sleep $sleep_seconds
  53. if kill -0 $PID 2>/dev/null; then
  54. echo "Failed to connect to server. Retrying..."
  55. ((attempt_num++))
  56. else
  57. echo "Connected to server successfully."
  58. return 0
  59. fi
  60. done
  61. echo "Failed to connect to server after $max_attempts attempts."
  62. return 1
  63. }
  64. if [ "$TERMINAL" = "SERVER" ]; then
  65. map_path=$map_dir/$MAP_ID.txt
  66. # allow spectator always.
  67. nice -10 ./Server --port 8888 --studentCount 4 --trickerCount 1 --resultFileName $playback_dir/result --gameTimeInSecond $GAME_TIME --mode $MODE_NUM --mapResource $map_path --url $SCORE_URL --token $TOKEN --fileName $playback_dir/playback --startLockFile $playback_dir/start.lock > $playback_dir/server.log 2>&1 &
  68. server_pid=$!
  69. echo "server pid: $server_pid"
  70. ls $playback_dir
  71. echo "SCORE URL: $SCORE_URL"
  72. echo "FINISH URL: $FINISH_URL"
  73. echo "waiting..."
  74. sleep 60 # wait connection time
  75. echo "watching..."
  76. if [ -f $playback_dir/start.lock ]; then
  77. ps -p $server_pid
  78. while [ $? -eq 0 ]
  79. do
  80. sleep 1
  81. ps -p $server_pid > /dev/null 2>&1
  82. done
  83. # update score by finish url
  84. echo "Getting result score..."
  85. result=$(cat $playback_dir/result.json)
  86. score0=$(echo "$result" | grep -oP '(?<="Student":)\d+') # Student score
  87. score1=$(echo "$result" | grep -oP '(?<="Tricker":)\d+') # Tricker score
  88. echo "Result score: Student: $score0, Tricker: $score1"
  89. # detect two team seqs:
  90. echo "Parsing TEAM_LABELS: $TEAM_LABELS"
  91. TEAM_SEQ_ID=0
  92. read_array get_current_team_label $TEAM_LABELS
  93. if [[ "${current_team_label}" == "Student" ]]; then
  94. echo "Parse Success: 1st team is Student"
  95. finish_payload='{"status": "Finished", "scores": ['${score0}', '${score1}']}'
  96. elif [[ "${current_team_label}" == "Tricker" ]]; then
  97. echo "Parse Success: 1st team is Tricker"
  98. finish_payload='{"status": "Finished", "scores": ['${score1}', '${score0}']}'
  99. else
  100. echo "Parse Failure: 1st team is Unknown"
  101. finish_payload='{"status": "Crashed", "scores": [0, 0]}'
  102. fi
  103. if [[ -n $finish_payload ]]; then
  104. echo "FINISH_URL: $FINISH_URL, payload: $finish_payload. Start update score..."
  105. curl $FINISH_URL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d "${finish_payload}" > $playback_dir/send.log 2>&1
  106. else
  107. echo "Payload not set."
  108. fi
  109. # Congratulations! You have finished the competition!!!!!
  110. # touch $playback_dir/finish.lock
  111. echo "Finish!"
  112. else
  113. echo "Failed to start game."
  114. # touch $playback_dir/finish.lock
  115. touch temp.lock
  116. mv -f temp.lock $playback_dir/playback.thuaipb
  117. kill -9 $server_pid
  118. finish_payload='{"status": "Crashed", "scores": [0, 0]}'
  119. curl $FINISH_URL -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $TOKEN" -d "${finish_payload}" > $playback_dir/send.log 2>&1
  120. fi
  121. elif [ "$TERMINAL" = "CLIENT" ]; then
  122. echo "Client Mode! Team Label data - $TEAM_LABEL"
  123. # parse team label name
  124. current_team_label=$TEAM_LABEL
  125. # k is an enum (1,2), 1 = Student, 2 = Tricker
  126. if [ "$current_team_label" = "Student" ]; then
  127. k=1
  128. elif [ "$current_team_label" = "Tricker" ]; then
  129. k=2
  130. else
  131. echo "Error: Invalid Team Label"
  132. exit
  133. fi
  134. pushd /usr/local/code
  135. if [ $k -eq 1 ]; then
  136. for i in {1..4}
  137. do
  138. j=$((i - 1)) # student player id from 0 to 3
  139. code_name=Student$i
  140. if [ -f "./$code_name.py" ]; then
  141. echo "find ./$code_name.py"
  142. cp -r $python_main_dir $python_main_dir$i
  143. cp -f ./$code_name.py $python_main_dir$i/PyAPI/AI.py
  144. command="nice -0 python3 $python_main_dir$i/PyAPI/main.py -I $CONNECT_IP -P $PORT -p $j > $playback_dir/team$k-player$j.log 2>&1"
  145. retry_command "$command" &
  146. ps -aux |grep main.py
  147. elif [ -f "./$code_name" ]; then
  148. echo "find ./$code_name"
  149. command="nice -0 ./$code_name -I $CONNECT_IP -P $PORT -p $j > $playback_dir/team$k-player$j.log 2>&1"
  150. retry_command "$command" &
  151. ps -aux |grep $code_name
  152. else
  153. echo "ERROR. $code_name is not found."
  154. fi
  155. done
  156. else
  157. i=5
  158. j=4 # tricker id is 4
  159. code_name=Tricker
  160. if [ -f "./$code_name.py" ]; then
  161. echo "find ./$code_name.py"
  162. cp -r $python_main_dir $python_main_dir$i
  163. cp -f ./$code_name.py $python_main_dir$i/PyAPI/AI.py
  164. command="nice -0 python3 $python_main_dir$i/PyAPI/main.py -I $CONNECT_IP -P $PORT -p $j > $playback_dir/team$k-player$j.log 2>&1"
  165. retry_command "$command" &
  166. ps -aux |grep main.py
  167. elif [ -f "./$code_name" ]; then
  168. echo "find ./$code_name"
  169. command="nice -0 ./$code_name -I $CONNECT_IP -P $PORT -p $j > $playback_dir/team$k-player$j.log 2>&1"
  170. retry_command "$command" &
  171. ps -aux |grep $code_name
  172. else
  173. echo "ERROR. $code_name is not found."
  174. fi
  175. fi
  176. # curl $CONNECT_IP:$PORT
  177. sleep $((GAME_TIME + 90))
  178. popd
  179. else
  180. echo "VALUE ERROR: TERMINAL is neither SERVER nor CLIENT."
  181. fi