player.rb 12 KB
Newer Older
Joe Libipl's avatar
Joe Libipl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
# Plan to build this out over time as necessary
DEFAULT_PLAYER = {
    accountID: 0,
    account: 'no_name',
    name: 'no_name',
    impLevel: 0
}

def db_insert_player(db_hash)
  # anything in the hash overrides defaults
  player = DEFAULT_PLAYER.merge(db_hash)
  client = connect_to_db(@server_database)
  query = "INSERT INTO [#{@server_database}].[dbo].[Player] ([notes], [accountID], [account], \
    [name], [gender], [race], [classFullName], [classType], [visualKey], [alignment], [confRoom], \
    [impLevel], \
    [ancestor], [ancestorID], [facet], [land], [map], [xCord], [yCord], [zCord], [dirPointer], \
    [stunned], [floating], [dead], [fighterSpecialization], [level], [exp], [hits], [hitsMax], \
    [hitsAdjustment], [hitsDoctored], [stamina], [stamLeft], [staminaAdjustment], [mana], \
    [manaMax], [manaAdjustment], [age], [roundsPlayed], [numKills], [numDeaths], [bankGold], \
    [strength], [dexterity], [intelligence], [wisdom], [constitution], [charisma], [strengthAdd], \
    [dexterityAdd], [birthday], [lastOnline], [deleteDate], [currentKarma], [lifetimeKarma], \
    [lifetimeMarks], [pvpKills], [pvpDeaths], [playersKilled], [playersFlagged], [UW_hitsMax], \
    [UW_hitsAdjustment], [UW_staminaMax], [UW_staminaAdjustment], [UW_manaMax], \
    [UW_manaAdjustment], [UW_intestines], [UW_liver], [UW_lungs], [UW_stomach]) \
    VALUES (N'', #{player[:accountID]}, \
    N'#{player[:account]}', \
    N'#{player[:name]}', \
    1, N'Illyria', N'Fighter', \
    N'Fighter', N'male_fighter_pc_brown', 1, 0, #{player[:impLevel]}, 0, 0, 0, 0, 0, 42, 27, 0, \
    N'v', 0, 3, 0, N'None', 4, 4996, 36, 36, 0, 0, 10, 10, 0, 0, 0, 0, 806, 806, 8, \
    0, 0, 17, 18, 10, 17, 8, 18, 1, 1, CAST(0x0000A4FC0001D163 AS DateTime), \
    CAST(0x0000A4FC001A5F6A AS DateTime), CAST(0x0000979200000000 AS DateTime), \
    0, 0, 0, 0, 0, N'', N'', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)"
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to insert player!" if 1 != affected_rows
37
  add_spell_slots_for_player(get_player_id(player[:name]))
Joe Libipl's avatar
Joe Libipl committed
38
  add_hand_slots_for_player(get_player_id(player[:name]))
Aqualung's avatar
Aqualung committed
39
  add_effect_slots_for_player(get_player_id(player[:name]))
Joe Libipl's avatar
Joe Libipl committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
end

def player_exists(player_hash)
  client = connect_to_db(@server_database)
  query = "SELECT [name], [playerID] FROM [#{@server_database}].[dbo].[Player] WHERE "
  conditions = []
  player_hash.keys.each do |key|
    if player_hash[key].is_a?(String)
      conditions << "#{key.to_s} = '#{player_hash[key]}'"
    else
      conditions << "#{key.to_s} = #{player_hash[key]}"
    end
  end
  query += conditions.join(" AND ")
54
  debug_msg "Query is #{query}"
Joe Libipl's avatar
Joe Libipl committed
55 56
  result = client.execute(query)
  row = result.each(:first => true)
57
  debug_msg row.inspect
Joe Libipl's avatar
Joe Libipl committed
58
  result.affected_rows > 0
59 60
end

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
def delete_all_test_players()
  client = connect_to_db(@server_database)
  player_tables = ["PlayerBelt","PlayerEffects","PlayerFlags","PlayerHeld","PlayerLocker",
    "PlayerQuests","PlayerRings","PlayerSack","PlayerSettings","PlayerSkills","PlayerSpells",
    "PlayerWearing","Player"]
  get_all_test_player_ids().each do |player_to_delete|
    debug_msg "Deleting player #{player_to_delete}"
    player_tables.each do |table|
      result = client.execute("DELETE
      FROM [#{@server_database}].[dbo].[#{table}]
      WHERE playerID = '#{player_to_delete}'")
      debug_msg "Rows deleted from [#{table}]: #{result.do}"
    end
  end
end

77 78 79 80 81 82 83 84 85 86 87 88 89
def get_all_test_player_ids()
  id_list = []
  client = connect_to_db(@server_database)
  query = "SELECT [playerID] FROM [#{@server_database}].[dbo].[Player] \
           WHERE account LIKE 'test%' \
           AND name LIKE 'Dude.%'"
  result = client.execute(query)
  result.each(:symbolize_keys => true) do |rowset|
    id_list << rowset[:playerID]
  end
  id_list
end

90
def get_player_id(name)
91
  puts "Getting ID from player #{name}"
92 93 94 95 96
  client = connect_to_db(@server_database)
  query = "SELECT [name], [playerID] FROM [#{@server_database}].[dbo].[Player] \
             WHERE name = N'#{name}'"
  result = client.execute(query)
  row = result.each(:first => true)
97
  debug_msg row.inspect
98
  fail "Unable to get playerID!" if 1 != result.affected_rows
Aqualung's avatar
WIP  
Aqualung committed
99
  puts "Player ID for #{name} is #{row[0]['playerID']}"
100
  row[0]['playerID']
Joe Libipl's avatar
Joe Libipl committed
101 102 103 104 105
end

def db_make_player_thief(name)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET classFullName = N'Thief', \
106
    classType = N'Thief', visualKey = N'male_thief_pc_brown', alignment = 2, mana = 10, manaMax = 10 \
Joe Libipl's avatar
Joe Libipl committed
107
    WHERE name = '#{name}'"
108
  debug_msg query
Joe Libipl's avatar
Joe Libipl committed
109 110 111 112
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end
Joe Libipl's avatar
Joe Libipl committed
113

114 115 116 117 118 119
def move_player_to(player_id, x, y, z)
  client = connect_to_db(@server_database)
  # [xCord], [yCord], [zCord]
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \ 
    xCord = #{x}, yCord = #{y}, zCord = #{z} \
    WHERE playerID = #{player_id}"
120
  debug_msg query
121 122 123 124 125
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

Joe Libipl's avatar
Joe Libipl committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
## PlayerHeld

# Assumes no slots yet exist
def add_hand_slots_for_player(player_id)
  client = connect_to_db(@server_database)
  (0..1).each do |hand|
    query = "INSERT [dbo].[PlayerHeld] ([playerID], [rightHand], [itemID], [attunedID], \
      [special], [charges], [coinValue], [figExp], [venom], [attuneType], [nocked], \
      [timeCreated], [whoCreated]) \
      VALUES (#{player_id}, #{hand}, 0, 0, N'', 0, 0, 0, 0, 0, 0, (CURRENT_TIMESTAMP), \
      N'SYSTEM')"
    result = client.execute(query)
    affected_rows = result.do
    fail "Unable to insert hand slot!" if 1 != affected_rows
  end
end

143
def put_in_player_hand(player_id, item_id, which_hand = :right)
Joe Libipl's avatar
Joe Libipl committed
144
  client = connect_to_db(@server_database)
145
  special = lookup_item_special( { itemID: item_id } )
Joe Libipl's avatar
Joe Libipl committed
146
  query = "UPDATE [#{@server_database}].[dbo].[PlayerHeld] SET \
Aqualung's avatar
Aqualung committed
147
    itemID = #{item_id}, special = \'#{special}\' \
Joe Libipl's avatar
Joe Libipl committed
148
    WHERE playerID = #{player_id} AND rightHand = #{which_hand == :right ? 1 : 0}"
149
  debug_msg query
Joe Libipl's avatar
Joe Libipl committed
150 151 152 153 154 155 156 157 158 159
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

def put_attuned_in_player_hand(player_id, item_id, which_hand = :right)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[PlayerHeld] SET \
    itemID = #{item_id}, attunedID = #{player_id} \
    WHERE playerID = #{player_id} AND rightHand = #{which_hand == :right ? 1 : 0}"
160
  debug_msg query
Joe Libipl's avatar
Joe Libipl committed
161 162 163 164
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end
Aqualung's avatar
WIP  
Aqualung committed
165

Joe Libipl's avatar
WIP  
Joe Libipl committed
166 167
## Attributes

168
def set_character_alignment(player_id, desired_alignment)
169 170 171 172
  alignment_list = [ :none, :lawful, :neutral, :chaotic, :evil, :amoral, :all ]
  alignment_num = alignment_list.index(desired_alignment)
  debug_msg "Alignment #{desired_alignment} is number #{alignment_num}"
  fail "Unknown alignment" if alignment_num.nil?
173 174
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
175
    alignment = #{alignment_num} \
176 177 178 179 180 181 182
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

Aqualung's avatar
WIP  
Aqualung committed
183 184 185 186 187 188 189 190 191 192 193
def set_character_karma(player_id, desired_karma)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    currentKarma = #{desired_karma} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

194 195 196 197 198 199 200 201 202 203 204
def set_character_stat(player_id, stat, desired_value)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    #{stat} = #{desired_value} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

Joe Libipl's avatar
WIP  
Joe Libipl committed
205 206 207 208 209 210 211 212 213 214 215
def set_character_max_hp(player_id, desired_hp)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    hitsMax = #{desired_hp} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

Aqualung's avatar
WIP  
Aqualung committed
216 217 218 219 220 221 222 223 224 225
def set_character_hp(player_id, desired_hp)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    hits = #{desired_hp} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
def set_character_max_stamina(player_id, desired_value)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    stamina = #{desired_value} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

def set_character_stamina(player_id, desired_value)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    stamLeft = #{desired_value} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

Joe Libipl's avatar
Joe Libipl committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
def set_character_max_mana(player_id, desired_value)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    manaMax = #{desired_value} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

def set_character_mana(player_id, desired_value)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    mana = #{desired_value} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end

271 272 273 274 275 276 277 278 279 280
def set_character_constitution(player_id, desired_constitution)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    constitution = #{desired_constitution} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end
Aqualung's avatar
Aqualung committed
281 282 283 284 285 286 287 288 289 290 291

def set_character_age(player_id, desired)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    age = #{desired} \
    WHERE playerID = '#{player_id}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to update player!" if 1 != affected_rows
end
Joe Libipl's avatar
Joe Libipl committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

# CREATE TABLE [dbo].[PlayerHeld](
#   [playerID] [int] NULL,
#   [rightHand] [bit] NULL,
#   [itemID] [int] NULL,
#   [attunedID] [int] NULL,
#   [special] [nvarchar](1000) NULL,
#   [charges] [int] NULL,
#   [coinValue] [bigint] NULL,
#   [figExp] [bigint] NULL,
#   [venom] [int] NULL,
#   [attuneType] [int] NOT NULL,
#   [nocked] [bit] NOT NULL,
#   [timeCreated] [datetime] NULL,
#   [whoCreated] [nvarchar](100) NULL
# ) ON [PRIMARY]
# GO

# INSERT [dbo].[PlayerHeld] ([playerID], [rightHand], [itemID], [attunedID], [special], [charges], [coinValue], [figExp], [venom], [attuneType], [nocked], [timeCreated], [whoCreated]) VALUES (1, 0, 0, 0, N'', 0, 0, 0, 0, 0, 0, CAST(0x0000980C009CCB8C AS DateTime), N'SYSTEM')
# INSERT [dbo].[PlayerHeld] ([playerID], [rightHand], [itemID], [attunedID], [special], [charges], [coinValue], [figExp], [venom], [attuneType], [nocked], [timeCreated], [whoCreated]) VALUES (1, 1, 0, 0, N'', 0, 0, 0, 0, 0, 0, CAST(0x0000981800906492 AS DateTime), N'SYSTEM')