player.rb 13.5 KB
Newer Older
Aqualung's avatar
WIP    
Aqualung 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
37
38
39
40
41
42
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Plan to build this out over time as necessary
DEFAULT_PLAYER = {
  accountID: 0,
  account: 'no_name',
  name: 'no_name',
  impLevel: 0
}.freeze

DEFAULT_PLAYER_SKILLS = {
  mace: 0, bow: 0, flail: 0, dagger: 0, rapier: 0, twoHanded: 0, staff: 0, shuriken: 0, sword: 0,
  threestaff: 0, halberd: 0, unarmed: 0, thievery: 0, magic: 0, bash: 0, highMace: 0, highBow: 0,
  highFlail: 0, highDagger: 0, highRapier: 0, highTwoHanded: 0, highStaff: 0, highShuriken: 0,
  highSword: 0, highThreestaff: 0, highHalberd: 0, highUnarmed: 0, highThievery: 0, highMagic: 0,
  highBash: 0, trainedMace: 0, trainedBow: 0, trainedFlail: 0, trainedDagger: 0, trainedRapier: 0,
  trainedTwoHanded: 0, trainedStaff: 0, trainedShuriken: 0, trainedSword: 0, trainedThreestaff: 0,
  trainedHalberd: 0, trainedUnarmed: 0, trainedThievery: 0, trainedMagic: 0, trainedBash: 0
}.freeze

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
  add_spell_slots_for_player(get_player_id(player[:name]))
  add_hand_slots_for_player(get_player_id(player[:name]))
  add_effect_slots_for_player(get_player_id(player[:name]))
  add_skills_for_player(get_player_id(player[:name]))
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 ")
  debug_msg "Query is #{query}"
  result = client.execute(query)
  row = result.each(:first => true)
  debug_msg row.inspect
  result.affected_rows > 0
end

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

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

def get_player_id(name)
  puts "Getting ID from player #{name}"
  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)
  debug_msg row.inspect
  fail "Unable to get playerID!" if 1 != result.affected_rows
  puts "Player ID for #{name} is #{row[0]['playerID']}"
  row[0]['playerID']
end

def db_make_player_thief(name)
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET classFullName = N'Thief', \
    classType = N'Thief', visualKey = N'male_thief_pc_brown', alignment = 2, mana = 10, manaMax = 10 \
    WHERE name = '#{name}'"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail 'Unable to update player!' if 1 != affected_rows
end

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}"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail 'Unable to update player!' if 1 != affected_rows
end

## PlayerSkills
def add_skills_for_player(player_id)
  client = connect_to_db(@server_database)
  player_skills = DEFAULT_PLAYER_SKILLS.merge({ playerID: player_id })
  debug_msg player_skills
  query = db_insert_from_hash("PlayerSkills", player_skills)
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail "Unable to insert player skills!" if 1 != affected_rows
end

## 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

166
167
168
169
170
171
172
173
174
175
def clear_player_hands
  client = connect_to_db(@server_database)
  query = "UPDATE [dbo].[PlayerHeld] SET \
    itemID = 0, attunedID = 0, special = ''"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  debug_msg "Cleared #{affected_rows} player hands"
end

Aqualung's avatar
WIP    
Aqualung committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
def put_in_player_hand(player_id, item_id, which_hand = :right)
  client = connect_to_db(@server_database)
  special = lookup_item_special( { itemID: item_id } )
  query = "UPDATE [#{@server_database}].[dbo].[PlayerHeld] SET \
    itemID = #{item_id}, special = \'#{special}\' \
    WHERE playerID = #{player_id} AND rightHand = #{which_hand == :right ? 1 : 0}"
  debug_msg query
  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}"
  debug_msg query
  result = client.execute(query)
  affected_rows = result.do
  fail 'Unable to update player!' if 1 != affected_rows
end

## Attributes

def set_character_alignment(player_id, desired_alignment)
  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?
  client = connect_to_db(@server_database)
  query = "UPDATE [#{@server_database}].[dbo].[Player] SET \
    alignment = #{alignment_num} \
    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_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

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

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

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

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

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

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

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

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

# 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')