Class: RuneRb::Game::Entity::ChatMessage

Inherits:
Object
  • Object
show all
Defined in:
app/game/entity/models/chat_message.rb

Overview

A entity Message is the object created when an Entity chats via the chatbox.

Since:

  • 0.9.3

Defined Under Namespace

Classes: MessageData

Constant Summary collapse

TRANSLATION_MAP =

Since:

  • 0.9.3

[' ', 'e', 't', 'a', 'o', 'i', 'h', 'n', 's', 'r', 'd', 'l',
'u', 'm', 'w', 'c', 'y', 'f', 'g', 'p', 'b', 'v', 'k', 'x', 'j', 'q', 'z', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', ' ', '!', '?', '.', ',', ':', ';', '(', ')', '-', '&', '*', '\\', '\'', '@', '#', '+',
'=', '\243', '$', '%', '"', '[', ']'].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(effects, colors, message) ⇒ ChatMessage

Called when a new entity Message is created.

Parameters:

  • effects (Integer)

    the effects the Message will have.

  • colors (Integer)

    the colors the Message will have.

Since:

  • 0.9.3


24
25
26
27
28
# File 'app/game/entity/models/chat_message.rb', line 24

def initialize(effects, colors, message)
  @effects = effects
  @colors = colors
  parse(message)
end

Instance Attribute Details

#colorsInteger (readonly)

Returns the colors of the Message.

Returns:

  • (Integer)

    the colors of the Message

Since:

  • 0.9.3


15
16
17
# File 'app/game/entity/models/chat_message.rb', line 15

def colors
  @colors
end

#effectsInteger (readonly)

Returns the effects of the Message.

Returns:

  • (Integer)

    the effects of the Message

Since:

  • 0.9.3


12
13
14
# File 'app/game/entity/models/chat_message.rb', line 12

def effects
  @effects
end

#textString (readonly)

Returns the text contained within the Message.

Returns:

  • (String)

    the text contained within the Message

Since:

  • 0.9.3


9
10
11
# File 'app/game/entity/models/chat_message.rb', line 9

def text
  @text
end

Class Method Details

.compress(data) ⇒ Object

Compresses the passed data into the resulting array of bytes.

Parameters:

  • data (String)

    the string data to compress.

Since:

  • 0.9.3


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/game/entity/models/chat_message.rb', line 43

def compress(data)
  result = []
  data = data.slice!(0..80).downcase!
  carry = -1
  result_idx = 0

  data.length.times do |itr|
    char = data[itr]
    index = TRANSLATION_MAP.index(char) || 0
    index += 195 if index > 12

    if carry == -1
      if index < 13
        carry = index
      else
        result[result_idx += 1] = index
      end
    elsif index < 13
      result[result_idx += 1] = ((carry << 4) + index)
      carry = -1
    else
      result[result_idx += 1] = ((carry << 4) + (index >> 4))
      carry = index & 0xF
    end
  end

  result[result_idx += 1] = (carry << 4) if carry != -1
  result
end

.decompress(data, length) ⇒ Object

Since:

  • 0.9.3


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/game/entity/models/chat_message.rb', line 73

def decompress(data, length)
  result = Array.new(4096, 0)
  index = 0
  carry = -1

  length.times do |itr|
    value = data[itr / 2] >> 4 - 4 * (itr % 2 ) & 0xF
    if carry == -1
      if value < 13
        result[index += 1] = TRANSLATION_MAP[value].bytes.first & 0xFF
      else
        carry = value
      end
    else
      result[index += 1] = TRANSLATION_MAP[(carry << 4) + value - 195].bytes.first & 0xFF
      carry = -1
    end
  end

  result[0...index].pack('C*')
end

.slim(text) ⇒ Object

Slims a substring.

Since:

  • 0.9.3


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/game/entity/models/chat_message.rb', line 96

def slim(text)
  term = true
  text.size.times do |itr|
    if term &&
      text[itr].chr >= 'a' &&
      text[itr].chr <= 'z'
      text[itr] = (text[itr].bytes.first - 0x20).chr
      term = false
    end

    term = true if text[itr].chr == '.'|| text[itr].chr == '!' || text[itr].chr == '?'
  end
  text
end

Instance Method Details

#parse(message) ⇒ Object

Since:

  • 0.9.3


30
31
32
# File 'app/game/entity/models/chat_message.rb', line 30

def parse(message)

end