runmyrobot / letsrobot_ios

iOS client
BSD 3-Clause Clear License
1 stars 1 forks source link

Chat colours are desynced from website #28

Closed Sherlouk closed 7 years ago

Sherlouk commented 7 years ago

There seems to be a bug in Swift 3 where the bitwise operator acts differently than JavaScript which means the "hash" function returns a different value ultimately throwing off the name colour logic.

Ideally the colour will be sent from the server to the client, allowing it to be synced Temporary fix may be to just not have coloured usernames and static colour for logged in users

The JavaScript code is as follows:

 hashCode(str) { // java String#hashCode
    var hash = 0;
    for (var i = 0; i < str.length; i++) {
      hash = str.charCodeAt(i) + ((hash << 5) - hash);
    }
    return hash;
  }

  letsRobotColors(i) {
    let absoluteValue = Math.abs(i)
    let colorToChoose = absoluteValue % 7
    let color = "#21BCE5"
    switch (colorToChoose) {
      case 0:
        color = "#21BCE5"
        break
      case 1:
        color = "#97E062"
        break
      case 2:
        color = "#F3Eb48"
        break
      case 3:
        color = "#5F79FF"
        break
      case 4:
        color = "#F9AA67"
        break
      case 5:
        color = "#F16B74"
        break
      case 6:
        color = "#a652af"
        break

    }
    return color
  }
runmyrobot commented 7 years ago

Okay, moved username colors over to server-side. On next deploy, you'll see a node called user.username_color at GET /internal.

Also, for every chat_message in chat_messages at GET /internal you'll see each message having a username_color.

Also, for socket.on('chat_message_with_name'), the payload will have username_color as well.