Skip to content

Commit

Permalink
Add several values and fix corrupted switch condition on game mode check
Browse files Browse the repository at this point in the history
  • Loading branch information
expiteRz committed Aug 13, 2023
1 parent 60e4c1a commit 20a9dba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
19 changes: 14 additions & 5 deletions utils/structs.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package utils

type RoomData struct {
Status string `json:"status"`
Id string `json:"id"`
Setting RoomSetting `json:"setting"`
Members []RoomMember `json:"members"`
Status string `json:"status"`
Id string `json:"id"`
Setting RoomSetting `json:"setting"`
MemberLen int `json:"player_amount"`
Members []RoomMember `json:"members"`
}

type RoomSetting struct {
GameMode int `json:"game_mode"`
Engine int `json:"engine"`
Engine int `json:"engine,omitempty"`
Course string `json:"course"`
CourseId int `json:"course_id"`
}
Expand All @@ -21,4 +22,12 @@ type RoomMember struct {
GuestName string `json:"guest_name,omitempty"`
RaceRating int `json:"vr"`
BattleRating int `json:"br"`
Status string `json:"role,omitempty"`
FinishTimes []int `json:"finish_times"`
}

//type MemberCourse struct {
// Name string `json:"name,omitempty"`
// Id int `json:"id,omitempty"`
// Allowed string `json:"allowed,omitempty"`
//}
41 changes: 22 additions & 19 deletions web/wiimmfi.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func StartParseRoom() {
//fmt.Printf("%v\n\n", string(body))
res.Body.Close()

var test interface{}
if err := json.Unmarshal(body, &test); err != nil {
var roomData interface{}
if err := json.Unmarshal(body, &roomData); err != nil {
log.Printf("Error occurred: %v", err)
time.Sleep(time.Duration(utils.LoadedConfig.Interval) * time.Second)
continue
}

if len(test.([]interface{})) < 4 {
if len(roomData.([]interface{})) < 4 {
if !loggingAvoider {
fmt.Println("Room not found. Seems the player is offline?")
}
Expand All @@ -65,31 +65,28 @@ func StartParseRoom() {
/// === Start to output room details === ///
loggingAvoider = false
/// Room name
fmt.Printf("=== Room: %s ===\n", test.([]interface{})[2].(map[string]interface{})["room_name"])
data.Id = test.([]interface{})[2].(map[string]interface{})["room_name"].(string)
fmt.Printf("=== Room: %s ===\n", roomData.([]interface{})[2].(map[string]interface{})["room_name"])
data.Id = roomData.([]interface{})[2].(map[string]interface{})["room_name"].(string)

/// Mode-related
gameMode := utils.CheckGameMode(int(test.([]interface{})[2].(map[string]interface{})["ol_status"].([]interface{})[0].(float64)))
gameMode := utils.CheckGameMode(cast.ToInt(roomData.([]interface{})[2].(map[string]interface{})["ol_status"].([]interface{})[0]))
data.Setting.GameMode = gameMode
switch gameMode {
case utils.ModePrivateVS:
case utils.ModeVS:
fmt.Printf("Engine: %s\n", utils.ENGINE[int(test.([]interface{})[2].(map[string]interface{})["engine"].(float64))])
data.Setting.Engine = int(test.([]interface{})[2].(map[string]interface{})["engine"].(float64))
case utils.ModePrivateBalloonBattle:
case utils.ModeBalloonBattle:
case utils.ModePrivateVS, utils.ModeVS:
fmt.Printf("Engine: %s\n", utils.ENGINE[int(roomData.([]interface{})[2].(map[string]interface{})["engine"].(float64))])
data.Setting.Engine = int(roomData.([]interface{})[2].(map[string]interface{})["engine"].(float64))
case utils.ModePrivateBalloonBattle, utils.ModeBalloonBattle:
fmt.Println("Balloon Battle")
case utils.ModePrivateCoinBattle:
case utils.ModeCoinBattle:
case utils.ModePrivateCoinBattle, utils.ModeCoinBattle:
fmt.Println("Coin Battle")
}

/// Current track/arena
fmt.Printf("Track: %s\n", test.([]interface{})[2].(map[string]interface{})["track"].([]interface{})[1])
data.Setting.Course = test.([]interface{})[2].(map[string]interface{})["track"].([]interface{})[1].(string)
data.Setting.CourseId = cast.ToInt(test.([]interface{})[2].(map[string]interface{})["track"].([]interface{})[0])
fmt.Printf("Track: %s\n", roomData.([]interface{})[2].(map[string]interface{})["track"].([]interface{})[1])
data.Setting.Course = roomData.([]interface{})[2].(map[string]interface{})["track"].([]interface{})[1].(string)
data.Setting.CourseId = cast.ToInt(roomData.([]interface{})[2].(map[string]interface{})["track"].([]interface{})[0])
/// Players
players := test.([]interface{})[2].(map[string]interface{})["members"].([]interface{})
players := roomData.([]interface{})[2].(map[string]interface{})["members"].([]interface{})
for _, player := range players {
fmt.Printf(
"%s %-15s %-20s %4sVR\n",
Expand All @@ -104,14 +101,20 @@ func StartParseRoom() {
Name: player.(map[string]interface{})["name"].([]interface{})[0].([]interface{})[0].(string),
RaceRating: cast.ToInt(player.(map[string]interface{})["ev"]),
BattleRating: cast.ToInt(player.(map[string]interface{})["eb"]),
Status: player.(map[string]interface{})["ol_role"].(string),
FinishTimes: []int{cast.ToInt(player.(map[string]interface{})["time"].([]interface{})[0])},
}
// If guest exists then print guest name
if player.(map[string]interface{})["name"].([]interface{})[1].([]interface{})[0] != nil {
fmt.Printf("%s %-15s %-20s\n", checkSelf(utils.LoadedConfig.Pid, cast.ToInt(player.(map[string]interface{})["pid"])), "", player.(map[string]interface{})["name"].([]interface{})[1].([]interface{})[0])
fmt.Printf(
"%s %-15s %-20s\n", checkSelf(utils.LoadedConfig.Pid, cast.ToInt(player.(map[string]interface{})["pid"])), "", player.(map[string]interface{})["name"].([]interface{})[1].([]interface{})[0],
)
member.GuestName = player.(map[string]interface{})["name"].([]interface{})[1].([]interface{})[0].(string)
member.FinishTimes = append(member.FinishTimes, cast.ToInt(player.(map[string]interface{})["time"].([]interface{})[1]))
}
data.Members = append(data.Members, member)
data.Status = "success"
data.MemberLen = cast.ToInt(roomData.([]interface{})[2].(map[string]interface{})["n_players"])

JSONByte, err = json.Marshal(data)
if err != nil {
Expand Down

0 comments on commit 20a9dba

Please sign in to comment.