· 5 years ago · Mar 15, 2020, 02:24 PM
1#include "nwnx_sql"
2#include "dmfi_init_inc"
3
4void main()
5{
6 object oPC=GetEnteringObject();
7 // Create user in database if not exists
8 if (GetIsPC(oPC))
9 {
10 string sQuery;
11
12 // Create Users table if not exist
13 sQuery = "CREATE TABLE IF NOT EXISTS Users (" +
14 "id MEDIUMINT NOT NULL AUTO_INCREMENT, " +
15 "name TEXT, " +
16 "facing TEXT, " +
17 "posx TEXT, " +
18 "posy TEXT, " +
19 "posz TEXT, " +
20 "area TEXT, " +
21 "gold INT, " +
22 "PRIMARY KEY (id))";
23 if (NWNX_SQL_PrepareQuery(sQuery)) {
24 NWNX_SQL_ExecutePreparedQuery();
25 }
26
27 // Set initial position if not exist
28 string sAccountName = GetPCPlayerName(oPC);
29 string sName = GetName(oPC);
30 location loc = GetLocation(oPC);
31 object oArea = GetAreaFromLocation(loc);
32 vector vPosition = GetPositionFromLocation(loc);
33 float fFacing = GetFacingFromLocation(loc);
34
35 //SendMessageToPC(oPC, FloatToString(fFacing));
36 //SendMessageToPC(oPC, "x: " + FloatToString(vPosition.x) +
37 // " y: " + FloatToString(vPosition.y) +
38 // " z: " + FloatToString(vPosition.z));
39 //SendMessageToPC(oPC, GetTag(oArea));
40
41 string sResult;
42 sQuery = "SELECT * FROM Users WHERE name=?";
43 if (NWNX_SQL_PrepareQuery(sQuery))
44 {
45 NWNX_SQL_PreparedString(0, sAccountName + "" + sName);
46 NWNX_SQL_ExecutePreparedQuery();
47
48 if (NWNX_SQL_ReadyToReadNextRow()) {
49 sQuery = "UPDATE Users SET (name=?)";
50 SendMessageToPC(oPC, sQuery);
51 if (NWNX_SQL_PrepareQuery(sQuery)) {
52 NWNX_SQL_PreparedString(0, sAccountName + "" + sName);
53 NWNX_SQL_ExecutePreparedQuery();
54 }
55 } else {
56 sQuery = "INSERT INTO Users VALUES (name=?)";
57 SendMessageToPC(oPC, sQuery);
58 if (NWNX_SQL_PrepareQuery(sQuery))
59 {
60 NWNX_SQL_PreparedString(0, sAccountName + "_" + sName);
61 NWNX_SQL_ExecutePreparedQuery();
62 }
63 }
64 }
65
66 //string sQuery = "SELECT * FROM Users WHERE name=?";
67 //if (NWNX_SQL_PrepareQuery(sQuery))
68 //{
69 // NWNX_SQL_PreparedString(0, sAccountName + "_" + sName);
70 // NWNX_SQL_ExecutePreparedQuery();
71 //
72 // string sResult;
73 // while (NWNX_SQL_ReadyToReadNextRow())
74 // {
75 // NWNX_SQL_ReadNextRow();
76 // sResult = NWNX_SQL_ReadDataInActiveRow()
77 // }
78 //}
79
80 }
81
82 // Default Script
83 ExecuteScript("x3_mod_pre_enter",OBJECT_SELF);
84
85 // DMFI
86 object oUser = GetEnteringObject();
87 dmfiInitialize(oUser);
88}