-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
225 lines (180 loc) · 6.87 KB
/
Game.cs
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
166
167
168
169
170
171
172
173
174
175
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
using LegendDialogue;
using LegendItems;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Text;
namespace LegendSharp
{
public class Game
{
Config config;
public bool active = false;
public String userId;
public String username;
public Player player;
public Dialogue dialogueOpen = null;
public HashSet<Entity> cachedEntities;
public bool justCached = false;
Legend legend;
public Game(String userId, String username, Config config, Legend legend)
{
cachedEntities = new HashSet<Entity>();
this.config = config;
active = true;
this.userId = userId;
this.username = username;
this.legend = legend;
BsonDocument userData = legend.GetUserData(this.userId);
if (userData == null)
{
userData = new BsonDocument(config.defaultUser);
userData.Set("user", this.userId);
legend.InsertUserData(userData);
}
Inventory inventory = new Inventory();
inventory.ItemAddedToInventory += new Inventory.ItemAddedToInventoryHandler(ItemAddedToInventory);
inventory.ItemModified += new Inventory.ItemModifiedHandler(ItemModified);
foreach (BsonValue itemValue in userData["inventory"].AsBsonArray)
{
Item item = LegendDB.DecodeItem(itemValue.AsBsonDocument, config.baseItems);
inventory.AddItem(item, false);
}
Dictionary<string, Flag> flags = new Dictionary<string, Flag>();
foreach (var flagPair in userData["flags"].AsBsonDocument)
{
string flagKey = flagPair.Name;
Flag flagValue = LegendDB.DecodeFlag(flagPair.Value);
flags[flagKey] = flagValue;
}
player = new Player(
userData.GetValue("sprite").ToString(),
userData.GetValue("pos_x").ToInt32(),
userData.GetValue("pos_y").ToInt32(),
userData.GetValue("inventory_size").ToInt32(),
inventory,
flags,
legend,
this
);
inventory.guid = player.uuid;
foreach (var item in player.inventory.items)
{
Console.WriteLine("{0}: [{1} / {2}] \"{3}\" x{4} ", item.GetName(), item.GetSprite(), item.GetItemType(), item.GetDescription(), item.GetQuantity());
}
}
public void Stop()
{
active = false;
if (this.player != null)
{
legend.world.RemoveEntity(player);
}
var userFilter = new BsonDocument();
userFilter.Set("user", this.userId);
var userUpdate = new BsonDocument();
var userData = new BsonDocument();
userData.Set("pos_x", this.player.pos.x);
userData.Set("pos_y", this.player.pos.y);
userData.Set("sprite", this.player.sprite);
userData.Set("inventory_size", this.player.inventorySize);
BsonArray bsonInventory = new BsonArray();
foreach (Item item in this.player.inventory.items)
{
BsonDocument itemDocument = new BsonDocument();
itemDocument.Set("base", item.baseItem.itemId);
Console.WriteLine("Saving {0} x{1}", item.GetName(), item.GetQuantity());
itemDocument.Set("quantity", item.GetQuantity());
if (item.HasDescription())
{
itemDocument.Set("description", item.GetDescription());
}
if (item.HasItemType())
{
itemDocument.Set("item_type", item.GetItemType());
}
if (item.HasName())
{
itemDocument.Set("name", item.GetName());
}
if (item.HasSprite())
{
itemDocument.Set("sprite", item.GetSprite());
}
if (item.HasMaxStack())
{
itemDocument.Set("max_stack", item.GetMaxStack());
}
if (item is Weapon)
{
Weapon weapon = (Weapon)item;
if (weapon.HasDamage())
{
itemDocument.Set("damage", weapon.GetDamage());
}
if (weapon.HasDamageType())
{
itemDocument.Set("damage_type", weapon.GetDamageType());
}
if (weapon.HasWeaponClass())
{
itemDocument.Set("weapon_class", weapon.GetWeaponClass());
}
}
bsonInventory.Add(itemDocument);
}
userData.Set("inventory", bsonInventory);
BsonDocument flags = new BsonDocument();
foreach (KeyValuePair<string, Flag> entry in player.flags)
{
flags.Set(entry.Key, entry.Value.GetValue());
}
userData.Set("flags", flags);
userUpdate.Set("$set", userData);
legend.userCollection.UpdateOne(userFilter, userUpdate);
}
public void ItemAddedToInventory(Inventory inventory, Item item, int index)
{
AddToInventory(inventory.guid, item, index);
}
public void ItemModified(Inventory inventory, Item item, int index)
{
ChangeInInventory(inventory.guid, item, index);
}
public void TryInteract(short interactType, Guid guid)
{
Entity entity = legend.world.GetEntity(guid);
if (entity != null && legend.world.GetSimpleDistance(player.pos, entity.pos) <= legend.config.interactRange)
{
entity.Interact(interactType, player);
}
}
public virtual void OpenDialogue(string dialogueKey)
{
}
public virtual void UpdateEntityPos(Entity entity)
{
}
public virtual void AddToCache(Entity entity)
{
cachedEntities.Add(entity);
}
public virtual void RemoveFromCache(Entity entity)
{
cachedEntities.Remove(entity);
}
public virtual void SendMessage(ChatMessage message, Position pos)
{
}
public virtual void AddToInventory(Guid guid, Item item, int index)
{
Console.WriteLine("Game called");
}
public virtual void RemoveFromInventory(int index, Item item)
{
}
public virtual void ChangeInInventory(Guid guid, Item item, int index)
{
}
}
}